action_args 2.3.2 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,194 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
- using ActionArgs::ParamsHandler
5
-
6
- class ActionArgs::ParamsHandlerTest < ActiveSupport::TestCase
7
- sub_test_case 'extract_method_arguments_from_params' do
8
- setup do
9
- params = {a: '1', b: '2'}
10
- @controller = Class.new(ApplicationController).new.tap {|c| c.params = params }
11
- end
12
- test 'no parameters' do
13
- def @controller.m() end
14
-
15
- assert_equal [], @controller.extract_method_arguments_from_params(:m)
16
- end
17
-
18
- test '1 req' do
19
- def @controller.m(a) end
20
-
21
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
22
- end
23
-
24
- test '2 reqs' do
25
- def @controller.m(a, b) end
26
-
27
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
28
- end
29
-
30
- test '1 opt with value' do
31
- def @controller.m(a = 'a') end
32
-
33
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
34
- end
35
-
36
- test '1 opt without value' do
37
- def @controller.m(x = 'x') end
38
-
39
- assert_equal [], @controller.extract_method_arguments_from_params(:m)
40
- end
41
-
42
- test 'req, opt with value' do
43
- def @controller.m(a, b = 'b') end
44
-
45
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
46
- end
47
-
48
- test 'req, opt without value' do
49
- def @controller.m(a, x = 'x') end
50
-
51
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
52
- end
53
-
54
- test 'opt with value, opt with value' do
55
- def @controller.m(a = 'a', b = 'b') end
56
-
57
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
58
- end
59
-
60
- test 'opt with value, opt without value' do
61
- def @controller.m(a = 'a', x = 'x') end
62
-
63
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
64
- end
65
-
66
- test 'opt without value, opt with value' do
67
- def @controller.m(x = 'x', a = 'a') end
68
-
69
- assert_equal [nil, '1'], @controller.extract_method_arguments_from_params(:m)
70
- end
71
-
72
- test 'opt without value, opt without value' do
73
- def @controller.m(x = 'x', y = 'y') end
74
-
75
- assert_equal [], @controller.extract_method_arguments_from_params(:m)
76
- end
77
-
78
- test 'opt with value, req' do
79
- def @controller.m(a = 'a', b) end
80
-
81
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
82
- end
83
-
84
- test 'opt without value, req' do
85
- def @controller.m(x = 'x', a) end
86
-
87
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
88
- end
89
-
90
- test 'opt without value, opt with value, req' do
91
- def @controller.m(x = 'x', b = 'b', a) end
92
-
93
- assert_equal [nil, '2', '1'], @controller.extract_method_arguments_from_params(:m)
94
- end
95
-
96
- test 'opt with value, opt without value, req' do
97
- def @controller.m(b = 'b', x = 'x', a) end
98
-
99
- assert_equal ['2', '1'], @controller.extract_method_arguments_from_params(:m)
100
- end
101
-
102
- test 'req without a value' do
103
- def @controller.m(x) end
104
-
105
- assert_raises(ActionController::BadRequest) { @controller.extract_method_arguments_from_params(:m) }
106
- end
107
-
108
- test 'key' do
109
- def @controller.m(a: nil) end
110
-
111
- assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
112
- end
113
-
114
- test 'key, key without value' do
115
- def @controller.m(a: nil, x: 'x') end
116
-
117
- assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
118
- end
119
-
120
- if RUBY_VERSION >= '2.1'
121
- eval <<-KWARGS_KEYREQ_TEST
122
- test 'keyreq' do
123
- def @controller.m(a:) end
124
-
125
- assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
126
- end
127
-
128
- test 'keyreq, keyreq without value' do
129
- def @controller.m(a:, x:) end
130
-
131
- assert_raises(ActionController::BadRequest) { @controller.extract_method_arguments_from_params(:m) }
132
- end
133
- KWARGS_KEYREQ_TEST
134
- end
135
- end
136
-
137
- sub_test_case 'strengthen_params!' do
138
- setup do
139
- @params = ActionController::Parameters.new(x: '1', y: '2', foo: {a: 'a', b: 'b'}, bar: {a: 'a', b: 'b'}, baz: {a: 'a', b: 'b'}, hoge: {a: 'a', b: 'b'}, fuga: {a: 'a', b: 'b'})
140
- end
141
-
142
- def execute_strengthen_params!(controller)
143
- c = controller.new
144
- c.instance_variable_set :@_params, @params
145
- c.strengthen_params! :a
146
- end
147
-
148
- test 'requiring via :req, permitting all scalars' do
149
- execute_strengthen_params! FooController ||= Class.new(ApplicationController) { permits :a, :b; def a(foo) end }
150
-
151
- assert @params[:foo].permitted?
152
- assert_not_nil @params[:foo][:a]
153
- assert_not_nil @params[:foo][:b]
154
- end
155
-
156
- test 'requiring via :req, not permitting all scalars' do
157
- execute_strengthen_params! BarController ||= Class.new(ApplicationController) { permits :a; def a(bar, x = 'x') end }
158
-
159
- assert @params[:bar].permitted?
160
- assert_not_nil @params[:bar][:a]
161
- assert_nil @params[:bar][:b]
162
- end
163
-
164
- test 'requiring via :req, not permitting any scalars' do
165
- execute_strengthen_params! BazController ||= Class.new(ApplicationController) { def a(baz, aho = 'omg') end }
166
-
167
- refute @params[:baz].permitted?
168
- end
169
-
170
- test 'requiring via :opt, permitting all scalars' do
171
- execute_strengthen_params! HogeController ||= Class.new(ApplicationController) { permits :a, :b; def a(hoge = {}) end }
172
-
173
- assert @params[:hoge].permitted?
174
- assert_not_nil @params[:hoge][:a]
175
- assert_not_nil @params[:hoge][:b]
176
- end
177
-
178
- test 'requiring via :key, permitting all scalars' do
179
- execute_strengthen_params! FugaController ||= Class.new(ApplicationController) { permits :a, :b; def a(fuga: {}) end }
180
-
181
- assert @params[:fuga].permitted?
182
- assert_not_nil @params[:fuga][:a]
183
- assert_not_nil @params[:fuga][:b]
184
- end
185
-
186
- test '"model_name" option' do
187
- execute_strengthen_params! PiyoController ||= Class.new(ApplicationController) { permits :a, :b, model_name: 'Foo'; def a(foo) end }
188
-
189
- assert @params[:foo].permitted?
190
- assert_not_nil @params[:foo][:a]
191
- assert_not_nil @params[:foo][:b]
192
- end
193
- end
194
- end
data/test/test_helper.rb DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- $LOAD_PATH.unshift(File.dirname(__FILE__))
5
- # load Rails first
6
- require 'rails'
7
- require 'active_record'
8
- require 'action_controller/railtie'
9
- begin
10
- require 'rails-controller-testing'
11
- rescue LoadError
12
- end
13
- require 'action_args'
14
- require 'fake_app'
15
- require 'test/unit/rails/test_help'
16
- Bundler.require
17
-
18
- if Rails.version < '5'
19
- module ActionControllerTestingMonkey
20
- def get(path, params: nil, session: nil)
21
- super path, params, session
22
- end
23
-
24
- def post(path, params: nil, session: nil)
25
- super path, params, session
26
- end
27
- end
28
-
29
- ActionController::TestCase.send :prepend, ActionControllerTestingMonkey
30
- end