2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2011-04-28 04:56:11 -04:00
|
|
|
|
|
|
|
module Admin; class User; end; end
|
|
|
|
|
2011-05-17 06:55:03 -04:00
|
|
|
module ParamsWrapperTestHelp
|
|
|
|
def with_default_wrapper_options(&block)
|
2016-08-06 13:44:11 -04:00
|
|
|
@controller.class._set_wrapper_options(format: [:json])
|
2011-05-17 06:55:03 -04:00
|
|
|
@controller.class.inherited(@controller.class)
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_parameters(expected)
|
|
|
|
assert_equal expected, self.class.controller_class.last_parameters
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
class ParamsWrapperTest < ActionController::TestCase
|
2011-05-17 06:55:03 -04:00
|
|
|
include ParamsWrapperTestHelp
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
class UsersController < ActionController::Base
|
2011-05-03 05:51:19 -04:00
|
|
|
class << self
|
|
|
|
attr_accessor :last_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_parameters = request.params.except(:controller, :action)
|
|
|
|
head :ok
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
2011-12-08 09:09:09 -05:00
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2015-09-05 10:58:21 -04:00
|
|
|
class User
|
|
|
|
def self.attribute_names
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2017-03-21 19:17:26 -04:00
|
|
|
def self.stored_attributes
|
2017-02-17 17:03:09 -05:00
|
|
|
{ settings: [:color, :size] }
|
|
|
|
end
|
2017-03-21 19:17:26 -04:00
|
|
|
end
|
2017-02-17 17:03:09 -05:00
|
|
|
|
2017-03-21 19:17:26 -04:00
|
|
|
class Person
|
2015-09-05 10:58:21 -04:00
|
|
|
def self.attribute_names
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
|
|
|
|
tests UsersController
|
|
|
|
|
2011-05-03 05:51:19 -04:00
|
|
|
def teardown
|
|
|
|
UsersController.last_parameters = nil
|
|
|
|
end
|
|
|
|
|
2012-04-29 11:16:32 -04:00
|
|
|
def test_filtered_parameters
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu" }
|
2016-12-28 21:53:51 -05:00
|
|
|
assert_equal({ "controller" => "params_wrapper_test/users", "action" => "parse", "username" => "sikachu", "user" => { "username" => "sikachu" } }, @request.filtered_parameters)
|
2012-04-29 11:16:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-03 14:05:01 -04:00
|
|
|
def test_derived_name_from_controller
|
2011-04-28 04:56:11 -04:00
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "user" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-17 17:03:09 -05:00
|
|
|
def test_store_accessors_wrapped
|
2017-03-21 19:17:26 -04:00
|
|
|
assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
|
|
|
|
with_default_wrapper_options do
|
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "color" => "blue", "size" => "large" }
|
|
|
|
assert_parameters("username" => "sikachu", "color" => "blue", "size" => "large",
|
|
|
|
"user" => { "username" => "sikachu", "color" => "blue", "size" => "large" })
|
|
|
|
end
|
2017-02-17 17:03:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
def test_specify_wrapper_name
|
|
|
|
with_default_wrapper_options do
|
|
|
|
UsersController.wrap_parameters :person
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "person" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_specify_wrapper_model
|
|
|
|
with_default_wrapper_options do
|
|
|
|
UsersController.wrap_parameters Person
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "person" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:33:25 -04:00
|
|
|
def test_specify_include_option
|
2011-04-28 04:56:11 -04:00
|
|
|
with_default_wrapper_options do
|
2016-08-06 13:35:13 -04:00
|
|
|
UsersController.wrap_parameters include: :username
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:33:25 -04:00
|
|
|
def test_specify_exclude_option
|
2011-04-28 04:56:11 -04:00
|
|
|
with_default_wrapper_options do
|
2016-08-06 13:35:13 -04:00
|
|
|
UsersController.wrap_parameters exclude: :title
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:33:25 -04:00
|
|
|
def test_specify_both_wrapper_name_and_include_option
|
2011-04-28 04:56:11 -04:00
|
|
|
with_default_wrapper_options do
|
2016-08-06 13:35:13 -04:00
|
|
|
UsersController.wrap_parameters :person, include: :username
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "person" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_not_enabled_format
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/xml"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer")
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-02 18:37:40 -04:00
|
|
|
def test_wrap_parameters_false
|
|
|
|
with_default_wrapper_options do
|
|
|
|
UsersController.wrap_parameters false
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer")
|
2011-05-02 18:37:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
def test_specify_format
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 13:35:13 -04:00
|
|
|
UsersController.wrap_parameters format: :xml
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/xml"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu", "title" => "Developer" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_not_wrap_reserved_parameters
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "authenticity_token" => "pwned", "_method" => "put", "utf8" => "☃", "username" => "sikachu" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("authenticity_token" => "pwned", "_method" => "put", "utf8" => "☃", "username" => "sikachu", "user" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_double_wrap_if_key_exists
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
2016-08-16 03:30:11 -04:00
|
|
|
post :parse, params: { "user" => { "username" => "sikachu" } }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("user" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-23 19:48:34 -04:00
|
|
|
def test_no_double_wrap_if_key_exists_and_value_is_nil
|
|
|
|
with_default_wrapper_options do
|
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "user" => nil }
|
|
|
|
assert_parameters("user" => nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
def test_nested_params
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
2016-08-16 03:30:11 -04:00
|
|
|
post :parse, params: { "person" => { "username" => "sikachu" } }
|
|
|
|
assert_parameters("person" => { "username" => "sikachu" }, "user" => { "person" => { "username" => "sikachu" } })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_derived_wrapped_keys_from_matching_model
|
2015-09-05 10:58:21 -04:00
|
|
|
assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_derived_wrapped_keys_from_specified_model
|
|
|
|
with_default_wrapper_options do
|
2015-09-05 10:58:21 -04:00
|
|
|
assert_called(Person, :attribute_names, times: 2, returns: ["username"]) do
|
|
|
|
UsersController.wrap_parameters Person
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "person" => { "username" => "sikachu" })
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-15 00:41:20 -04:00
|
|
|
def test_not_wrapping_abstract_model
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu", "title" => "Developer" })
|
2011-05-15 00:41:20 -04:00
|
|
|
end
|
|
|
|
end
|
2014-01-28 16:51:01 -05:00
|
|
|
|
|
|
|
def test_preserves_query_string_params
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
get :parse, params: { "user" => { "username" => "nixon" } }
|
2014-01-28 16:51:01 -05:00
|
|
|
assert_parameters(
|
2016-08-06 13:44:11 -04:00
|
|
|
"user" => { "username" => "nixon" }
|
2014-01-28 16:51:01 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-24 21:13:05 -04:00
|
|
|
def test_preserves_query_string_params_in_filtered_params
|
|
|
|
with_default_wrapper_options do
|
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
get :parse, params: { "user" => { "username" => "nixon" } }
|
|
|
|
assert_equal({ "controller" => "params_wrapper_test/users", "action" => "parse", "user" => { "username" => "nixon" } }, @request.filtered_parameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-28 16:51:01 -05:00
|
|
|
def test_empty_parameter_set
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
2015-01-04 04:35:06 -05:00
|
|
|
post :parse, params: {}
|
2014-01-28 16:51:01 -05:00
|
|
|
assert_parameters(
|
2016-08-16 03:30:11 -04:00
|
|
|
"user" => {}
|
2014-01-28 16:51:01 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
Don't error on an empty CONTENT_TYPE
This commit prevents a possible issue wherein an empty CONTENT_TYPE
header is sent in a request to a Rails application, and then `request.content_mime_type`
would return `nil`. This is because the `has_content_type?` guard method
was not properly checking the validity of a request's content type; it
was only checking to see whether or not the header existed, not whether
it had a value stored inside.
Relatedly, after an internal discussion, it was determined that the
`has_content_type?` method is not meant to be part of the public API,
and is therefore changed to a `:nodoc:` method in this commit.
The test for this behavior is a little bit ugly, for two reasons. One is
that it was difficult to determine where to place the test... I figured
the best place would be with the rest of the ParamsWrapper stuff, since
that's where the original issue was. Also, we have to do some fancy
footwork in calling `dispatch` on the test's controller manually... this
is because `ActionController::TestCase` will throw an error if you try
and pass in a nil content type, which is exactly what we are trying to
test here... Because of that, we have to manually call in to the
controller, and bypass the `post` request helper.
Fixes #26912.
This is a regression in behavior between Rails versions 4.2.x and 5.0.x,
which was introduced via [this commit](https://github.com/rails/rails/commit/a9f28600e901b11a9222e34bfae8642bfb753186).
2016-11-10 14:24:24 -05:00
|
|
|
|
|
|
|
def test_handles_empty_content_type
|
|
|
|
with_default_wrapper_options do
|
2016-11-13 23:08:19 -05:00
|
|
|
@request.env["CONTENT_TYPE"] = nil
|
Don't error on an empty CONTENT_TYPE
This commit prevents a possible issue wherein an empty CONTENT_TYPE
header is sent in a request to a Rails application, and then `request.content_mime_type`
would return `nil`. This is because the `has_content_type?` guard method
was not properly checking the validity of a request's content type; it
was only checking to see whether or not the header existed, not whether
it had a value stored inside.
Relatedly, after an internal discussion, it was determined that the
`has_content_type?` method is not meant to be part of the public API,
and is therefore changed to a `:nodoc:` method in this commit.
The test for this behavior is a little bit ugly, for two reasons. One is
that it was difficult to determine where to place the test... I figured
the best place would be with the rest of the ParamsWrapper stuff, since
that's where the original issue was. Also, we have to do some fancy
footwork in calling `dispatch` on the test's controller manually... this
is because `ActionController::TestCase` will throw an error if you try
and pass in a nil content type, which is exactly what we are trying to
test here... Because of that, we have to manually call in to the
controller, and bypass the `post` request helper.
Fixes #26912.
This is a regression in behavior between Rails versions 4.2.x and 5.0.x,
which was introduced via [this commit](https://github.com/rails/rails/commit/a9f28600e901b11a9222e34bfae8642bfb753186).
2016-11-10 14:24:24 -05:00
|
|
|
_controller_class.dispatch(:parse, @request, @response)
|
|
|
|
|
|
|
|
assert_equal 200, @response.status
|
2016-11-13 23:08:19 -05:00
|
|
|
assert_equal "", @response.body
|
Don't error on an empty CONTENT_TYPE
This commit prevents a possible issue wherein an empty CONTENT_TYPE
header is sent in a request to a Rails application, and then `request.content_mime_type`
would return `nil`. This is because the `has_content_type?` guard method
was not properly checking the validity of a request's content type; it
was only checking to see whether or not the header existed, not whether
it had a value stored inside.
Relatedly, after an internal discussion, it was determined that the
`has_content_type?` method is not meant to be part of the public API,
and is therefore changed to a `:nodoc:` method in this commit.
The test for this behavior is a little bit ugly, for two reasons. One is
that it was difficult to determine where to place the test... I figured
the best place would be with the rest of the ParamsWrapper stuff, since
that's where the original issue was. Also, we have to do some fancy
footwork in calling `dispatch` on the test's controller manually... this
is because `ActionController::TestCase` will throw an error if you try
and pass in a nil content type, which is exactly what we are trying to
test here... Because of that, we have to manually call in to the
controller, and bypass the `post` request helper.
Fixes #26912.
This is a regression in behavior between Rails versions 4.2.x and 5.0.x,
which was introduced via [this commit](https://github.com/rails/rails/commit/a9f28600e901b11a9222e34bfae8642bfb753186).
2016-11-10 14:24:24 -05:00
|
|
|
end
|
|
|
|
end
|
2017-10-20 21:07:14 -04:00
|
|
|
|
|
|
|
def test_derived_wrapped_keys_from_nested_attributes
|
|
|
|
def User.nested_attributes_options
|
|
|
|
{ person: {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
|
|
|
|
with_default_wrapper_options do
|
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "person_attributes" => { "title" => "Developer" } }
|
|
|
|
assert_parameters("username" => "sikachu", "person_attributes" => { "title" => "Developer" }, "user" => { "username" => "sikachu", "person_attributes" => { "title" => "Developer" } })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class NamespacedParamsWrapperTest < ActionController::TestCase
|
2011-05-17 06:55:03 -04:00
|
|
|
include ParamsWrapperTestHelp
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
module Admin
|
2011-05-10 18:08:18 -04:00
|
|
|
module Users
|
2017-10-28 08:15:34 -04:00
|
|
|
class UsersController < ActionController::Base
|
2011-05-10 18:08:18 -04:00
|
|
|
class << self
|
|
|
|
attr_accessor :last_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_parameters = request.params.except(:controller, :action)
|
|
|
|
head :ok
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
2011-05-02 18:37:40 -04:00
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2011-05-10 18:08:18 -04:00
|
|
|
class SampleOne
|
2011-05-15 14:25:00 -04:00
|
|
|
def self.attribute_names
|
2011-05-02 18:37:40 -04:00
|
|
|
["username"]
|
|
|
|
end
|
2020-01-19 16:03:58 -05:00
|
|
|
|
|
|
|
def self.attribute_aliases
|
|
|
|
{ "nick" => "username" }
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
|
2011-05-10 18:08:18 -04:00
|
|
|
class SampleTwo
|
2011-05-15 14:25:00 -04:00
|
|
|
def self.attribute_names
|
2011-05-10 18:08:18 -04:00
|
|
|
["title"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
tests Admin::Users::UsersController
|
2011-04-28 04:56:11 -04:00
|
|
|
|
2011-05-03 05:51:19 -04:00
|
|
|
def teardown
|
2011-05-10 18:08:18 -04:00
|
|
|
Admin::Users::UsersController.last_parameters = nil
|
2011-05-03 05:51:19 -04:00
|
|
|
end
|
|
|
|
|
2011-05-03 14:05:01 -04:00
|
|
|
def test_derived_name_from_controller
|
2011-04-28 04:56:11 -04:00
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "user" => { "username" => "sikachu" })
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-02 18:37:40 -04:00
|
|
|
def test_namespace_lookup_from_model
|
2011-05-10 18:08:18 -04:00
|
|
|
Admin.const_set(:User, Class.new(SampleOne))
|
2011-05-02 18:37:40 -04:00
|
|
|
begin
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
|
2011-05-02 18:37:40 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Admin.send :remove_const, :User
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-19 16:03:58 -05:00
|
|
|
def test_namespace_lookup_from_model_alias
|
|
|
|
Admin.const_set(:User, Class.new(SampleOne))
|
|
|
|
begin
|
|
|
|
with_default_wrapper_options do
|
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "nick" => "sikachu", "title" => "Developer" }
|
|
|
|
assert_parameters({ "nick" => "sikachu", "title" => "Developer", "user" => { "nick" => "sikachu" } })
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Admin.send :remove_const, :User
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-03 14:05:01 -04:00
|
|
|
def test_hierarchy_namespace_lookup_from_model
|
2011-05-10 18:08:18 -04:00
|
|
|
Object.const_set(:User, Class.new(SampleTwo))
|
2011-05-03 05:51:19 -04:00
|
|
|
begin
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "title" => "Developer" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "title" => "Developer" })
|
2011-05-03 05:51:19 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Object.send :remove_const, :User
|
|
|
|
end
|
|
|
|
end
|
2011-05-17 06:55:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class AnonymousControllerParamsWrapperTest < ActionController::TestCase
|
|
|
|
include ParamsWrapperTestHelp
|
|
|
|
|
|
|
|
tests(Class.new(ActionController::Base) do
|
|
|
|
class << self
|
|
|
|
attr_accessor :last_parameters
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
2011-05-03 05:51:19 -04:00
|
|
|
|
2011-05-17 06:55:03 -04:00
|
|
|
def parse
|
|
|
|
self.class.last_parameters = request.params.except(:controller, :action)
|
|
|
|
head :ok
|
2011-05-03 05:51:19 -04:00
|
|
|
end
|
2011-05-17 06:55:03 -04:00
|
|
|
end)
|
|
|
|
|
|
|
|
def test_does_not_implicitly_wrap_params
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu")
|
2011-05-17 06:55:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_does_wrap_params_if_name_provided
|
|
|
|
with_default_wrapper_options do
|
2016-08-06 13:35:13 -04:00
|
|
|
@controller.class.wrap_parameters(name: "guest")
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "guest" => { "username" => "sikachu" })
|
2011-05-17 06:55:03 -04:00
|
|
|
end
|
|
|
|
end
|
2011-05-15 00:41:20 -04:00
|
|
|
end
|
2011-12-05 11:05:03 -05:00
|
|
|
|
|
|
|
class IrregularInflectionParamsWrapperTest < ActionController::TestCase
|
|
|
|
include ParamsWrapperTestHelp
|
|
|
|
|
|
|
|
class ParamswrappernewsItem
|
|
|
|
def self.attribute_names
|
2016-08-06 12:54:50 -04:00
|
|
|
["test_attr"]
|
2011-12-05 11:05:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ParamswrappernewsController < ActionController::Base
|
|
|
|
class << self
|
|
|
|
attr_accessor :last_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_parameters = request.params.except(:controller, :action)
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
tests ParamswrappernewsController
|
|
|
|
|
|
|
|
def test_uses_model_attribute_names_with_irregular_inflection
|
2014-05-28 10:14:57 -04:00
|
|
|
with_dup do
|
|
|
|
ActiveSupport::Inflector.inflections do |inflect|
|
2016-08-06 12:54:50 -04:00
|
|
|
inflect.irregular "paramswrappernews_item", "paramswrappernews"
|
2014-05-28 10:14:57 -04:00
|
|
|
end
|
2011-12-05 11:05:03 -05:00
|
|
|
|
2014-05-28 10:14:57 -04:00
|
|
|
with_default_wrapper_options do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :parse, params: { "username" => "sikachu", "test_attr" => "test_value" }
|
2016-08-06 13:44:11 -04:00
|
|
|
assert_parameters("username" => "sikachu", "test_attr" => "test_value", "paramswrappernews_item" => { "test_attr" => "test_value" })
|
2014-05-28 10:14:57 -04:00
|
|
|
end
|
2011-12-05 11:05:03 -05:00
|
|
|
end
|
|
|
|
end
|
2014-05-28 10:14:57 -04:00
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def with_dup
|
|
|
|
original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__)[:en]
|
|
|
|
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original.dup)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, en: original)
|
|
|
|
end
|
2011-12-05 11:05:03 -05:00
|
|
|
end
|