Commit 5b7c9c25 authored by John E. Vincent's avatar John E. Vincent

fix specs and such on 1.8.7

parent 4386dac7
......@@ -41,7 +41,7 @@ class Noah::App
r = {"result" => "success","id" => app.id, "action" => action, "name" => app.name }
r.to_json
else
raise "#{app.errors}"
raise "#{format_errors(app)}"
end
end
......
......@@ -59,7 +59,7 @@ class Noah::App
r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name}
r.to_json
else
raise "#{config.errors}"
raise "#{format_errors(config)}"
end
end
......
......@@ -24,7 +24,7 @@ class Noah::App
raise("Data too large") if request.body.size > 512
d = Base64.encode64(request.body.read) || nil
e = Noah::Ephemeral.new(:path => params[:splat][0], :data => d)
e.valid? ? (e.save; e.to_json) : (raise "#{e.errors}")
e.valid? ? (e.save; e.to_json) : (raise "#{format_errors(e)}")
end
delete '/e/*' do
......
module Noah
module SinatraHelpers
def format_errors(model)
error_messages = model.errors.present do |e|
# Missing attributes
e.on [:name, :not_present], "Name attribute missing"
e.on [:status, :not_present], "Status attribute missing"
e.on [:format, :not_present], "Format attribute missing"
e.on [:body, :not_present], "Body attribute missing"
e.on [:application_id, :not_present], "Application attribute missing"
e.on [:path, :not_present], "Path attribute missing"
e.on [:pattern, :not_present], "Pattern attribute missing"
e.on [:endpoint, :not_present], "Endpoint attribute missing"
# Invalid option
e.on [:status, :not_member], "Status must be up, down or pending"
# Duplicate keys
e.on [[:name, :application_id], :not_unique], "Record already exists"
e.on [[:name, :host_id], :not_unique], "Record already exists"
e.on [[:endpoint, :pattern], :not_unique], "Record already exists"
e.on [:path, :not_unique], "Record already exists"
# Custom exceptions
e.on [:pattern, :already_provided], "Pattern is already provided"
e.on [:pattern, :replaces_existing], "Pattern would overwrite existing"
end
error_messages.first
end
def host(opts = {})
Noah::Host.find(opts).first
......
......@@ -49,7 +49,7 @@ class Noah::App
r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?}
r.to_json
else
raise "#{host.errors}"
raise "#{format_errors(host)}"
end
end
......
......@@ -50,7 +50,7 @@ class Noah::App
r = {"action" => action, "result" => "success", "id" => service.id, "host" => h.name, "name" => service.name}
r.to_json
else
raise "#{service.errors}"
raise "#{format_errors(service)}"
end
else
raise "Missing Parameters"
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Configuration Model", :reset_redis => true do
before(:all) do
app = Noah::Application.create(:name => "my_application")
app.save
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var", :application_id => app.id}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json, :application_id => app.id}
@appconf_missing_name = @appconf_string.reject {|x| x == :name}
@appconf_missing_format = @appconf_string.reject {|x| x == :format}
@appconf_missing_body = @appconf_string.reject {|x| x == :body}
@appconf_missing_application = @appconf_string.reject {|x| x == :application_id}
end
before(:each) do
Ohm.redis.flushdb
app = Noah::Application.create :name => "my_application"
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var", :application_id => app.id}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json, :application_id => app.id}
@appconf_missing_name = @appconf_string.reject {|k, v| k == :name}
@appconf_missing_format = @appconf_string.reject {|k, v| k == :format}
@appconf_missing_body = @appconf_string.reject {|k, v| k == :body}
@appconf_missing_application = @appconf_string.reject {|k, v| k == :application_id}
end
after(:each) do
Ohm.redis.flushdb
......
......@@ -3,8 +3,8 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Ephemeral Model", :reset_redis => true do
before(:all) do
@edata = {:path => "/foo/bar/baz", :data => "some_value"}
@emissing_path = @edata.reject {|x| x == :path}
@emissing_data = @edata.reject {|x| x == :data}
@emissing_path = @edata.reject {|k, v| k == :path}
@emissing_data = @edata.reject {|k, v| k == :data}
@good_ephemeral = Noah::Ephemeral.new(@edata)
@missing_path = Noah::Ephemeral.new(@emissing_path)
@missing_data = Noah::Ephemeral.new(@emissing_data)
......@@ -28,15 +28,15 @@ describe "Using the Ephemeral Model", :reset_redis => true do
b = Noah::Ephemeral[@missing_data.id]
b.should == @missing_data
end
# it "update an existing Noah::Ephemeral" do
# @good_ephemeral.save
# Noah::Ephemeral.all.size.should == 1
# c = Noah::Ephemeral[@good_ephemeral.id]
# c.data = "updated_data"
# c.save
# sleep(2)
# c.is_new?.should == false
# end
it "update an existing Noah::Ephemeral" do
e = Noah::Ephemeral.create :path => "/is/new/test"
Noah::Ephemeral.all.size.should == 1
sleep(2)
c = Noah::Ephemeral[e.id]
c.data = "updated_data"
c.save
c.is_new?.should == false
end
it "delete an existing Noah::Ephemeral" do
@good_ephemeral.save
@good_ephemeral.delete
......@@ -45,8 +45,15 @@ describe "Using the Ephemeral Model", :reset_redis => true do
end
describe "should not" do
it "create a new Noah::Ephemeral with missing path" do
@missing_path.valid?.should == false
@missing_path.errors.should == [[:path, :not_present]]
e = Noah::Ephemeral.create
e.valid?.should == false
e.errors.should == [[:path, :not_present]]
end
it "create a duplicate Noah::Ephemeral" do
e = Noah::Ephemeral.create :path => "/random/path"
f = Noah::Ephemeral.create :path => "/random/path"
f.valid?.should == false
f.errors.should == [[:path, :not_unique]]
end
end
end
......@@ -34,8 +34,8 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
response = YAML.load(last_response.body)
response.is_a?(Hash).should == true
response.keys.should == ["development"]
response["development"].keys.should == ["database", "adapter", "username", "password"]
response["development"].values.should == ["development_database", "mysql", "dev_user", "dev_password"]
response["development"].keys.sort.should == ["adapter", "database", "password", "username"]
response["development"].values.sort.should == ["dev_password", "dev_user", "development_database", "mysql"]
end
it "invalid application should not work" do
get '/c/badapp'
......
......@@ -82,7 +82,7 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t
response = last_response.should return_json
response["result"].should == "failure"
response["error_message"].should == "[[:status, :not_member]]"
response["error_message"].should == "Status must be up, down or pending"
end
end
......
......@@ -68,7 +68,7 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
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]]"
response["error_message"].should == "Status must be up, down or pending"
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"
......
......@@ -32,30 +32,30 @@ describe "Using the Watcher Model", :reset_redis => true do
it "create a new Noah::Watcher with missing endpoint" do
a = Noah::Watcher.create(:pattern => "/foo/bar")
a.valid?.should == false
a.errors.to_s.should == "[[:endpoint, :not_present]]"
a.errors.should == [[:endpoint, :not_present]]
end
it "create a new Noah::Watcher with missing pattern" do
a = Noah::Watcher.create(:endpoint => "http://localhost/webhook")
a.valid?.should == false
a.errors.to_s.should == "[[:pattern, :not_present]]"
a.errors.should == [[:pattern, :not_present]]
end
it "create a new Noah::Watcher with subset pattern" do
a = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/")
b = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b.valid?.should == false
b.errors.to_s.should == "[[:pattern, :already_provided]]"
b.errors.should == [[:pattern, :already_provided]]
end
it "create a new Noah::Watcher with superset pattern" do
a = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah")
b.valid?.should == false
b.errors.to_s.should == "[[:pattern, :replaces_existing]]"
b.errors.should == [[:pattern, :replaces_existing]]
end
it "create a duplicate Noah::Watcher" do
a = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b.valid?.should == false
b.errors.to_s.should == "[[[:endpoint, :pattern], :not_unique]]"
b.errors.should == [[[:endpoint, :pattern], :not_unique]]
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