diff options
author | Thomas Drake-Brockman <thom@sfedb.com> | 2013-01-19 22:20:13 +0800 |
---|---|---|
committer | Thomas Drake-Brockman <thom@sfedb.com> | 2013-01-20 00:39:24 +0800 |
commit | 130370b1c872e9c7645e1c3cc26365d9dc4d90b1 (patch) | |
tree | 1015b42e13d2b1a0233ba4b467b90281c08e79bf /actionpack/test/controller | |
parent | 0d65969e6869f01bf6f459add3635497ed630156 (diff) | |
download | rails-130370b1c872e9c7645e1c3cc26365d9dc4d90b1.tar.gz rails-130370b1c872e9c7645e1c3cc26365d9dc4d90b1.tar.bz2 rails-130370b1c872e9c7645e1c3cc26365d9dc4d90b1.zip |
Added ability to raise or log on unpermitted params.
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r-- | actionpack/test/controller/parameters/log_on_unpermitted_params_test.rb | 50 | ||||
-rw-r--r-- | actionpack/test/controller/parameters/raise_on_unpermitted_params_test.rb | 33 |
2 files changed, 83 insertions, 0 deletions
diff --git a/actionpack/test/controller/parameters/log_on_unpermitted_params_test.rb b/actionpack/test/controller/parameters/log_on_unpermitted_params_test.rb new file mode 100644 index 0000000000..fb50dabff9 --- /dev/null +++ b/actionpack/test/controller/parameters/log_on_unpermitted_params_test.rb @@ -0,0 +1,50 @@ +require 'abstract_unit' +require 'action_controller/metal/strong_parameters' + +class LogOnUnpermittedParamsTest < ActiveSupport::TestCase + def setup + ActionController::Parameters.action_on_unpermitted = :log + end + + def teardown + ActionController::Parameters.action_on_unpermitted = false + end + + test "logs on unexpected params" do + params = ActionController::Parameters.new({ + book: { pages: 65 }, + fishing: "Turnips" + }) + + assert_logged("Unpermitted parameters: fishing") do + params.permit(book: [:pages]) + end + end + + test "logs on unexpected nested params" do + params = ActionController::Parameters.new({ + book: { pages: 65, title: "Green Cats and where to find then." } + }) + + assert_logged("Unpermitted parameters: title") do + params.permit(book: [:pages]) + end + end + + private + + def assert_logged(message) + old_logger = ActionController::Base.logger + log = StringIO.new + ActionController::Base.logger = Logger.new(log) + + begin + yield + + log.rewind + assert_match message, log.read + ensure + ActionController::Base.logger = old_logger + end + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/parameters/raise_on_unpermitted_params_test.rb b/actionpack/test/controller/parameters/raise_on_unpermitted_params_test.rb new file mode 100644 index 0000000000..e2933b86eb --- /dev/null +++ b/actionpack/test/controller/parameters/raise_on_unpermitted_params_test.rb @@ -0,0 +1,33 @@ +require 'abstract_unit' +require 'action_controller/metal/strong_parameters' + +class RaiseOnUnpermittedParamsTest < ActiveSupport::TestCase + def setup + ActionController::Parameters.action_on_unpermitted = :raise + end + + def teardown + ActionController::Parameters.action_on_unpermitted = false + end + + test "raises on unexpected params" do + params = ActionController::Parameters.new({ + book: { pages: 65 }, + fishing: "Turnips" + }) + + assert_raises(ActionController::UnexpectedParameters) do + params.permit(book: [:pages]) + end + end + + test "raises on unexpected nested params" do + params = ActionController::Parameters.new({ + book: { pages: 65, title: "Green Cats and where to find then." } + }) + + assert_raises(ActionController::UnexpectedParameters) do + params.permit(book: [:pages]) + end + end +end
\ No newline at end of file |