ForgotPasswordControllerTest.php 3.0 KB

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