Commit 82be084e authored by John E. Vincent's avatar John E. Vincent

more DRY on the tests

parent 4f1941f3
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment