2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "action_dispatch/http/upload"
|
|
|
|
require "action_controller/metal/strong_parameters"
|
2012-07-12 01:50:42 -04:00
|
|
|
|
2012-08-29 11:08:58 -04:00
|
|
|
class ParametersPermitTest < ActiveSupport::TestCase
|
2013-01-20 11:59:53 -05:00
|
|
|
def assert_filtered_out(params, key)
|
|
|
|
assert !params.has_key?(key), "key #{key.inspect} has not been filtered out"
|
|
|
|
end
|
|
|
|
|
2012-07-12 01:50:42 -04:00
|
|
|
setup do
|
2013-12-23 16:55:03 -05:00
|
|
|
@params = ActionController::Parameters.new(
|
|
|
|
person: {
|
2016-08-06 12:54:50 -04:00
|
|
|
age: "32",
|
2013-12-23 16:55:03 -05:00
|
|
|
name: {
|
2016-08-06 12:54:50 -04:00
|
|
|
first: "David",
|
|
|
|
last: "Heinemeier Hansson"
|
2013-12-23 16:55:03 -05:00
|
|
|
},
|
2016-08-16 03:30:11 -04:00
|
|
|
addresses: [{ city: "Chicago", state: "Illinois" }]
|
2013-12-23 16:55:03 -05:00
|
|
|
}
|
|
|
|
)
|
2013-01-20 11:59:53 -05:00
|
|
|
|
|
|
|
@struct_fields = []
|
|
|
|
%w(0 1 12).each do |number|
|
2016-08-06 12:54:50 -04:00
|
|
|
["", "i", "f"].each do |suffix|
|
2013-01-20 11:59:53 -05:00
|
|
|
@struct_fields << "sf(#{number}#{suffix})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 14:20:22 -04:00
|
|
|
def walk_permitted(params)
|
2016-10-28 23:05:58 -04:00
|
|
|
params.each do |k, v|
|
2016-02-17 19:12:18 -05:00
|
|
|
case v
|
|
|
|
when ActionController::Parameters
|
|
|
|
walk_permitted v
|
|
|
|
when Array
|
|
|
|
v.each { |x| walk_permitted v }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "iteration should not impact permit" do
|
2016-10-28 23:05:58 -04:00
|
|
|
hash = { "foo" => { "bar" => { "0" => { "baz" => "hello", "zot" => "1" } } } }
|
2016-02-17 19:12:18 -05:00
|
|
|
params = ActionController::Parameters.new(hash)
|
|
|
|
|
|
|
|
walk_permitted params
|
|
|
|
|
|
|
|
sanitized = params[:foo].permit(bar: [:baz])
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "0" => { "baz" => "hello" } }, sanitized[:bar].to_unsafe_h)
|
2016-02-17 19:12:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "if nothing is permitted, the hash becomes empty" do
|
|
|
|
params = ActionController::Parameters.new(id: "1234")
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate permitted, :permitted?
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty permitted
|
2013-01-20 11:59:53 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key: permitted scalar values" do
|
|
|
|
values = ["a", :a, nil]
|
2017-12-14 19:13:07 -05:00
|
|
|
values += [0, 1.0, 2**128, BigDecimal(1)]
|
2013-01-20 11:59:53 -05:00
|
|
|
values += [true, false]
|
|
|
|
values += [Date.today, Time.now, DateTime.now]
|
2013-02-21 09:00:26 -05:00
|
|
|
values += [STDOUT, StringIO.new, ActionDispatch::Http::UploadedFile.new(tempfile: __FILE__),
|
|
|
|
Rack::Test::UploadedFile.new(__FILE__)]
|
2013-01-20 11:59:53 -05:00
|
|
|
|
|
|
|
values.each do |value|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: value)
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit(:id)
|
2017-01-18 02:09:58 -05:00
|
|
|
if value.nil?
|
|
|
|
assert_nil permitted[:id]
|
|
|
|
else
|
|
|
|
assert_equal value, permitted[:id]
|
|
|
|
end
|
2013-01-20 11:59:53 -05:00
|
|
|
|
|
|
|
@struct_fields.each do |sf|
|
|
|
|
params = ActionController::Parameters.new(sf => value)
|
|
|
|
permitted = params.permit(:sf)
|
2017-01-18 02:09:58 -05:00
|
|
|
if value.nil?
|
|
|
|
assert_nil permitted[sf]
|
|
|
|
else
|
|
|
|
assert_equal value, permitted[sf]
|
|
|
|
end
|
2013-01-20 11:59:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key: unknown keys are filtered out" do
|
|
|
|
params = ActionController::Parameters.new(id: "1234", injected: "injected")
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit(:id)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "1234", permitted[:id]
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_filtered_out permitted, :injected
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key: arrays are filtered out" do
|
|
|
|
[[], [1], ["1"]].each do |array|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: array)
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit(:id)
|
|
|
|
assert_filtered_out permitted, :id
|
|
|
|
|
|
|
|
@struct_fields.each do |sf|
|
|
|
|
params = ActionController::Parameters.new(sf => array)
|
|
|
|
permitted = params.permit(:sf)
|
|
|
|
assert_filtered_out permitted, sf
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key: hashes are filtered out" do
|
2016-08-16 03:30:11 -04:00
|
|
|
[{}, { foo: 1 }, { foo: "bar" }].each do |hash|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: hash)
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit(:id)
|
|
|
|
assert_filtered_out permitted, :id
|
|
|
|
|
|
|
|
@struct_fields.each do |sf|
|
|
|
|
params = ActionController::Parameters.new(sf => hash)
|
|
|
|
permitted = params.permit(:sf)
|
|
|
|
assert_filtered_out permitted, sf
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key: non-permitted scalar values are filtered out" do
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: Object.new)
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit(:id)
|
|
|
|
assert_filtered_out permitted, :id
|
|
|
|
|
|
|
|
@struct_fields.each do |sf|
|
|
|
|
params = ActionController::Parameters.new(sf => Object.new)
|
|
|
|
permitted = params.permit(:sf)
|
|
|
|
assert_filtered_out permitted, sf
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key: it is not assigned if not present in params" do
|
|
|
|
params = ActionController::Parameters.new(name: "Joe")
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit(:id)
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not permitted.has_key?(:id)
|
2013-01-20 11:59:53 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key to empty array: empty arrays pass" do
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: [])
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_equal [], permitted[:id]
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "do not break params filtering on nil values" do
|
2013-09-07 08:16:45 -04:00
|
|
|
params = ActionController::Parameters.new(a: 1, b: [1, 2, 3], c: nil)
|
|
|
|
|
|
|
|
permitted = params.permit(:a, c: [], b: [])
|
|
|
|
assert_equal 1, permitted[:a]
|
|
|
|
assert_equal [1, 2, 3], permitted[:b]
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil permitted[:c]
|
2013-09-07 08:16:45 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key to empty array: arrays of permitted scalars pass" do
|
|
|
|
[["foo"], [1], ["foo", "bar"], [1, 2, 3]].each do |array|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: array)
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_equal array, permitted[:id]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key to empty array: permitted scalar values do not pass" do
|
|
|
|
["foo", 1].each do |permitted_scalar|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: permitted_scalar)
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_filtered_out permitted, :id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "key to empty array: arrays of non-permitted scalar do not pass" do
|
2016-08-16 03:30:11 -04:00
|
|
|
[[Object.new], [[]], [[1]], [{}], [{ id: "1" }]].each do |non_permitted_scalar|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: non_permitted_scalar)
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_filtered_out permitted, :id
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 03:58:58 -05:00
|
|
|
test "key to empty hash: arbitrary hashes are permitted" do
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
username: "fxn",
|
|
|
|
preferences: {
|
|
|
|
scheme: "Marazul",
|
|
|
|
font: {
|
|
|
|
name: "Source Code Pro",
|
|
|
|
size: 12
|
|
|
|
},
|
|
|
|
tabstops: [4, 8, 12, 16],
|
|
|
|
suspicious: [true, Object.new, false, /yo!/],
|
2016-11-14 13:18:31 -05:00
|
|
|
dubious: [{ a: :a, b: /wtf!/ }, { c: :c }],
|
2016-11-08 03:58:58 -05:00
|
|
|
injected: Object.new
|
|
|
|
},
|
|
|
|
hacked: 1 # not a hash
|
|
|
|
)
|
|
|
|
|
|
|
|
permitted = params.permit(:username, preferences: {}, hacked: {})
|
|
|
|
|
|
|
|
assert_equal "fxn", permitted[:username]
|
|
|
|
assert_equal "Marazul", permitted[:preferences][:scheme]
|
|
|
|
assert_equal "Source Code Pro", permitted[:preferences][:font][:name]
|
|
|
|
assert_equal 12, permitted[:preferences][:font][:size]
|
|
|
|
assert_equal [4, 8, 12, 16], permitted[:preferences][:tabstops]
|
|
|
|
assert_equal [true, false], permitted[:preferences][:suspicious]
|
|
|
|
assert_equal :a, permitted[:preferences][:dubious][0][:a]
|
|
|
|
assert_equal :c, permitted[:preferences][:dubious][1][:c]
|
|
|
|
|
|
|
|
assert_filtered_out permitted[:preferences][:dubious][0], :b
|
|
|
|
assert_filtered_out permitted[:preferences], :injected
|
|
|
|
assert_filtered_out permitted, :hacked
|
|
|
|
end
|
|
|
|
|
2012-07-12 01:50:42 -04:00
|
|
|
test "fetch raises ParameterMissing exception" do
|
|
|
|
e = assert_raises(ActionController::ParameterMissing) do
|
|
|
|
@params.fetch :foo
|
|
|
|
end
|
|
|
|
assert_equal :foo, e.param
|
|
|
|
end
|
|
|
|
|
2013-10-26 22:22:31 -04:00
|
|
|
test "fetch with a default value of a hash does not mutate the object" do
|
|
|
|
params = ActionController::Parameters.new({})
|
|
|
|
params.fetch :foo, {}
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil params[:foo]
|
2013-10-26 22:22:31 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "hashes in array values get wrapped" do
|
2013-12-20 19:11:47 -05:00
|
|
|
params = ActionController::Parameters.new(foo: [{}, {}])
|
|
|
|
params[:foo].each do |hash|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate hash, :permitted?
|
2013-12-20 19:11:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-07 07:17:50 -04:00
|
|
|
# Strong params has an optimization to avoid looping every time you read
|
|
|
|
# a key whose value is an array and building a new object. We check that
|
|
|
|
# optimization here.
|
2016-08-06 12:54:50 -04:00
|
|
|
test "arrays are converted at most once" do
|
2013-12-21 08:22:08 -05:00
|
|
|
params = ActionController::Parameters.new(foo: [{}])
|
2014-06-07 07:17:50 -04:00
|
|
|
assert_same params[:foo], params[:foo]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Strong params has an internal cache to avoid duplicated loops in the most
|
2014-06-07 07:30:03 -04:00
|
|
|
# common usage pattern. See the docs of the method `converted_arrays`.
|
2014-06-07 07:17:50 -04:00
|
|
|
#
|
|
|
|
# This test checks that if we push a hash to an array (in-place modification)
|
|
|
|
# the cache does not get fooled, the hash is still wrapped as strong params,
|
|
|
|
# and not permitted.
|
2016-08-06 12:54:50 -04:00
|
|
|
test "mutated arrays are detected" do
|
2016-08-16 03:30:11 -04:00
|
|
|
params = ActionController::Parameters.new(users: [{ id: 1 }])
|
2014-06-07 07:17:50 -04:00
|
|
|
|
|
|
|
permitted = params.permit(users: [:id])
|
2016-08-16 03:30:11 -04:00
|
|
|
permitted[:users] << { injected: 1 }
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate permitted[:users].last, :permitted?
|
2013-12-21 08:22:08 -05:00
|
|
|
end
|
|
|
|
|
2012-07-12 01:50:42 -04:00
|
|
|
test "fetch doesnt raise ParameterMissing exception if there is a default" do
|
2012-08-29 10:54:27 -04:00
|
|
|
assert_equal "monkey", @params.fetch(:foo, "monkey")
|
|
|
|
assert_equal "monkey", @params.fetch(:foo) { "monkey" }
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
|
|
|
|
2015-07-18 18:40:47 -04:00
|
|
|
test "fetch doesnt raise ParameterMissing exception if there is a default that is nil" do
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil @params.fetch(:foo, nil)
|
|
|
|
assert_nil @params.fetch(:foo) { nil }
|
2015-07-18 18:40:47 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "KeyError in fetch block should not be covered up" do
|
2015-07-18 18:40:47 -04:00
|
|
|
params = ActionController::Parameters.new
|
|
|
|
e = assert_raises(KeyError) do
|
|
|
|
params.fetch(:missing_key) { {}.fetch(:also_missing) }
|
|
|
|
end
|
|
|
|
assert_match(/:also_missing$/, e.message)
|
|
|
|
end
|
|
|
|
|
2012-11-06 15:23:24 -05:00
|
|
|
test "not permitted is sticky beyond merges" do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @params.merge(a: "b"), :permitted?
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
|
|
|
|
2012-11-06 15:30:12 -05:00
|
|
|
test "permitted is sticky beyond merges" do
|
|
|
|
@params.permit!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @params.merge(a: "b"), :permitted?
|
2012-11-06 15:30:12 -05:00
|
|
|
end
|
|
|
|
|
2016-09-07 03:16:39 -04:00
|
|
|
test "merge with parameters" do
|
|
|
|
other_params = ActionController::Parameters.new(id: "1234").permit!
|
|
|
|
merged_params = @params.merge(other_params)
|
|
|
|
|
|
|
|
assert merged_params[:id]
|
|
|
|
end
|
|
|
|
|
2016-10-17 19:17:54 -04:00
|
|
|
test "not permitted is sticky beyond merge!" do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @params.merge!(a: "b"), :permitted?
|
2016-10-17 19:17:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "permitted is sticky beyond merge!" do
|
|
|
|
@params.permit!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @params.merge!(a: "b"), :permitted?
|
2016-10-17 19:17:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "merge! with parameters" do
|
|
|
|
other_params = ActionController::Parameters.new(id: "1234").permit!
|
|
|
|
@params.merge!(other_params)
|
|
|
|
|
|
|
|
assert_equal "1234", @params[:id]
|
|
|
|
assert_equal "32", @params[:person][:age]
|
|
|
|
end
|
|
|
|
|
2017-03-09 01:42:16 -05:00
|
|
|
test "#reverse_merge with parameters" do
|
|
|
|
default_params = ActionController::Parameters.new(id: "1234", person: {}).permit!
|
|
|
|
merged_params = @params.reverse_merge(default_params)
|
|
|
|
|
|
|
|
assert_equal "1234", merged_params[:id]
|
2018-04-03 21:34:51 -04:00
|
|
|
assert_not_predicate merged_params[:person], :empty?
|
2017-03-09 01:42:16 -05:00
|
|
|
end
|
|
|
|
|
2017-03-29 10:56:27 -04:00
|
|
|
test "#with_defaults is an alias of reverse_merge" do
|
|
|
|
default_params = ActionController::Parameters.new(id: "1234", person: {}).permit!
|
|
|
|
merged_params = @params.with_defaults(default_params)
|
|
|
|
|
|
|
|
assert_equal "1234", merged_params[:id]
|
2018-04-03 21:34:51 -04:00
|
|
|
assert_not_predicate merged_params[:person], :empty?
|
2017-03-29 10:56:27 -04:00
|
|
|
end
|
|
|
|
|
2017-03-11 00:03:20 -05:00
|
|
|
test "not permitted is sticky beyond reverse_merge" do
|
2018-04-03 21:34:51 -04:00
|
|
|
assert_not_predicate @params.reverse_merge(a: "b"), :permitted?
|
2017-03-09 01:42:16 -05:00
|
|
|
end
|
|
|
|
|
2017-03-11 00:03:20 -05:00
|
|
|
test "permitted is sticky beyond reverse_merge" do
|
2017-03-09 01:42:16 -05:00
|
|
|
@params.permit!
|
2017-03-11 00:03:20 -05:00
|
|
|
assert_predicate @params.reverse_merge(a: "b"), :permitted?
|
2017-03-09 01:42:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "#reverse_merge! with parameters" do
|
|
|
|
default_params = ActionController::Parameters.new(id: "1234", person: {}).permit!
|
|
|
|
@params.reverse_merge!(default_params)
|
|
|
|
|
|
|
|
assert_equal "1234", @params[:id]
|
2018-04-03 21:34:51 -04:00
|
|
|
assert_not_predicate @params[:person], :empty?
|
2017-03-09 01:42:16 -05:00
|
|
|
end
|
|
|
|
|
2017-03-29 10:56:27 -04:00
|
|
|
test "#with_defaults! is an alias of reverse_merge!" do
|
|
|
|
default_params = ActionController::Parameters.new(id: "1234", person: {}).permit!
|
|
|
|
@params.with_defaults!(default_params)
|
|
|
|
|
|
|
|
assert_equal "1234", @params[:id]
|
2018-04-03 21:34:51 -04:00
|
|
|
assert_not_predicate @params[:person], :empty?
|
2017-03-29 10:56:27 -04:00
|
|
|
end
|
|
|
|
|
2012-07-12 01:50:42 -04:00
|
|
|
test "modifying the parameters" do
|
|
|
|
@params[:person][:hometown] = "Chicago"
|
|
|
|
@params[:person][:family] = { brother: "Jonas" }
|
|
|
|
|
|
|
|
assert_equal "Chicago", @params[:person][:hometown]
|
|
|
|
assert_equal "Jonas", @params[:person][:family][:brother]
|
|
|
|
end
|
|
|
|
|
2018-04-16 15:50:18 -04:00
|
|
|
test "permit! is recursive" do
|
|
|
|
@params[:nested_array] = [[{ x: 2, y: 3 }, { x: 21, y: 42 }]]
|
2012-10-04 14:51:08 -04:00
|
|
|
@params.permit!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @params, :permitted?
|
|
|
|
assert_predicate @params[:person], :permitted?
|
|
|
|
assert_predicate @params[:person][:name], :permitted?
|
|
|
|
assert_predicate @params[:person][:addresses][0], :permitted?
|
2018-04-16 15:50:18 -04:00
|
|
|
assert_predicate @params[:nested_array][0][0], :permitted?
|
|
|
|
assert_predicate @params[:nested_array][0][1], :permitted?
|
2012-10-04 14:51:08 -04:00
|
|
|
end
|
|
|
|
|
2012-08-30 17:36:59 -04:00
|
|
|
test "permitted takes a default value when Parameters.permit_all_parameters is set" do
|
|
|
|
begin
|
|
|
|
ActionController::Parameters.permit_all_parameters = true
|
2016-08-06 13:44:11 -04:00
|
|
|
params = ActionController::Parameters.new(person: {
|
2012-08-30 17:36:59 -04:00
|
|
|
age: "32", name: { first: "David", last: "Heinemeier Hansson" }
|
2016-08-06 13:44:11 -04:00
|
|
|
})
|
2012-08-30 17:36:59 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate params.slice(:person), :permitted?
|
|
|
|
assert_predicate params[:person][:name], :permitted?
|
2012-08-30 17:36:59 -04:00
|
|
|
ensure
|
|
|
|
ActionController::Parameters.permit_all_parameters = false
|
|
|
|
end
|
|
|
|
end
|
2012-11-06 15:22:57 -05:00
|
|
|
|
2012-10-31 11:32:24 -04:00
|
|
|
test "permitting parameters as an array" do
|
|
|
|
assert_equal "32", @params[:person].permit([ :age ])[:age]
|
|
|
|
end
|
2014-07-25 12:00:14 -04:00
|
|
|
|
2017-04-11 21:45:39 -04:00
|
|
|
test "to_h raises UnfilteredParameters on unfiltered params" do
|
|
|
|
assert_raises(ActionController::UnfilteredParameters) do
|
|
|
|
@params.to_h
|
|
|
|
end
|
2014-07-25 12:00:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "to_h returns converted hash on permitted params" do
|
|
|
|
@params.permit!
|
|
|
|
|
2017-04-11 22:03:30 -04:00
|
|
|
assert_instance_of ActiveSupport::HashWithIndifferentAccess, @params.to_h
|
|
|
|
assert_not_kind_of ActionController::Parameters, @params.to_h
|
2014-07-25 12:00:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "to_h returns converted hash when .permit_all_parameters is set" do
|
|
|
|
begin
|
|
|
|
ActionController::Parameters.permit_all_parameters = true
|
|
|
|
params = ActionController::Parameters.new(crab: "Senjougahara Hitagi")
|
|
|
|
|
2017-04-11 22:03:30 -04:00
|
|
|
assert_instance_of ActiveSupport::HashWithIndifferentAccess, params.to_h
|
|
|
|
assert_not_kind_of ActionController::Parameters, params.to_h
|
2014-07-25 12:00:14 -04:00
|
|
|
assert_equal({ "crab" => "Senjougahara Hitagi" }, params.to_h)
|
|
|
|
ensure
|
|
|
|
ActionController::Parameters.permit_all_parameters = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-11 21:56:55 -04:00
|
|
|
test "to_hash raises UnfilteredParameters on unfiltered params" do
|
|
|
|
assert_raises(ActionController::UnfilteredParameters) do
|
|
|
|
@params.to_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_hash returns converted hash on permitted params" do
|
|
|
|
@params.permit!
|
|
|
|
|
|
|
|
assert_instance_of Hash, @params.to_hash
|
|
|
|
assert_not_kind_of ActionController::Parameters, @params.to_hash
|
|
|
|
end
|
|
|
|
|
2017-04-20 22:33:36 -04:00
|
|
|
test "parameters can be implicit converted to Hash" do
|
|
|
|
params = ActionController::Parameters.new
|
|
|
|
params.permit!
|
|
|
|
|
|
|
|
assert_equal({ a: 1 }, { a: 1 }.merge!(params))
|
|
|
|
end
|
|
|
|
|
2017-04-11 21:56:55 -04:00
|
|
|
test "to_hash returns converted hash when .permit_all_parameters is set" do
|
|
|
|
begin
|
|
|
|
ActionController::Parameters.permit_all_parameters = true
|
|
|
|
params = ActionController::Parameters.new(crab: "Senjougahara Hitagi")
|
|
|
|
|
|
|
|
assert_instance_of Hash, params.to_hash
|
|
|
|
assert_not_kind_of ActionController::Parameters, params.to_hash
|
|
|
|
assert_equal({ "crab" => "Senjougahara Hitagi" }, params.to_hash)
|
|
|
|
assert_equal({ "crab" => "Senjougahara Hitagi" }, params)
|
|
|
|
ensure
|
|
|
|
ActionController::Parameters.permit_all_parameters = false
|
|
|
|
end
|
|
|
|
end
|
2014-12-12 06:48:42 -05:00
|
|
|
|
|
|
|
test "to_unsafe_h returns unfiltered params" do
|
2017-04-11 22:03:30 -04:00
|
|
|
assert_instance_of ActiveSupport::HashWithIndifferentAccess, @params.to_unsafe_h
|
|
|
|
assert_not_kind_of ActionController::Parameters, @params.to_unsafe_h
|
2014-12-12 06:48:42 -05:00
|
|
|
end
|
2015-12-17 15:21:12 -05:00
|
|
|
|
2015-12-30 09:20:25 -05:00
|
|
|
test "to_unsafe_h returns unfiltered params even after accessing few keys" do
|
2016-10-28 23:05:58 -04:00
|
|
|
params = ActionController::Parameters.new("f" => { "language_facet" => ["Tibetan"] })
|
|
|
|
expected = { "f" => { "language_facet" => ["Tibetan"] } }
|
2015-12-30 09:20:25 -05:00
|
|
|
|
2017-04-11 22:03:30 -04:00
|
|
|
assert_instance_of ActionController::Parameters, params["f"]
|
2015-12-30 09:20:25 -05:00
|
|
|
assert_equal expected, params.to_unsafe_h
|
|
|
|
end
|
|
|
|
|
2017-04-11 22:00:14 -04:00
|
|
|
test "to_unsafe_h does not mutate the parameters" do
|
|
|
|
params = ActionController::Parameters.new("f" => { "language_facet" => ["Tibetan"] })
|
|
|
|
params[:f]
|
|
|
|
|
|
|
|
params.to_unsafe_h
|
|
|
|
|
|
|
|
assert_not_predicate params, :permitted?
|
|
|
|
assert_not_predicate params[:f], :permitted?
|
|
|
|
end
|
|
|
|
|
2015-12-17 15:21:12 -05:00
|
|
|
test "to_h only deep dups Ruby collections" do
|
|
|
|
company = Class.new do
|
|
|
|
attr_reader :dupped
|
|
|
|
def dup; @dupped = true; end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
params = ActionController::Parameters.new(prem: { likes: %i( dancing ) })
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal({ "prem" => { "likes" => %i( dancing ) } }, params.permit!.to_h)
|
2015-12-17 15:21:12 -05:00
|
|
|
|
|
|
|
params = ActionController::Parameters.new(companies: [ company, :acme ])
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal({ "companies" => [ company, :acme ] }, params.permit!.to_h)
|
2015-12-17 15:21:12 -05:00
|
|
|
assert_not company.dupped
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_unsafe_h only deep dups Ruby collections" do
|
|
|
|
company = Class.new do
|
|
|
|
attr_reader :dupped
|
|
|
|
def dup; @dupped = true; end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
params = ActionController::Parameters.new(prem: { likes: %i( dancing ) })
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal({ "prem" => { "likes" => %i( dancing ) } }, params.to_unsafe_h)
|
2015-12-17 15:21:12 -05:00
|
|
|
|
|
|
|
params = ActionController::Parameters.new(companies: [ company, :acme ])
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal({ "companies" => [ company, :acme ] }, params.to_unsafe_h)
|
2015-12-17 15:21:12 -05:00
|
|
|
assert_not company.dupped
|
|
|
|
end
|
2015-12-29 11:10:04 -05:00
|
|
|
|
2016-01-08 19:24:10 -05:00
|
|
|
test "include? returns true when the key is present" do
|
2015-12-29 11:10:04 -05:00
|
|
|
assert @params.include? :person
|
2016-08-06 12:54:50 -04:00
|
|
|
assert @params.include? "person"
|
2015-12-29 11:10:04 -05:00
|
|
|
assert_not @params.include? :gorilla
|
|
|
|
end
|
2016-04-15 11:08:47 -04:00
|
|
|
|
|
|
|
test "scalar values should be filtered when array or hash is specified" do
|
|
|
|
params = ActionController::Parameters.new(foo: "bar")
|
|
|
|
|
|
|
|
assert params.permit(:foo).has_key?(:foo)
|
2018-01-24 22:04:11 -05:00
|
|
|
assert_not params.permit(foo: []).has_key?(:foo)
|
|
|
|
assert_not params.permit(foo: [:bar]).has_key?(:foo)
|
|
|
|
assert_not params.permit(foo: :bar).has_key?(:foo)
|
2016-04-15 11:08:47 -04:00
|
|
|
end
|
2016-06-22 14:34:30 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#permitted? is false by default" do
|
2016-06-22 14:34:30 -04:00
|
|
|
params = ActionController::Parameters.new
|
|
|
|
|
|
|
|
assert_equal false, params.permitted?
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|