ResetPasswordControllerTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Tests\Api\v1\Controllers\Auth;
  3. use App\Models\User;
  4. use Illuminate\Support\Facades\Hash;
  5. use Illuminate\Support\Facades\Password;
  6. use Illuminate\Support\Facades\Notification;
  7. use Tests\FeatureTestCase;
  8. class ResetPasswordControllerTest extends FeatureTestCase
  9. {
  10. /**
  11. * @var \App\Models\User
  12. */
  13. protected $user;
  14. /**
  15. * @test
  16. */
  17. public function test_submit_reset_password_without_input_returns_validation_error()
  18. {
  19. $response = $this->json('POST', '/api/v1/user/password/reset', [
  20. 'email' => '',
  21. 'password' => '',
  22. 'password_confirmation' => '',
  23. 'token' => ''
  24. ]);
  25. $response->assertStatus(422)
  26. ->assertJsonValidationErrors(['email', 'password', 'token']);
  27. }
  28. /**
  29. * @test
  30. */
  31. public function test_submit_reset_password_with_invalid_data_returns_validation_error()
  32. {
  33. $response = $this->json('POST', '/api/v1/user/password/reset', [
  34. 'email' => 'qsdqsdqsd',
  35. 'password' => 'foofoofoo',
  36. 'password_confirmation' => 'barbarbar',
  37. 'token' => 'token'
  38. ]);
  39. $response->assertStatus(422)
  40. ->assertJsonValidationErrors(['email', 'password']);
  41. }
  42. /**
  43. * @test
  44. */
  45. public function test_submit_reset_password_with_too_short_pwd_returns_validation_error()
  46. {
  47. $response = $this->json('POST', '/api/v1/user/password/reset', [
  48. 'email' => 'foo@bar.com',
  49. 'password' => 'foo',
  50. 'password_confirmation' => 'foo',
  51. 'token' => 'token'
  52. ]);
  53. $response->assertStatus(422)
  54. ->assertJsonValidationErrors(['password']);
  55. }
  56. /**
  57. * @test
  58. */
  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', '/api/v1/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. }