92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Events\Verified;
|
|
use Illuminate\Auth\Notifications\VerifyEmail;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
use Tests\UpdatesSettings;
|
|
|
|
uses(RefreshDatabase::class);
|
|
uses(UpdatesSettings::class);
|
|
|
|
test('unverified user sees verification notice', function () {
|
|
$user = User::factory()->create(['email_verified_at' => null]);
|
|
|
|
$this->actingAs($user)
|
|
->get('email/verify')
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page->component('auth/VerifyEmail'));
|
|
});
|
|
|
|
test('already verified user is redirected from notice page', function () {
|
|
$user = User::factory()->create(['email_verified_at' => Carbon::now()]);
|
|
|
|
$this->actingAs($user)
|
|
->get('email/verify')
|
|
->assertRedirect('/');
|
|
});
|
|
|
|
test('signed verification link marks email as verified', function () {
|
|
Event::fake([Verified::class]);
|
|
|
|
$user = User::factory()->create(['email_verified_at' => null]);
|
|
|
|
$signedUrl = URL::temporarySignedRoute(
|
|
'verification.verify',
|
|
Carbon::now()->addMinutes(60),
|
|
['id' => $user->id, 'hash' => sha1($user->email)]
|
|
);
|
|
|
|
$this->actingAs($user)
|
|
->get($signedUrl)
|
|
->assertRedirect('/');
|
|
|
|
$this->assertNotNull($user->fresh()->email_verified_at);
|
|
|
|
Event::assertDispatched(Verified::class);
|
|
});
|
|
|
|
test('verification link with invalid signature fails', function () {
|
|
$user = User::factory()->create(['email_verified_at' => null]);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('verification.verify', ['id' => $user->id, 'hash' => sha1($user->email)]))
|
|
->assertStatus(403);
|
|
|
|
$this->assertNull($user->fresh()->email_verified_at);
|
|
});
|
|
|
|
test('resend verification email', function () {
|
|
Notification::fake();
|
|
|
|
$user = User::factory()->create(['email_verified_at' => null]);
|
|
|
|
$this->actingAs($user)
|
|
->post('email/resend')
|
|
->assertRedirect();
|
|
|
|
Notification::assertSentTo($user, VerifyEmail::class);
|
|
});
|
|
|
|
test('resend does nothing for already verified user', function () {
|
|
Notification::fake();
|
|
|
|
$user = User::factory()->create(['email_verified_at' => Carbon::now()]);
|
|
|
|
$this->actingAs($user)
|
|
->post('email/resend')
|
|
->assertRedirect('/');
|
|
|
|
Notification::assertNothingSent();
|
|
});
|
|
|
|
test('guest cannot access verification routes', function () {
|
|
$this->get('email/verify')->assertRedirect('login');
|
|
$this->post('email/resend')->assertRedirect('login');
|
|
});
|