strongly_typed_parameters 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+ require 'action_controller/parameters'
3
+
4
+ class ParametersRequireTest < ActiveSupport::TestCase
5
+ test "required parameters must be present not merely not nil" do
6
+ assert_raises(ActionController::ParameterMissing) do
7
+ ActionController::Parameters.new(:person => {}).require(:person)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+ require 'action_controller/parameters'
3
+
4
+ class ParametersTaintTest < ActiveSupport::TestCase
5
+ setup do
6
+ @params = ActionController::Parameters.new({ :person => {
7
+ :age => "32", :name => { :first => "David", :last => "Heinemeier Hansson" }
8
+ }})
9
+ end
10
+
11
+ test "fetch raises ParameterMissing exception" do
12
+ e = assert_raises(ActionController::ParameterMissing) do
13
+ @params.fetch :foo
14
+ end
15
+ assert_equal :foo, e.param
16
+ end
17
+
18
+ test "fetch doesnt raise ParameterMissing exception if there is a default" do
19
+ assert_nothing_raised do
20
+ assert_equal "monkey", @params.fetch(:foo, "monkey")
21
+ assert_equal "monkey", @params.fetch(:foo) { "monkey" }
22
+ end
23
+ end
24
+
25
+ test "not permitted is sticky on accessors" do
26
+ assert !@params.slice(:person).permitted?
27
+ assert !@params[:person][:name].permitted?
28
+ assert !@params[:person].except(:name).permitted?
29
+
30
+ @params.each { |key, value| assert(!value.permitted?) if key == "person" }
31
+
32
+ assert !@params.fetch(:person).permitted?
33
+
34
+ assert !@params.values_at(:person).first.permitted?
35
+ end
36
+
37
+ test "permitted is sticky on accessors" do
38
+ @params.permit!
39
+ assert @params.slice(:person).permitted?
40
+ assert @params[:person][:name].permitted?
41
+ assert @params[:person].except(:name).permitted?
42
+
43
+ @params.each { |key, value| assert(value.permitted?) if key == "person" }
44
+
45
+ assert @params.fetch(:person).permitted?
46
+
47
+ assert @params.values_at(:person).first.permitted?
48
+ end
49
+
50
+ test "not permitted is sticky on mutators" do
51
+ assert !@params.delete_if { |k, v| k == "person" }.permitted?
52
+ assert !@params.keep_if { |k, v| k == "person" }.permitted? if @params.respond_to?(:keep_if)
53
+ end
54
+
55
+ test "permitted is sticky on mutators" do
56
+ @params.permit!
57
+ assert @params.delete_if { |k, v| k == "person" }.permitted?
58
+ assert @params.keep_if { |k, v| k == "person" }.permitted? if @params.respond_to?(:keep_if)
59
+ end
60
+
61
+ test "not permitted is sticky beyond merges" do
62
+ assert !@params.merge(:a => "b").permitted?
63
+ end
64
+
65
+ test "permitted is sticky beyond merges" do
66
+ @params.permit!
67
+ assert @params.merge(:a => "b").permitted?
68
+ end
69
+
70
+ test "modifying the parameters" do
71
+ @params[:person][:hometown] = "Chicago"
72
+ @params[:person][:family] = { :brother => "Jonas" }
73
+
74
+ assert_equal "Chicago", @params[:person][:hometown]
75
+ assert_equal "Jonas", @params[:person][:family][:brother]
76
+ end
77
+
78
+ test "permitting parameters that are not there should not include the keys" do
79
+ assert !@params.permit(:person, :funky).has_key?(:funky)
80
+ end
81
+
82
+ test "permit state is kept on a dup" do
83
+ @params.permit!
84
+ assert_equal @params.permitted?, @params.dup.permitted?
85
+ end
86
+
87
+ test "permit is recursive" do
88
+ @params.permit!
89
+ assert @params.permitted?
90
+ assert @params[:person].permitted?
91
+ assert @params[:person][:name].permitted?
92
+ end
93
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+ require 'action_controller/parameters'
3
+
4
+ class RaiseOnUnpermittedParamsTest < ActiveSupport::TestCase
5
+ def setup
6
+ ActionController::Parameters.action_on_unpermitted_parameters = :raise
7
+ end
8
+
9
+ def teardown
10
+ ActionController::Parameters.action_on_unpermitted_parameters = false
11
+ end
12
+
13
+ test "raises on unexpected params" do
14
+ params = ActionController::Parameters.new({
15
+ :book => { :pages => 65 },
16
+ :fishing => "Turnips"
17
+ })
18
+
19
+ assert_raises(ActionController::UnpermittedParameters) do
20
+ params.permit(:book => [:pages])
21
+ end
22
+ end
23
+
24
+ test "raises on unexpected nested params" do
25
+ params = ActionController::Parameters.new({
26
+ :book => { :pages => 65, :title => "Green Cats and where to find then." }
27
+ })
28
+
29
+ assert_raises(ActionController::UnpermittedParameters) do
30
+ params.permit(:book => [:pages])
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require 'test/unit'
5
+ require 'rails'
6
+
7
+ class FakeApplication < Rails::Application; end
8
+
9
+ Rails.application = FakeApplication
10
+ Rails.configuration.action_controller = ActiveSupport::OrderedOptions.new
11
+
12
+ require 'strongly_typed_parameters'
13
+ require 'mocha'
14
+
15
+ module ActionController
16
+ SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
17
+ SharedTestRoutes.draw do
18
+ match ':controller(/:action)'
19
+ end
20
+
21
+ class Base
22
+ include ActionController::Testing
23
+ include SharedTestRoutes.url_helpers
24
+ end
25
+
26
+ class ActionController::TestCase
27
+ setup do
28
+ @routes = SharedTestRoutes
29
+ end
30
+ end
31
+ end
32
+
33
+ ActionController::Parameters.action_on_unpermitted_parameters = false
34
+
35
+ # Load support files
36
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strongly_typed_parameters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Heinemeier Hansson
9
+ - Aaron Weiner
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-10-18 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '3.0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: activemodel
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '3.0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: railties
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: mocha
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 0.12.0
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 0.12.0
95
+ description:
96
+ email:
97
+ - aweiner@mdsol.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - lib/action_controller/parameters.rb
103
+ - lib/active_model/forbidden_attributes_protection.rb
104
+ - lib/generators/rails/strong_parameters_controller_generator.rb
105
+ - lib/generators/rails/templates/controller.rb
106
+ - lib/generators/rails/USAGE
107
+ - lib/strongly_typed_parameters/boolean.rb
108
+ - lib/strongly_typed_parameters/railtie.rb
109
+ - lib/strongly_typed_parameters/version.rb
110
+ - lib/strongly_typed_parameters.rb
111
+ - MIT-LICENSE
112
+ - Rakefile
113
+ - README.rdoc
114
+ - test/action_controller_required_params_test.rb
115
+ - test/action_controller_tainted_params_test.rb
116
+ - test/active_model_mass_assignment_taint_protection_test.rb
117
+ - test/active_model_smart_type_defaulting_test.rb
118
+ - test/controller_generator_test.rb
119
+ - test/gemfiles/Gemfile.rails-3.0.x
120
+ - test/gemfiles/Gemfile.rails-3.0.x.lock
121
+ - test/gemfiles/Gemfile.rails-3.1.x
122
+ - test/gemfiles/Gemfile.rails-3.2.x
123
+ - test/log_on_unpermitted_params_test.rb
124
+ - test/multi_parameter_attributes_test.rb
125
+ - test/parameters_permit_test.rb
126
+ - test/parameters_require_test.rb
127
+ - test/parameters_taint_test.rb
128
+ - test/raise_on_unpermitted_params_test.rb
129
+ - test/test_helper.rb
130
+ homepage: https://github.com/mdsol/strong_parameters
131
+ licenses: []
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 1.8.24
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Whitelist and typecheck your parameters at the controller level
154
+ test_files:
155
+ - test/action_controller_required_params_test.rb
156
+ - test/action_controller_tainted_params_test.rb
157
+ - test/active_model_mass_assignment_taint_protection_test.rb
158
+ - test/active_model_smart_type_defaulting_test.rb
159
+ - test/controller_generator_test.rb
160
+ - test/gemfiles/Gemfile.rails-3.0.x
161
+ - test/gemfiles/Gemfile.rails-3.0.x.lock
162
+ - test/gemfiles/Gemfile.rails-3.1.x
163
+ - test/gemfiles/Gemfile.rails-3.2.x
164
+ - test/log_on_unpermitted_params_test.rb
165
+ - test/multi_parameter_attributes_test.rb
166
+ - test/parameters_permit_test.rb
167
+ - test/parameters_require_test.rb
168
+ - test/parameters_taint_test.rb
169
+ - test/raise_on_unpermitted_params_test.rb
170
+ - test/test_helper.rb
171
+ has_rdoc: