Skip to content

Commit cb61623

Browse files
committed
Getting tests to pass
1 parent 23fd86c commit cb61623

File tree

4 files changed

+63
-37
lines changed

4 files changed

+63
-37
lines changed

.DS_Store

-6 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/storage/*.key
88
/storage/pail
99
/vendor
10+
.DS_Store
1011
.env
1112
.env.backup
1213
.env.production

app/Http/Controllers/Auth/TwoFactorAuthenticatedSessionController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,21 @@ public function store(Request $request)
6464

6565
// Handle recovery code
6666
if ($request->filled('recovery_code')) {
67-
$recoveryCodes = $user->recoveryCodes();
67+
$recoveryCodes = json_decode(decrypt($user->two_factor_recovery_codes), true);
6868
$provided = $request->recovery_code;
6969
$match = collect($recoveryCodes)->first(function ($code) use ($provided) {
7070
return hash_equals($code, $provided);
7171
});
7272
if (! $match) {
7373
return back()->withErrors(['recovery_code' => __('The provided two factor authentication recovery code was invalid.')]);
7474
}
75-
// Remove used recovery code
76-
$user->replaceRecoveryCode($match);
75+
// Remove used recovery code using the ProcessRecoveryCode action
76+
$updatedCodes = app(\App\Actions\TwoFactorAuth\ProcessRecoveryCode::class)($recoveryCodes, $match);
77+
if ($updatedCodes === false) {
78+
return back()->withErrors(['recovery_code' => __('The provided two factor authentication recovery code was invalid.')]);
79+
}
80+
$user->two_factor_recovery_codes = encrypt(json_encode($updatedCodes));
81+
$user->save();
7782
Auth::login($user, $request->session()->get('login.remember', false));
7883
$request->session()->regenerate();
7984
$request->session()->forget(['login.id', 'login.remember']);

tests/Feature/Auth/TwoFactorAuthTest.php

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

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Livewire\Volt\Volt as LivewireVolt;
7+
// Removed LivewireVolt import; not needed for React stack tests.
88
use Tests\TestCase;
99
use App\Actions\TwoFactorAuth\GenerateQrCodeAndSecretKey;
1010
use App\Actions\TwoFactorAuth\GenerateNewRecoveryCodes;
@@ -23,23 +23,32 @@ public function test_can_view_two_factor_settings_page()
2323
->get('/settings/two-factor');
2424

2525
$response->assertStatus(200);
26-
$response->assertSee('Two Factor Authentication');
27-
$response->assertSee('Disabled');
26+
27+
// Check Inertia props instead of HTML
28+
$inertiaProps = $response->original?->getData() ?? [];
29+
if (isset($inertiaProps['page']['props'])) {
30+
$props = $inertiaProps['page']['props'];
31+
$this->assertArrayHasKey('enabled', $props);
32+
$this->assertArrayHasKey('confirmed', $props);
33+
$this->assertArrayHasKey('recoveryCodes', $props);
34+
$this->assertFalse($props['enabled']);
35+
$this->assertFalse($props['confirmed']);
36+
} else {
37+
// Fallback: check for expected strings in HTML (legacy/SSR)
38+
$response->assertSee('Two Factor Authentication');
39+
$response->assertSee('Disabled');
40+
}
2841
}
2942

3043
public function test_can_enable_two_factor_authentication()
3144
{
3245
$user = User::factory()->create();
33-
3446
$this->actingAs($user);
35-
36-
$component = LivewireVolt::test('settings.two-factor')
37-
->call('enable')
38-
->assertSet('enabled', true);
39-
40-
$this->assertTrue($component->enabled);
41-
42-
// Verify the database was updated with two-factor secret and recovery codes
47+
48+
// Simulate enabling 2FA via POST
49+
$response = $this->post('/settings/two-factor');
50+
$response->assertRedirect(); // Should redirect after enabling
51+
4352
$user->refresh();
4453
$this->assertNotNull($user->two_factor_secret);
4554
$this->assertNotNull($user->two_factor_recovery_codes);
@@ -69,8 +78,20 @@ public function test_can_manually_enable_two_factor_authentication()
6978
->get('/settings/two-factor');
7079

7180
$response->assertStatus(200);
72-
$response->assertSee('Enabled');
73-
$response->assertSee('2FA Recovery Codes');
81+
// For Inertia/React, check the JSON response props instead of HTML content
82+
$inertiaProps = $response->original?->getData() ?? [];
83+
if (isset($inertiaProps['page']['props'])) {
84+
$props = $inertiaProps['page']['props'];
85+
$this->assertTrue(
86+
($props['enabled'] ?? false) === true,
87+
'2FA should be enabled in page props.'
88+
);
89+
$this->assertArrayHasKey('recoveryCodes', $props);
90+
} else {
91+
// Fallback: check for expected strings in HTML (for legacy Inertia SSR)
92+
$response->assertSee('Enabled');
93+
$response->assertSee('2FA Recovery Codes');
94+
}
7495
}
7596

7697
public function test_user_with_two_factor_enabled_is_redirected_to_challenge_page_after_login()
@@ -93,17 +114,15 @@ public function test_user_with_two_factor_enabled_is_redirected_to_challenge_pag
93114
])->save();
94115

95116
// Attempt to login and check for redirect
96-
$component = LivewireVolt::test('auth.login')
97-
->set('email', $user->email)
98-
->set('password', 'password')
99-
->call('login');
100-
101-
// Check for redirect to two-factor challenge page
102-
$component->assertRedirect('/two-factor-challenge');
103-
117+
$response = $this->post('/login', [
118+
'email' => $user->email,
119+
'password' => 'password',
120+
]);
121+
$response->assertRedirect('/two-factor-challenge');
122+
104123
// Also verify the session has the login.id value set
105-
$this->assertTrue(Session::has('login.id'));
106-
$this->assertEquals($user->id, Session::get('login.id'));
124+
$this->assertTrue(session()->has('login.id'));
125+
$this->assertEquals($user->id, session('login.id'));
107126
}
108127

109128
public function test_can_authenticate_with_recovery_code()
@@ -131,20 +150,20 @@ public function test_can_authenticate_with_recovery_code()
131150
// Get the first recovery code
132151
$recoveryCode = $recoveryCodes[0];
133152

134-
// Test authentication with recovery code
135-
$component = LivewireVolt::test('auth.two-factor-challenge')
136-
->set('recovery', true)
137-
->set('recovery_code', $recoveryCode)
138-
->call('submit_recovery_code');
153+
// Test authentication with recovery code via POST
154+
$response = $this->post('/two-factor-challenge', [
155+
'recovery_code' => $recoveryCode,
156+
]);
157+
$response->assertRedirect('/dashboard');
139158

140159
// Verify the recovery code was removed
141160
$user->refresh();
142161
$updatedRecoveryCodes = json_decode(decrypt($user->two_factor_recovery_codes));
143162
$this->assertCount(count($recoveryCodes) - 1, $updatedRecoveryCodes);
144163
$this->assertNotContains($recoveryCode, $updatedRecoveryCodes);
145-
164+
146165
// Verify the session was cleared
147-
$this->assertFalse(Session::has('login.id'));
166+
$this->assertFalse(session()->has('login.id'));
148167
}
149168

150169
public function test_unauthenticated_user_is_redirected_to_login_when_accessing_two_factor_challenge()
@@ -230,9 +249,10 @@ public function test_can_disable_two_factor_authentication()
230249
// Test disabling 2FA
231250
$this->actingAs($user);
232251

233-
$component = LivewireVolt::test('settings.two-factor');
234-
$component->call('disable');
235-
252+
// Simulate disabling 2FA via DELETE
253+
$response = $this->delete('/settings/two-factor');
254+
$response->assertRedirect();
255+
236256
// Verify the user's 2FA settings were cleared
237257
$user->refresh();
238258
$this->assertNull($user->two_factor_secret);

0 commit comments

Comments
 (0)