44
55class TwoFactorAuthenticatableTest < ActiveSupport ::TestCase
66 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
7+ assert_equal [ :test_otp ] , UserWithTwoFactor . two_factor_modules
218 end
229
2310 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
11+ user = new_user_with_two_factor
12+ user . stubs ( :test_otp_two_factor_enabled? ) . returns ( true )
13+ assert user . two_factor_enabled?
14+ assert_equal [ :test_otp ] , user . enabled_two_factors
3415 end
3516
3617 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
18+ user = new_user_with_two_factor
19+ user . stubs ( :test_otp_two_factor_enabled? ) . returns ( false )
20+ assert_not user . two_factor_enabled?
21+ assert_empty user . enabled_two_factors
6022 end
6123
6224 test '.two_factor_methods= raises on unknown method' do
@@ -69,29 +31,9 @@ class TwoFactorAuthenticatableTest < ActiveSupport::TestCase
6931 end
7032 end
7133
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
34+ private
9135
92- klass . two_factor_methods = [ :includable_test ]
93- assert_equal [ :includable_test ] , Array ( klass . instance_variable_get ( :@two_factor_methods ) )
94- Devise . two_factor_method_configs . delete ( :includable_test )
95- Devise ::STRATEGIES . delete ( :includable_test )
36+ def new_user_with_two_factor ( attributes = { } )
37+ UserWithTwoFactor . new ( valid_attributes ( attributes ) )
9638 end
9739end
0 commit comments