Skip to content

Commit

Permalink
test(metrics): Update test for new getRemediationForIp return
Browse files Browse the repository at this point in the history
  • Loading branch information
julienloizelet committed Jan 9, 2025
1 parent 07ab2e0 commit e62dce8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
16 changes: 8 additions & 8 deletions tests/Integration/GeolocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public function testCanVerifyIpAndCountryWithMaxmindInLiveMode(array $maxmindCon

$this->assertEquals(
'captcha',
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN)['remediation'],
'Get decisions for a clean IP but bad country (captcha)'
);

$this->assertEquals(
'bypass',
$bouncer->getRemediationForIp(TestHelpers::IP_FRANCE),
$bouncer->getRemediationForIp(TestHelpers::IP_FRANCE)['remediation'],
'Get decisions for a clean IP and clean country'
);

Expand All @@ -139,7 +139,7 @@ public function testCanVerifyIpAndCountryWithMaxmindInLiveMode(array $maxmindCon

$this->assertEquals(
'bypass',
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN)['remediation'],
'Get decisions for a clean IP and bad country but with geolocation disabled'
);

Expand All @@ -152,13 +152,13 @@ public function testCanVerifyIpAndCountryWithMaxmindInLiveMode(array $maxmindCon

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN)['remediation'],
'Get decisions for a bad IP (ban) and bad country (captcha)'
);

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::IP_FRANCE),
$bouncer->getRemediationForIp(TestHelpers::IP_FRANCE)['remediation'],
'Get decisions for a bad IP (ban) and clean country'
);
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public function testCanVerifyIpAndCountryWithMaxmindInStreamMode(array $maxmindC

$this->assertEquals(
'captcha',
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN)['remediation'],
'Should captcha a clean IP coming from a bad country (captcha)'
);

Expand All @@ -207,7 +207,7 @@ public function testCanVerifyIpAndCountryWithMaxmindInStreamMode(array $maxmindC

$this->assertEquals(
'captcha',
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN)['remediation'],
'Should still captcha a bad IP (ban) coming from a bad country (captcha) as cache has not been refreshed'
);

Expand All @@ -216,7 +216,7 @@ public function testCanVerifyIpAndCountryWithMaxmindInStreamMode(array $maxmindC

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN)['remediation'],
'The new decision should now be added, so the previously captcha IP should now be ban'
);
}
Expand Down
47 changes: 18 additions & 29 deletions tests/Integration/IpVerificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,25 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::BAD_IP),
$bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'],
'Get decisions for a bad IP (for the first time, it should be a cache miss)'
);

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::BAD_IP),
$bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'],
'Call the same thing for the second time (now it should be a cache hit)'
);

$cleanRemediation1stCall = $bouncer->getRemediationForIp(TestHelpers::CLEAN_IP);
$cleanRemediation1stCall = $bouncer->getRemediationForIp(TestHelpers::CLEAN_IP)['remediation'];
$this->assertEquals(
'bypass',
$cleanRemediation1stCall,
'Get decisions for a clean IP for the first time (it should be a cache miss)'
);

// Call the same thing for the second time (now it should be a cache hit)
$cleanRemediation2ndCall = $bouncer->getRemediationForIp(TestHelpers::CLEAN_IP);
$cleanRemediation2ndCall = $bouncer->getRemediationForIp(TestHelpers::CLEAN_IP)['remediation'];
$this->assertEquals('bypass', $cleanRemediation2ndCall);

// Prune cache
Expand All @@ -336,13 +336,13 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori

// Call one more time (should miss as the cache has been cleared)

$remediation3rdCall = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
$remediation3rdCall = $bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'];
$this->assertEquals('ban', $remediation3rdCall);

// Reconfigure the bouncer to set maximum remediation level to "captcha"
$bouncerConfigs['bouncing_level'] = Constants::BOUNCING_LEVEL_FLEX;
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'];
$this->assertEquals('captcha', $cappedRemediation, 'The remediation for the banned IP should now be "captcha"');
// Reset the max remediation level to its origin state
$bouncerConfigs['bouncing_level'] = Constants::BOUNCING_LEVEL_NORMAL;
Expand All @@ -357,7 +357,7 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
TestHelpers::BAD_IP . '/' . TestHelpers::LARGE_IPV4_RANGE,
'ban'
);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'];
$this->assertEquals(
'ban',
$cappedRemediation,
Expand All @@ -373,7 +373,7 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
TestHelpers::BAD_IPV6 . '/' . TestHelpers::IPV6_RANGE,
'ban'
);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6)['remediation'];
$this->assertEquals(
'ban',
$cappedRemediation,
Expand All @@ -387,7 +387,7 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
TestHelpers::BAD_IPV6,
'ban'
);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6)['remediation'];
$this->assertEquals(
'ban',
$cappedRemediation,
Expand Down Expand Up @@ -432,27 +432,27 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::BAD_IP),
$bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'],
'Get decisions for a bad IP for the first time (as the cache has been warmed up should be a cache hit)'
);

// Reconfigure the bouncer to set maximum remediation level to "captcha"
$bouncerConfigs['bouncing_level'] = Constants::BOUNCING_LEVEL_FLEX;
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'];
$this->assertEquals('captcha', $cappedRemediation, 'The remediation for the banned IP should now be "captcha"');
$bouncerConfigs['bouncing_level'] = Constants::BOUNCING_LEVEL_NORMAL;
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
$this->assertEquals(
'bypass',
$bouncer->getRemediationForIp(TestHelpers::CLEAN_IP),
$bouncer->getRemediationForIp(TestHelpers::CLEAN_IP)['remediation'],
'Get decisions for a clean IP for the first time (as the cache has been warmed up should be a cache hit)'
);

// Preload the remediation to prepare the next tests.
$this->assertEquals(
'bypass',
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP),
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP)['remediation'],
'Preload the bypass remediation to prepare the next tests'
);

Expand All @@ -464,13 +464,13 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP),
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP)['remediation'],
'The new decision should now be added, so the previously clean IP should now be bad'
);

$this->assertEquals(
'bypass',
$bouncer->getRemediationForIp(TestHelpers::BAD_IP),
$bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'],
'The old decisions should now be removed, so the previously bad IP should now be clean'
);

Expand All @@ -497,7 +497,7 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o

$this->assertEquals(
'ban',
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP),
$bouncer->getRemediationForIp(TestHelpers::NEWLY_BAD_IP)['remediation'],
'The cache warm up should be stored across each instantiation'
);

Expand All @@ -520,13 +520,13 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
// Pull updates
$bouncer->refreshBlocklistCache();

$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP)['remediation'];
$this->assertEquals(
'ban',
$cappedRemediation,
'The remediation for the banned IP with a large range should be "ban" even in stream mode'
);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6);
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6)['remediation'];
$this->assertEquals(
'bypass',
$cappedRemediation,
Expand Down Expand Up @@ -710,12 +710,6 @@ public function testAppSecFlow()
);
}

$this->assertEquals(
['bypass' => 1],
$originCountItem['clean'],
'The origin count for clean should be 1'
);

// Test 3: clean IP and clean request
$bouncer->clearCache();
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
Expand Down Expand Up @@ -743,11 +737,6 @@ public function testAppSecFlow()
'The origin count for clean appsec should be 1'
);
}
$this->assertEquals(
['bypass' => 1],
$originCountItem['clean'],
'The origin count for clean should be 1'
);
}

/**
Expand Down

0 comments on commit e62dce8

Please sign in to comment.