Commit 9fc817d8 authored by John E. Vincent's avatar John E. Vincent

latest specs

parent 6cbca4db
......@@ -7,6 +7,5 @@ rescue LoadError
require 'json'
end
require File.join(File.dirname(__FILE__), 'config/db')
require File.join(File.dirname(__FILE__), 'lib/models')
......@@ -91,10 +91,10 @@ class NoahApp < Sinatra::Base
end
end
put '/:hostname/?' do
put '/:hostname/?' do |hostname|
required_params = ["name", "status"]
data = JSON.parse(request.body.read)
data.keys.sort == required_params.sort ? (host = Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters")
(data.keys.sort == required_params.sort && data['name'] == hostname) ? (host = Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters")
if host.valid?
r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?}
r.to_json
......@@ -149,15 +149,15 @@ class NoahApp < Sinatra::Base
end
put '/:servicename/?' do |servicename|
# message format: {"status":"initial_status", "host":"hostname"}
required_params = ["status", "host", "name"]
data = JSON.parse(request.body.read)
if data.keys.sort == required_params.sort
h = Host.find(:name => data['host']).first || (raise "Invalid Host")
service = Service.create(:name => servicename, :status => data['status'], :host => h)
service = Service.find_or_create(:name => servicename, :status => data['status'], :host => h)
if service.valid?
action = service.is_new? ? "create" : "update"
service.save
r = {"action" => "add", "result" => "success", "id" => service.id, "host" => h.name, "name" => service.name}
r = {"action" => action, "result" => "success", "id" => service.id, "host" => h.name, "name" => service.name}
r.to_json
else
raise "#{service.errors}"
......@@ -208,7 +208,7 @@ class NoahApp < Sinatra::Base
if data.keys.sort == required_params.sort && data['name'] == appname
app = Application.find_or_create(:name => appname)
else
raise "Missing or invalid parameters"
raise "Missing Parameters"
end
if app.valid?
action = app.is_new? ? "create" : "update"
......@@ -228,7 +228,7 @@ class NoahApp < Sinatra::Base
configurations = []
Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
app.delete
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{app.name}", "configurations" => "#{configurations.size}"}
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
r.to_json
end
end
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Application Model", :reset_redis => true do
before(:all) do
@appdata1 = {:name => "my_application"}
@appdata2 = {:name => "my_other_app"}
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var"}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json}
end
before(:each) do
Ohm.redis.flushdb
end
after(:each) do
Ohm.redis.flushdb
end
describe "should" do
it "create a new Application" do
a = Application.create(@appdata1)
a.valid?.should == true
a.is_new?.should == true
b = Application.find(@appdata1).first
b.should == a
end
it "create a new Application with Configurations" do
a = Application.create(@appdata1)
a.configurations << Configuration.create(@appconf_string.merge({:application => a}))
a.valid?.should == true
a.is_new?.should == true
a.save
b = Application.find(@appdata1).first
b.should == a
b.configurations.size.should == 1
b.configurations.first.name.should == @appconf_string[:name]
b.configurations.first.format.should == @appconf_string[:format]
b.configurations.first.body.should == @appconf_string[:body]
end
it "create a new Application via find_or_create" do
a = Application.find_or_create(@appdata2)
a.valid?.should == true
a.is_new?.should == true
b = Application.find(@appdata2).first
b.should == a
end
it "update an existing Application via find_or_create" do
a = Application.create(@appdata1)
a.is_new?.should == true
sleep 2
b = Application.find_or_create(@appdata1)
b.is_new?.should == false
end
it "delete an existing Application" do
a = Application.create(@appdata1)
b = Application.find(@appdata1).first
b.should == a
b.delete
c = Application.find(@appdata1).first
c.nil?.should == true
end
it "return all Applications" do
a = Application.create(@appdata1)
b = Application.create(@appdata2)
c = Applications.all
c.size.should == 2
c.member?(a).should == true
c.member?(b).should == true
end
end
describe "should not" do
it "should not create a new Application without a name" do
a = Application.create
a.valid?.should == false
a.errors.should == [[:name, :not_present]]
end
it "should not create a duplicate Application" do
a = Application.create(@appdata1)
a.valid?.should == true
b = Application.create(@appdata1)
b.valid?.should == false
b.errors.should == [[:name, :not_unique]]
end
end
end
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Application API", :reset_redis => false, :populate_sample_data => true do
describe "Using the Application API", :reset_redis => false do
before(:all) do
@a = Application.create(:name => 'rspec_sample_app')
@a.configurations << Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great', :application => @a)
@a.save
@c = @a.configurations.first
end
describe "calling" do
describe "GET" do
it "all applications should work"
it "named application should work"
it "named configuration for application should work"
it "named configuration should work with mime-type"
it "invalud application should not work"
it "invalid configuration for application should not work"
it "all applications should work" do
get '/a'
last_response.should be_ok
response = last_response.should return_json
response.is_a?(Array).should == true
end
it "named application should work" do
get '/a/rspec_sample_app'
last_response.should be_ok
response = last_response.should return_json
response["id"].should == @a.id
response["name"].should == @a.name
c = response["configurations"].first
c["id"].should == @c.id
c["name"].should == @c.name
c["body"].should == @c.body
c["format"].should == @c.format
end
it "named configuration for application should work" do
get "/a/#{@a.name}/#{@c.name}"
last_response.should be_ok
response = last_response.should return_json
response["id"].should == @c.id
response["name"].should == @c.name
response["format"].should == @c.format
response["body"].should == @c.body
response["application"].should == @a.name
end
it "invalid application should not work" do
get "/a/should_not_exist"
last_response.should be_missing
end
it "invalid configuration for application should not work" do
get "/a/should_not_exist/should_not_exist"
last_response.should be_missing
end
end
describe "PUT" do
it "new application should work"
it "new application with missing name should not work"
it "existing application should work"
before(:all) do
@appdata = {:name => "should_now_exist"}
end
it "new application should work" do
put "/a/#{@appdata[:name]}", @appdata.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_ok
response = last_response.should return_json
response["result"].should == "success"
response["id"].nil?.should == false
response["name"].should == @appdata[:name]
response["action"].should == "create"
Application.find(:name => @appdata[:name]).size.should == 1
Application.find(:name => @appdata[:name]).first.is_new?.should == true
end
it "new application with missing name should not work" do
put "/a/should_not_work", '{"foo":"bar"}', "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
it "existing application should work" do
sleep 3
put "/a/#{@appdata[:name]}", @appdata.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_ok
response = last_response.should return_json
response["result"].should == "success"
response["id"].nil?.should == false
response["name"].should == @appdata[:name]
response["action"].should == "update"
Application.find(:name => @appdata[:name]).size.should == 1
Application.find(:name => @appdata[:name]).first.is_new?.should == false
end
end
describe "DELETE" do
it "existing application should work"
it "invalid application should not work"
before(:each) do
@appdata = {:name => "should_now_exist"}
end
it "existing application should work" do
delete "/a/#{@appdata[:name]}"
last_response.should be_ok
response = last_response.should return_json
response["result"].should == "success"
response["action"].should == "delete"
response["id"].nil?.should == false
response["name"].should == @appdata[:name]
response["configurations"].should == "0"
end
it "invalid application should not work" do
delete "/a/should_not_work"
last_response.should be_missing
end
end
end
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Service API", :reset_redis => false, :populate_sample_data => true do
before(:all) do
@sample_host = {:name => 'rspec_sample_host', :status => 'up'}
@sample_service = {:name => 'rspec_sample_service', :status => 'up'}
@h = Host.create(:name => 'rspec_sample_host', :status => 'up')
@h.services << Service.create({:host => @h}.merge(@sample_service))
@h.save
@s = Service.find(@sample_service).first
end
describe "calling" do
describe "GET" do
it "all services should work"
it "all named services should work"
it "named service for host should work"
it "all services should work" do
get '/s'
last_response.should be_ok
response = last_response.should return_json
response.is_a?(Array).should == true
end
it "all named services should work" do
get "/s/#{@sample_service[:name]}"
last_response.should be_ok
response = last_response.should return_json
response.is_a?(Array).should == true
s = response.first
s["id"].should == @s.id
s["name"].should == @s.name
s["status"].should == @s.status
s["host"].should == @h.name
end
it "named service for host should work" do
get "/s/#{@sample_service[:name]}/#{@sample_host[:name]}"
last_response.should be_ok
response = last_response.should return_json
response["id"].should == @s.id
response["name"].should == @s.name
response["status"].should == @s.status
response["host"].should == @h.name
end
it "missing service for host should not work" do
get '/s/foobar/baz'
last_response.should be_missing
......@@ -14,12 +45,52 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
end
describe "PUT" do
it "new service should work"
it "new service without host should not work"
it "new service with invalid status should not work"
it "new service with missing name should not work"
it "new service with missing status should not work"
it "existing service should work"
before(:all) do
@payload = {:name => 'another_rspec_service', :status => 'up', :host => @h.name}
end
it "new service should work" do
put "/s/#{@payload[:name]}/", @payload.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_ok
response = last_response.should return_json
response["result"].should == "success"
response["action"].should == "create"
response["id"].nil?.should == false
response["name"].should == @payload[:name]
response["host"].should == @payload[:host]
Service.find(:name => @payload[:name]).size.should == 1
Service.find(:name => @payload[:name]).first.is_new?.should == true
end
it "new service without host should not work" do
put "/s/foobar", {:name => "foobar", :status => "up"}.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
it "new service with invalid status should not work" do
put "/s/foobar", {:name => "foobar", :status => "fsck", :host => @h.name}.to_json, "CONTENT_TYPE" => "application/json"
last_response.should_not be_ok
response = last_response.should return_json
response["error_message"].should == "[[:status, :not_member]]"
end
it "new service with missing name should not work" do
put "/s/foobar", {:status => "fsck", :host => @h.name}.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
it "new service with missing status should not work" do
put "/s/foobar", {:name => "foobar", :host => @h.name}.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
it "existing service should work" do
sleep 3
put "/s/#{@payload[:name]}", {:name => @payload[:name], :status => "down", :host => @payload[:host]}.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_ok
response = last_response.should return_json
response["result"].should == "success"
response["action"].should == "update"
response["id"].nil?.should == false
response["name"].should == @payload[:name]
response["host"].should == @payload[:host]
Service.find(:name => @payload[:name]).size.should == 1
Service.find(:name => @payload[:name]).first.is_new?.should == false
end
end
describe "DELETE" do
......
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