diff options
Diffstat (limited to 'actionpack/test/controller/params_wrapper_test.rb')
-rw-r--r-- | actionpack/test/controller/params_wrapper_test.rb | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb index bed4a6a553..a50065bcc7 100644 --- a/actionpack/test/controller/params_wrapper_test.rb +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -2,7 +2,21 @@ require 'abstract_unit' module Admin; class User; end; end +module ParamsWrapperTestHelp + def with_default_wrapper_options(&block) + @controller.class._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 @@ -133,9 +147,8 @@ class ParamsWrapperTest < ActionController::TestCase end def test_derived_wrapped_keys_from_matching_model - User.expects(:respond_to?).with(:abstract_class?).returns(false) - User.expects(:respond_to?).with(:column_names).returns(true) - User.expects(:column_names).returns(["username"]) + User.expects(:respond_to?).with(:attribute_names).returns(true) + User.expects(:attribute_names).twice.returns(["username"]) with_default_wrapper_options do @request.env['CONTENT_TYPE'] = 'application/json' @@ -146,9 +159,8 @@ class ParamsWrapperTest < ActionController::TestCase def test_derived_wrapped_keys_from_specified_model with_default_wrapper_options do - Person.expects(:respond_to?).with(:abstract_class?).returns(false) - Person.expects(:respond_to?).with(:column_names).returns(true) - Person.expects(:column_names).returns(["username"]) + Person.expects(:respond_to?).with(:attribute_names).returns(true) + Person.expects(:attribute_names).twice.returns(["username"]) UsersController.wrap_parameters Person @@ -159,8 +171,8 @@ class ParamsWrapperTest < ActionController::TestCase end def test_not_wrapping_abstract_model - User.expects(:respond_to?).with(:abstract_class?).returns(true) - User.expects(:abstract_class?).returns(true) + User.expects(:respond_to?).with(:attribute_names).returns(true) + User.expects(:attribute_names).returns([]) with_default_wrapper_options do @request.env['CONTENT_TYPE'] = 'application/json' @@ -168,20 +180,11 @@ class ParamsWrapperTest < ActionController::TestCase assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu', 'title' => 'Developer' }}) end end - - private - def with_default_wrapper_options(&block) - @controller.class._wrapper_options = {:format => [:json]} - @controller.class.inherited(@controller.class) - yield - end - - def assert_parameters(expected) - assert_equal expected, UsersController.last_parameters - end end class NamespacedParamsWrapperTest < ActionController::TestCase + include ParamsWrapperTestHelp + module Admin module Users class UsersController < ActionController::Base; @@ -198,13 +201,13 @@ class NamespacedParamsWrapperTest < ActionController::TestCase end class SampleOne - def self.column_names + def self.attribute_names ["username"] end end class SampleTwo - def self.column_names + def self.attribute_names ["title"] end end @@ -249,14 +252,36 @@ class NamespacedParamsWrapperTest < ActionController::TestCase end end - private - def with_default_wrapper_options(&block) - @controller.class._wrapper_options = {:format => [:json]} - @controller.class.inherited(@controller.class) - yield +end + +class AnonymousControllerParamsWrapperTest < ActionController::TestCase + include ParamsWrapperTestHelp + + tests(Class.new(ActionController::Base) do + class << self + attr_accessor :last_parameters end - def assert_parameters(expected) - assert_equal expected, Admin::Users::UsersController.last_parameters + 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, { '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, { 'username' => 'sikachu' } + assert_parameters({ 'username' => 'sikachu', 'guest' => { 'username' => 'sikachu' }}) + end + end end |