Skip to content

Commit e72ce33

Browse files
committed
Add Tests
1 parent d965439 commit e72ce33

File tree

2 files changed

+532
-0
lines changed

2 files changed

+532
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Tests\Feature\Auth;
4+
5+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Support\Facades\RateLimiter;
8+
use Laravel\Fortify\Features;
9+
use Livewire\Volt\Volt;
10+
use Tests\TestCase;
11+
12+
class TwoFactorChallengeTest extends TestCase
13+
{
14+
use RefreshDatabase;
15+
16+
public function test_two_factor_challenge_redirects_when_not_authenticated(): void
17+
{
18+
if (!Features::canManageTwoFactorAuthentication()) {
19+
$this->markTestSkipped('Two-factor authentication is not enabled.');
20+
}
21+
22+
$response = $this->get(route('two-factor.login'));
23+
24+
$response->assertRedirect(route('login'));
25+
}
26+
27+
public function test_two_factor_challenge_renders_correct_livewire_component(): void
28+
{
29+
if (!Features::canManageTwoFactorAuthentication()) {
30+
$this->markTestSkipped('Two-factor authentication is not enabled.');
31+
}
32+
33+
Features::twoFactorAuthentication([
34+
'confirm' => true,
35+
'confirmPassword' => true,
36+
]);
37+
38+
$user = User::factory()->create();
39+
40+
$user->forceFill([
41+
'two_factor_secret' => encrypt('test-secret'),
42+
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
43+
'two_factor_confirmed_at' => now(),
44+
])->save();
45+
46+
Volt::test('auth.login')
47+
->set('email', $user->email)
48+
->set('password', 'password')
49+
->call('login')
50+
->assertRedirect(route('two-factor.login'))
51+
->assertOk();
52+
}
53+
54+
public function test_two_factor_authentication_is_rate_limited(): void
55+
{
56+
if (!Features::enabled(Features::twoFactorAuthentication())) {
57+
$this->markTestSkipped('Two-factor authentication is not enabled.');
58+
}
59+
60+
Features::twoFactorAuthentication([
61+
'confirm' => true,
62+
'confirmPassword' => true,
63+
]);
64+
65+
$user = User::factory()->create();
66+
67+
$user->forceFill([
68+
'two_factor_secret' => encrypt(implode(range('A', 'P'))),
69+
'two_factor_recovery_codes' => encrypt(json_encode(['recovery-code-1', 'recovery-code-2'])),
70+
'two_factor_confirmed_at' => now(),
71+
])->save();
72+
73+
collect(range(1, 5))->each(function () use ($user) {
74+
$this->post(route('two-factor.login.store'), ['code' => '21212'])
75+
->assertRedirect(route('two-factor.login'))
76+
->assertSessionHasErrors('code');
77+
});
78+
79+
$this->post(route('two-factor.login.store'), ['code' => '000000'])
80+
->assertTooManyRequests();
81+
}
82+
}

0 commit comments

Comments
 (0)