Skip to content

Instantly share code, notes, and snippets.

@lewislarsen
Created January 17, 2023 20:00
Show Gist options
  • Save lewislarsen/1b3857e206bbd60e5811b41c15625638 to your computer and use it in GitHub Desktop.
Save lewislarsen/1b3857e206bbd60e5811b41c15625638 to your computer and use it in GitHub Desktop.
The tests used for adding magic links to your Laravel application.
// tests/Feature/Mail/Auth/MagicLinkEmailTest.php
<?php
use App\Mail\Auth\MagicLinkEmail;
use App\Models\User;
test('the mail has the correct contents', function () {
$user = User::factory()->create();
$mailable = new MagicLinkEmail($user);
$mailable->assertHasSubject(__('Your login link for :app', ['app' => config('app.name')]));
$mailable->assertSeeInHtml('/magiclink/'); // We can't assert the full URL because it's dynamic and we don't know what it will be.
$mailable->assertSeeInText($user->name);
// Language lines
$mailable->assertSeeInText(__('If you did not request a login, no further action is required.'));
$mailable->assertSeeInText(__('You are receiving this email because we received a login request for your account.'));
$mailable->assertSeeInText(__('Please do not share the link with anybody as it will grant access to your account.'));
$mailable->assertSeeInText(__('Use the button below to log in. The link will expire in one hour.'));
});
// tests/Feature/Auth/MagicLinkTest.php
<?php
use App\Mail\Auth\MagicLinkEmail;
use App\Models\User;
it('can be rendered', function () {
$response = $this->get(route('login.magic-link'));
$response->assertStatus(200);
$response->assertViewIs('auth.magic-link');
});
it('does not pass validation with an empty email field', function () {
$response = $this->post(route('login.magic-link.submit'));
$response->assertStatus(302);
$response->assertSessionHasErrors('email');
});
it('does not pass validation with an invalid email field', function () {
$response = $this->post(route('login.magic-link.submit'), [
'email' => 'invalid-email',
]);
$response->assertStatus(302);
$response->assertSessionHasErrors('email');
});
it('can be requested with a valid users email address', function () {
Mail::fake();
$user = User::factory()->create();
$response = $this->post(route('login.magic-link.submit'), [
'email' => $user->email,
]);
Mail::assertQueued(MagicLinkEmail::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
});
$response->assertStatus(302);
$response->assertSessionHas('status', __('If an account exists for the email address you entered, a magic link will be sent to that address.'));
});
it('does not queue a mailable for an email that is not registered', function () {
Mail::fake();
$response = $this->post(route('login.magic-link.submit'), [
'email' => 'fake-email@gmail.com',
]);
Mail::assertNotQueued(MagicLinkEmail::class);
$response->assertStatus(302);
$response->assertSessionHas('status', __('If an account exists for the email address you entered, a magic link will be sent to that address.'));
});
it('can be rate limited if there are too many requests', function () {
for ($i = 0; $i < 5; $i++) {
$this->post(route('login.magic-link.submit'), [
'email' => 'john.doe@email.com'
]);
}
$response = $this->post(route('login.magic-link.submit'), [
'email' => 'jane.doe@email.com'
]);
$response->assertStatus(302);
$response->assertSessionHasErrors('email');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment