forked from rails/strong_parameters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters_taint_test.rb
74 lines (59 loc) · 2.45 KB
/
parameters_taint_test.rb
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
require 'test_helper'
require 'action_controller/parameters'
class ParametersTaintTest < ActiveSupport::TestCase
setup do
@params = ActionController::Parameters.new({ :person=> {
:age=> "32", :name=> { :first=> "David", :last=> "Heinemeier" }
}})
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
assert_nothing_raised do
assert_equal "monkey", @params.fetch(:foo, "monkey")
assert_equal "monkey", @params.fetch(:foo) { "monkey" }
end
end
test "permitted is sticky on accessors" do
assert [email protected](:person).permitted?
assert !@params[:person][:name].permitted?
@params.each { |key, value| assert(value.permitted?) if key == :person }
assert [email protected](:person).permitted?
assert [email protected]_at(:person).first.permitted?
end
test "permitted is sticky on mutators" do
assert [email protected]_if { |k,v| k == :person }.permitted?
#assert [email protected]_if { |k,v| k == :person }.permitted? ### keep_if is not present in 3.1,its a feature of Rails 3.2 . So commenting it out
end
test "deleting the parameters" do
params = {:app_bundle => {"release_path"=>"test",
"domain_name"=>"foo.bar.com",
"name"=>"thenewapp",
"repository_name"=>"https://repo2.com/branches",
"supports_primary_user"=>"1"}}
params = ActionController::Parameters.new(params)
x = params[:app_bundle].delete(:repository_name)
assert_equal "https://repo2.com/branches", x
assert_nil params[:app_bundle]["repository_name"]
end
test "permitted is sticky beyond merges" do
assert [email protected](: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 [email protected](: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
end