LoginTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Exceptions\Handler;
  4. use App\Http\Controllers\Auth\LoginController;
  5. use App\Http\Middleware\KickOutInactiveUser;
  6. use App\Http\Middleware\LogUserLastSeen;
  7. use App\Http\Middleware\RejectIfAuthenticated;
  8. use App\Http\Middleware\RejectIfDemoMode;
  9. use App\Http\Middleware\RejectIfReverseProxy;
  10. use App\Listeners\Authentication\FailedLoginListener;
  11. use App\Listeners\Authentication\LoginListener;
  12. use App\Listeners\Authentication\LogoutListener;
  13. use App\Listeners\Authentication\VisitedByProxyUserListener;
  14. use App\Listeners\LogNotificationListener;
  15. use App\Models\AuthLog;
  16. use App\Models\User;
  17. use App\Notifications\FailedLoginNotification;
  18. use App\Notifications\SignedInWithNewDeviceNotification;
  19. use App\Rules\CaseInsensitiveEmailExists;
  20. use Illuminate\Support\Carbon;
  21. use Illuminate\Support\Facades\Config;
  22. use Illuminate\Support\Facades\Notification;
  23. use PHPUnit\Framework\Attributes\CoversClass;
  24. use PHPUnit\Framework\Attributes\CoversMethod;
  25. use PHPUnit\Framework\Attributes\Test;
  26. use Tests\FeatureTestCase;
  27. /**
  28. * LoginTest test class
  29. */
  30. #[CoversClass(LoginController::class)]
  31. #[CoversClass(RejectIfAuthenticated::class)]
  32. #[CoversClass(RejectIfReverseProxy::class)]
  33. #[CoversClass(RejectIfDemoMode::class)]
  34. #[CoversClass(LoginListener::class)]
  35. #[CoversClass(LogoutListener::class)]
  36. #[CoversClass(FailedLoginListener::class)]
  37. #[CoversClass(VisitedByProxyUserListener::class)]
  38. #[CoversMethod(CaseInsensitiveEmailExists::class, 'validate')]
  39. #[CoversMethod(Handler::class, 'register')]
  40. #[CoversMethod(KickOutInactiveUser::class, 'handle')]
  41. #[CoversMethod(LogUserLastSeen::class, 'handle')]
  42. #[CoversClass(LogNotificationListener::class)]
  43. #[CoversClass(SignedInWithNewDeviceNotification::class)]
  44. #[CoversClass(FailedLoginNotification::class)]
  45. class LoginTest extends FeatureTestCase
  46. {
  47. /**
  48. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  49. */
  50. protected $user;
  51. /**
  52. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  53. */
  54. protected $admin;
  55. private const WEB_GUARD = 'web-guard';
  56. private const REVERSE_PROXY_GUARD = 'reverse-proxy-guard';
  57. private const PASSWORD = 'password';
  58. private const WRONG_PASSWORD = 'wrong_password';
  59. private const USER_NAME = 'John';
  60. private const USER_EMAIL = 'john@example.com';
  61. public function setUp() : void
  62. {
  63. parent::setUp();
  64. $this->user = User::factory()->create();
  65. $this->admin = User::factory()->administrator()->create();
  66. }
  67. #[Test]
  68. public function test_user_login_returns_success()
  69. {
  70. Notification::fake();
  71. $response = $this->json('POST', '/user/login', [
  72. 'email' => $this->user->email,
  73. 'password' => self::PASSWORD,
  74. ])
  75. ->assertOk()
  76. ->assertJsonFragment([
  77. 'message' => 'authenticated',
  78. 'id' => $this->user->id,
  79. 'name' => $this->user->name,
  80. 'email' => $this->user->email,
  81. 'is_admin' => false,
  82. ])
  83. ->assertJsonStructure([
  84. 'preferences',
  85. ]);
  86. }
  87. #[Test]
  88. public function test_login_send_new_device_notification()
  89. {
  90. Notification::fake();
  91. $this->user['preferences->notifyOnNewAuthDevice'] = 1;
  92. $this->user->save();
  93. $this->json('POST', '/user/login', [
  94. 'email' => $this->user->email,
  95. 'password' => self::PASSWORD,
  96. ])->assertOk();
  97. $this->actingAs($this->user, self::WEB_GUARD)
  98. ->json('GET', '/user/logout');
  99. $this->travel(1)->minute();
  100. $this->json('POST', '/user/login', [
  101. 'email' => $this->user->email,
  102. 'password' => self::PASSWORD,
  103. ], [
  104. 'HTTP_USER_AGENT' => 'another_useragent_to_be_identified_as_new_device',
  105. ])->assertOk();
  106. Notification::assertSentTo($this->user, SignedInWithNewDeviceNotification::class);
  107. }
  108. #[Test]
  109. public function test_login_does_not_send_new_device_notification_if_user_disabled_it()
  110. {
  111. Notification::fake();
  112. $this->user['preferences->notifyOnNewAuthDevice'] = 0;
  113. $this->user->save();
  114. $this->json('POST', '/user/login', [
  115. 'email' => $this->user->email,
  116. 'password' => self::PASSWORD,
  117. ])->assertOk();
  118. $this->actingAs($this->user, self::WEB_GUARD)
  119. ->json('GET', '/user/logout');
  120. $this->travel(1)->minute();
  121. $this->json('POST', '/user/login', [
  122. 'email' => $this->user->email,
  123. 'password' => self::PASSWORD,
  124. ], [
  125. 'HTTP_USER_AGENT' => 'another_useragent_to_be_identified_as_new_device',
  126. ])->assertOk();
  127. Notification::assertNothingSentTo($this->user);
  128. }
  129. #[Test]
  130. public function test_login_does_not_send_new_device_notification_if_user_is_considered_new()
  131. {
  132. Notification::fake();
  133. $this->user['preferences->notifyOnNewAuthDevice'] = 1;
  134. $this->user->save();
  135. $this->json('POST', '/user/login', [
  136. 'email' => $this->user->email,
  137. 'password' => self::PASSWORD,
  138. ])->assertOk();
  139. Notification::assertNothingSentTo($this->user);
  140. }
  141. #[Test]
  142. public function test_admin_login_returns_admin_role()
  143. {
  144. $response = $this->json('POST', '/user/login', [
  145. 'email' => $this->admin->email,
  146. 'password' => self::PASSWORD,
  147. ])
  148. ->assertOk()
  149. ->assertJsonFragment([
  150. 'is_admin' => true,
  151. ]);
  152. }
  153. #[Test]
  154. public function test_user_login_with_uppercased_email_returns_success()
  155. {
  156. $response = $this->json('POST', '/user/login', [
  157. 'email' => strtoupper($this->user->email),
  158. 'password' => self::PASSWORD,
  159. ])
  160. ->assertOk()
  161. ->assertJsonFragment([
  162. 'message' => 'authenticated',
  163. 'name' => $this->user->name,
  164. ])
  165. ->assertJsonStructure([
  166. 'message',
  167. 'name',
  168. 'preferences',
  169. ]);
  170. }
  171. #[Test]
  172. public function test_successful_web_login_with_password_is_logged()
  173. {
  174. $this->json('POST', '/user/login', [
  175. 'email' => $this->user->email,
  176. 'password' => self::PASSWORD,
  177. ])->assertOk();
  178. $this->assertDatabaseHas('auth_logs', [
  179. 'authenticatable_id' => $this->user->id,
  180. 'login_successful' => true,
  181. 'guard' => self::WEB_GUARD,
  182. 'login_method' => self::PASSWORD,
  183. 'logout_at' => null,
  184. ]);
  185. }
  186. #[Test]
  187. public function test_failed_web_login_with_password_is_logged()
  188. {
  189. $this->json('POST', '/user/login', [
  190. 'email' => $this->user->email,
  191. 'password' => self::WRONG_PASSWORD,
  192. ])->assertStatus(401);
  193. $this->assertDatabaseHas('auth_logs', [
  194. 'authenticatable_id' => $this->user->id,
  195. 'login_successful' => false,
  196. 'guard' => self::WEB_GUARD,
  197. 'login_method' => self::PASSWORD,
  198. 'logout_at' => null,
  199. ]);
  200. }
  201. #[Test]
  202. public function test_user_login_already_authenticated_is_rejected()
  203. {
  204. $response = $this->json('POST', '/user/login', [
  205. 'email' => $this->user->email,
  206. 'password' => self::PASSWORD,
  207. ]);
  208. $response = $this->actingAs($this->user, self::WEB_GUARD)
  209. ->json('POST', '/user/login', [
  210. 'email' => $this->user->email,
  211. 'password' => self::PASSWORD,
  212. ])
  213. ->assertStatus(400)
  214. ->assertJsonStructure([
  215. 'message',
  216. ]);
  217. }
  218. #[Test]
  219. public function test_user_login_with_missing_data_returns_validation_error()
  220. {
  221. $response = $this->json('POST', '/user/login', [
  222. 'email' => '',
  223. 'password' => '',
  224. ])
  225. ->assertStatus(422)
  226. ->assertJsonValidationErrors([
  227. 'email',
  228. 'password',
  229. ]);
  230. }
  231. #[Test]
  232. public function test_user_login_with_invalid_credentials_returns_unauthorized()
  233. {
  234. $response = $this->json('POST', '/user/login', [
  235. 'email' => $this->user->email,
  236. 'password' => self::WRONG_PASSWORD,
  237. ])
  238. ->assertStatus(401)
  239. ->assertJson([
  240. 'message' => 'unauthorized',
  241. ]);
  242. }
  243. #[Test]
  244. public function test_login_with_invalid_credentials_send_failed_login_notification()
  245. {
  246. Notification::fake();
  247. $this->user['preferences->notifyOnFailedLogin'] = 1;
  248. $this->user->save();
  249. $this->json('POST', '/user/login', [
  250. 'email' => $this->user->email,
  251. 'password' => self::WRONG_PASSWORD,
  252. ])->assertStatus(401);
  253. Notification::assertSentTo($this->user, FailedLoginNotification::class);
  254. }
  255. #[Test]
  256. public function test_login_with_invalid_credentials_does_not_send_new_device_notification()
  257. {
  258. Notification::fake();
  259. $this->user['preferences->notifyOnFailedLogin'] = 0;
  260. $this->user->save();
  261. $this->json('POST', '/user/login', [
  262. 'email' => $this->user->email,
  263. 'password' => self::WRONG_PASSWORD,
  264. ])->assertStatus(401);
  265. Notification::assertNothingSentTo($this->user);
  266. }
  267. #[Test]
  268. public function test_too_many_login_attempts_with_invalid_credentials_returns_too_many_request_error()
  269. {
  270. $throttle = 8;
  271. Config::set('auth.throttle.login', $throttle);
  272. $post = [
  273. 'email' => $this->user->email,
  274. 'password' => self::WRONG_PASSWORD,
  275. ];
  276. for ($i = 0; $i < $throttle - 1; $i++) {
  277. $this->json('POST', '/user/login', $post);
  278. }
  279. $this->json('POST', '/user/login', $post)
  280. ->assertUnauthorized();
  281. $this->json('POST', '/user/login', $post)
  282. ->assertStatus(429);
  283. }
  284. #[Test]
  285. public function test_user_logout_returns_validation_success()
  286. {
  287. $response = $this->json('POST', '/user/login', [
  288. 'email' => $this->user->email,
  289. 'password' => self::PASSWORD,
  290. ]);
  291. $response = $this->actingAs($this->user, self::WEB_GUARD)
  292. ->json('GET', '/user/logout')
  293. ->assertOk()
  294. ->assertExactJson([
  295. 'message' => 'signed out',
  296. ]);
  297. }
  298. #[Test]
  299. public function test_user_logout_after_inactivity_returns_teapot()
  300. {
  301. // Set the autolock period to 1 minute
  302. $this->user['preferences->kickUserAfter'] = 1;
  303. $this->user->save();
  304. $response = $this->json('POST', '/user/login', [
  305. 'email' => $this->user->email,
  306. 'password' => self::PASSWORD,
  307. ]);
  308. // Ping a protected endpoint to log last_seen_at time
  309. $response = $this->actingAs($this->user, 'api-guard')
  310. ->json('GET', '/api/v1/twofaccounts');
  311. $this->travelTo(Carbon::now()->addMinutes(2));
  312. $response = $this->actingAs($this->user, 'api-guard')
  313. ->json('GET', '/api/v1/twofaccounts')
  314. ->assertStatus(418);
  315. }
  316. #[Test]
  317. public function test_successful_web_logout_is_logged()
  318. {
  319. $this->json('POST', '/user/login', [
  320. 'email' => $this->user->email,
  321. 'password' => self::PASSWORD,
  322. ])->assertOk();
  323. $this->actingAs($this->user, self::WEB_GUARD)
  324. ->json('GET', '/user/logout')
  325. ->assertOk();
  326. $authlog = $this->user->latestAuthentication()->first();
  327. $this->assertEquals($this->user->id, $authlog->authenticatable_id);
  328. $this->assertTrue($authlog->login_successful);
  329. $this->assertEquals(self::WEB_GUARD, $authlog->guard);
  330. $this->assertEquals(self::PASSWORD, $authlog->login_method);
  331. $this->assertNotNull($authlog->logout_at);
  332. }
  333. #[Test]
  334. public function test_orphan_web_logout_is_logged()
  335. {
  336. $this->actingAs($this->user, self::WEB_GUARD)
  337. ->json('GET', '/user/logout')
  338. ->assertOk();
  339. $authlog = AuthLog::first();
  340. $this->assertEquals($this->user->id, $authlog->authenticatable_id);
  341. $this->assertFalse($authlog->login_successful);
  342. $this->assertEquals(self::WEB_GUARD, $authlog->guard);
  343. $this->assertNull($authlog->login_method);
  344. $this->assertNotNull($authlog->logout_at);
  345. }
  346. #[Test]
  347. public function test_reverse_proxy_access_is_logged()
  348. {
  349. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  350. $user = User::factory()->create([
  351. 'name' => self::USER_NAME,
  352. 'email' => strtolower(self::USER_NAME) . '@remote',
  353. ]);
  354. $this->app['auth']->shouldUse(self::REVERSE_PROXY_GUARD);
  355. $this->json('GET', '/api/v1/groups', [], [
  356. 'HTTP_REMOTE_USER' => self::USER_NAME,
  357. ]);
  358. $this->assertDatabaseHas('auth_logs', [
  359. 'authenticatable_id' => $user->id,
  360. 'login_successful' => true,
  361. 'guard' => self::REVERSE_PROXY_GUARD,
  362. 'login_method' => null,
  363. 'logout_at' => null,
  364. ]);
  365. }
  366. #[Test]
  367. public function test_reverse_proxy_access_is_logged_only_once_during_a_quarter()
  368. {
  369. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  370. $user = User::factory()->create([
  371. 'name' => self::USER_NAME,
  372. 'email' => strtolower(self::USER_NAME) . '@remote',
  373. ]);
  374. $this->app['auth']->shouldUse(self::REVERSE_PROXY_GUARD);
  375. $this->json('GET', '/api/v1/groups', [], [
  376. 'HTTP_REMOTE_USER' => self::USER_NAME,
  377. ]);
  378. $this->json('GET', '/api/v1/groups', [], [
  379. 'HTTP_REMOTE_USER' => self::USER_NAME,
  380. ]);
  381. $this->assertDatabaseCount('auth_logs', 1);
  382. $this->travel(16)->minutes();
  383. $this->json('GET', '/api/v1/groups', [], [
  384. 'HTTP_REMOTE_USER' => self::USER_NAME,
  385. ]);
  386. $this->assertDatabaseCount('auth_logs', 2);
  387. }
  388. #[Test]
  389. public function test_reverse_proxy_access_sends_new_device_notification()
  390. {
  391. Notification::fake();
  392. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  393. $user = User::factory()->create([
  394. 'name' => self::USER_NAME,
  395. 'email' => strtolower(self::USER_NAME) . '@remote',
  396. ]);
  397. $user['preferences->notifyOnNewAuthDevice'] = true;
  398. $user->save();
  399. $user->refresh();
  400. $this->app['auth']->shouldUse(self::REVERSE_PROXY_GUARD);
  401. // We travel back for 2 minutes to avoid the user being considered as a new user
  402. $this->travelTo(Carbon::now()->subMinutes(2));
  403. $this->json('GET', '/api/v1/groups', [], [
  404. 'HTTP_REMOTE_USER' => self::USER_NAME,
  405. ]);
  406. Notification::assertSentTo($user, SignedInWithNewDeviceNotification::class);
  407. }
  408. #[Test]
  409. public function test_reverse_proxy_access_does_not_send_new_device_notification_if_user_disabled_it()
  410. {
  411. Notification::fake();
  412. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  413. $user = User::factory()->create([
  414. 'name' => self::USER_NAME,
  415. 'email' => strtolower(self::USER_NAME) . '@remote',
  416. ]);
  417. $user['preferences->notifyOnNewAuthDevice'] = false;
  418. $user->save();
  419. $user->refresh();
  420. $this->app['auth']->shouldUse(self::REVERSE_PROXY_GUARD);
  421. // We travel back for 2 minutes to avoid the user being considered as a new user
  422. $this->travelTo(Carbon::now()->subMinutes(2));
  423. $this->json('GET', '/api/v1/groups', [], [
  424. 'HTTP_REMOTE_USER' => self::USER_NAME,
  425. ]);
  426. Notification::assertNothingSentTo($user);
  427. }
  428. #[Test]
  429. public function test_reverse_proxy_does_not_send_new_device_notification_if_user_is_considered_new()
  430. {
  431. Notification::fake();
  432. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  433. $user = User::factory()->create([
  434. 'name' => self::USER_NAME,
  435. 'email' => strtolower(self::USER_NAME) . '@remote',
  436. ]);
  437. $user['preferences->notifyOnNewAuthDevice'] = true;
  438. $user->save();
  439. $user->refresh();
  440. $this->app['auth']->shouldUse(self::REVERSE_PROXY_GUARD);
  441. $this->json('GET', '/api/v1/groups', [], [
  442. 'HTTP_REMOTE_USER' => self::USER_NAME,
  443. ]);
  444. Notification::assertNothingSentTo($user);
  445. }
  446. }