44
55use App \Models \User ;
66use Illuminate \Foundation \Testing \RefreshDatabase ;
7+ use Inertia \Testing \AssertableInertia as Assert ;
78use Laravel \Fortify \Features ;
89use Tests \TestCase ;
910
1011class TwoFactorAuthenticationTest extends TestCase
1112{
1213 use RefreshDatabase;
1314
14- public function test_can_view_two_factor_settings_page ()
15+ public function test_renders_two_factor_settings_component ()
1516 {
1617 $ user = User::factory ()->create ();
1718
18- $ response = $ this ->actingAs ($ user )
19- ->get ('/settings/two-factor ' );
20-
21- $ response ->assertStatus (200 );
22-
23- $ inertiaProps = $ response ->original ?->getData() ?? [];
24- $ props = $ inertiaProps ['page ' ]['props ' ];
25- $ this ->assertArrayHasKey ('confirmed ' , $ props );
26- $ this ->assertArrayHasKey ('requiresConfirmation ' , $ props );
27- $ this ->assertFalse ($ props ['confirmed ' ]);
19+ $ this ->actingAs ($ user )
20+ ->get ('/settings/two-factor ' )
21+ ->assertInertia (fn (Assert $ page ) => $ page
22+ ->component ('settings/TwoFactor ' )
23+ );
2824 }
2925
30- public function test_can_enable_two_factor_authentication ()
26+ public function test_passes_correct_props_to_two_factor_component ()
3127 {
32- if (! Features::canManageTwoFactorAuthentication ()) {
33- $ this ->markTestSkipped ('Two factor authentication is not enabled. ' );
34- }
35-
36- $ this ->actingAs ($ user = User::factory ()->create ());
28+ $ user = User::factory ()->create ();
3729
38- $ this ->withSession (['auth.password_confirmed_at ' => time ()]);
30+ $ this ->actingAs ($ user )
31+ ->get ('/settings/two-factor ' )
32+ ->assertInertia (fn (Assert $ page ) => $ page
33+ ->component ('settings/TwoFactor ' )
34+ ->has ('requiresConfirmation ' )
35+ ->where ('requiresConfirmation ' , true )
36+ ->has ('twoFactorEnabled ' )
37+ ->where ('twoFactorEnabled ' , false )
38+ );
39+ }
3940
40- $ this ->post ('/user/two-factor-authentication ' );
41+ public function test_shows_two_factor_disabled_status_initially ()
42+ {
43+ $ user = User::factory ()->create ();
4144
42- $ this ->assertNotNull ($ user ->fresh ()->two_factor_secret );
43- $ this ->assertCount (8 , $ user ->fresh ()->recoveryCodes ());
45+ $ this ->actingAs ($ user )
46+ ->get ('/settings/two-factor ' )
47+ ->assertInertia (fn (Assert $ page ) => $ page
48+ ->where ('twoFactorEnabled ' , false )
49+ );
4450 }
4551
46- public function test_recovery_codes_can_be_regenerated ()
52+ public function test_shows_two_factor_status_reflects_user_state ()
4753 {
4854 if (! Features::canManageTwoFactorAuthentication ()) {
4955 $ this ->markTestSkipped ('Two factor authentication is not enabled. ' );
5056 }
5157
52- $ this ->actingAs ($ user = User::factory ()->create ());
58+ $ user = User::factory ()->create ();
59+ $ this ->actingAs ($ user );
5360
5461 $ this ->withSession (['auth.password_confirmed_at ' => time ()]);
55-
5662 $ this ->post ('/user/two-factor-authentication ' );
57- $ this ->post ('/user/two-factor-recovery-codes ' );
5863
59- $ user = $ user ->fresh ();
60-
61- $ this ->post ('/user/two-factor-recovery-codes ' );
62-
63- $ this ->assertCount (8 , $ user ->recoveryCodes ());
64- $ this ->assertCount (8 , array_diff ($ user ->recoveryCodes (), $ user ->fresh ()->recoveryCodes ()));
64+ $ this ->get ('/settings/two-factor ' )
65+ ->assertInertia (fn (Assert $ page ) => $ page
66+ ->where ('twoFactorEnabled ' , $ user ->fresh ()->hasEnabledTwoFactorAuthentication ())
67+ );
6568 }
6669
67- public function test_two_factor_authentication_can_be_disabled ()
70+ public function test_requires_confirmation_prop_matches_fortify_config ()
6871 {
69- if (! Features::canManageTwoFactorAuthentication ()) {
70- $ this ->markTestSkipped ('Two factor authentication is not enabled. ' );
71- }
72-
73- $ this ->actingAs ($ user = User::factory ()->create ());
74-
75- $ this ->withSession (['auth.password_confirmed_at ' => time ()]);
76-
77- $ this ->post ('/user/two-factor-authentication ' );
78-
79- $ this ->assertNotNull ($ user ->fresh ()->two_factor_secret );
72+ $ user = User::factory ()->create ();
73+ $ expectedRequiresConfirmation = Features::optionEnabled (Features::twoFactorAuthentication (), 'confirm ' );
8074
81- $ this ->delete ('/user/two-factor-authentication ' );
75+ $ this ->actingAs ($ user )
76+ ->get ('/settings/two-factor ' )
77+ ->assertInertia (fn (Assert $ page ) => $ page
78+ ->where ('requiresConfirmation ' , $ expectedRequiresConfirmation )
79+ );
80+ }
8281
83- $ this ->assertNull ($ user ->fresh ()->two_factor_secret );
82+ public function test_two_factor_settings_page_requires_authentication ()
83+ {
84+ $ this ->get ('/settings/two-factor ' )
85+ ->assertRedirect ('/login ' );
8486 }
85- }
87+ }
0 commit comments