1
0

RoutesTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\App;
  4. use App\Auth\UserContext;
  5. use App\Tests\Integration\Support\AppTestCase;
  6. /**
  7. * Smoke tests for the basic routing surface: home redirect, healthz,
  8. * /app/* gating, /no-access page.
  9. */
  10. final class RoutesTest extends AppTestCase
  11. {
  12. protected function setUp(): void
  13. {
  14. $this->bootApp();
  15. }
  16. public function testHomeRedirectsToLoginWhenAnonymous(): void
  17. {
  18. $response = $this->request('GET', '/');
  19. self::assertSame(302, $response->getStatusCode());
  20. self::assertSame('/login', $response->getHeaderLine('Location'));
  21. }
  22. public function testHomeRedirectsToMeWhenAuthenticated(): void
  23. {
  24. $_SESSION['_user'] = (new UserContext(1, 'Admin', 'admin', null, UserContext::SOURCE_LOCAL))->toArray();
  25. $_SESSION['_last_active'] = time();
  26. $_SESSION['_authenticated_at'] = time();
  27. $response = $this->request('GET', '/');
  28. self::assertSame(302, $response->getStatusCode());
  29. self::assertSame('/app/me', $response->getHeaderLine('Location'));
  30. }
  31. public function testHealthzReturnsOk(): void
  32. {
  33. $response = $this->request('GET', '/healthz');
  34. self::assertSame(200, $response->getStatusCode());
  35. $body = json_decode((string) $response->getBody(), true);
  36. self::assertSame('ok', $body['status']);
  37. self::assertArrayHasKey('api_reachable', $body);
  38. self::assertArrayHasKey('last_api_check_at', $body);
  39. }
  40. public function testAppMeRedirectsAnonymousToLogin(): void
  41. {
  42. $response = $this->request('GET', '/app/me');
  43. self::assertSame(302, $response->getStatusCode());
  44. self::assertSame('/login', $response->getHeaderLine('Location'));
  45. self::assertSame('/app/me', $_SESSION['_next'] ?? null);
  46. }
  47. public function testAppMeRendersForLoggedInUser(): void
  48. {
  49. $_SESSION['_user'] = (new UserContext(7, 'Admin', 'admin', 'a@x', UserContext::SOURCE_LOCAL))->toArray();
  50. $_SESSION['_last_active'] = time();
  51. $_SESSION['_authenticated_at'] = time();
  52. $this->enqueueApiResponse(200, [
  53. 'user_id' => 7, 'role' => 'admin', 'email' => 'a@x', 'display_name' => 'Admin', 'is_local' => true,
  54. ]);
  55. $response = $this->request('GET', '/app/me');
  56. self::assertSame(200, $response->getStatusCode());
  57. $body = (string) $response->getBody();
  58. self::assertStringContainsString('My identity', $body);
  59. self::assertStringContainsString('admin', $body);
  60. self::assertStringContainsString('7', $body);
  61. }
  62. public function testNoAccessPageRenders(): void
  63. {
  64. $response = $this->request('GET', '/no-access');
  65. self::assertSame(200, $response->getStatusCode());
  66. self::assertStringContainsString('No access', (string) $response->getBody());
  67. }
  68. }