devise_test.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. require 'test_helper'
  2. module Devise
  3. def self.yield_and_restore
  4. @@warden_configured = nil
  5. c, b = @@warden_config, @@warden_config_block
  6. yield
  7. ensure
  8. @@warden_config, @@warden_config_block = c, b
  9. end
  10. end
  11. class DeviseTest < ActiveSupport::TestCase
  12. test 'model options can be configured through Devise' do
  13. swap Devise, :allow_unconfirmed_access_for => 113, :pepper => "foo" do
  14. assert_equal 113, Devise.allow_unconfirmed_access_for
  15. assert_equal "foo", Devise.pepper
  16. end
  17. end
  18. test 'setup block yields self' do
  19. Devise.setup do |config|
  20. assert_equal Devise, config
  21. end
  22. end
  23. test 'stores warden configuration' do
  24. assert_kind_of Devise::Delegator, Devise.warden_config.failure_app
  25. assert_equal :user, Devise.warden_config.default_scope
  26. end
  27. test 'warden manager user configuration through a block' do
  28. Devise.yield_and_restore do
  29. @executed = false
  30. Devise.warden do |config|
  31. @executed = true
  32. assert_kind_of Warden::Config, config
  33. end
  34. Devise.configure_warden!
  35. assert @executed
  36. end
  37. end
  38. test 'add new module using the helper method' do
  39. assert_nothing_raised(Exception) { Devise.add_module(:coconut) }
  40. assert_equal 1, Devise::ALL.select { |v| v == :coconut }.size
  41. assert_not Devise::STRATEGIES.include?(:coconut)
  42. assert_not defined?(Devise::Models::Coconut)
  43. Devise::ALL.delete(:coconut)
  44. assert_nothing_raised(Exception) { Devise.add_module(:banana, :strategy => :fruits) }
  45. assert_equal :fruits, Devise::STRATEGIES[:banana]
  46. Devise::ALL.delete(:banana)
  47. Devise::STRATEGIES.delete(:banana)
  48. assert_nothing_raised(Exception) { Devise.add_module(:kivi, :controller => :fruits) }
  49. assert_equal :fruits, Devise::CONTROLLERS[:kivi]
  50. Devise::ALL.delete(:kivi)
  51. Devise::CONTROLLERS.delete(:kivi)
  52. end
  53. test 'should complain when comparing empty or different sized passes' do
  54. [nil, ""].each do |empty|
  55. assert_not Devise.secure_compare(empty, "something")
  56. assert_not Devise.secure_compare("something", empty)
  57. assert_not Devise.secure_compare(empty, empty)
  58. end
  59. assert_not Devise.secure_compare("size_1", "size_four")
  60. end
  61. end