ForgotPasswordControllerTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Config;
  6. use Illuminate\Auth\Notifications\ResetPassword;
  7. use Illuminate\Support\Facades\Notification;
  8. use Tests\FeatureTestCase;
  9. class ForgotPasswordControllerTest extends FeatureTestCase
  10. {
  11. /**
  12. * @var \App\Models\User
  13. */
  14. protected $user;
  15. /**
  16. * @test
  17. */
  18. public function test_submit_email_password_request_without_email_returns_validation_error()
  19. {
  20. $response = $this->json('POST', '/api/v1/user/password/lost', [
  21. 'email' => ''
  22. ]);
  23. $response->assertStatus(422)
  24. ->assertJsonValidationErrors(['email']);
  25. }
  26. /**
  27. * @test
  28. */
  29. public function test_submit_email_password_request_with_invalid_email_returns_validation_error()
  30. {
  31. $response = $this->json('POST', '/api/v1/user/password/lost', [
  32. 'email' => 'nametest.com'
  33. ]);
  34. $response->assertStatus(422)
  35. ->assertJsonValidationErrors(['email']);
  36. }
  37. /**
  38. * @test
  39. */
  40. public function test_submit_email_password_request_with_unknown_email_returns_validation_error()
  41. {
  42. $response = $this->json('POST', '/api/v1/user/password/lost', [
  43. 'email' => 'name@test.com'
  44. ]);
  45. $response->assertStatus(422)
  46. ->assertJsonValidationErrors(['email']);
  47. }
  48. /**
  49. * @test
  50. */
  51. public function test_submit_email_password_request_returns_success()
  52. {
  53. Notification::fake();
  54. $this->user = User::factory()->create();
  55. $response = $this->json('POST', '/api/v1/user/password/lost', [
  56. 'email' => $this->user->email
  57. ]);
  58. $response->assertStatus(200);
  59. $token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
  60. $this->assertNotNull($token);
  61. Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
  62. return Hash::check($notification->token, $token->token) === true;
  63. });
  64. }
  65. /**
  66. * @test
  67. */
  68. public function test_submit_email_password_request_in_demo_mode_returns_unauthorized()
  69. {
  70. Config::set('2fauth.config.isDemoApp', true);
  71. $response = $this->json('POST', '/api/v1/user/password/lost', [
  72. 'email' => ''
  73. ]);
  74. $response->assertStatus(401);
  75. }
  76. }