Prime-Chat/tests/Feature/Auth/EmailVerificationTest.php

50 lines
1.4 KiB
PHP
Raw Normal View History

2024-12-19 07:21:23 +04:00
<?php
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
2025-01-16 22:22:38 +04:00
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
2024-12-19 07:21:23 +04:00
test('email verification screen can be rendered', function () {
$user = User::factory()->unverified()->create();
$response = $this->actingAs($user)->get('/verify-email');
$response->assertStatus(200);
});
test('email can be verified', function () {
$user = User::factory()->unverified()->create();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$response = $this->actingAs($user)->get($verificationUrl);
Event::assertDispatched(Verified::class);
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
2025-01-16 22:22:38 +04:00
$response->assertRedirect(route('profile.edit', absolute: false).'?verified=1');
2024-12-19 07:21:23 +04:00
});
test('email is not verified with invalid hash', function () {
$user = User::factory()->unverified()->create();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);
$this->actingAs($user)->get($verificationUrl);
expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
});