2013-03-13 12:37:54 -04:00
|
|
|
require 'test_helper'
|
2013-04-10 11:33:50 -04:00
|
|
|
require 'devise/parameter_sanitizer'
|
2013-03-13 12:37:54 -04:00
|
|
|
|
2013-04-10 11:33:50 -04:00
|
|
|
class BaseSanitizerTest < ActiveSupport::TestCase
|
2013-08-11 16:18:29 -04:00
|
|
|
def sanitizer(params)
|
|
|
|
Devise::BaseSanitizer.new(User, :user, params)
|
2013-03-13 12:37:54 -04:00
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
test 'returns chosen params' do
|
2013-08-11 16:18:29 -04:00
|
|
|
sanitizer = sanitizer(user: { "email" => "jose" })
|
|
|
|
assert_equal({ "email" => "jose" }, sanitizer.sanitize(:sign_in))
|
|
|
|
end
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
2013-03-13 12:37:54 -04:00
|
|
|
|
2013-04-10 11:33:50 -04:00
|
|
|
if defined?(ActionController::StrongParameters)
|
|
|
|
require 'active_model/forbidden_attributes_protection'
|
2013-03-13 12:37:54 -04:00
|
|
|
|
2013-04-10 11:33:50 -04:00
|
|
|
class ParameterSanitizerTest < ActiveSupport::TestCase
|
2013-04-14 02:21:46 -04:00
|
|
|
def sanitizer(params)
|
|
|
|
params = ActionController::Parameters.new(params)
|
|
|
|
Devise::ParameterSanitizer.new(User, :user, params)
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
2013-03-13 12:37:54 -04:00
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
test 'filters some parameters on sign in by default' do
|
2013-07-12 15:14:32 -04:00
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "password" => "invalid", "remember_me" => "1" })
|
2013-08-11 16:18:29 -04:00
|
|
|
assert_equal({ "email" => "jose", "password" => "invalid", "remember_me" => "1" }, sanitizer.sanitize(:sign_in))
|
2013-06-25 14:44:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'handles auth keys as a hash' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, authentication_keys: {email: true} do
|
2013-06-25 14:44:39 -04:00
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "password" => "invalid" })
|
2013-08-11 16:18:29 -04:00
|
|
|
assert_equal({ "email" => "jose", "password" => "invalid" }, sanitizer.sanitize(:sign_in))
|
2013-06-25 14:44:39 -04:00
|
|
|
end
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
test 'filters some parameters on sign up by default' do
|
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "role" => "invalid" })
|
2013-08-11 16:18:29 -04:00
|
|
|
assert_equal({ "email" => "jose" }, sanitizer.sanitize(:sign_up))
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
test 'filters some parameters on account update by default' do
|
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "role" => "invalid" })
|
2013-08-11 16:18:29 -04:00
|
|
|
assert_equal({ "email" => "jose" }, sanitizer.sanitize(:account_update))
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
test 'allows custom hooks' do
|
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "password" => "invalid" })
|
|
|
|
sanitizer.for(:sign_in) { |user| user.permit(:email, :password) }
|
2013-08-11 16:18:29 -04:00
|
|
|
assert_equal({ "email" => "jose", "password" => "invalid" }, sanitizer.sanitize(:sign_in))
|
2013-08-11 14:47:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'adding multiple permitted parameters' do
|
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "username" => "jose1", "role" => "valid" })
|
2013-08-11 16:18:29 -04:00
|
|
|
sanitizer.for(:sign_in).concat([:username, :role])
|
|
|
|
assert_equal({ "email" => "jose", "username" => "jose1", "role" => "valid" }, sanitizer.sanitize(:sign_in))
|
2013-08-11 14:47:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'removing multiple default parameters' do
|
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "password" => "invalid", "remember_me" => "1" })
|
2013-08-11 16:18:29 -04:00
|
|
|
sanitizer.for(:sign_in).delete(:email)
|
|
|
|
sanitizer.for(:sign_in).delete(:password)
|
|
|
|
assert_equal({ "remember_me" => "1" }, sanitizer.sanitize(:sign_in))
|
2013-08-11 14:47:18 -04:00
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
test 'raises on unknown hooks' do
|
|
|
|
sanitizer = sanitizer(user: { "email" => "jose", "password" => "invalid" })
|
|
|
|
assert_raise NotImplementedError do
|
2013-08-11 16:18:29 -04:00
|
|
|
sanitizer.sanitize(:unknown)
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
|
|
|
end
|
2013-10-31 09:38:30 -04:00
|
|
|
|
|
|
|
test 'passes parameters to filter as arguments to sanitizer' do
|
|
|
|
params = {user: stub}
|
|
|
|
sanitizer = Devise::ParameterSanitizer.new(User, :user, params)
|
|
|
|
|
|
|
|
params[:user].expects(:permit).with(kind_of(Symbol), kind_of(Symbol), kind_of(Symbol))
|
|
|
|
|
|
|
|
sanitizer.sanitize(:sign_in)
|
|
|
|
end
|
2013-03-13 12:37:54 -04:00
|
|
|
end
|
|
|
|
end
|