Skip to content

Commit 6172b74

Browse files
committed
Test Fix
1 parent e72ce33 commit 6172b74

File tree

6 files changed

+184
-457
lines changed

6 files changed

+184
-457
lines changed

app/Providers/FortifyServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Support\Facades\RateLimiter;
88
use Illuminate\Support\ServiceProvider;
99
use Laravel\Fortify\Fortify;
10-
use Livewire\Volt\Volt;
1110

1211
class FortifyServiceProvider extends ServiceProvider
1312
{

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
->name('settings.two-factor');
2929
});
3030

31-
require __DIR__ . '/auth.php';
31+
require __DIR__.'/auth.php';

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Laravel\Fortify\Features;
78
use Livewire\Volt\Volt as LivewireVolt;
89
use Tests\TestCase;
910

@@ -58,4 +59,47 @@ public function test_users_can_logout(): void
5859

5960
$this->assertGuest();
6061
}
62+
63+
public function test_users_with_two_factor_enabled_are_redirected_to_two_factor_challenge(): void
64+
{
65+
if (! Features::canManageTwoFactorAuthentication()) {
66+
$this->markTestSkipped('Two-factor authentication is not enabled.');
67+
}
68+
69+
Features::twoFactorAuthentication([
70+
'confirm' => true,
71+
'confirmPassword' => true,
72+
]);
73+
74+
$user = User::factory()->create();
75+
76+
$user->forceFill([
77+
'two_factor_secret' => encrypt('test-secret'),
78+
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
79+
'two_factor_confirmed_at' => now(),
80+
])->save();
81+
82+
$response = LivewireVolt::test('auth.login')
83+
->set('email', $user->email)
84+
->set('password', 'password')
85+
->call('login');
86+
87+
$response->assertRedirect(route('two-factor.login'));
88+
$response->assertSessionHas('login.id', $user->id);
89+
$this->assertGuest();
90+
}
91+
92+
public function test_users_without_two_factor_enabled_login_normally(): void
93+
{
94+
$user = User::factory()->create();
95+
96+
$response = LivewireVolt::test('auth.login')
97+
->set('email', $user->email)
98+
->set('password', 'password')
99+
->call('login');
100+
101+
$this->assertAuthenticated();
102+
$response->assertRedirect(route('dashboard', absolute: false));
103+
$response->assertSessionMissing('login.id');
104+
}
61105
}

tests/Feature/Auth/TwoFactorChallengeTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Illuminate\Support\Facades\RateLimiter;
87
use Laravel\Fortify\Features;
98
use Livewire\Volt\Volt;
109
use Tests\TestCase;
@@ -15,7 +14,7 @@ class TwoFactorChallengeTest extends TestCase
1514

