EffectiveStatusServiceTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Reputation;
  4. use App\Domain\Ip\Cidr;
  5. use App\Domain\Ip\IpAddress;
  6. use App\Domain\Reputation\CidrEvaluator;
  7. use App\Domain\Reputation\EffectiveStatus;
  8. use App\Domain\Reputation\EffectiveStatusService;
  9. use App\Infrastructure\Reputation\CidrEvaluatorFactory;
  10. use App\Infrastructure\Reputation\IpScoreRepository;
  11. use Doctrine\DBAL\Connection;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * Locks the SPEC §5 precedence: allowlist > manual block > scored > clean.
  15. * M09 wires `Scored` to "any non-zero score in `ip_scores`" via a stub
  16. * IpScoreRepository here; the integration tests cover the real path.
  17. */
  18. final class EffectiveStatusServiceTest extends TestCase
  19. {
  20. public function testAllowlistWinsOverManualBlock(): void
  21. {
  22. $bin = IpAddress::fromString('198.51.100.5')->binary();
  23. $factory = $this->factoryReturning(new CidrEvaluator(
  24. manualIpBins: [$bin],
  25. manualSubnets: [],
  26. allowlistIpBins: [$bin],
  27. allowlistSubnets: [],
  28. ));
  29. $service = new EffectiveStatusService($factory, $this->scoreRepoWithoutScores());
  30. self::assertSame(
  31. EffectiveStatus::Allowlisted,
  32. $service->forIp(IpAddress::fromString('198.51.100.5'))
  33. );
  34. }
  35. public function testManualBlockReturnedWhenNotAllowlisted(): void
  36. {
  37. $bin = IpAddress::fromString('203.0.113.42')->binary();
  38. $factory = $this->factoryReturning(new CidrEvaluator([$bin], [], [], []));
  39. $service = new EffectiveStatusService($factory, $this->scoreRepoWithoutScores());
  40. self::assertSame(
  41. EffectiveStatus::ManuallyBlocked,
  42. $service->forIp(IpAddress::fromString('203.0.113.42'))
  43. );
  44. }
  45. public function testIpInsideAllowlistedSubnetEvenWithSeparateManualBlockIsAllowlisted(): void
  46. {
  47. $allow = Cidr::fromString('203.0.113.0/24');
  48. $factory = $this->factoryReturning(new CidrEvaluator(
  49. manualIpBins: [IpAddress::fromString('203.0.113.42')->binary()],
  50. manualSubnets: [],
  51. allowlistIpBins: [],
  52. allowlistSubnets: [$allow],
  53. ));
  54. $service = new EffectiveStatusService($factory, $this->scoreRepoWithoutScores());
  55. self::assertSame(
  56. EffectiveStatus::Allowlisted,
  57. $service->forIp(IpAddress::fromString('203.0.113.42'))
  58. );
  59. }
  60. public function testScoredWhenScoreRepoHasRowsAndNoOverrides(): void
  61. {
  62. $factory = $this->factoryReturning(new CidrEvaluator([], [], [], []));
  63. $service = new EffectiveStatusService($factory, $this->scoreRepoWithScores());
  64. self::assertSame(
  65. EffectiveStatus::Scored,
  66. $service->forIp(IpAddress::fromString('203.0.113.99'))
  67. );
  68. }
  69. public function testCleanWhenNothingMatches(): void
  70. {
  71. $factory = $this->factoryReturning(new CidrEvaluator([], [], [], []));
  72. $service = new EffectiveStatusService($factory, $this->scoreRepoWithoutScores());
  73. self::assertSame(
  74. EffectiveStatus::Clean,
  75. $service->forIp(IpAddress::fromString('203.0.113.99'))
  76. );
  77. }
  78. private function factoryReturning(CidrEvaluator $evaluator): CidrEvaluatorFactory
  79. {
  80. return new class ($evaluator) extends CidrEvaluatorFactory {
  81. public function __construct(private readonly CidrEvaluator $fixed)
  82. {
  83. // Deliberately not calling parent::__construct — this stub
  84. // never queries the DB.
  85. }
  86. public function get(): CidrEvaluator
  87. {
  88. return $this->fixed;
  89. }
  90. public function invalidate(): void
  91. {
  92. // no-op
  93. }
  94. };
  95. }
  96. private function scoreRepoWithoutScores(): IpScoreRepository
  97. {
  98. return new class ($this->createMock(Connection::class)) extends IpScoreRepository {
  99. public function hasAnyScore(string $ipBin): bool
  100. {
  101. return false;
  102. }
  103. };
  104. }
  105. private function scoreRepoWithScores(): IpScoreRepository
  106. {
  107. return new class ($this->createMock(Connection::class)) extends IpScoreRepository {
  108. public function hasAnyScore(string $ipBin): bool
  109. {
  110. return true;
  111. }
  112. };
  113. }
  114. }