aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/params_wrapper_test.rb
blob: 1eb92abae4f6a55f3a44beea79253f00ccb22648 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
require "abstract_unit"

module Admin; class User; end; end

module ParamsWrapperTestHelp
  def with_default_wrapper_options(&block)
    @controller.class._set_wrapper_options(format: [:json])
    @controller.class.inherited(@controller.class)
    yield
  end

  def assert_parameters(expected)
    assert_equal expected, self.class.controller_class.last_parameters
  end
end

class ParamsWrapperTest < ActionController::TestCase
  include ParamsWrapperTestHelp

  class UsersController < ActionController::Base
    class << self
      attr_accessor :last_parameters
    end

    def parse
      self.class.last_parameters = request.params.except(:controller, :action)
      head :ok
    end
  end

  class User
    def self.attribute_names
      []
    end
  end

  class Person
    def self.stores_attributes
      { settings: [:color, :size] }
    end

    def self.attribute_names
      []
    end
  end

  tests UsersController

  def teardown
    UsersController.last_parameters = nil
  end

  def test_filtered_parameters
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu" }
      assert_equal({ "controller" => "params_wrapper_test/users", "action" => "parse", "username" => "sikachu", "user" => { "username" => "sikachu" } }, @request.filtered_parameters)
    end
  end

  def test_derived_name_from_controller
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu" }
      assert_parameters("username" => "sikachu", "user" => { "username" => "sikachu" })
    end
  end

  def test_store_accessors_wrapped
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "color" => "blue", "size" => "large" }
      assert_parameters("username" => "sikachu", "color" => "blue", "size" => "large",
                         "user" => { "username" => "sikachu", "color" => "blue", "size" => "large" })
    end
  end

  def test_specify_wrapper_name
    with_default_wrapper_options do
      UsersController.wrap_parameters :person

      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu" }
      assert_parameters("username" => "sikachu", "person" => { "username" => "sikachu" })
    end
  end

  def test_specify_wrapper_model
    with_default_wrapper_options do
      UsersController.wrap_parameters Person

      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu" }
      assert_parameters("username" => "sikachu", "person" => { "username" => "sikachu" })
    end
  end

  def test_specify_include_option
    with_default_wrapper_options do
      UsersController.wrap_parameters include: :username

      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
    end
  end

  def test_specify_exclude_option
    with_default_wrapper_options do
      UsersController.wrap_parameters exclude: :title

      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
    end
  end

  def test_specify_both_wrapper_name_and_include_option
    with_default_wrapper_options do
      UsersController.wrap_parameters :person, include: :username

      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer", "person" => { "username" => "sikachu" })
    end
  end

  def test_not_enabled_format
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/xml"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer")
    end
  end

  def test_wrap_parameters_false
    with_default_wrapper_options do
      UsersController.wrap_parameters false
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer")
    end
  end

  def test_specify_format
    with_default_wrapper_options do
      UsersController.wrap_parameters format: :xml

      @request.env["CONTENT_TYPE"] = "application/xml"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu", "title" => "Developer" })
    end
  end

  def test_not_wrap_reserved_parameters
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "authenticity_token" => "pwned", "_method" => "put", "utf8" => "&#9731;", "username" => "sikachu" }
      assert_parameters("authenticity_token" => "pwned", "_method" => "put", "utf8" => "&#9731;", "username" => "sikachu", "user" => { "username" => "sikachu" })
    end
  end

  def test_no_double_wrap_if_key_exists
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "user" => { "username" => "sikachu" } }
      assert_parameters("user" => { "username" => "sikachu" })
    end
  end

  def test_nested_params
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "person" => { "username" => "sikachu" } }
      assert_parameters("person" => { "username" => "sikachu" }, "user" => { "person" => { "username" => "sikachu" } })
    end
  end

  def test_derived_wrapped_keys_from_matching_model
    assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
      with_default_wrapper_options do
        @request.env["CONTENT_TYPE"] = "application/json"
        post :parse, params: { "username" => "sikachu", "title" => "Developer" }
        assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
      end
    end
  end

  def test_derived_wrapped_keys_from_specified_model
    with_default_wrapper_options do
      assert_called(Person, :attribute_names, times: 2, returns: ["username"]) do
        UsersController.wrap_parameters Person

        @request.env["CONTENT_TYPE"] = "application/json"
        post :parse, params: { "username" => "sikachu", "title" => "Developer" }
        assert_parameters("username" => "sikachu", "title" => "Developer", "person" => { "username" => "sikachu" })
      end
    end
  end

  def test_not_wrapping_abstract_model
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu", "title" => "Developer" })
    end
  end

  def test_preserves_query_string_params
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      get :parse, params: { "user" => { "username" => "nixon" } }
      assert_parameters(
        "user" => { "username" => "nixon" }
      )
    end
  end

  def test_empty_parameter_set
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: {}
      assert_parameters(
        "user" => {}
      )
    end
  end

  def test_handles_empty_content_type
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = nil
      _controller_class.dispatch(:parse, @request, @response)

      assert_equal 200, @response.status
      assert_equal "", @response.body
    end
  end
