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
|
require 'test_helper'
require 'abstract_unit'
require 'action_controller/accessible_params_wrapper'
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 AccessibleParamsWrapperTest < 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; end
class Person; end
tests UsersController
def teardown
UsersController.last_parameters = nil
end
def test_derived_wrapped_keys_from_matching_model
User.expects(:respond_to?).with(:accessible_attributes).returns(false)
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'
post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
end
end
def test_derived_wrapped_keys_from_specified_model
with_default_wrapper_options do
Person.expects(:respond_to?).with(:accessible_attributes).returns(false)
Person.expects(:respond_to?).with(:attribute_names).returns(true)
Person.expects(:attribute_names).twice.returns(["username"])
UsersController.wrap_parameters Person
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
end
end
def test_accessible_wrapped_keys_from_matching_model
User.expects(:respond_to?).with(:accessible_attributes).returns(true)
User.expects(:accessible_attributes).with(:default).twice.returns(["username"])
with_default_wrapper_options do
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
end
end
end
|