You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

  1. <?php
  2. namespace Tests\Feature\Auth;
  3. use App\Models\User;
  4. use Illuminate\Auth\Events\Verified;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Support\Facades\Event;
  7. use Illuminate\Support\Facades\URL;
  8. use Tests\TestCase;
  9. class EmailVerificationTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. public function test_email_verification_screen_can_be_rendered(): void
  13. {
  14. $user = User::factory()->unverified()->create();
  15. $response = $this->actingAs($user)->get('/verify-email');
  16. $response->assertStatus(200);
  17. }
  18. public function test_email_can_be_verified(): void
  19. {
  20. $user = User::factory()->unverified()->create();
  21. Event::fake();
  22. $verificationUrl = URL::temporarySignedRoute(
  23. 'verification.verify',
  24. now()->addMinutes(60),
  25. ['id' => $user->id, 'hash' => sha1($user->email)]
  26. );
  27. $response = $this->actingAs($user)->get($verificationUrl);
  28. Event::assertDispatched(Verified::class);
  29. $this->assertTrue($user->fresh()->hasVerifiedEmail());
  30. $response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
  31. }
  32. public function test_email_is_not_verified_with_invalid_hash(): void
  33. {
  34. $user = User::factory()->unverified()->create();
  35. $verificationUrl = URL::temporarySignedRoute(
  36. 'verification.verify',
  37. now()->addMinutes(60),
  38. ['id' => $user->id, 'hash' => sha1('wrong-email')]
  39. );
  40. $this->actingAs($user)->get($verificationUrl);
  41. $this->assertFalse($user->fresh()->hasVerifiedEmail());
  42. }
  43. }