1
0

CountriesEndpointTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Admin;
  4. use App\Domain\Auth\Role;
  5. use App\Domain\Auth\TokenKind;
  6. use App\Domain\Ip\IpAddress;
  7. use App\Tests\Integration\Support\AppTestCase;
  8. /**
  9. * `/admin/ips/countries` — distinct ISO codes from `ip_enrichment` with
  10. * counts. Empty when there's nothing to show; sorted by code.
  11. */
  12. final class CountriesEndpointTest extends AppTestCase
  13. {
  14. public function testReturnsEmptyWhenNothingEnriched(): void
  15. {
  16. $token = $this->createToken(TokenKind::Admin, Role::Viewer);
  17. $resp = $this->request('GET', '/api/v1/admin/ips/countries', [
  18. 'Authorization' => 'Bearer ' . $token,
  19. ]);
  20. self::assertSame(200, $resp->getStatusCode());
  21. $body = $this->decode($resp);
  22. self::assertSame([], $body['items']);
  23. }
  24. public function testReturnsCountriesWithCounts(): void
  25. {
  26. $now = (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
  27. foreach ([['1.1.1.1', 'GB'], ['2.2.2.2', 'GB'], ['3.3.3.3', 'US']] as [$ip, $cc]) {
  28. $bin = IpAddress::fromString($ip)->binary();
  29. $this->db->insert('ip_enrichment', [
  30. 'ip_bin' => $bin,
  31. 'country_code' => $cc,
  32. 'asn' => null,
  33. 'as_org' => null,
  34. 'enriched_at' => $now,
  35. ], ['ip_bin' => \Doctrine\DBAL\ParameterType::LARGE_OBJECT]);
  36. }
  37. $token = $this->createToken(TokenKind::Admin, Role::Viewer);
  38. $resp = $this->request('GET', '/api/v1/admin/ips/countries', [
  39. 'Authorization' => 'Bearer ' . $token,
  40. ]);
  41. self::assertSame(200, $resp->getStatusCode());
  42. $body = $this->decode($resp);
  43. self::assertSame([
  44. ['code' => 'GB', 'count' => 2],
  45. ['code' => 'US', 'count' => 1],
  46. ], $body['items']);
  47. }
  48. public function testRequiresViewer(): void
  49. {
  50. $resp = $this->request('GET', '/api/v1/admin/ips/countries');
  51. self::assertSame(401, $resp->getStatusCode());
  52. }
  53. }