ForgotPasswordControllerTest.php 3.3 KB

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