1615
public function test_two_factor_challenge_redirects_when_not_authenticated(): void
1716
{
18-
if (!Features::canManageTwoFactorAuthentication()) {
17+
if (! Features::canManageTwoFactorAuthentication()) {
1918
$this->markTestSkipped('Two-factor authentication is not enabled.');
2019
}
2120

@@ -26,7 +25,7 @@ public function test_two_factor_challenge_redirects_when_not_authenticated(): vo
2625

2726
public function test_two_factor_challenge_renders_correct_livewire_component(): void
2827
{
29-
if (!Features::canManageTwoFactorAuthentication()) {
28+
if (! Features::canManageTwoFactorAuthentication()) {
3029
$this->markTestSkipped('Two-factor authentication is not enabled.');
3130
}
3231

@@ -53,7 +52,7 @@ public function test_two_factor_challenge_renders_correct_livewire_component():
5352

5453
public function test_two_factor_authentication_is_rate_limited(): void
5554
{
56-
if (!Features::enabled(Features::twoFactorAuthentication())) {
55+
if (! Features::enabled(Features::twoFactorAuthentication())) {
5756
$this->markTestSkipped('Two-factor authentication is not enabled.');
5857
}
5958

@@ -70,7 +69,7 @@ public function test_two_factor_authentication_is_rate_limited(): void
7069
'two_factor_confirmed_at' => now(),
7170
])->save();
7271

73-
collect(range(1, 5))->each(function () use ($user) {
72+
collect(range(1, 5))->each(function () {
7473
$this->post(route('two-factor.login.store'), ['code' => '21212'])
7574
->assertRedirect(route('two-factor.login'))
7675
->assertSessionHasErrors('code');
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Tests\Feature\Settings;
4+
5+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Routing\Route;
8+
use Illuminate\Support\Facades\Artisan;
9+
use Laravel\Fortify\Features;
10+
use Livewire\Volt\Volt;
11+
use Tests\TestCase;
12+
13+
class TwoFactorAuthenticationTest extends TestCase
14+
{
15+
use RefreshDatabase;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
if (! Features::canManageTwoFactorAuthentication()) {
22+
$this->markTestSkipped('Two-factor authentication is not enabled.');
23+
}
24+
25+
Features::twoFactorAuthentication([
26+
'confirm' => true,
27+
'confirmPassword' => true,
28+
]);
29+
}
30+
31+
public function test_two_factor_settings_page_is_displayed(): void
32+
{
33+
$user = User::factory()->create();
34+
35+
$this->actingAs($user)
36+
->withSession(['auth.password_confirmed_at' => time()])
37+
->get(route('settings.two-factor'))
38+
->assertOk()
39+
->assertSee('Two Factor Authentication')
40+
->assertSee('Disabled');
41+
}
42+
43+
public function test_two_factor_settings_page_requires_password_confirmation(): void
44+
{
45+
$user = User::factory()->create();
46+
47+
$response = $this->actingAs($user)
48+
->get(route('settings.two-factor'));
49+
50+
$response->assertRedirect(route('password.confirm'));
51+
}
52+
53+
public function test_two_factor_settings_page_returns_forbidden_when_two_factor_is_disabled(): void
54+
{
55+
config(['fortify.features' => []]);
56+
57+
$user = User::factory()->create();
58+
59+
$response = $this->actingAs($user)
60+
->withSession(['auth.password_confirmed_at' => time()])
61+
->get(route('settings.two-factor'));
62+
63+
$response->assertForbidden();
64+
}
65+
66+
public function test_enable_two_factor_sets_up_confirmation_flow_when_confirmation_required(): void
67+
{
68+
Features::twoFactorAuthentication([
69+
'confirm' => true,
70+
'confirmPassword' => false,
71+
]);
72+
73+
$user = User::factory()->create();
74+
75+
$this->actingAs($user);
76+
77+
$component = Volt::test('settings.two-factor')
78+
->call('enable');
79+
80+
$component->assertSet('twoFactorEnabled', false);
81+
$this->assertNotEmpty($component->get('qrCodeSvg'));
82+
$this->assertNotEmpty($component->get('manualSetupKey'));
83+
84+
$user->refresh();
85+
$this->assertNotNull($user->two_factor_secret);
86+
$this->assertNotNull($user->two_factor_recovery_codes);
87+
$this->assertNull($user->two_factor_confirmed_at);
88+
}
89+
90+
public function test_enable_two_factor_immediately_enables_when_confirmation_not_required(): void
91+
{
92+
Features::twoFactorAuthentication([
93+
'confirm' => false,
94+
'confirmPassword' => false,
95+
]);
96+
97+
$user = User::factory()->create();
98+
99+
$this->actingAs($user);
100+
101+
$component = Volt::test('settings.two-factor')
102+
->call('enable')
103+
->assertSet('twoFactorEnabled', true);
104+
105+
$this->assertNotEmpty($component->get('qrCodeSvg'));
106+
$this->assertNotEmpty($component->get('manualSetupKey'));
107+
108+
$user->refresh();
109+
$this->assertNotNull($user->two_factor_secret);
110+
$this->assertNotNull($user->two_factor_recovery_codes);
111+
}
112+
113+
public function test_two_factor_authentication_disabled_when_confirmation_abandoned_between_requests(): void
114+
{
115+
$user = User::factory()->create();
116+
117+
$user->forceFill([
118+
'two_factor_secret' => encrypt('test-secret'),
119+
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
120+
'two_factor_confirmed_at' => null,
121+
])->save();
122+
123+
$this->actingAs($user);
124+
125+
$component = Volt::test('settings.two-factor');
126+
127+
$component->assertSet('twoFactorEnabled', false);
128+
129+
$this->assertDatabaseHas('users', [
130+
'id' => $user->id,
131+
'two_factor_secret' => null,
132+
'two_factor_recovery_codes' => null,
133+
]);
134+
}
135+
}

0 commit comments

Comments
 (0)