Skip to content

Commit 849d768

Browse files
committed
wip
Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent e23528a commit 849d768

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed

VerificationNotificationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
<?php
2+
3+
use App\Models\User;

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"require": {
1414
"php": "^8.3",
1515
"pestphp/pest": "^4.0",
16-
"pestphp/pest-plugin-browser": "^4.0"
16+
"pestphp/pest-plugin-browser": "^4.0",
17+
"pestphp/pest-plugin-laravel": "^4.0"
1718
},
1819
"require-dev": {
1920
"laravel/vue-starter-kit": "dev-pest-ci",
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,132 @@
11
<?php
22

3+
use App\Models\User;
4+
use Illuminate\Auth\Events\Verified;
5+
use Illuminate\Support\Facades\Event;
6+
use Illuminate\Support\Facades\URL;
7+
8+
use function Pest\Laravel\actingAs;
9+
310
test('email verification screen can be rendered', function () {
11+
$user = User::factory()->unverified()->create();
12+
13+
actingAs($user);
14+
15+
visit(route('verification.notice'))
16+
->assertSee('Verify email')
17+
->assertSee('Please verify your email address by clicking on the link we just emailed to you.')
18+
->assertSee('Resend verification email')
19+
->assertNoConsoleLogs()
20+
->assertNoJavaScriptErrors();
21+
});
22+
23+
test('email can be verified', function () {
24+
$user = User::factory()->unverified()->create();
25+
26+
actingAs($user);
27+
28+
Event::fake();
29+
30+
$verificationUrl = URL::temporarySignedRoute(
31+
'verification.verify',
32+
now()->addMinutes(60),
33+
['id' => $user->id, 'hash' => sha1($user->email)]
34+
);
35+
36+
visit($verificationUrl)
37+
->assertUrlIs(route('dashboard'))
38+
->assertQueryStringHas('verified', '1')
39+
->assertNoConsoleLogs()
40+
->assertNoJavaScriptErrors();
41+
42+
Event::assertDispatched(Verified::class);
43+
$this->assertTrue($user->fresh()->hasVerifiedEmail());
44+
});
45+
46+
test('email is not verified with invalid hash', function () {
47+
$user = User::factory()->unverified()->create();
48+
49+
actingAs($user);
50+
51+
Event::fake();
52+
53+
$verificationUrl = URL::temporarySignedRoute(
54+
'verification.verify',
55+
now()->addMinutes(60),
56+
['id' => $user->id, 'hash' => sha1('wrong-email')]
57+
);
58+
59+
visit($verificationUrl)
60+
->assertSee('403')
61+
->assertSee('This action is unauthorized.')
62+
->assertNoConsoleLogs()
63+
->assertNoJavaScriptErrors();
64+
65+
Event::assertNotDispatched(Verified::class);
66+
$this->assertFalse($user->fresh()->hasVerifiedEmail());
67+
});
68+
69+
test('email is not verified with invalid user id', function () {
70+
$user = User::factory()->unverified()->create();
71+
72+
actingAs($user);
73+
74+
Event::fake();
75+
76+
$verificationUrl = URL::temporarySignedRoute(
77+
'verification.verify',
78+
now()->addMinutes(60),
79+
['id' => 123, 'hash' => sha1($user->email)]
80+
);
81+
82+
visit($verificationUrl)
83+
->assertSee('403')
84+
->assertSee('This action is unauthorized.')
85+
->assertNoConsoleLogs()
86+
->assertNoJavaScriptErrors();
87+
88+
Event::assertNotDispatched(Verified::class);
89+
$this->assertFalse($user->fresh()->hasVerifiedEmail());
90+
});
91+
92+
test('verified user is redirected to dashboard from verification prompt', function () {
93+
$user = User::factory()->create([
94+
'email_verified_at' => now(),
95+
]);
96+
97+
actingAs($user);
98+
99+
Event::fake();
100+
101+
visit(route('verification.notice'))
102+
->assertUrlIs(route('dashboard'))
103+
->assertNoConsoleLogs()
104+
->assertNoJavaScriptErrors();
105+
106+
Event::assertNotDispatched(Verified::class);
107+
});
108+
109+
test('already_verified_user_visiting_verification_link_is_redirected_without_firing_event_again', function () {
110+
$user = User::factory()->create([
111+
'email_verified_at' => now(),
112+
]);
113+
114+
actingAs($user);
115+
116+
Event::fake();
117+
118+
$verificationUrl = URL::temporarySignedRoute(
119+
'verification.verify',
120+
now()->addMinutes(60),
121+
['id' => $user->id, 'hash' => sha1($user->email)]
122+
);
123+
124+
visit($verificationUrl)
125+
->assertUrlIs(route('dashboard'))
126+
->assertQueryStringHas('verified', '1')
127+
->assertNoConsoleLogs()
128+
->assertNoJavaScriptErrors();
4129

130+
Event::assertNotDispatched(Verified::class);
131+
$this->assertTrue($user->fresh()->hasVerifiedEmail());
5132
});

tests/Browser/Auth/RegistrationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
test('registration screen can be rendered', function () {
66
visit(route('register'))
7-
->assertNoConsoleLogs()
8-
->assertNoJavaScriptErrors()
97
->assertSee('Create an account')
10-
->assertSee('Enter your details below to create your account');
8+
->assertSee('Enter your details below to create your account')
9+
->assertNoConsoleLogs()
10+
->assertNoJavaScriptErrors();
1111
});
1212

1313
test('new user can be registered', function () {

0 commit comments

Comments
 (0)