Skip to content

Commit 974a427

Browse files
Merge pull request #152 from JaavierR/test/add-missing-test
test: Add missing feature tests to ensure 100% coverage in starter kit
2 parents 33a99fe + e6e6ffe commit 974a427

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,29 @@ public function test_users_can_logout()
5151
$this->assertGuest();
5252
$response->assertRedirect('/');
5353
}
54+
55+
public function test_users_are_rate_limited()
56+
{
57+
$user = User::factory()->create();
58+
59+
for ($i = 0; $i < 5; $i++) {
60+
$this->post('/login', [
61+
'email' => $user->email,
62+
'password' => 'wrong-password',
63+
])->assertStatus(302)->assertSessionHasErrors([
64+
'email' => 'These credentials do not match our records.',
65+
]);
66+
}
67+
68+
$response = $this->post('/login', [
69+
'email' => $user->email,
70+
'password' => 'wrong-password',
71+
]);
72+
73+
$response->assertSessionHasErrors('email');
74+
75+
$errors = session('errors');
76+
77+
$this->assertStringContainsString('Too many login attempts', $errors->first('email'));
78+
}
5479
}

tests/Feature/Auth/EmailVerificationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,53 @@ public function test_email_is_not_verified_with_invalid_hash()
5555

5656
$this->assertFalse($user->fresh()->hasVerifiedEmail());
5757
}
58+
59+
public function test_email_is_not_verified_with_invalid_user_id(): void
60+
{
61+
$user = User::factory()->create([
62+
'email_verified_at' => null,
63+
]);
64+
65+
$verificationUrl = URL::temporarySignedRoute(
66+
'verification.verify',
67+
now()->addMinutes(60),
68+
['id' => 123, 'hash' => sha1($user->email)]
69+
);
70+
71+
$this->actingAs($user)->get($verificationUrl);
72+
73+
$this->assertFalse($user->fresh()->hasVerifiedEmail());
74+
}
75+
76+
public function test_verified_user_is_redirected_to_dashboard_from_verification_prompt(): void
77+
{
78+
$user = User::factory()->create([
79+
'email_verified_at' => now(),
80+
]);
81+
82+
$response = $this->actingAs($user)->get('/verify-email');
83+
84+
$response->assertRedirect(route('dashboard', absolute: false));
85+
}
86+
87+
public function test_already_verified_user_visiting_verification_link_is_redirected_without_firing_event_again(): void
88+
{
89+
$user = User::factory()->create([
90+
'email_verified_at' => now(),
91+
]);
92+
93+
Event::fake();
94+
95+
$verificationUrl = URL::temporarySignedRoute(
96+
'verification.verify',
97+
now()->addMinutes(60),
98+
['id' => $user->id, 'hash' => sha1($user->email)]
99+
);
100+
101+
$this->actingAs($user)->get($verificationUrl)
102+
->assertRedirect(route('dashboard', absolute: false).'?verified=1');
103+
104+
$this->assertTrue($user->fresh()->hasVerifiedEmail());
105+
Event::assertNotDispatched(Verified::class);
106+
}
58107
}

tests/Feature/Auth/PasswordResetTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,18 @@ public function test_password_can_be_reset_with_valid_token()
7070
return true;
7171
});
7272
}
73+
74+
public function test_password_cannot_be_reset_with_invalid_token(): void
75+
{
76+
$user = User::factory()->create();
77+
78+
$response = $this->post('/reset-password', [
79+
'token' => 'invalid-token',
80+
'email' => $user->email,
81+
'password' => 'newpassword123',
82+
'password_confirmation' => 'newpassword123',
83+
]);
84+
85+
$response->assertSessionHasErrors('email');
86+
}
7387
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tests\Feature\Auth;
4+
5+
use App\Models\User;
6+
use Illuminate\Auth\Notifications\VerifyEmail;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Support\Facades\Notification;
9+
use Tests\TestCase;
10+
11+
class VerificationNotificationTest extends TestCase
12+
{
13+
use RefreshDatabase;
14+
15+
public function test_sends_verification_notification(): void
16+
{
17+
Notification::fake();
18+
19+
$user = User::factory()->create([
20+
'email_verified_at' => null,
21+
]);
22+
23+
$this->actingAs($user)
24+
->post('email/verification-notification')
25+
->assertRedirect('/');
26+
27+
Notification::assertSentTo($user, VerifyEmail::class);
28+
}
29+
30+
public function test_does_not_send_verification_notification_if_email_is_verified(): void
31+
{
32+
Notification::fake();
33+
34+
$user = User::factory()->create([
35+
'email_verified_at' => now(),
36+
]);
37+
38+
$this->actingAs($user)
39+
->post('email/verification-notification')
40+
->assertRedirect(route('dashboard', absolute: false));
41+
42+
Notification::assertNothingSent();
43+
}
44+
}

tests/Feature/Settings/PasswordUpdateTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ class PasswordUpdateTest extends TestCase
1111
{
1212
use RefreshDatabase;
1313

14+
public function test_password_update_page_is_displayed()
15+
{
16+
$user = User::factory()->create();
17+
18+
$response = $this
19+
->actingAs($user)
20+
->get('/settings/password');
21+
22+
$response->assertStatus(200);
23+
}
24+
1425
public function test_password_can_be_updated()
1526
{
1627
$user = User::factory()->create();

0 commit comments

Comments
 (0)