PageRenderTime 58ms CodeModel.GetById 1ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 0ms

/actionpack/test/controller/params_wrapper_test.rb

https://github.com/edajade/rails
Ruby | 330 lines | 272 code | 58 blank | 0 comment | 0 complexity | 3b8e3e380eefcfd6fc50fc41b31d3d5c 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
191end
192
193class NamespacedParamsWrapperTest < ActionController::TestCase
194  include ParamsWrapperTestHelp
195
196  module Admin
197    module Users
198      class UsersController < ActionController::Base;
199        class << self
200          attr_accessor :last_parameters
201        end
202
203        def parse
204          self.class.last_parameters = request.params.except(:controller, :action)
205          head :ok
206        end
207      end
208    end
209  end
210
211  class SampleOne
212    def self.attribute_names
213      ["username"]
214    end
215  end
216
217  class SampleTwo
218    def self.attribute_names
219      ["title"]
220    end
221  end
222
223  tests Admin::Users::UsersController
224
225  def teardown
226    Admin::Users::UsersController.last_parameters = nil
227  end
228
229  def test_derived_name_from_controller
230    with_default_wrapper_options do
231      @request.env['CONTENT_TYPE'] = 'application/json'
232      post :parse, { 'username' => 'sikachu' }
233      assert_parameters({'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
234    end
235  end
236
237  def test_namespace_lookup_from_model
238    Admin.const_set(:User, Class.new(SampleOne))
239    begin
240      with_default_wrapper_options do
241        @request.env['CONTENT_TYPE'] = 'application/json'
242        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
243        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
244      end
245    ensure
246      Admin.send :remove_const, :User
247    end
248  end
249
250  def test_hierarchy_namespace_lookup_from_model
251    Object.const_set(:User, Class.new(SampleTwo))
252    begin
253      with_default_wrapper_options do
254        @request.env['CONTENT_TYPE'] = 'application/json'
255        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
256        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'title' => 'Developer' }})
257      end
258    ensure
259      Object.send :remove_const, :User
260    end
261  end
262
263end
264
265class AnonymousControllerParamsWrapperTest < ActionController::TestCase
266  include ParamsWrapperTestHelp
267
268  tests(Class.new(ActionController::Base) do
269    class << self
270      attr_accessor :last_parameters
271    end
272
273    def parse
274      self.class.last_parameters = request.params.except(:controller, :action)
275      head :ok
276    end
277  end)
278
279  def test_does_not_implicitly_wrap_params
280    with_default_wrapper_options do
281      @request.env['CONTENT_TYPE'] = 'application/json'
282      post :parse, { 'username' => 'sikachu' }
283      assert_parameters({ 'username' => 'sikachu' })
284    end
285  end
286
287  def test_does_wrap_params_if_name_provided
288    with_default_wrapper_options do
289      @controller.class.wrap_parameters(:name => "guest")
290      @request.env['CONTENT_TYPE'] = 'application/json'
291      post :parse, { 'username' => 'sikachu' }
292      assert_parameters({ 'username' => 'sikachu', 'guest' => { 'username' => 'sikachu' }})
293    end
294  end
295end
296
297class IrregularInflectionParamsWrapperTest < ActionController::TestCase
298  include ParamsWrapperTestHelp
299
300  class ParamswrappernewsItem
301    def self.attribute_names
302      ['test_attr']
303    end
304  end
305
306  class ParamswrappernewsController < ActionController::Base
307    class << self
308      attr_accessor :last_parameters
309    end
310
311    def parse
312      self.class.last_parameters = request.params.except(:controller, :action)
313      head :ok
314    end
315  end
316
317  tests ParamswrappernewsController
318
319  def test_uses_model_attribute_names_with_irregular_inflection
320    ActiveSupport::Inflector.inflections do |inflect|
321      inflect.irregular 'paramswrappernews_item', 'paramswrappernews'
322    end
323
324    with_default_wrapper_options do
325      @request.env['CONTENT_TYPE'] = 'application/json'
326      post :parse, { 'username' => 'sikachu', 'test_attr' => 'test_value' }
327      assert_parameters({ 'username' => 'sikachu', 'test_attr' => 'test_value', 'paramswrappernews_item' => { 'test_attr' => 'test_value' }})
328    end
329  end
330end