2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2009-11-03 06:35:11 -05:00
|
|
|
|
|
|
|
module Devise
|
2010-03-28 08:51:03 -04:00
|
|
|
def self.yield_and_restore
|
2010-05-15 18:38:40 -04:00
|
|
|
@@warden_configured = nil
|
2014-04-15 17:20:21 -04:00
|
|
|
c, b = @@warden_config, @@warden_config_blocks
|
2010-03-28 08:51:03 -04:00
|
|
|
yield
|
|
|
|
ensure
|
2014-04-15 17:20:21 -04:00
|
|
|
@@warden_config, @@warden_config_blocks = c, b
|
2009-11-03 06:35:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DeviseTest < ActiveSupport::TestCase
|
2013-08-28 14:23:41 -04:00
|
|
|
test 'bcrypt on the class' do
|
|
|
|
password = "super secret"
|
|
|
|
klass = Struct.new(:pepper, :stretches).new("blahblah", 2)
|
2014-03-25 09:44:40 -04:00
|
|
|
hash = Devise::Encryptor.digest(klass, password)
|
2013-10-06 07:19:08 -04:00
|
|
|
assert_equal ::BCrypt::Password.create(hash), hash
|
2013-08-28 14:23:41 -04:00
|
|
|
|
|
|
|
klass = Struct.new(:pepper, :stretches).new("bla", 2)
|
2014-03-25 09:44:40 -04:00
|
|
|
hash = Devise::Encryptor.digest(klass, password)
|
2013-10-06 07:19:08 -04:00
|
|
|
assert_not_equal ::BCrypt::Password.new(hash), hash
|
2013-08-28 14:23:41 -04:00
|
|
|
end
|
|
|
|
|
2009-11-03 06:35:11 -05:00
|
|
|
test 'model options can be configured through Devise' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, allow_unconfirmed_access_for: 113, pepper: "foo" do
|
2011-12-11 14:18:02 -05:00
|
|
|
assert_equal 113, Devise.allow_unconfirmed_access_for
|
2009-11-03 06:35:11 -05:00
|
|
|
assert_equal "foo", Devise.pepper
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'setup block yields self' do
|
|
|
|
Devise.setup do |config|
|
|
|
|
assert_equal Devise, config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-31 16:04:48 -04:00
|
|
|
test 'stores warden configuration' do
|
2011-11-07 05:47:28 -05:00
|
|
|
assert_kind_of Devise::Delegator, Devise.warden_config.failure_app
|
2010-03-31 16:04:48 -04:00
|
|
|
assert_equal :user, Devise.warden_config.default_scope
|
2009-11-19 10:09:05 -05:00
|
|
|
end
|
|
|
|
|
2009-11-03 06:35:11 -05:00
|
|
|
test 'warden manager user configuration through a block' do
|
2010-03-28 08:51:03 -04:00
|
|
|
Devise.yield_and_restore do
|
2014-05-07 17:50:50 -04:00
|
|
|
executed = false
|
2010-01-05 07:44:13 -05:00
|
|
|
Devise.warden do |config|
|
2014-05-07 17:50:50 -04:00
|
|
|
executed = true
|
2010-01-05 07:44:13 -05:00
|
|
|
assert_kind_of Warden::Config, config
|
2009-11-03 06:35:11 -05:00
|
|
|
end
|
|
|
|
|
2010-03-31 16:04:48 -04:00
|
|
|
Devise.configure_warden!
|
2014-05-07 17:50:50 -04:00
|
|
|
assert executed
|
2009-11-03 06:35:11 -05:00
|
|
|
end
|
|
|
|
end
|
2010-01-20 19:19:36 -05:00
|
|
|
|
2014-04-15 17:20:21 -04:00
|
|
|
test 'warden manager user configuration through multiple blocks' do
|
|
|
|
Devise.yield_and_restore do
|
2014-05-07 17:53:09 -04:00
|
|
|
executed = 0
|
|
|
|
|
|
|
|
3.times do
|
|
|
|
Devise.warden { |config| executed += 1 }
|
2014-04-15 17:20:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Devise.configure_warden!
|
2014-05-07 17:53:09 -04:00
|
|
|
assert_equal 3, executed
|
2014-04-15 17:20:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-20 19:19:36 -05:00
|
|
|
test 'add new module using the helper method' do
|
|
|
|
assert_nothing_raised(Exception) { Devise.add_module(:coconut) }
|
|
|
|
assert_equal 1, Devise::ALL.select { |v| v == :coconut }.size
|
2016-05-03 12:57:10 -04:00
|
|
|
refute Devise::STRATEGIES.include?(:coconut)
|
|
|
|
refute defined?(Devise::Models::Coconut)
|
2010-01-20 19:19:36 -05:00
|
|
|
Devise::ALL.delete(:coconut)
|
|
|
|
|
2014-02-25 11:42:55 -05:00
|
|
|
assert_nothing_raised(Exception) { Devise.add_module(:banana, strategy: :fruits) }
|
2010-03-28 08:51:03 -04:00
|
|
|
assert_equal :fruits, Devise::STRATEGIES[:banana]
|
2010-01-20 19:19:36 -05:00
|
|
|
Devise::ALL.delete(:banana)
|
|
|
|
Devise::STRATEGIES.delete(:banana)
|
|
|
|
|
2014-02-25 11:42:55 -05:00
|
|
|
assert_nothing_raised(Exception) { Devise.add_module(:kivi, controller: :fruits) }
|
2010-03-03 05:57:23 -05:00
|
|
|
assert_equal :fruits, Devise::CONTROLLERS[:kivi]
|
2010-01-20 19:19:36 -05:00
|
|
|
Devise::ALL.delete(:kivi)
|
2010-03-03 05:57:23 -05:00
|
|
|
Devise::CONTROLLERS.delete(:kivi)
|
2010-01-20 19:19:36 -05:00
|
|
|
end
|
2013-10-06 07:19:08 -04:00
|
|
|
|
2011-02-26 22:41:22 -05:00
|
|
|
test 'should complain when comparing empty or different sized passes' do
|
|
|
|
[nil, ""].each do |empty|
|
2016-05-03 12:57:10 -04:00
|
|
|
refute Devise.secure_compare(empty, "something")
|
|
|
|
refute Devise.secure_compare("something", empty)
|
|
|
|
refute Devise.secure_compare(empty, empty)
|
2011-02-26 22:41:22 -05:00
|
|
|
end
|
2016-05-03 12:57:10 -04:00
|
|
|
refute Devise.secure_compare("size_1", "size_four")
|
2011-02-26 22:41:22 -05:00
|
|
|
end
|
2012-11-19 10:38:22 -05:00
|
|
|
|
|
|
|
test 'Devise.email_regexp should match valid email addresses' do
|
2016-04-26 10:13:07 -04:00
|
|
|
valid_emails = ["test@example.com", "jo@jo.co", "f4$_m@you.com", "testing.example@example.com.ua", "test@tt", "test@valid---domain.com"]
|
|
|
|
non_valid_emails = ["rex", "test user@example.com", "test_user@example server.com"]
|
2012-11-19 10:38:22 -05:00
|
|
|
|
|
|
|
valid_emails.each do |email|
|
|
|
|
assert_match Devise.email_regexp, email
|
|
|
|
end
|
|
|
|
non_valid_emails.each do |email|
|
|
|
|
assert_no_match Devise.email_regexp, email
|
|
|
|
end
|
|
|
|
end
|
2009-11-03 06:35:11 -05:00
|
|
|
end
|