PageRenderTime 5ms CodeModel.GetById 2ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 0ms

/actionpack/test/controller/params_wrapper_test.rb

https://github.com/zucaritask/rails
Ruby | 362 lines | 300 code | 62 blank | 0 comment | 0 complexity | 6a307a8bfb98e1f08a6475433eb19f6a MD5 | raw file
  1require 'abstract_unit'
  2
  3module Admin; class User; end; end
  4
  5module ParamsWrapperTestHelp
  6  def with_default_wrapper_options(&block)
  7    @controller.class._set_wrapper_options({:format => [:json]})
  8    @controller.class.inherited(@controller.class)
  9    yield
 10  end
 11
 12  def assert_parameters(expected)
 13    assert_equal expected, self.class.controller_class.last_parameters
 14  end
 15end
 16
 17class ParamsWrapperTest < ActionController::TestCase
 18  include ParamsWrapperTestHelp
 19
 20  class UsersController < ActionController::Base
 21    class << self
 22      attr_accessor :last_parameters
 23    end
 24
 25    def parse
 26      self.class.last_parameters = request.params.except(:controller, :action)
 27      head :ok
 28    end
 29  end
 30
 31  class User; end
 32  class Person; end
 33
 34  tests UsersController
 35
 36  def teardown
 37    UsersController.last_parameters = nil
 38  end
 39
 40  def test_filtered_parameters
 41    with_default_wrapper_options do
 42      @request.env['CONTENT_TYPE'] = 'application/json'
 43      post :parse, { 'username' => 'sikachu' }
 44      assert_equal @request.filtered_parameters, { 'controller' => 'params_wrapper_test/users', 'action' => 'parse', 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' } }
 45    end
 46  end
 47
 48  def test_derived_name_from_controller
 49    with_default_wrapper_options do
 50      @request.env['CONTENT_TYPE'] = 'application/json'
 51      post :parse, { 'username' => 'sikachu' }
 52      assert_parameters({ 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
 53    end
 54  end
 55
 56  def test_specify_wrapper_name
 57    with_default_wrapper_options do
 58      UsersController.wrap_parameters :person
 59
 60      @request.env['CONTENT_TYPE'] = 'application/json'
 61      post :parse, { 'username' => 'sikachu' }
 62      assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }})
 63    end
 64  end
 65
 66  def test_specify_wrapper_model
 67    with_default_wrapper_options do
 68      UsersController.wrap_parameters Person
 69
 70      @request.env['CONTENT_TYPE'] = 'application/json'
 71      post :parse, { 'username' => 'sikachu' }
 72      assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }})
 73    end
 74  end
 75
 76  def test_specify_include_option
 77    with_default_wrapper_options do
 78      UsersController.wrap_parameters :include => :username
 79
 80      @request.env['CONTENT_TYPE'] = 'application/json'
 81      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
 82      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
 83    end
 84  end
 85
 86  def test_specify_exclude_option
 87    with_default_wrapper_options do
 88      UsersController.wrap_parameters :exclude => :title
 89
 90      @request.env['CONTENT_TYPE'] = 'application/json'
 91      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
 92      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
 93    end
 94  end
 95
 96  def test_specify_both_wrapper_name_and_include_option
 97    with_default_wrapper_options do
 98      UsersController.wrap_parameters :person, :include => :username
 99
