ResetPasswordControllerTest.php 2.6 KB

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