ForgotPasswordControllerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Http\Controllers\Auth\ForgotPasswordController;
  4. use App\Http\Middleware\RejectIfAuthenticated;
  5. use App\Http\Middleware\RejectIfDemoMode;
  6. use App\Models\User;
  7. use Illuminate\Auth\Notifications\ResetPassword;
  8. use Illuminate\Support\Facades\Config;
  9. use Illuminate\Support\Facades\Hash;
  10. use Illuminate\Support\Facades\Notification;
  11. use PHPUnit\Framework\Attributes\CoversClass;
  12. use PHPUnit\Framework\Attributes\Test;
  13. use Tests\FeatureTestCase;
  14. /**
  15. * ForgotPasswordControllerTest test class
  16. */
  17. #[CoversClass(ForgotPasswordController::class)]
  18. #[CoversClass(User::class)]
  19. #[CoversClass(RejectIfDemoMode::class)]
  20. #[CoversClass(RejectIfAuthenticated::class)]
  21. class ForgotPasswordControllerTest extends FeatureTestCase
  22. {
  23. /**
  24. * @var \App\Models\User
  25. */
  26. protected $user;
  27. #[Test]
  28. public function test_submit_email_password_request_without_email_returns_validation_error()
  29. {
  30. $response = $this->json('POST', '/user/password/lost', [
  31. 'email' => '',
  32. ]);
  33. $response->assertStatus(422)
  34. ->assertJsonValidationErrors(['email']);
  35. }
  36. #[Test]
  37. public function test_submit_email_password_request_with_invalid_email_returns_validation_error()
  38. {
  39. $response = $this->json('POST', '/user/password/lost', [
  40. 'email' => 'nametest.com',
  41. ]);
  42. $response->assertStatus(422)
  43. ->assertJsonValidationErrors(['email']);
  44. }
  45. #[Test]
  46. public function test_submit_email_password_request_with_unknown_email_returns_validation_error()
  47. {
  48. $response = $this->json('POST', '/user/password/lost', [
  49. 'email' => 'name@test.com',
  50. ]);
  51. $response->assertStatus(422)
  52. ->assertJsonValidationErrors(['email']);
  53. }
  54. #[Test]
  55. public function test_submit_email_password_request_returns_success()
  56. {
  57. Notification::fake();
  58. $this->user = User::factory()->create();
  59. $response = $this->json('POST', '/user/password/lost', [
  60. 'email' => $this->user->email,
  61. ]);
  62. $response->assertStatus(200);
  63. $token = \Illuminate\Support\Facades\DB::table(config('auth.passwords.users.table'))->first();
  64. $this->assertNotNull($token);
  65. Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
  66. return Hash::check($notification->token, $token->token) === true;
  67. });
  68. }
  69. #[Test]
  70. public function test_submit_email_password_request_in_demo_mode_returns_unauthorized()
  71. {
  72. Config::set('2fauth.config.isDemoApp', true);
  73. $response = $this->json('POST', '/user/password/lost', [
  74. 'email' => '',
  75. ]);
  76. $response->assertStatus(401);
  77. }
  78. #[Test]
  79. public function test_submit_email_password_request_when_authenticated_returns_bad_request()
  80. {
  81. /**
  82. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  83. */
  84. $user = User::factory()->create();
  85. $this->actingAs($user, 'web-guard')
  86. ->json('POST', '/user/password/lost', [
  87. 'email' => $user->email,
  88. ])
  89. ->assertStatus(400)
  90. ->assertJsonStructure([
  91. 'message',
  92. ]);
  93. }
  94. }