100      @request.env['CONTENT_TYPE'] = 'application/json'
101      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
102      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
103    end
104  end
105
106  def test_not_enabled_format
107    with_default_wrapper_options do
108      @request.env['CONTENT_TYPE'] = 'application/xml'
109      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
110      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' })
111    end
112  end
113
114  def test_wrap_parameters_false
115    with_default_wrapper_options do
116      UsersController.wrap_parameters false
117      @request.env['CONTENT_TYPE'] = 'application/json'
118      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
119      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' })
120    end
121  end
122
123  def test_specify_format
124    with_default_wrapper_options do
125      UsersController.wrap_parameters :format => :xml
126
127      @request.env['CONTENT_TYPE'] = 'application/xml'
128      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
129      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu', 'title' => 'Developer' }})
130    end
131  end
132
133  def test_not_wrap_reserved_parameters
134    with_default_wrapper_options do
135      @request.env['CONTENT_TYPE'] = 'application/json'
136      post :parse, { 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu' }
137      assert_parameters({ 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
138    end
139  end
140
141  def test_no_double_wrap_if_key_exists
142    with_default_wrapper_options do
143      @request.env['CONTENT_TYPE'] = 'application/json'
144      post :parse, { 'user' => { 'username' => 'sikachu' }}
145      assert_parameters({ 'user' => { 'username' => 'sikachu' }})
146    end
147  end
148
149  def test_nested_params
150    with_default_wrapper_options do
151      @request.env['CONTENT_TYPE'] = 'application/json'
152      post :parse, { 'person' => { 'username' => 'sikachu' }}
153      assert_parameters({ 'person' => { 'username' => 'sikachu' }, 'user' => {'person' => { 'username' => 'sikachu' }}})
154    end
155  end
156
157  def test_derived_wrapped_keys_from_matching_model
158    User.expects(:respond_to?).with(:attribute_names).returns(true)
159    User.expects(:attribute_names).twice.returns(["username"])
160
161    with_default_wrapper_options do
162      @request.env['CONTENT_TYPE'] = 'application/json'
163      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
164      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
165    end
166  end
167
168  def test_derived_wrapped_keys_from_specified_model
169    with_default_wrapper_options do
170      Person.expects(:respond_to?).with(:attribute_names).returns(true)
171      Person.expects(:attribute_names).twice.returns(["username"])
172
173      UsersController.wrap_parameters Person
174
175      @request.env['CONTENT_TYPE'] = 'application/json'
176      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
177      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
178    end
179  end
180
181  def test_not_wrapping_abstract_model
182    User.expects(:respond_to?).with(:attribute_names).returns(true)
183    User.expects(:attribute_names).returns([])
184
185    with_default_wrapper_options do
186      @request.env['CONTENT_TYPE'] = 'application/json'
187      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
188      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu', 'title' => 'Developer' }})
189    end
190  end
191
192  def test_preserves_query_string_params
193    with_default_wrapper_options do
194      @request.env['CONTENT_TYPE'] = 'application/json'
195      get :parse, { 'user' => { 'username' => 'nixon' } }
196      assert_parameters(
197        {'user' => { 'username' => 'nixon' } }
198      )
199    end
200  end
201
202  def test_empty_parameter_set
203    with_default_wrapper_options do
204      @request.env['CONTENT_TYPE'] = 'application/json'
205      post :parse, {}
206      assert_parameters(
207        {'user' => { } }
208      )
209    end
210  end
211end
212
213class NamespacedParamsWrapperTest < ActionController::TestCase
214  include ParamsWrapperTestHelp
215
216  module Admin
217    module Users
218      class UsersController < ActionController::Base;
219        class << self
220          attr_accessor :last_parameters
221        end
222
223        def parse
224          self.class.last_parameters = request.params.except(:controller, :action)
225          head :ok
226        end
227      end
228    end
229  end
230
231  class SampleOne
232    def self.attribute_names
233      ["username"]
234    end
235  end
236
237  class SampleTwo
238    def self.attribute_names
239      ["title"]
240    end
241  end
242
243  tests Admin::Users::UsersController
244
245  def teardown
246    Admin::Users::UsersController.last_parameters = nil
247  end
248
249  def test_derived_name_from_controller
250    with_default_wrapper_options do
251      @request.env['CONTENT_TYPE'] = 'application/json'
252      post :parse, { 'username' => 'sikachu' }
253      assert_parameters({'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
254    end
255  end
256
257  def test_namespace_lookup_from_model
258    Admin.const_set(:User, Class.new(SampleOne))
259    begin
260      with_default_wrapper_options do
261        @request.env['CONTENT_TYPE'] = 'application/json'
262        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
263        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
264      end
265    ensure
266      Admin.send :remove_const, :User
267    end
268  end
269
270  def test_hierarchy_namespace_lookup_from_model
271    Object.const_set(:User, Class.new(SampleTwo))
272    begin
273      with_default_wrapper_options do
274        @request.env['CONTENT_TYPE'] = 'application/json'
275        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
276        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'title' => 'Developer' }})
277      end
278    ensure
279      Object.send :remove_const, :User
280    end
281  end
282
283end
284
285class AnonymousControllerParamsWrapperTest < ActionController::TestCase
286  include ParamsWrapperTestHelp
287
288  tests(Class.new(ActionController::Base) do
289    class << self
290      attr_accessor :last_parameters
291    end
292
293    def parse
294      self.class.last_parameters = request.params.except(:controller, :action)
295      head :ok
296    end
297  end)
298
299  def test_does_not_implicitly_wrap_params
300    with_default_wrapper_options do
301      @request.env['CONTENT_TYPE'] = 'application/json'
302      post :parse, { 'username' => 'sikachu' }
303      assert_parameters({ 'username' => 'sikachu' })
304    end
305  end
306
307  def test_does_wrap_params_if_name_provided
308    with_default_wrapper_options do
309      @controller.class.wrap_parameters(:name => "guest")
310      @request.env['CONTENT_TYPE'] = 'application/json'
311      post :parse, { 'username' => 'sikachu' }
312      assert_parameters({ 'username' => 'sikachu', 'guest' => { 'username' => 'sikachu' }})
313    end
314  end
315end
316
317class IrregularInflectionParamsWrapperTest < ActionController::TestCase
318  include ParamsWrapperTestHelp
319
320  class ParamswrappernewsItem
321    def self.attribute_names
322      ['test_attr']
323    end
324  end
325
326  class ParamswrappernewsController < ActionController::Base
327    class << self
328      attr_accessor :last_parameters
329    end
330
331    def parse
332      self.class.last_parameters = request.params.except(:controller, :action)
333      head :ok
334    end
335  end
336
337  tests ParamswrappernewsController
338
339  def test_uses_model_attribute_names_with_irregular_inflection
340    with_dup do
341      ActiveSupport::Inflector.inflections do |inflect|
342        inflect.irregular 'paramswrappernews_item', 'paramswrappernews'
343      end
344
345      with_default_wrapper_options do
346        @request.env['CONTENT_TYPE'] = 'application/json'
347        post :parse, { 'username' => 'sikachu', 'test_attr' => 'test_value' }
348        assert_parameters({ 'username' => 'sikachu', 'test_attr' => 'test_value', 'paramswrappernews_item' => { 'test_attr' => 'test_value' }})
349      end
350    end
351  end
352
353  private
354
355  def with_dup
356    original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
357    ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original.dup)
358    yield
359  ensure
360    ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original)
361  end
362end