LoginTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Tests\Feature\Auth;
  3. use App\Models\User;
  4. use Tests\FeatureTestCase;
  5. class LoginTest extends FeatureTestCase
  6. {
  7. /**
  8. * @var \App\Models\User
  9. */
  10. protected $user;
  11. private const PASSWORD = 'password';
  12. private const WRONG_PASSWORD = 'wrong_password';
  13. /**
  14. * @test
  15. */
  16. public function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->user = User::factory()->create();
  20. }
  21. /**
  22. * @test
  23. */
  24. public function test_user_login_returns_success()
  25. {
  26. $response = $this->json('POST', '/user/login', [
  27. 'email' => $this->user->email,
  28. 'password' => self::PASSWORD
  29. ])
  30. ->assertOk()
  31. ->assertExactJson([
  32. 'message' => 'authenticated',
  33. 'name' => $this->user->name,
  34. ]);
  35. }
  36. /**
  37. * @test
  38. */
  39. public function test_user_login_already_authenticated_returns_bad_request()
  40. {
  41. $response = $this->json('POST', '/user/login', [
  42. 'email' => $this->user->email,
  43. 'password' => self::PASSWORD
  44. ]);
  45. $response = $this->actingAs($this->user, 'web-guard')
  46. ->json('POST', '/user/login', [
  47. 'email' => $this->user->email,
  48. 'password' => self::PASSWORD
  49. ])
  50. ->assertStatus(400)
  51. ->assertJson([
  52. 'message' => __('auth.already_authenticated')
  53. ]);
  54. }
  55. /**
  56. * @test
  57. */
  58. public function test_user_login_with_missing_data_returns_validation_error()
  59. {
  60. $response = $this->json('POST', '/user/login', [
  61. 'email' => '',
  62. 'password' => ''
  63. ])
  64. ->assertStatus(422)
  65. ->assertJsonValidationErrors([
  66. 'email',
  67. 'password'
  68. ]);
  69. }
  70. /**
  71. * @test
  72. */
  73. public function test_user_login_with_invalid_credentials_returns_validation_error()
  74. {
  75. $response = $this->json('POST', '/user/login', [
  76. 'email' => $this->user->email,
  77. 'password' => self::WRONG_PASSWORD
  78. ])
  79. ->assertStatus(401)
  80. ->assertJson([
  81. 'message' => 'unauthorised'
  82. ]);
  83. }
  84. /**
  85. * @test
  86. */
  87. public function test_too_many_login_attempts_with_invalid_credentials_returns_too_many_request_error()
  88. {
  89. $response = $this->json('POST', '/user/login', [
  90. 'email' => $this->user->email,
  91. 'password' => self::WRONG_PASSWORD
  92. ]);
  93. $response = $this->json('POST', '/user/login', [
  94. 'email' => $this->user->email,
  95. 'password' => self::WRONG_PASSWORD
  96. ]);
  97. $response = $this->json('POST', '/user/login', [
  98. 'email' => $this->user->email,
  99. 'password' => self::WRONG_PASSWORD
  100. ]);
  101. $response = $this->json('POST', '/user/login', [
  102. 'email' => $this->user->email,
  103. 'password' => self::WRONG_PASSWORD
  104. ]);
  105. $response = $this->json('POST', '/user/login', [
  106. 'email' => $this->user->email,
  107. 'password' => self::WRONG_PASSWORD
  108. ]);
  109. $response = $this->json('POST', '/user/login', [
  110. 'email' => $this->user->email,
  111. 'password' => self::WRONG_PASSWORD
  112. ]);
  113. $response->assertStatus(429);
  114. }
  115. /**
  116. * @test
  117. */
  118. public function test_user_logout_returns_validation_success()
  119. {
  120. $response = $this->json('POST', '/user/login', [
  121. 'email' => $this->user->email,
  122. 'password' => self::PASSWORD
  123. ]);
  124. $response = $this->actingAs($this->user, 'web-guard')
  125. ->json('GET', '/user/logout')
  126. ->assertOk()
  127. ->assertExactJson([
  128. 'message' => 'signed out',
  129. ]);
  130. }
  131. /**
  132. * @test
  133. */
  134. public function test_user_logout_after_inactivity_returns_unauthorized()
  135. {
  136. // Set the autolock period to 1 minute
  137. $settingService = resolve('App\Services\SettingService');
  138. $settingService->set('kickUserAfter', 1);
  139. $response = $this->json('POST', '/user/login', [
  140. 'email' => $this->user->email,
  141. 'password' => self::PASSWORD
  142. ]);
  143. // Ping a protected endpoint to log last_seen_at time
  144. $response = $this->actingAs($this->user, 'api-guard')
  145. ->json('GET', '/api/v1/twofaccounts');
  146. sleep(61);
  147. $response = $this->actingAs($this->user, 'api-guard')
  148. ->json('GET', '/api/v1/twofaccounts')
  149. ->assertUnauthorized();
  150. }
  151. }