end

class NamespacedParamsWrapperTest < ActionController::TestCase
  include ParamsWrapperTestHelp

  module Admin
    module Users
      class UsersController < ActionController::Base;
        class << self
          attr_accessor :last_parameters
        end

        def parse
          self.class.last_parameters = request.params.except(:controller, :action)
          head :ok
        end
      end
    end
  end

  class SampleOne
    def self.attribute_names
      ["username"]
    end
  end

  class SampleTwo
    def self.attribute_names
      ["title"]
    end
  end

  tests Admin::Users::UsersController

  def teardown
    Admin::Users::UsersController.last_parameters = nil
  end

  def test_derived_name_from_controller
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu" }
      assert_parameters("username" => "sikachu", "user" => { "username" => "sikachu" })
    end
  end

  def test_namespace_lookup_from_model
    Admin.const_set(:User, Class.new(SampleOne))
    begin
      with_default_wrapper_options do
        @request.env["CONTENT_TYPE"] = "application/json"
        post :parse, params: { "username" => "sikachu", "title" => "Developer" }
        assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
      end
    ensure
      Admin.send :remove_const, :User
    end
  end

  def test_hierarchy_namespace_lookup_from_model
    Object.const_set(:User, Class.new(SampleTwo))
    begin
      with_default_wrapper_options do
        @request.env["CONTENT_TYPE"] = "application/json"
        post :parse, params: { "username" => "sikachu", "title" => "Developer" }
        assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "title" => "Developer" })
      end
    ensure
      Object.send :remove_const, :User
    end
  end
end

class AnonymousControllerParamsWrapperTest < ActionController::TestCase
  include ParamsWrapperTestHelp

  tests(Class.new(ActionController::Base) do
    class << self
      attr_accessor :last_parameters
    end

    def parse
      self.class.last_parameters = request.params.except(:controller, :action)
      head :ok
    end
  end)

  def test_does_not_implicitly_wrap_params
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu" }
      assert_parameters("username" => "sikachu")
    end
  end

  def test_does_wrap_params_if_name_provided
    with_default_wrapper_options do
      @controller.class.wrap_parameters(name: "guest")
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu" }
      assert_parameters("username" => "sikachu", "guest" => { "username" => "sikachu" })
    end
  end
end

class IrregularInflectionParamsWrapperTest < ActionController::TestCase
  include ParamsWrapperTestHelp

  class ParamswrappernewsItem
    def self.attribute_names
      ["test_attr"]
    end
  end

  class ParamswrappernewsController < ActionController::Base
    class << self
      attr_accessor :last_parameters
    end

    def parse
      self.class.last_parameters = request.params.except(:controller, :action)
      head :ok
    end
  end

  tests ParamswrappernewsController

  def test_uses_model_attribute_names_with_irregular_inflection
    with_dup do
      ActiveSupport::Inflector.inflections do |inflect|
        inflect.irregular "paramswrappernews_item", "paramswrappernews"
      end

      with_default_wrapper_options do
        @request.env["CONTENT_TYPE"] = "application/json"
        post :parse, params: { "username" => "sikachu", "test_attr" => "test_value" }
        assert_parameters("username" => "sikachu", "test_attr" => "test_value", "paramswrappernews_item" => { "test_attr" => "test_value" })
      end
    end
  end

  private

    def with_dup
      original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
      ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original.dup)
      yield
    ensure
      ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original)
    end
end