diff --git a/noah.rb b/noah.rb index a4400e9c3b4d7900273d4906db2ae47352a94105..380496fffe0bcdf1c719fa857647baa685a20195 100644 --- a/noah.rb +++ b/noah.rb @@ -295,9 +295,9 @@ class NoahApp < Sinatra::Base data = JSON.parse(request.body.read) data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters") if config.valid? + config.save action = config.is_new? ? "create" : "update" dependency_action = app.is_new? ? "created" : "updated" - config.save r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name} r.to_json else diff --git a/spec/noahapp_configuration_spec.rb b/spec/noahapp_configuration_spec.rb index 0e73b2575044877fa283ef6a28133b149bfdf6a7..c236b27bbe94d91f130f49e3752abf24a7867176 100644 --- a/spec/noahapp_configuration_spec.rb +++ b/spec/noahapp_configuration_spec.rb @@ -52,15 +52,70 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_ end describe "PUT" do - it "new configuration should work" - it "existing configuration should work" - it "new configuration with missing format should not work" - it "new configuration with missing body should not work" + it "new configuration should work" do + config_data = {:format => "string", :body => "sample_config_entry"}.to_json + put '/c/newapp/newconfig', config_data, "CONTENT_TYPE" => "application/json" + last_response.should be_ok + response = last_response.should return_json + response["result"].should == "success" + response["action"].should == "create" + response["dependencies"].should == "created" + response["application"].should == "newapp" + response["item"].should == "newconfig" + end + it "existing configuration should work" do + config_data = {:format => "string", :body => "sample_config_entry"}.to_json + sleep 3 + put '/c/newapp/newconfig', config_data, "CONTENT_TYPE" => "application/json" + last_response.should be_ok + response = last_response.should return_json + response["result"].should == "success" + response["action"].should == "update" + response["dependencies"].should == "updated" + response["application"].should == "newapp" + response["item"].should == "newconfig" + end + it "new configuration with missing format should not work" do + config_data = {:body => "a string"}.to_json + put '/c/newnewapp/someconfig', config_data, "CONTENT_TYPE" => "application/json" + last_response.should_not be_ok + response = last_response.should return_json + response["result"].should == "failure" + response["error_message"].should == "Missing Parameters" + end + it "new configuration with missing body should not work" do + config_data = {:body => "a string"}.to_json + put '/c/newnewapp/someconfig', config_data, "CONTENT_TYPE" => "application/json" + last_response.should_not be_ok + response = last_response.should return_json + response["result"].should == "failure" + response["error_message"].should == "Missing Parameters" + end end describe "DELETE" do - it "existing configuration should work" - it "invalid configuration should not work" + before(:all) do + cparms = {:name => 'a', :format => 'string', :body => 'asdf'} + @a = Application.create(:name => 'delete_test_app') + @a.configurations << Configuration.create(cparms) + @a.save + @c = @a.configurations.first + end + + it "existing configuration should work" do + delete "/c/#{@a.name}/#{@c.name}" + last_response.should be_ok + response = last_response.should return_json + response["result"].should == "success" + response["id"].should == @c.id + response["action"].should == "delete" + response["application"].should == @a.name + response["item"].should == @c.name + end + it "invalid configuration should not work" do + delete "/c/#{@a.name}/#{@c.name}" + last_response.should be_missing + end end end diff --git a/spec/noahapp_host_spec.rb b/spec/noahapp_host_spec.rb index ba3045c878f55ba7ca3009e9ae5d7ceeb6168ce6..ba274b552f33267094d9e7e0cfe80da151f9023b 100644 --- a/spec/noahapp_host_spec.rb +++ b/spec/noahapp_host_spec.rb @@ -95,29 +95,28 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t end describe "DELETE" do + before(:all) do + @h = Host.create(:name => 'h', :status => 'up') + sparms = {:name => 's', :status => "up"} + @h.services << Service.create(sparms.merge({:host => @h})) + @h.save + @s = @h.services.first + end it "existing host should work" do - h = Host.find(:name => "localhost").first - svc_size = h.services.size - host_data = {:name => "localhost"} - delete '/h/localhost', host_data, "CONTENT_TYPE" => "application/json" + svc_size = @h.services.size + delete "/h/#{@h.name}" last_response.should be_ok response = last_response.should return_json response["result"].should == "success" - response["id"].should == h.id - response["name"].should == "localhost" + response["id"].should == @h.id + response["name"].should == @h.name response["service_count"].should == svc_size.to_s end it "invalid host should not work" do - host_data = {:name => "invalid_host.asdf.com"} - delete '/h/invalid_host.asdf.com', host_data, "CONTENT_TYPE" => "application/json" - last_response.should_not be_ok - last_response.status.should == 404 - response = last_response.should return_json - - response["result"].should == "failure" - response["error_message"].should == "Resource not found" + delete "/h/#{@h.name}" + last_response.should be_missing end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 09e6a5d7da2322d0368b697650365210c22c193a..24af91948ba770d25496081fbdfd6a62462ca60c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -73,9 +73,23 @@ def app NoahApp end -RSpec::Matchers.define :return_json do |attribute| +RSpec::Matchers.define :return_json do match do |last_response| last_response.headers["Content-Type"].should == "application/json" response = JSON.parse(last_response.body) end + + failure_message_for_should do + "Response was not valid JSON" + end +end + +RSpec::Matchers.define :be_missing do + match do |last_response| + last_response.headers["Content-Type"].should == "application/json" + last_response.status.should == 404 + response = JSON.parse(last_response.body) + response["result"].should == "failure" + response["error_message"].should == "Resource not found" + end end