ResetPasswordControllerTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Http\Controllers\Auth\ResetPasswordController;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\Hash;
  6. use Illuminate\Support\Facades\Notification;
  7. use Illuminate\Support\Facades\Password;
  8. use PHPUnit\Framework\Attributes\CoversClass;
  9. use Tests\FeatureTestCase;
  10. /**
  11. * ResetPasswordControllerTest test class
  12. */
  13. #[CoversClass(ResetPasswordController::class)]
  14. #[CoversClass(User::class)]
  15. class ResetPasswordControllerTest extends FeatureTestCase
  16. {
  17. /**
  18. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  19. */
  20. protected $user;
  21. /**
  22. * @test
  23. */
  24. public function test_submit_reset_password_without_input_returns_validation_error()
  25. {
  26. $response = $this->json('POST', '/user/password/reset', [
  27. 'email' => '',
  28. 'password' => '',
  29. 'password_confirmation' => '',
  30. 'token' => '',
  31. ]);
  32. $response->assertStatus(422)
  33. ->assertJsonValidationErrors(['email', 'password', 'token']);
  34. }
  35. /**
  36. * @test
  37. */
  38. public function test_submit_reset_password_with_invalid_data_returns_validation_error()
  39. {
  40. $response = $this->json('POST', '/user/password/reset', [
  41. 'email' => 'qsdqsdqsd',
  42. 'password' => 'foofoofoo',
  43. 'password_confirmation' => 'barbarbar',
  44. 'token' => 'token',
  45. ]);
  46. $response->assertStatus(422)
  47. ->assertJsonValidationErrors(['email', 'password']);
  48. }
  49. /**
  50. * @test
  51. */
  52. public function test_submit_reset_password_with_too_short_pwd_returns_validation_error()
  53. {
  54. $response = $this->json('POST', '/user/password/reset', [
  55. 'email' => 'foo@bar.com',
  56. 'password' => 'foo',
  57. 'password_confirmation' => 'foo',
  58. 'token' => 'token',
  59. ]);
  60. $response->assertStatus(422)
  61. ->assertJsonValidationErrors(['password']);
  62. }
  63. /**
  64. * @test
  65. */
  66. public function test_submit_reset_password_returns_success()
  67. {
  68. Notification::fake();
  69. $this->user = User::factory()->create();
  70. $token = Password::broker()->createToken($this->user);
  71. $response = $this->json('POST', '/user/password/reset', [
  72. 'email' => $this->user->email,
  73. 'password' => 'newpassword',
  74. 'password_confirmation' => 'newpassword',
  75. 'token' => $token,
  76. ]);
  77. $this->user->refresh();
  78. $response->assertOk();
  79. $this->assertTrue(Hash::check('newpassword', $this->user->password));
  80. }
  81. }