Skip to content

Commit

Permalink
✅ add tests for email verification (#2735)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Jul 5, 2024
1 parent bc50784 commit 37ce046
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions tests/Feature/Frontend/Auth/EmailVerificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php declare(strict_types=1);

namespace Tests\Feature\Frontend\Auth;

use App\Models\User;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\FeatureTestCase;

class EmailVerificationTest extends FeatureTestCase
{
use RefreshDatabase;

public function testEmailVerificationNotification(): void {
Notification::fake();

$user = User::factory(['email_verified_at' => null])->create();
$response = $this->actingAs($user)
->followingRedirects()
->post(route('verification.resend'));
$response->assertOk();

Notification::assertSentTo($user, VerifyEmail::class);
}

public function testEmailVerificationJSONNotification(): void {
Notification::fake();

$user = User::factory(['email_verified_at' => null])->create();
$response = $this->actingAs($user)
->followingRedirects()
->post(route('verification.resend'), [], [
'Accept' => 'application/json',
]);
$response->assertAccepted();

Notification::assertSentTo($user, VerifyEmail::class);
}

public function testEmailVerificationNotificationWithVerifiedEmail(): void {
Notification::fake();

$user = User::factory(['email_verified_at' => today()])->create();
$response = $this->actingAs($user)
->followingRedirects()
->post(route('verification.resend'));
$response->assertOk();

// test json response (should be 204 with already verified email)
$response = $this->actingAs($user)
->followingRedirects()
->post(route('verification.resend'), [], [
'Accept' => 'application/json',
]);
$response->assertNoContent();

Notification::assertNothingSent();
}

public function testEmailVerificationNotificationWithTooManyRequests(): void {
Notification::fake();

$user = User::factory(['email_verified_at' => null])->create();
for ($i = 0; $i < 6; $i++) {
$response = $this->actingAs($user)
->followingRedirects()
->post(route('verification.resend'));
$response->assertOk();
}

$response = $this->actingAs($user)
->post(route('verification.resend'));
$response->assertTooManyRequests();

// but should still send the email once
Notification::assertSentTo($user, VerifyEmail::class, 1);
}

}

0 comments on commit 37ce046

Please sign in to comment.