Skip to content

Commit f608bc4

Browse files
committed
refactor: simplify TwoFactor test structure, replace manual assertions with Inertia assertions, and improve naming consistency
1 parent f93dc53 commit f608bc4

File tree

2 files changed

+52
-67
lines changed

2 files changed

+52
-67
lines changed

tests/Feature/Auth/PasswordConfirmationTest.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,14 @@ public function test_confirm_password_screen_can_be_rendered()
1414
{
1515
$user = User::factory()->create();
1616

17-
$response = $this->actingAs($user)->get('/confirm-password');
17+
$response = $this->actingAs($user)->get('/user/confirm-password');
1818

1919
$response->assertStatus(200);
2020
}
2121

22-
public function test_password_can_be_confirmed()
22+
public function test_password_confirmation_requires_authentication()
2323
{
24-
$user = User::factory()->create();
25-
26-
$response = $this->actingAs($user)->post('/confirm-password', [
27-
'password' => 'password',
28-
]);
29-
30-
$response->assertRedirect();
31-
$response->assertSessionHasNoErrors();
32-
}
33-
34-
public function test_password_is_not_confirmed_with_invalid_password()
35-
{
36-
$user = User::factory()->create();
37-
38-
$response = $this->actingAs($user)->post('/confirm-password', [
39-
'password' => 'wrong-password',
40-
]);
41-
42-
$response->assertSessionHasErrors();
24+
$response = $this->get('/user/confirm-password');
25+
$response->assertRedirect('/login');
4326
}
4427
}

tests/Feature/Settings/TwoFactorAuthenticationTest.php

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,84 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Inertia\Testing\AssertableInertia as Assert;
78
use Laravel\Fortify\Features;
89
use Tests\TestCase;
910

1011
class 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

Comments
 (0)