Skip to content

Commit 0a7883f

Browse files
test(geolocation): Update Unit Test for geolocation
1 parent a157b24 commit 0a7883f

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

tests/GeolocationTest.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use CrowdSecBouncer\ApiCache;
88
use CrowdSecBouncer\ApiClient;
99
use CrowdSecBouncer\Bouncer;
10-
use PHPUnit\Framework\MockObject\MockObject;
1110
use PHPUnit\Framework\TestCase;
1211
use Psr\Log\LoggerInterface;
1312
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
@@ -46,7 +45,6 @@ public function testCanVerifyIpAndCountryWithMaxmindInLiveMode(array $maxmindCon
4645
// Check if MaxMind database exist
4746
if (!file_exists($maxmindConfig['database_path'])) {
4847
$this->fail('There must be a MaxMind Database here: '.$maxmindConfig['database_path']);
49-
$this->stopFlag = true; // Stop further processing if this occurs
5048
}
5149
// Init context
5250
$cacheAdapter = new PhpFilesAdapter('php_array_adapter_backup_cache', 0,
@@ -81,9 +79,14 @@ public function testCanVerifyIpAndCountryWithMaxmindInLiveMode(array $maxmindCon
8179
$this->assertEquals(
8280
'captcha',
8381
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
84-
'Get decisions for a clean IP but bad country'
82+
'Get decisions for a clean IP but bad country (captcha)'
8583
);
8684

85+
$this->assertEquals(
86+
'bypass',
87+
$bouncer->getRemediationForIp(TestHelpers::IP_FRANCE),
88+
'Get decisions for a clean IP and clean country'
89+
);
8790

8891
// Disable Geolocation feature
8992
$geolocationConfig['enabled'] = false;
@@ -98,5 +101,42 @@ public function testCanVerifyIpAndCountryWithMaxmindInLiveMode(array $maxmindCon
98101
'Get decisions for a clean IP and bad country but with geolocation disabled'
99102
);
100103

104+
105+
// Enable again geolocation and change testing conditions
106+
$this->watcherClient->setSecondState();
107+
$geolocationConfig['enabled'] = true;
108+
$bouncerConfig['geolocation'] = $geolocationConfig;
109+
$bouncer = new Bouncer(null, $this->logger, $apiCache);
110+
$bouncer->configure($bouncerConfig);
111+
$cacheAdapter->clear();
112+
113+
$this->assertEquals(
114+
'ban',
115+
$bouncer->getRemediationForIp(TestHelpers::IP_JAPAN),
116+
'Get decisions for a bad IP (ban) and bad country (captcha)'
117+
);
118+
119+
120+
$this->assertEquals(
121+
'ban',
122+
$bouncer->getRemediationForIp(TestHelpers::IP_FRANCE),
123+
'Get decisions for a bad IP (ban) and clean country'
124+
);
125+
126+
}
127+
128+
129+
/**
130+
* @group integration
131+
* @covers \Bouncer
132+
* @dataProvider maxmindConfigProvider
133+
* @group ignore_
134+
* @throws \Symfony\Component\Cache\Exception\CacheException
135+
*/
136+
public function testCanVerifyIpAndCountryWithMaxmindInStreamMode(array $maxmindConfig):
137+
void
138+
{
139+
$this->markTestIncomplete('This test has not been implemented yet.');
140+
101141
}
102142
}

tests/TestHelpers.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class TestHelpers
2121
public const IPV6_RANGE = '64';
2222
public const JAPAN = 'JP';
2323
public const IP_JAPAN = '210.249.74.42';
24-
public const FRANCE = 'FR';
2524
public const IP_FRANCE = '78.119.253.85';
2625

2726

tests/WatcherClient.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public function setSecondState(): void
6565
$now = new DateTime();
6666
$this->addDecision($now, '36h', '+36 hours', TestHelpers::NEWLY_BAD_IP, 'ban');
6767
$this->addDecision($now, '48h', '+48 hours', TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE, 'captcha');
68+
$this->addDecision($now, '24h', '+24 hours', TestHelpers::JAPAN, 'captcha', Constants::SCOPE_COUNTRY);
69+
$this->addDecision($now, '24h', '+24 hours', TestHelpers::IP_JAPAN, 'ban');
70+
$this->addDecision($now, '24h', '+24 hours', TestHelpers::IP_FRANCE, 'ban');
6871
}
6972

7073
/**
@@ -102,11 +105,11 @@ public function deleteAllDecisions(): void
102105
}
103106

104107
protected function getFinalScope($scope, $value){
105-
return ($scope === 'Ip' && 2 === count(explode('/', $value))) ? 'Range' : $scope;
108+
return ($scope === Constants::SCOPE_IP && 2 === count(explode('/', $value))) ? Constants::SCOPE_RANGE : $scope;
106109
}
107110

108111
public function addDecision(DateTime $now, string $durationString, string $dateTimeDurationString, string
109-
$value, string $type, string $scope = 'Ip')
112+
$value, string $type, string $scope = Constants::SCOPE_IP)
110113
{
111114
$stopAt = (clone $now)->modify($dateTimeDurationString)->format('Y-m-d\TH:i:s.000\Z');
112115
$startAt = $now->format('Y-m-d\TH:i:s.000\Z');

0 commit comments

Comments
 (0)