1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Fix error when params is not a hash

This commit is contained in:
Andrey Andreev 2018-02-28 14:35:32 +03:00 committed by Leonardo Tegon
parent 1c8e97c75a
commit 8866b8e5eb
2 changed files with 41 additions and 1 deletions

View file

@ -135,7 +135,19 @@ module Devise
end
def default_params
@params.fetch(@resource_name, {})
if hashable_resource_params?
@params.fetch(@resource_name)
else
empty_params
end
end
def hashable_resource_params?
@params[@resource_name].respond_to?(:permit)
end
def empty_params
ActionController::Parameters.new({})
end
def permit_keys(parameters, keys)

View file

@ -16,6 +16,34 @@ class ParameterSanitizerTest < ActiveSupport::TestCase
assert_equal({ 'email' => 'jose' }, sanitized)
end
test 'permits empty params when received not a hash' do
sanitizer = sanitizer({ 'user' => 'string' })
sanitized = sanitizer.sanitize(:sign_in)
assert_equal({}, sanitized)
end
test 'does not rise error when received string instead of hash' do
sanitizer = sanitizer('user' => 'string')
assert_nothing_raised do
sanitizer.sanitize(:sign_in)
end
end
test 'does not rise error when received nil instead of hash' do
sanitizer = sanitizer('user' => nil)
assert_nothing_raised do
sanitizer.sanitize(:sign_in)
end
end
test 'permits empty params when received nil instead of hash' do
sanitizer = sanitizer({ 'user' => nil })
sanitized = sanitizer.sanitize(:sign_in)
assert_equal({}, sanitized)
end
test 'permits the default parameters for sign up' do
sanitizer = sanitizer('user' => { 'email' => 'jose', 'role' => 'invalid' })
sanitized = sanitizer.sanitize(:sign_up)