2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2012-07-12 01:50:42 -04:00
|
|
|
|
|
|
|
class BooksController < ActionController::Base
|
|
|
|
def create
|
|
|
|
params.require(:book).require(:name)
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ActionControllerRequiredParamsTest < ActionController::TestCase
|
|
|
|
tests BooksController
|
|
|
|
|
|
|
|
test "missing required parameters will raise exception" do
|
2013-01-16 06:37:13 -05:00
|
|
|
assert_raise ActionController::ParameterMissing do
|
2015-01-04 04:35:06 -05:00
|
|
|
post :create, params: { magazine: { name: "Mjallo!" } }
|
2013-01-15 19:07:13 -05:00
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
|
2013-01-16 06:37:13 -05:00
|
|
|
assert_raise ActionController::ParameterMissing do
|
2015-01-04 04:35:06 -05:00
|
|
|
post :create, params: { book: { title: "Mjallo!" } }
|
2013-01-15 19:07:13 -05:00
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "required parameters that are present will not raise" do
|
2015-01-04 04:35:06 -05:00
|
|
|
post :create, params: { book: { name: "Mjallo!" } }
|
2012-07-12 01:50:42 -04:00
|
|
|
assert_response :ok
|
|
|
|
end
|
2014-06-13 13:42:38 -04:00
|
|
|
|
|
|
|
test "required parameters with false value will not raise" do
|
2015-01-04 04:35:06 -05:00
|
|
|
post :create, params: { book: { name: false } }
|
2014-06-13 13:42:38 -04:00
|
|
|
assert_response :ok
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
2014-01-23 22:28:24 -05:00
|
|
|
|
|
|
|
class ParametersRequireTest < ActiveSupport::TestCase
|
2014-06-13 13:42:38 -04:00
|
|
|
test "required parameters should accept and return false value" do
|
2014-06-13 16:23:45 -04:00
|
|
|
assert_equal(false, ActionController::Parameters.new(person: false).require(:person))
|
2014-06-13 13:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "required parameters must not be nil" do
|
|
|
|
assert_raises(ActionController::ParameterMissing) do
|
|
|
|
ActionController::Parameters.new(person: nil).require(:person)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "required parameters must not be empty" do
|
2014-01-23 22:28:24 -05:00
|
|
|
assert_raises(ActionController::ParameterMissing) do
|
|
|
|
ActionController::Parameters.new(person: {}).require(:person)
|
|
|
|
end
|
|
|
|
end
|
2015-03-28 02:38:36 -04:00
|
|
|
|
2015-08-28 10:23:31 -04:00
|
|
|
test "require array when all required params are present" do
|
2016-08-16 03:30:11 -04:00
|
|
|
safe_params = ActionController::Parameters.new(person: { first_name: "Gaurish", title: "Mjallo", city: "Barcelona" })
|
2015-03-28 02:38:36 -04:00
|
|
|
.require(:person)
|
2015-08-28 10:23:31 -04:00
|
|
|
.require([:first_name, :title])
|
2015-03-28 02:38:36 -04:00
|
|
|
|
|
|
|
assert_kind_of Array, safe_params
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal ["Gaurish", "Mjallo"], safe_params
|
2015-03-28 02:38:36 -04:00
|
|
|
end
|
|
|
|
|
2015-08-28 10:23:31 -04:00
|
|
|
test "require array when a required param is missing" do
|
2015-03-28 02:38:36 -04:00
|
|
|
assert_raises(ActionController::ParameterMissing) do
|
2016-08-16 03:30:11 -04:00
|
|
|
ActionController::Parameters.new(person: { first_name: "Gaurish", title: nil })
|
2015-03-28 02:38:36 -04:00
|
|
|
.require(:person)
|
2015-08-28 10:23:31 -04:00
|
|
|
.require([:first_name, :title])
|
2015-03-28 02:38:36 -04:00
|
|
|
end
|
|
|
|
end
|
2016-01-19 12:45:34 -05:00
|
|
|
|
2016-01-19 12:53:27 -05:00
|
|
|
test "value params" do
|
|
|
|
params = ActionController::Parameters.new(foo: "bar", dog: "cinco")
|
|
|
|
assert_equal ["bar", "cinco"], params.values
|
|
|
|
assert params.has_value?("cinco")
|
|
|
|
assert params.value?("cinco")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Deprecated methods are deprecated" do
|
2016-01-19 12:45:34 -05:00
|
|
|
assert_deprecated do
|
2016-08-06 13:44:11 -04:00
|
|
|
ActionController::Parameters.new(foo: "bar").merge!(bar: "foo")
|
2016-01-19 12:45:34 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-23 22:28:24 -05:00
|
|
|
end
|