2014-08-18 17:02:56 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'action_controller/metal/strong_parameters'
|
|
|
|
require 'active_support/core_ext/hash/transform_values'
|
|
|
|
|
|
|
|
class ParametersAccessorsTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
|
|
|
@params = ActionController::Parameters.new(
|
|
|
|
person: {
|
|
|
|
age: '32',
|
|
|
|
name: {
|
|
|
|
first: 'David',
|
|
|
|
last: 'Heinemeier Hansson'
|
|
|
|
},
|
|
|
|
addresses: [{city: 'Chicago', state: 'Illinois'}]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "[] retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params[:person].permitted?
|
|
|
|
assert @params[:person][:name].permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "[] retains unpermitted status" do
|
|
|
|
assert_not @params[:person].permitted?
|
|
|
|
assert_not @params[:person][:name].permitted?
|
|
|
|
end
|
|
|
|
|
2016-01-12 13:27:29 -05:00
|
|
|
test "as_json returns the JSON representation of the parameters hash" do
|
|
|
|
assert_not @params.as_json.key? "parameters"
|
|
|
|
assert_not @params.as_json.key? "permitted"
|
|
|
|
assert @params.as_json.key? "person"
|
|
|
|
end
|
|
|
|
|
2014-08-18 17:02:56 -04:00
|
|
|
test "each carries permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
@params.each { |key, value| assert(value.permitted?) if key == "person" }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "each carries unpermitted status" do
|
|
|
|
@params.each { |key, value| assert_not(value.permitted?) if key == "person" }
|
|
|
|
end
|
|
|
|
|
2014-08-18 20:39:00 -04:00
|
|
|
test "each_pair carries permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
@params.each_pair { |key, value| assert(value.permitted?) if key == "person" }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "each_pair carries unpermitted status" do
|
|
|
|
@params.each_pair { |key, value| assert_not(value.permitted?) if key == "person" }
|
|
|
|
end
|
|
|
|
|
2014-08-18 17:02:56 -04:00
|
|
|
test "except retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.except(:person).permitted?
|
|
|
|
assert @params[:person].except(:name).permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "except retains unpermitted status" do
|
|
|
|
assert_not @params.except(:person).permitted?
|
|
|
|
assert_not @params[:person].except(:name).permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetch retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.fetch(:person).permitted?
|
|
|
|
assert @params[:person].fetch(:name).permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetch retains unpermitted status" do
|
|
|
|
assert_not @params.fetch(:person).permitted?
|
|
|
|
assert_not @params[:person].fetch(:name).permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "reject retains permitted status" do
|
|
|
|
assert_not @params.reject { |k| k == "person" }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "reject retains unpermitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.reject { |k| k == "person" }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "select retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.select { |k| k == "person" }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "select retains unpermitted status" do
|
|
|
|
assert_not @params.select { |k| k == "person" }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "slice retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.slice(:person).permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "slice retains unpermitted status" do
|
|
|
|
assert_not @params.slice(:person).permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "transform_keys retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.transform_keys { |k| k }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "transform_keys retains unpermitted status" do
|
|
|
|
assert_not @params.transform_keys { |k| k }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "transform_values retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.transform_values { |v| v }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "transform_values retains unpermitted status" do
|
|
|
|
assert_not @params.transform_values { |v| v }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "values_at retains permitted status" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.values_at(:person).first.permitted?
|
|
|
|
assert @params[:person].values_at(:name).first.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "values_at retains unpermitted status" do
|
|
|
|
assert_not @params.values_at(:person).first.permitted?
|
|
|
|
assert_not @params[:person].values_at(:name).first.permitted?
|
|
|
|
end
|
2016-01-21 17:28:18 -05:00
|
|
|
|
|
|
|
test "equality with another hash works" do
|
|
|
|
hash1 = { foo: :bar }
|
|
|
|
params1 = ActionController::Parameters.new(hash1)
|
|
|
|
assert(params1 == hash1)
|
|
|
|
end
|
2016-02-17 07:35:55 -05:00
|
|
|
|
2016-02-17 08:44:07 -05:00
|
|
|
test "equality with simple types works" do
|
|
|
|
assert(@params != 'Hello')
|
|
|
|
assert(@params != 42)
|
|
|
|
assert(@params != false)
|
|
|
|
end
|
|
|
|
|
2016-02-17 07:35:55 -05:00
|
|
|
test "inspect shows both class name and parameters" do
|
|
|
|
assert_equal(
|
|
|
|
'<ActionController::Parameters {"person"=>{"age"=>"32", '\
|
|
|
|
'"name"=>{"first"=>"David", "last"=>"Heinemeier Hansson"}, ' \
|
|
|
|
'"addresses"=>[{"city"=>"Chicago", "state"=>"Illinois"}]}}>',
|
|
|
|
@params.inspect
|
|
|
|
)
|
|
|
|
end
|
2014-08-18 17:02:56 -04:00
|
|
|
end
|