Commit fec88421 authored by John E. Vincent's avatar John E. Vincent

finished watcher API and tests

parent c0f73265
class Noah::App class Noah::App
# Stubbing Ephemeral endpoints
get '/e/?' do get '/e/?' do
halt 404 halt 404
end end
......
...@@ -7,6 +7,7 @@ module Noah ...@@ -7,6 +7,7 @@ module Noah
attribute :pattern attribute :pattern
attribute :endpoint attribute :endpoint
attribute :name
index :pattern index :pattern
index :endpoint index :endpoint
...@@ -29,12 +30,19 @@ module Noah ...@@ -29,12 +30,19 @@ module Noah
super.merge(h) super.merge(h)
end end
def self.watch_list class << self
def find_by_name(name)
pattern, endpoint = Base64.decode64(name).split('|')
find(:pattern => pattern, :endpoint => endpoint).first
end
def watch_list
arr = [] arr = []
watches = self.all.sort_by(:pattern) watches = self.all.sort_by(:pattern)
watches.each {|w| arr << w.name} watches.each {|w| arr << w.name}
arr arr
end end
end
private private
# Not sure about these next two. # Not sure about these next two.
......
class Noah::App class Noah::App
get '/w/:name' do |name|
w = Noah::Watcher.find_by_name(name)
w.nil? ? (halt 404) : w.to_json
end
get '/w/?' do get '/w/?' do
w = Noah::Watcher.all.sort_by(:pattern) w = Noah::Watcher.all.sort_by(:pattern)
if w.size == 0 if w.size == 0
halt 404 halt 404
else else
w.to_json w.to_json
end end
end
put '/w/?' do
required_params = %w[endpoint pattern]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (pattern, endpoint = data['pattern'],data['endpoint']) : (raise "Missing Parameters")
w = Noah::Watcher.create(:pattern => pattern, :endpoint => endpoint)
if w.valid?
w.save
r = {"action" => "create", "result" => "success"}.merge(w.to_hash)
r.to_json
else
raise "#{format_errors(w)}"
end
end
delete '/w/?' do
required_params = %w[endpoint pattern]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (pattern, endpoint = data['pattern'],data['endpoint']) : (raise "Missing Parameters")
w = Noah::Watcher.find(:pattern => pattern, :endpoint => endpoint).first
if w.nil?
halt 404
else
w.delete
r = {"result" => "success", "action" => "delete"}.merge(w.to_hash)
r.to_json
end
end end
end end
...@@ -28,7 +28,96 @@ describe "Using the Watcher API", :reset_redis => true do ...@@ -28,7 +28,96 @@ describe "Using the Watcher API", :reset_redis => true do
response.is_a?(Array).should == true response.is_a?(Array).should == true
response.size.should == 4 response.size.should == 4
end end
it "named watch should work" do
w = Noah::Watcher.create(:pattern => '//noah/application/myapp', :endpoint => 'http://localhost/webhook')
get "/w/#{w.name}"
last_response.should be_ok
response = last_response.should return_json
response['pattern'].should == w.pattern
response['endpoint'].should == w.endpoint
end
it "invalid watch should not work" do
get '/w/asdfasdfasdfasdfasdfsdf'
last_response.should be_missing
end
end end
describe "PUT" do
it "new watch should work" do
data = {:pattern => "//noah/application", :endpoint => "http://myendpoint/webhook"}
put '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_ok
response = last_response.should return_json
response['pattern'].should == data[:pattern]
response['endpoint'].should == data[:endpoint]
w = Noah::Watcher.find(data).first
w.pattern.should == data[:pattern]
w.endpoint.should == data[:endpoint]
end
it "new watch without pattern should not work" do
data = {:endpoint => "http://myendpoint/webhook"}
put '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
it "new watch without endpoint should not work" do
data = {:pattern => "//noah/application"}
put '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
it "new watch that supercedes existing should not work" do
Noah::Watcher.create(:endpoint => 'http://myendpoint/webhook', :pattern => '//noah/application/foo')
data = {:endpoint => "http://myendpoint/webhook", :pattern => '//noah/application'}
put '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should_not be_ok
response = last_response.should return_json
response['error_message'].should == 'Pattern would overwrite existing'
end
it "new watch that subsets an existing should not work" do
Noah::Watcher.create(:endpoint => 'http://myendpoint/webhook', :pattern => '//noah/application')
data = {:endpoint => "http://myendpoint/webhook", :pattern => '//noah/application/foo'}
put '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should_not be_ok
response = last_response.should return_json
response['error_message'].should == 'Pattern is already provided'
end
end
describe "DELETE" do
it "delete an existing watch should work" do
data = {:endpoint => "http://myendpoint/webhookd", :pattern => '//noah/application/d'}
w = Noah::Watcher.create(data)
delete '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_ok
response = last_response.should return_json
response['pattern'].should == data[:pattern]
response['endpoint'].should == data[:endpoint]
response['name'].should_not == "null"
Noah::Watcher.find(data).size.should == 0
end
it "delete an invalid watch should not work" do
data = {:endpoint => 'missing', :pattern => '//noah/application/dag'}
delete '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_missing
end
it "delete without pattern should not work" do
data = {:endpoint => "invalid"}
delete '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
it "delete without endpoint should not work" do
data = {:pattern => "//noah/invalid"}
delete '/w', data.to_json, "CONTENT_TYPE" => "application/json"
last_response.should be_invalid
end
end
end end
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