LoginTest.php 4.5 KB

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