PasswordResetTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. use App\Models\User;
  3. use Illuminate\Auth\Notifications\ResetPassword;
  4. use Illuminate\Support\Facades\Notification;
  5. test('reset password link screen can be rendered', function () {
  6. $response = $this->get(route('password.request'));
  7. $response->assertStatus(200);
  8. });
  9. test('reset password link can be requested', function () {
  10. Notification::fake();
  11. $user = User::factory()->create();
  12. $this->post(route('password.email'), ['email' => $user->email]);
  13. Notification::assertSentTo($user, ResetPassword::class);
  14. });
  15. test('reset password screen can be rendered', function () {
  16. Notification::fake();
  17. $user = User::factory()->create();
  18. $this->post(route('password.email'), ['email' => $user->email]);
  19. Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
  20. $response = $this->get(route('password.reset', $notification->token));
  21. $response->assertStatus(200);
  22. return true;
  23. });
  24. });
  25. test('password can be reset with valid token', function () {
  26. Notification::fake();
  27. $user = User::factory()->create();
  28. $this->post(route('password.email'), ['email' => $user->email]);
  29. Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
  30. $response = $this->post(route('password.store'), [
  31. 'token' => $notification->token,
  32. 'email' => $user->email,
  33. 'password' => 'password',
  34. 'password_confirmation' => 'password',
  35. ]);
  36. $response
  37. ->assertSessionHasNoErrors()
  38. ->assertRedirect(route('login'));
  39. return true;
  40. });
  41. });
  42. test('password cannot be reset with invalid token', function () {
  43. $user = User::factory()->create();
  44. $response = $this->post(route('password.store'), [
  45. 'token' => 'invalid-token',
  46. 'email' => $user->email,
  47. 'password' => 'newpassword123',
  48. 'password_confirmation' => 'newpassword123',
  49. ]);
  50. $response->assertSessionHasErrors('email');
  51. });