ForgotPasswordTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Tests\Feature\Auth;
  3. use App\User;
  4. use Illuminate\Support\Facades\Hash;
  5. use Illuminate\Support\Facades\Config;
  6. use Illuminate\Support\Facades\Password;
  7. use Illuminate\Auth\Notifications\ResetPassword;
  8. use Illuminate\Support\Facades\Notification;
  9. use Tests\TestCase;
  10. class ForgotPasswordTest extends TestCase
  11. {
  12. /** @var \App\User */
  13. protected $user;
  14. /**
  15. * Testing submitting the email password request without
  16. * email address.
  17. */
  18. public function testSubmitEmailPasswordRequestWithoutEmail()
  19. {
  20. $response = $this->json('POST', '/api/password/email', [
  21. 'email' => ''
  22. ]);
  23. $response->assertStatus(422)
  24. ->assertJsonValidationErrors(['email']);
  25. }
  26. /**
  27. * Testing submitting the email password request with an invalid
  28. * email address.
  29. */
  30. public function testSubmitEmailPasswordRequestWithInvalidEmail()
  31. {
  32. $response = $this->json('POST', '/api/password/email', [
  33. 'email' => 'nametest.com'
  34. ]);
  35. $response->assertStatus(422)
  36. ->assertJsonValidationErrors(['email']);
  37. }
  38. /**
  39. * Testing submitting the email password request with an unknown
  40. * email address.
  41. */
  42. public function testSubmitEmailPasswordRequestWithUnknownEmail()
  43. {
  44. $response = $this->json('POST', '/api/password/email', [
  45. 'email' => 'name@test.com'
  46. ]);
  47. $response->assertStatus(422)
  48. ->assertJsonValidationErrors(['email']);
  49. }
  50. /**
  51. * Testing submitting the email password request with a valid email address.
  52. */
  53. public function testSubmitEmailPasswordRequest()
  54. {
  55. Notification::fake();
  56. $this->user = factory(User::class)->create([
  57. 'name' => 'user',
  58. 'email' => 'user@example.org',
  59. 'password' => bcrypt('password'),
  60. 'email_verified_at' => now(),
  61. 'remember_token' => \Illuminate\Support\Str::random(10),
  62. ]);
  63. $response = $this->json('POST', '/api/password/email', [
  64. 'email' => $this->user->email
  65. ]);
  66. $response->assertStatus(200);
  67. $token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
  68. $this->assertNotNull($token);
  69. Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
  70. return Hash::check($notification->token, $token->token) === true;
  71. });
  72. }
  73. /**
  74. * Testing submitting the email password request in Demo mode
  75. */
  76. public function testSubmitEmailPasswordRequestInDemoMode()
  77. {
  78. Config::set('2fauth.config.isDemoApp', true);
  79. $response = $this->json('POST', '/api/password/email', [
  80. 'email' => ''
  81. ]);
  82. $response->assertStatus(401);
  83. }
  84. }