IpsPageTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $response = $this->request('GET', '/app/ips');
  45. self::assertSame(200, $response->getStatusCode());
  46. $body = (string) $response->getBody();
  47. self::assertStringContainsString('203.0.113.10', $body);
  48. self::assertStringContainsString('2001:db8::1', $body);
  49. self::assertStringContainsString('brute_force', $body);
  50. self::assertStringContainsString('2 total', $body);
  51. }
  52. public function testListPageRendersEmptyState(): void
  53. {
  54. $this->enqueueApiResponse(200, [
  55. 'page' => 1,
  56. 'page_size' => 25,
  57. 'total' => 0,
  58. 'items' => [],
  59. ]);
  60. $response = $this->request('GET', '/app/ips');
  61. self::assertSame(200, $response->getStatusCode());
  62. self::assertStringContainsString('No results', (string) $response->getBody());
  63. }
  64. public function testListPagePassesFiltersThrough(): void
  65. {
  66. $this->enqueueApiResponse(200, ['page' => 1, 'page_size' => 25, 'total' => 0, 'items' => []]);
  67. $response = $this->request('GET', '/app/ips?q=2001&category=spam');
  68. $body = (string) $response->getBody();
  69. self::assertSame(200, $response->getStatusCode());
  70. // The filter form preserves the user's selection.
  71. self::assertMatchesRegularExpression('/value="2001"/', $body);
  72. self::assertMatchesRegularExpression('/<option value="spam"\s+selected/', $body);
  73. }
  74. public function testDetailPageRendersScoresAndHistory(): void
  75. {
  76. $this->enqueueApiResponse(200, [
  77. 'ip' => '203.0.113.10',
  78. 'is_ipv4' => true,
  79. 'status' => 'scored',
  80. 'scores' => [
  81. ['category' => 'brute_force', 'category_id' => 1, 'score' => 1.5, 'last_report_at' => '2026-04-29T10:00:00Z', 'report_count_30d' => 5],
  82. ],
  83. 'enrichment' => ['country_code' => null, 'asn' => null, 'as_org' => null, 'enriched_at' => null],
  84. 'manual_block' => null,
  85. 'allowlist' => null,
  86. 'history' => [
  87. ['type' => 'report', 'at' => '2026-04-29T10:00:00Z', 'category' => 'brute_force', 'reporter' => 'web-prod-01', 'weight' => 1.0, 'metadata' => null],
  88. ],
  89. 'has_more' => false,
  90. ]);
  91. $response = $this->request('GET', '/app/ips/203.0.113.10');
  92. self::assertSame(200, $response->getStatusCode());
  93. $body = (string) $response->getBody();
  94. self::assertStringContainsString('203.0.113.10', $body);
  95. self::assertStringContainsString('brute_force', $body);
  96. self::assertStringContainsString('web-prod-01', $body);
  97. self::assertStringContainsString('Score per category', $body);
  98. self::assertStringContainsString('History', $body);
  99. }
  100. public function testDetailPageReturnsTwig404OnApiNotFound(): void
  101. {
  102. $this->enqueueApiResponse(404, ['error' => 'not_found']);
  103. $response = $this->request('GET', '/app/ips/not-an-ip');
  104. self::assertSame(404, $response->getStatusCode());
  105. self::assertStringContainsString('IP not found', (string) $response->getBody());
  106. }
  107. public function testRedirectsToLoginWhenAnonymous(): void
  108. {
  109. $_SESSION = []; // wipe seeded user
  110. $response = $this->request('GET', '/app/ips');
  111. self::assertSame(302, $response->getStatusCode());
  112. self::assertSame('/login', $response->getHeaderLine('Location'));
  113. }
  114. }