IpsPageTest.php 5.4 KB

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