ResetPasswordControllerTest.php 2.7 KB

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