LogoutTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Auth;
  4. use App\Auth\UserContext;
  5. use App\Http\CsrfMiddleware;
  6. use App\Tests\Integration\Support\AppTestCase;
  7. final class LogoutTest extends AppTestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. $this->bootApp();
  12. }
  13. public function testLogoutClearsSessionAndRedirectsToLogin(): void
  14. {
  15. // Seed a logged-in session.
  16. $_SESSION['_user'] = (new UserContext(1, 'Admin', 'admin', null, UserContext::SOURCE_LOCAL))->toArray();
  17. $_SESSION['_last_active'] = time();
  18. $_SESSION['_authenticated_at'] = time();
  19. $_SESSION[CsrfMiddleware::SESSION_KEY] = 'fixed-token';
  20. $body = http_build_query(['csrf_token' => 'fixed-token']);
  21. $response = $this->request('POST', '/logout', [], $body, 'application/x-www-form-urlencoded');
  22. self::assertSame(303, $response->getStatusCode());
  23. self::assertSame('/login', $response->getHeaderLine('Location'));
  24. self::assertArrayNotHasKey('_user', $_SESSION);
  25. }
  26. public function testLogoutWithoutCsrfIs403(): void
  27. {
  28. $_SESSION['_user'] = (new UserContext(1, 'Admin', 'admin', null, UserContext::SOURCE_LOCAL))->toArray();
  29. $_SESSION['_last_active'] = time();
  30. $_SESSION['_authenticated_at'] = time();
  31. $response = $this->request('POST', '/logout');
  32. self::assertSame(403, $response->getStatusCode());
  33. self::assertArrayHasKey('_user', $_SESSION);
  34. }
  35. }