PageRenderTime 5ms CodeModel.GetById 4ms app.highlight 44ms RepoModel.GetById 5ms app.codeStats 0ms

/actionpack/test/controller/params_wrapper_test.rb

https://github.com/zooks-zz/rails
Ruby | 287 lines | 237 code | 50 blank | 0 comment | 0 complexity | 4243ad3a656f72a574205f23a172fd38 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._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_derived_name_from_controller
 41    with_default_wrapper_options do
 42      @request.env['CONTENT_TYPE'] = 'application/json'
 43      post :parse, { 'username' => 'sikachu' }
 44      assert_parameters({ 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
 45    end
 46  end
 47
 48  def test_specify_wrapper_name
 49    with_default_wrapper_options do
 50      UsersController.wrap_parameters :person
 51
 52      @request.env['CONTENT_TYPE'] = 'application/json'
 53      post :parse, { 'username' => 'sikachu' }
 54      assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }})
 55    end
 56  end
 57
 58  def test_specify_wrapper_model
 59    with_default_wrapper_options do
 60      UsersController.wrap_parameters Person
 61
 62      @request.env['CONTENT_TYPE'] = 'application/json'
 63      post :parse, { 'username' => 'sikachu' }
 64      assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }})
 65    end
 66  end
 67
 68  def test_specify_include_option
 69    with_default_wrapper_options do
 70      UsersController.wrap_parameters :include => :username
 71
 72      @request.env['CONTENT_TYPE'] = 'application/json'
 73      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
 74      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
 75    end
 76  end
 77
 78  def test_specify_exclude_option
 79    with_default_wrapper_options do
 80      UsersController.wrap_parameters :exclude => :title
 81
 82      @request.env['CONTENT_TYPE'] = 'application/json'
 83      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
 84      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
 85    end
 86  end
 87
 88  def test_specify_both_wrapper_name_and_include_option
 89    with_default_wrapper_options do
 90      UsersController.wrap_parameters :person, :include => :username
 91
 92      @request.env['CONTENT_TYPE'] = 'application/json'
 93      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
 94      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
 95    end
 96  end
 97
 98  def test_not_enabled_format
 99    with_default_wrapper_options do
100      @request.env['CONTENT_TYPE'] = 'application/xml'
101      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
102      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' })
103    end
104  end
105
106  def test_wrap_parameters_false
107    with_default_wrapper_options do
108      UsersController.wrap_parameters false
109      @request.env['CONTENT_TYPE'] = 'application/json'
110      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
111      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' })
112    end
113  end
114
115  def test_specify_format
116    with_default_wrapper_options do
117      UsersController.wrap_parameters :format => :xml
118
119      @request.env['CONTENT_TYPE'] = 'application/xml'
120      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
121      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu', 'title' => 'Developer' }})
122    end
123  end
124
125  def test_not_wrap_reserved_parameters
126    with_default_wrapper_options do
127      @request.env['CONTENT_TYPE'] = 'application/json'
128      post :parse, { 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu' }
129      assert_parameters({ 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
130    end
131  end
132
133  def test_no_double_wrap_if_key_exists
134    with_default_wrapper_options do
135      @request.env['CONTENT_TYPE'] = 'application/json'
136      post :parse, { 'user' => { 'username' => 'sikachu' }}
137      assert_parameters({ 'user' => { 'username' => 'sikachu' }})
138    end
139  end
140
141  def test_nested_params
142    with_default_wrapper_options do
143      @request.env['CONTENT_TYPE'] = 'application/json'
144      post :parse, { 'person' => { 'username' => 'sikachu' }}
145      assert_parameters({ 'person' => { 'username' => 'sikachu' }, 'user' => {'person' => { 'username' => 'sikachu' }}})
146    end
147  end
148
149  def test_derived_wrapped_keys_from_matching_model
150    User.expects(:respond_to?).with(:attribute_names).returns(true)
151    User.expects(:attribute_names).twice.returns(["username"])
152
153    with_default_wrapper_options do
154      @request.env['CONTENT_TYPE'] = 'application/json'
155      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
156      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
157    end
158  end
159
160  def test_derived_wrapped_keys_from_specified_model
161    with_default_wrapper_options do
162      Person.expects(:respond_to?).with(:attribute_names).returns(true)
163      Person.expects(:attribute_names).twice.returns(["username"])
164
165      UsersController.wrap_parameters Person
166
167      @request.env['CONTENT_TYPE'] = 'application/json'
168      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
169      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
170    end
171  end
172
173  def test_not_wrapping_abstract_model
174    User.expects(:respond_to?).with(:attribute_names).returns(true)
175    User.expects(:attribute_names).returns([])
176
177    with_default_wrapper_options do
178      @request.env['CONTENT_TYPE'] = 'application/json'
179      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
180      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu', 'title' => 'Developer' }})
181    end
182  end
183end
184
185class NamespacedParamsWrapperTest < ActionController::TestCase
186  include ParamsWrapperTestHelp
187
188  module Admin
189    module Users
190      class UsersController < ActionController::Base;
191        class << self
192          attr_accessor :last_parameters
193        end
194
195        def parse
196          self.class.last_parameters = request.params.except(:controller, :action)
197          head :ok
198        end
199      end
200    end
201  end
202
203  class SampleOne
204    def self.attribute_names
205      ["username"]
206    end
207  end
208
209  class SampleTwo
210    def self.attribute_names
211      ["title"]
212    end
213  end
214
215  tests Admin::Users::UsersController
216
217  def teardown
218    Admin::Users::UsersController.last_parameters = nil
219  end
220
221  def test_derived_name_from_controller
222    with_default_wrapper_options do
223      @request.env['CONTENT_TYPE'] = 'application/json'
224      post :parse, { 'username' => 'sikachu' }
225      assert_parameters({'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
226    end
227  end
228
229  def test_namespace_lookup_from_model
230    Admin.const_set(:User, Class.new(SampleOne))
231    begin
232      with_default_wrapper_options do
233        @request.env['CONTENT_TYPE'] = 'application/json'
234        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
235        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
236      end
237    ensure
238      Admin.send :remove_const, :User
239    end
240  end
241
242  def test_hierarchy_namespace_lookup_from_model
243    Object.const_set(:User, Class.new(SampleTwo))
244    begin
245      with_default_wrapper_options do
246        @request.env['CONTENT_TYPE'] = 'application/json'
247        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
248        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'title' => 'Developer' }})
249      end
250    ensure
251      Object.send :remove_const, :User
252    end
253  end
254
255end
256
257class AnonymousControllerParamsWrapperTest < ActionController::TestCase
258  include ParamsWrapperTestHelp
259
260  tests(Class.new(ActionController::Base) do
261    class << self
262      attr_accessor :last_parameters
263    end
264
265    def parse
266      self.class.last_parameters = request.params.except(:controller, :action)
267      head :ok
268    end
269  end)
270
271  def test_does_not_implicitly_wrap_params
272    with_default_wrapper_options do
273      @request.env['CONTENT_TYPE'] = 'application/json'
274      post :parse, { 'username' => 'sikachu' }
275      assert_parameters({ 'username' => 'sikachu' })
276    end
277  end
278
279  def test_does_wrap_params_if_name_provided
280    with_default_wrapper_options do
281      @controller.class.wrap_parameters(:name => "guest")
282      @request.env['CONTENT_TYPE'] = 'application/json'
283      post :parse, { 'username' => 'sikachu' }
284      assert_parameters({ 'username' => 'sikachu', 'guest' => { 'username' => 'sikachu' }})
285    end
286  end
287end