Revision 0a54c7f8
Added by Dominic Cleal over 7 years ago
test/unit/parameter_filter_test.rb | ||
---|---|---|
|
||
test "permitting second-level attributes via permit(Symbol)" do
|
||
filter.permit(:test)
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:example => {:test => 'a', :denied => 'b'}), ui_context))
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:example => {:test => 'a', :denied => 'b'}), ui_context).to_h)
|
||
end
|
||
|
||
test "permitting second-level attributes via block" do
|
||
filter.permit { |ctx| ctx.permit(:test) }
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:example => {:test => 'a', :denied => 'b'}), ui_context))
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:example => {:test => 'a', :denied => 'b'}), ui_context).to_h)
|
||
end
|
||
|
||
test "block contains controller/action names" do
|
||
... | ... | |
|
||
test "permitting second-level arrays via permit(Symbol => Array)" do
|
||
filter.permit(:test => [])
|
||
assert_equal({}, filter.filter_params(params(:example => {:test => 'a'}), ui_context))
|
||
assert_equal({'test' => ['a']}, filter.filter_params(params(:example => {:test => ['a']}), ui_context))
|
||
assert_equal({}, filter.filter_params(params(:example => {:test => 'a'}), ui_context).to_h)
|
||
assert_equal({'test' => ['a']}, filter.filter_params(params(:example => {:test => ['a']}), ui_context).to_h)
|
||
end
|
||
|
||
test "permitting third-level attributes via permit(Symbol => Array[Symbol])" do
|
||
filter.permit(:test => [:inner])
|
||
assert_equal({'test' => {'inner' => 'a'}}, filter.filter_params(params(:example => {:test => {:inner => 'a', :denied => 'b'}}), ui_context))
|
||
assert_equal({'test' => {'inner' => 'a'}}, filter.filter_params(params(:example => {:test => {:inner => 'a', :denied => 'b'}}), ui_context).to_h)
|
||
end
|
||
|
||
test "constructs permit() args for second-level attribute" do
|
||
... | ... | |
|
||
test "blocks second-level attributes for UI when :ui => false" do
|
||
filter.permit_by_context(:test, :ui => false)
|
||
assert_equal({}, filter.filter_params(params(:example => {:test => 'a'}), ui_context))
|
||
assert_equal({}, filter.filter_params(params(:example => {:test => 'a'}), ui_context).to_h)
|
||
end
|
||
|
||
test "#permit_by_context raises error for unknown context types" do
|
||
... | ... | |
filter2.permit_by_context(:inner, :nested => true)
|
||
filter2.permit(:ui_only)
|
||
filter.permit(:test, :nested => [filter2])
|
||
assert_equal({'test' => 'a', 'nested' => [{'inner' => 'b'}]}, filter.filter_params(params(:example => {:test => 'a', :nested => [{:inner => 'b', :ui_only => 'b'}]}), ui_context))
|
||
assert_equal({'test' => 'a', 'nested' => {'123' => {'inner' => 'b'}}}, filter.filter_params(params(:example => {:test => 'a', :nested => {'123' => {:inner => 'b', :ui_only => 'b'}}}), ui_context))
|
||
assert_equal({'test' => 'a', 'nested' => [{'inner' => 'b'}]}, filter.filter_params(params(:example => {:test => 'a', :nested => [{:inner => 'b', :ui_only => 'b'}]}), ui_context).to_h)
|
||
assert_equal({'test' => 'a', 'nested' => {'123' => {'inner' => 'b'}}}, filter.filter_params(params(:example => {:test => 'a', :nested => {'123' => {:inner => 'b', :ui_only => 'b'}}}), ui_context).to_h)
|
||
end
|
||
|
||
test "second filter block has access to original controller/action" do
|
||
... | ... | |
plugin = mock('plugin')
|
||
plugin.expects(:parameter_filters).with(klass).returns([[:plugin_ext, :another]])
|
||
Foreman::Plugin.expects(:all).returns([plugin])
|
||
assert_equal({'plugin_ext' => 'b'}, filter.filter_params(params(:example => {:test => 'a', :plugin_ext => 'b'}), ui_context))
|
||
assert_equal({'plugin_ext' => 'b'}, filter.filter_params(params(:example => {:test => 'a', :plugin_ext => 'b'}), ui_context).to_h)
|
||
end
|
||
|
||
test "permits plugin-added attributes from blocks" do
|
||
plugin = mock('plugin')
|
||
plugin.expects(:parameter_filters).with(klass).returns([[Proc.new { |ctx| ctx.permit(:plugin_ext) }]])
|
||
Foreman::Plugin.expects(:all).returns([plugin])
|
||
assert_equal({'plugin_ext' => 'b'}, filter.filter_params(params(:example => {:test => 'a', :plugin_ext => 'b'}), ui_context))
|
||
assert_equal({'plugin_ext' => 'b'}, filter.filter_params(params(:example => {:test => 'a', :plugin_ext => 'b'}), ui_context).to_h)
|
||
end
|
||
end
|
||
|
||
context "with top_level_hash" do
|
||
test "applies filters without top-level hash" do
|
||
filter.permit(:test)
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:changed => {:test => 'a', :denied => 'b'}), ui_context, :changed))
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:changed => {:test => 'a', :denied => 'b'}), ui_context, :changed).to_h)
|
||
end
|
||
end
|
||
|
||
context "with top_level_hash => :none" do
|
||
test "applies filters without top-level hash" do
|
||
filter.permit(:test)
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:test => 'a', :denied => 'b'), ui_context, :none))
|
||
assert_equal({'test' => 'a'}, filter.filter_params(params(:test => 'a', :denied => 'b'), ui_context, :none).to_h)
|
||
end
|
||
end
|
||
|
Also available in: Unified diff
fixes #18665 - call #to_h before comparing AC::Parameters to hash
Allows comparisons when ActionController::Parameters is separated from
Hash in Rails 5.0. #permit! is now called on inner hashes sent through
KeepParam (similar to rails/rails@e86524c in 5.1) so they are included
in the #to_h permitted output.