1
0

IpsPageTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. final class IpsPageTest extends AppTestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. $this->bootApp();
  11. // The first /app/* request in a fresh process triggers
  12. // session_start() and clobbers the $_SESSION values primed
  13. // here. Hit a public route first so the session is active
  14. // before any test request fires.
  15. $this->request('GET', '/healthz');
  16. $_SESSION['_user'] = (new UserContext(1, 'Admin', 'admin', null, UserContext::SOURCE_LOCAL))->toArray();
  17. $_SESSION['_last_active'] = time();
  18. $_SESSION['_authenticated_at'] = time();
  19. }
  20. public function testListPageRendersResults(): void
  21. {
  22. $this->enqueueApiResponse(200, [
  23. 'page' => 1,
  24. 'page_size' => 25,
  25. 'total' => 2,
  26. 'items' => [
  27. [
  28. 'ip' => '203.0.113.10',
  29. 'is_ipv4' => true,
  30. 'max_score' => 1.5,
  31. 'top_category' => 'brute_force',
  32. 'pair_count' => 1,
  33. 'last_report_at' => '2026-04-29T10:00:00Z',
  34. 'status' => 'scored',
  35. 'enrichment' => null,
  36. ],
  37. [
  38. 'ip' => '2001:db8::1',
  39. 'is_ipv4' => false,
  40. 'max_score' => 0.8,
  41. 'top_category' => 'spam',
  42. 'pair_count' => 1,
  43. 'last_report_at' => '2026-04-29T09:55:00Z',
  44. 'status' => 'scored',
  45. 'enrichment' => null,
  46. ],
  47. ],
  48. ]);
  49. // listCountries() — issued after searchIps() by the controller.
  50. $this->enqueueApiResponse(200, ['items' => []]);
  51. $response = $this->request('GET', '/app/ips');
  52. self::assertSame(200, $response->getStatusCode());
  53. $body = (string) $response->getBody();
  54. self::assertStringContainsString('203.0.113.10', $body);
  55. self::assertStringContainsString('2001:db8::1', $body);
  56. self::assertStringContainsString('brute_force', $body);
  57. self::assertStringContainsString('2 total', $body);
  58. }
  59. public function testListPageRendersEmptyState(): void
  60. {
  61. $this->enqueueApiResponse(200, [
  62. 'page' => 1,
  63. 'page_size' => 25,
  64. 'total' => 0,
  65. 'items' => [],
  66. ]);
  67. $this->enqueueApiResponse(200, ['items' => []]);
  68. $response = $this->request('GET', '/app/ips');
  69. self::assertSame(200, $response->getStatusCode());
  70. self::assertStringContainsString('No results', (string) $response->getBody());
  71. }
  72. public function testListPagePassesFiltersThrough(): void
  73. {
  74. $this->enqueueApiResponse(200, ['page' => 1, 'page_size' => 25, 'total' => 0, 'items' => []]);
  75. $this->enqueueApiResponse(200, ['items' => []]);
  76. $response = $this->request('GET', '/app/ips?q=2001&category=spam');
  77. $body = (string) $response->getBody();
  78. self::assertSame(200, $response->getStatusCode());
  79. // The filter form preserves the user's selection.
  80. self::assertMatchesRegularExpression('/value="2001"/', $body);
  81. self::assertMatchesRegularExpression('/<option value="spam"\s+selected/', $body);
  82. }
  83. public function testDetailPageRendersScoresAndHistory(): void
  84. {
  85. $this->enqueueApiResponse(200, [
  86. 'ip' => '203.0.113.10',
  87. 'is_ipv4' => true,
  88. 'status' => 'scored',
  89. 'scores' => [
  90. ['category' => 'brute_force', 'category_id' => 1, 'score' => 1.5, 'last_report_at' => '2026-04-29T10:00:00Z', 'report_count_30d' => 5],
  91. ],
  92. 'enrichment' => ['country_code' => null, 'asn' => null, 'as_org' => null, 'enriched_at' => null],
  93. 'manual_block' => null,
  94. 'allowlist' => null,
  95. 'history' => [
  96. ['type' => 'report', 'at' => '2026-04-29T10:00:00Z', 'category' => 'brute_force', 'reporter' => 'web-prod-01', 'weight' => 1.0, 'metadata' => null],
  97. ],
  98. 'has_more' => false,
  99. ]);
  100. $this->enqueueApiResponse(200, [
  101. 'items' => [
  102. ['slug' => 'brute_force', 'decay_function' => 'exponential', 'decay_param' => 14],
  103. ],
  104. 'total' => 1,
  105. ]);
  106. $response = $this->request('GET', '/app/ips/203.0.113.10');
  107. self::assertSame(200, $response->getStatusCode());
  108. $body = (string) $response->getBody();
  109. self::assertStringContainsString('203.0.113.10', $body);
  110. self::assertStringContainsString('brute_force', $body);
  111. self::assertStringContainsString('web-prod-01', $body);
  112. self::assertStringContainsString('Score per category', $body);
  113. self::assertStringContainsString('Score over time', $body);
  114. self::assertStringContainsString('History', $body);
  115. }
  116. public function testDetailPageReturnsTwig404OnApiNotFound(): void
  117. {
  118. // Use an IP-shaped value so the SEC_REVIEW F43 route regex
  119. // (`[0-9a-fA-F.:%]+`) accepts it and the request reaches the
  120. // controller — the test is about the controller's
  121. // ApiNotFoundException → Twig 404 path, not the route layer.
  122. $this->enqueueApiResponse(404, ['error' => 'not_found']);
  123. $response = $this->request('GET', '/app/ips/198.51.100.99');
  124. self::assertSame(404, $response->getStatusCode());
  125. self::assertStringContainsString('IP not found', (string) $response->getBody());
  126. }
  127. public function testRedirectsToLoginWhenAnonymous(): void
  128. {
  129. $_SESSION = []; // wipe seeded user
  130. $response = $this->request('GET', '/app/ips');
  131. self::assertSame(302, $response->getStatusCode());
  132. self::assertSame('/login', $response->getHeaderLine('Location'));
  133. }
  134. }