LocalLoginTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Auth;
  4. use App\Http\CsrfMiddleware;
  5. use App\Tests\Integration\Support\AppTestCase;
  6. /**
  7. * Drive the local-admin login flow against the real Slim app + a mocked
  8. * api-side `upsertLocal` response. Exercises CSRF, throttle, redirect,
  9. * session-set, and api-down handling.
  10. */
  11. final class LocalLoginTest extends AppTestCase
  12. {
  13. protected function setUp(): void
  14. {
  15. $this->bootApp();
  16. }
  17. public function testGetLoginRendersForm(): void
  18. {
  19. $response = $this->request('GET', '/login');
  20. self::assertSame(200, $response->getStatusCode());
  21. $body = (string) $response->getBody();
  22. self::assertStringContainsString('Sign in', $body);
  23. // Local sign-in toggle present (oidc disabled in this fixture).
  24. self::assertStringContainsString('name="username"', $body);
  25. self::assertStringContainsString('csrf_token', $body);
  26. }
  27. public function testCorrectCredentialsLogInAndRedirectToMe(): void
  28. {
  29. $this->enqueueApiResponse(200, [
  30. 'user_id' => 1,
  31. 'role' => 'admin',
  32. 'email' => null,
  33. 'display_name' => 'Local Admin',
  34. 'is_local' => true,
  35. ]);
  36. // Need a session + csrf token; first GET /login to set one up.
  37. $this->request('GET', '/login');
  38. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  39. self::assertNotEmpty($token);
  40. $body = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  41. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  42. self::assertSame(303, $response->getStatusCode());
  43. self::assertSame('/app/dashboard', $response->getHeaderLine('Location'));
  44. self::assertNotNull($_SESSION['_user'] ?? null);
  45. self::assertSame('admin', $_SESSION['_user']['role']);
  46. }
  47. public function testWrongPasswordRedirectsBackToLoginWithFlash(): void
  48. {
  49. $this->request('GET', '/login');
  50. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  51. $body = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  52. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  53. self::assertSame(303, $response->getStatusCode());
  54. self::assertSame('/login', $response->getHeaderLine('Location'));
  55. $flash = $_SESSION['_flash'] ?? [];
  56. self::assertNotEmpty($flash);
  57. self::assertSame('error', $flash[0]['type']);
  58. }
  59. public function testWrongUsernameAlsoRecordsFailure(): void
  60. {
  61. $this->request('GET', '/login');
  62. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  63. $body = http_build_query(['csrf_token' => $token, 'username' => 'someone', 'password' => 'test1234']);
  64. $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  65. $throttle = $_SESSION['_login_throttle'] ?? null;
  66. self::assertNotNull($throttle);
  67. self::assertSame(1, $throttle['count']);
  68. }
  69. public function testCsrfMissingIs403(): void
  70. {
  71. $this->request('GET', '/login');
  72. $body = http_build_query(['username' => 'admin', 'password' => 'test1234']);
  73. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  74. self::assertSame(403, $response->getStatusCode());
  75. }
  76. public function testFiveFailuresLockOutNextAttempt(): void
  77. {
  78. $this->request('GET', '/login');
  79. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  80. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  81. for ($i = 0; $i < 5; ++$i) {
  82. $this->request('POST', '/login/local', [], $bad, 'application/x-www-form-urlencoded');
  83. }
  84. // 6th attempt — even with correct credentials — gets the lockout flash.
  85. $this->enqueueApiResponse(200, [
  86. 'user_id' => 1, 'role' => 'admin', 'email' => null, 'display_name' => 'Local Admin', 'is_local' => true,
  87. ]);
  88. $good = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  89. $response = $this->request('POST', '/login/local', [], $good, 'application/x-www-form-urlencoded');
  90. self::assertSame(303, $response->getStatusCode());
  91. self::assertSame('/login', $response->getHeaderLine('Location'));
  92. $flash = $_SESSION['_flash'] ?? [];
  93. self::assertNotEmpty($flash);
  94. self::assertStringContainsStringIgnoringCase('too many', $flash[0]['message']);
  95. }
  96. public function testApiDownDuringUpsertFlashesError(): void
  97. {
  98. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  99. 'connection refused',
  100. new \GuzzleHttp\Psr7\Request('POST', '/api/v1/auth/users/upsert-local'),
  101. ));
  102. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  103. 'connection refused',
  104. new \GuzzleHttp\Psr7\Request('POST', '/api/v1/auth/users/upsert-local'),
  105. ));
  106. $this->request('GET', '/login');
  107. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  108. $body = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  109. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  110. self::assertSame(303, $response->getStatusCode());
  111. self::assertSame('/login', $response->getHeaderLine('Location'));
  112. $flash = $_SESSION['_flash'] ?? [];
  113. self::assertNotEmpty($flash);
  114. self::assertStringContainsStringIgnoringCase('api', $flash[0]['message']);
  115. }
  116. }