2012-07-20 01:32:15 -04:00
|
|
|
require 'abstract_unit'
|
2012-07-12 01:50:42 -04:00
|
|
|
require 'action_controller/metal/strong_parameters'
|
|
|
|
|
2012-08-29 11:08:58 -04:00
|
|
|
class ParametersPermitTest < ActiveSupport::TestCase
|
2012-07-12 01:50:42 -04:00
|
|
|
setup do
|
|
|
|
@params = ActionController::Parameters.new({ person: {
|
|
|
|
age: "32", name: { first: "David", last: "Heinemeier Hansson" }
|
|
|
|
}})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetch raises ParameterMissing exception" do
|
|
|
|
e = assert_raises(ActionController::ParameterMissing) do
|
|
|
|
@params.fetch :foo
|
|
|
|
end
|
|
|
|
assert_equal :foo, e.param
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
test "permitted is sticky on accessors" do
|
|
|
|
assert !@params.slice(:person).permitted?
|
|
|
|
assert !@params[:person][:name].permitted?
|
|
|
|
|
|
|
|
@params.each { |key, value| assert(value.permitted?) if key == :person }
|
|
|
|
|
|
|
|
assert !@params.fetch(:person).permitted?
|
|
|
|
|
|
|
|
assert !@params.values_at(:person).first.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "permitted is sticky on mutators" do
|
|
|
|
assert !@params.delete_if { |k| k == :person }.permitted?
|
|
|
|
assert !@params.keep_if { |k,v| k == :person }.permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "permitted is sticky beyond merges" do
|
|
|
|
assert !@params.merge(a: "b").permitted?
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
test "permitting parameters that are not there should not include the keys" do
|
|
|
|
assert !@params.permit(:person, :funky).has_key?(:funky)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "permit state is kept on a dup" do
|
|
|
|
@params.permit!
|
|
|
|
assert_equal @params.permitted?, @params.dup.permitted?
|
|
|
|
end
|
2012-08-30 17:36:59 -04:00
|
|
|
|
2012-10-04 14:51:08 -04:00
|
|
|
test "permit is recursive" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.permitted?
|
|
|
|
assert @params[:person].permitted?
|
|
|
|
assert @params[:person][:name].permitted?
|
|
|
|
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
|
|
|
|
params = ActionController::Parameters.new({ person: {
|
|
|
|
age: "32", name: { first: "David", last: "Heinemeier Hansson" }
|
|
|
|
}})
|
|
|
|
|
|
|
|
assert params.slice(:person).permitted?
|
|
|
|
assert params[:person][:name].permitted?
|
|
|
|
ensure
|
|
|
|
ActionController::Parameters.permit_all_parameters = false
|
|
|
|
end
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|