Commit 140f5211 authored by John E. Vincent's avatar John E. Vincent

reserving paths in epehemerals

parent 41913163
module Noah
PROTECTED_PATHS = %w[applications configurations hosts services watches]
PROTECTED_PATHS = %w[applications configurations hosts services watches ark noah]
end
begin
require 'yajl'
......
......@@ -22,6 +22,7 @@ module Noah
# Custom exceptions
e.on [:pattern, :already_provided], "Pattern is already provided"
e.on [:pattern, :replaces_existing], "Pattern would overwrite existing"
e.on [:path, :reserved_path], "Path is reserved"
end
error_messages.first
end
......
module Noah
class Ephemeral < Model
include EphemeralValidations
attribute :path
attribute :data
......@@ -11,6 +12,7 @@ module Noah
super
assert_present :path
assert_unique :path
assert_not_reserved
end
def name
......
require 'digest/sha1'
module Noah
class Watcher < Model #NYI
# Don't trust anything in here yet
# I'm still trying a few things
class Watcher < Model
include WatcherValidations
attribute :pattern
......
require File.join(File.dirname(__FILE__), 'validations','watcher_validations')
require File.join(File.dirname(__FILE__), 'validations','ephemeral_validations')
module Noah
module EphemeralValidations
def assert_not_reserved(error = [:path, :reserved_path])
# This is to work around how Ohm evaluates validations
# Allows assert_present :path to override
return if self.path.nil?
# This is the real test
self.instance_of?(Noah::Ephemeral) ? (assert valid_path?, error) : (assert false, "Validation not applicable")
end
private
def valid_path?
PROTECTED_PATHS.member?(self.path.split("/")[1]) ? (return false) : (return true)
rescue ArgumentError
return false
end
end
end
......@@ -42,6 +42,12 @@ describe "Using the Ephemeral Model", :reset_redis => true do
@good_ephemeral.delete
Noah::Ephemeral[@good_ephemeral.id].should == nil
end
it "should allow reserved keywords as subpaths" do
Noah::PROTECTED_PATHS.each do |path|
e = Noah::Ephemeral.new :path => "/this/should/be/#{path}"
e.valid?.should == true
end
end
end
describe "should not" do
it "create a new Noah::Ephemeral with missing path" do
......@@ -55,5 +61,12 @@ describe "Using the Ephemeral Model", :reset_redis => true do
f.valid?.should == false
f.errors.should == [[:path, :not_unique]]
end
it "allow a reserved keyword as a path" do
Noah::PROTECTED_PATHS.each do |path|
e = Noah::Ephemeral.new :path => "/#{path}"
e.valid?.should == false
e.errors.should == [[:path, :reserved_path]]
end
end
end
end
......@@ -87,6 +87,21 @@ describe "Using the Ephemeral API", :reset_redis => true do
last_response.should be_ok
last_response.body.should == 'a new value'
end
it "ephemeral with reserved word in subpath should work" do
Noah::PROTECTED_PATHS.each do |path|
put "/e/a/valid/path/with/#{path}"
last_response.should be_ok
end
end
it "ephemeral with reserved word as path should not work" do
Noah::PROTECTED_PATHS.each do |path|
put "/e/#{path}/other/stuff"
last_response.should_not be_ok
response = last_response.should return_json
response['error_message'].should == 'Path is reserved'
response['result'].should == 'failure'
end
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