|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +class TwoFactorAuthenticatableTest < ActiveSupport::TestCase |
| 6 | + test '.two_factor_modules returns the configured two_factor_methods' do |
| 7 | + klass = Class.new do |
| 8 | + extend Devise::Models::TwoFactorAuthenticatable::ClassMethods |
| 9 | + end |
| 10 | + klass.instance_variable_set(:@two_factor_methods, [:fake_method]) |
| 11 | + |
| 12 | + assert_equal [:fake_method], klass.two_factor_modules |
| 13 | + end |
| 14 | + |
| 15 | + test '.two_factor_modules returns empty array when no methods configured' do |
| 16 | + klass = Class.new do |
| 17 | + extend Devise::Models::TwoFactorAuthenticatable::ClassMethods |
| 18 | + end |
| 19 | + |
| 20 | + assert_equal [], klass.two_factor_modules |
| 21 | + end |
| 22 | + |
| 23 | + test '#two_factor_enabled? returns true when any method reports enabled' do |
| 24 | + klass = Class.new do |
| 25 | + include Devise::Models::TwoFactorAuthenticatable |
| 26 | + end |
| 27 | + klass.instance_variable_set(:@two_factor_methods, [:fake_method]) |
| 28 | + |
| 29 | + instance = klass.new |
| 30 | + instance.define_singleton_method(:fake_method_two_factor_enabled?) { true } |
| 31 | + |
| 32 | + assert instance.two_factor_enabled? |
| 33 | + assert_equal [:fake_method], instance.enabled_two_factors |
| 34 | + end |
| 35 | + |
| 36 | + test '#two_factor_enabled? returns false when no method reports enabled' do |
| 37 | + klass = Class.new do |
| 38 | + include Devise::Models::TwoFactorAuthenticatable |
| 39 | + end |
| 40 | + klass.instance_variable_set(:@two_factor_methods, [:fake_method]) |
| 41 | + |
| 42 | + instance = klass.new |
| 43 | + instance.define_singleton_method(:fake_method_two_factor_enabled?) { false } |
| 44 | + |
| 45 | + assert_not instance.two_factor_enabled? |
| 46 | + assert_empty instance.enabled_two_factors |
| 47 | + end |
| 48 | + |
| 49 | + test '#enabled_two_factors returns only enabled methods' do |
| 50 | + klass = Class.new do |
| 51 | + include Devise::Models::TwoFactorAuthenticatable |
| 52 | + end |
| 53 | + klass.instance_variable_set(:@two_factor_methods, [:method_a, :method_b]) |
| 54 | + |
| 55 | + instance = klass.new |
| 56 | + instance.define_singleton_method(:method_a_two_factor_enabled?) { true } |
| 57 | + instance.define_singleton_method(:method_b_two_factor_enabled?) { false } |
| 58 | + |
| 59 | + assert_equal [:method_a], instance.enabled_two_factors |
| 60 | + end |
| 61 | + |
| 62 | + test '.two_factor_methods= raises on unknown method' do |
| 63 | + klass = Class.new do |
| 64 | + extend Devise::Models::TwoFactorAuthenticatable::ClassMethods |
| 65 | + end |
| 66 | + |
| 67 | + assert_raises(RuntimeError, /Unknown two-factor method/) do |
| 68 | + klass.two_factor_methods = [:nonexistent] |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + test '.two_factor_methods= includes model concern from registry' do |
| 73 | + # Register a fake method |
| 74 | + Devise.register_two_factor_method(:includable_test, |
| 75 | + model: 'devise/models/test_otp', |
| 76 | + strategy: :test_strategy) |
| 77 | + |
| 78 | + klass = Class.new do |
| 79 | + extend Devise::Models::TwoFactorAuthenticatable::ClassMethods |
| 80 | + |
| 81 | + # Stub include to track what gets included |
| 82 | + def self.included_modules_tracker |
| 83 | + @included_modules_tracker ||= [] |
| 84 | + end |
| 85 | + |
| 86 | + def self.include(mod) |
| 87 | + included_modules_tracker << mod |
| 88 | + super |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + klass.two_factor_methods = [:includable_test] |
| 93 | + assert_equal [:includable_test], Array(klass.instance_variable_get(:@two_factor_methods)) |
| 94 | + ensure |
| 95 | + Devise.two_factor_method_configs.delete(:includable_test) |
| 96 | + Devise::STRATEGIES.delete(:includable_test) |
| 97 | + end |
| 98 | +end |
0 commit comments