spec/sanitizer_spec.rb in input_sanitizer-0.1.10 vs spec/sanitizer_spec.rb in input_sanitizer-0.2.0

- old
+ new

@@ -1,13 +1,21 @@ require 'spec_helper' +class NestedSanitizer < InputSanitizer::Sanitizer + integer :foo +end + class BasicSanitizer < InputSanitizer::Sanitizer string :x, :y, :z + integer :namespaced, :namespace => :value + integer :array, :collection => true + integer :namespaced_array, :collection => true, :namespace => :value integer :num date :birthday time :updated_at custom :cust1, :cust2, :converter => lambda { |v| v.reverse } + nested :stuff, :sanitizer => NestedSanitizer, :collection => true, :namespace => :nested end class BrokenCustomSanitizer < InputSanitizer::Sanitizer end @@ -26,16 +34,21 @@ class RequiredCustom < BasicSanitizer custom :c1, :required => true, :converter => lambda { |v| v } end +class DefaultParameters < BasicSanitizer + integer :funky_number, :default => 5 + custom :fixed_stuff, :converter => lambda {|v| v }, :default => "default string" +end + describe InputSanitizer::Sanitizer do let(:sanitizer) { BasicSanitizer.new(@params) } describe ".clean" do it "returns cleaned data" do - clean_data = mock() + clean_data = double BasicSanitizer.any_instance.should_receive(:cleaned).and_return(clean_data) BasicSanitizer.clean({}).should be(clean_data) end end @@ -81,10 +94,34 @@ cleaned.should have_key(:x) cleaned.should_not have_key(:d) end + it "preserves namespace" do + value = { :value => 5 } + @params = { :namespaced => value } + + cleaned[:namespaced].should eq(value) + end + + it "maps values for collection fields" do + numbers = [3, 5, 6] + @params = { :array => numbers } + + cleaned[:array].should eq(numbers) + end + + it "maps values for collection fields with namespace" do + numbers = [ + { :value => 2 }, + { :value => 5 } + ] + @params = { :namespaced_array => numbers } + + cleaned[:namespaced_array].should eq(numbers) + end + it "silently discards cast errors" do @params = {:num => "f"} cleaned.should_not have_key(:num) end @@ -104,10 +141,36 @@ cleaned.should have_key(:is_nice) cleaned[:is_nice].should == 42 end + context "when sanitizer is initialized with default values" do + context "when paremeters are not overwriten" do + let(:sanitizer) { DefaultParameters.new({}) } + + it "returns default value for non custom key" do + sanitizer.cleaned[:funky_number].should == 5 + end + + it "returns default value for custom key" do + sanitizer.cleaned[:fixed_stuff].should == "default string" + end + end + + context "when parameters are overwriten" do + let(:sanitizer) { DefaultParameters.new({ :funky_number => 2, :fixed_stuff => "fixed" }) } + + it "returns default value for non custom key" do + sanitizer.cleaned[:funky_number].should == 2 + end + + it "returns default value for custom key" do + sanitizer.cleaned[:fixed_stuff].should == "fixed" + end + end + end + end describe ".custom" do let(:sanitizer) { BasicSanitizer.new(@params) } let(:cleaned) { sanitizer.cleaned } @@ -121,9 +184,28 @@ it "raises an error when converter is not defined" do expect do BrokenCustomSanitizer.custom(:x) end.to raise_error + end + end + + describe ".nested" do + let(:sanitizer) { BasicSanitizer.new(@params) } + let(:cleaned) { sanitizer.cleaned } + + it "sanitizes nested values" do + nested = [ + { :nested => { :foo => "5" } }, + { :nested => { :foo => 8 } }, + ] + @params = { :stuff => nested } + + expected = [ + { :nested => { :foo => 5 } }, + { :nested => { :foo => 8 } }, + ] + cleaned[:stuff].should eq(expected) end end describe ".converters" do let(:sanitizer) { InputSanitizer::Sanitizer }