Skip to content

Commit 5405819

Browse files
test(geolocation): Add end to end test with geolcation script
1 parent dac6940 commit 5405819

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

.github/workflows/test-suite.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ jobs:
121121
sed -i 's/\x27forced_test_ip\x27 => \x27210.249.74.42\x27/\x27forced_test_ip\x27 => \x27\x27/g' examples/auto-prepend/settings.php
122122
sed -i 's/\x27stream_mode\x27 => false/\x27stream_mode\x27 => true/g' examples/auto-prepend/settings.php
123123
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
124-
./run-tests.sh ci "./__tests__/3-stream-mode.js"
124+
./run-tests.sh ci "./__tests__/3-stream-mode.js"
125+
126+
- name: Run End to end test (standalone geolocation)
127+
run: |
128+
cd ${{ github.workspace }}/my-own-modules/crowdsec-php-lib/tests/end-to-end/__scripts__
129+
./run-tests.sh ci "./__tests__/4-geolocation.js"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
require_once __DIR__.'/../../../vendor/autoload.php';
4+
require_once __DIR__.'/../settings.php';
5+
6+
use CrowdSecBouncer\Geolocation;
7+
8+
if (isset($_GET['ip'])) {
9+
if (\PHP_SESSION_NONE === session_status()) {
10+
session_name('crowdsec-geoloc');
11+
session_start();
12+
}
13+
$requestedIp = $_GET['ip'];
14+
$dbName = $_GET['db-name'] ?? 'GeoLite2-Country.mmdb';
15+
$dbType = $_GET['db-type'] ?? 'country';
16+
$saveInSession = isset($_GET['session-save']);
17+
18+
$geolocConfig = [
19+
'enabled' => true,
20+
'save_in_session' => $saveInSession,
21+
'type' => 'maxmind',
22+
'maxmind' => [
23+
'database_type' => $dbType,
24+
'database_path' => '/var/www/html/my-own-modules/crowdsec-php-lib/tests/' . $dbName
25+
]
26+
];
27+
28+
$geolocation = new Geolocation();
29+
if(!$saveInSession){
30+
$geolocation->clearGeolocationSessionContext();
31+
}
32+
33+
$countryResult = $geolocation->getCountryResult($geolocConfig, $requestedIp);
34+
$country = $countryResult['country'];
35+
$notFound = $countryResult['not_found'];
36+
$error = $countryResult['error'];
37+
$sessionMessage = $saveInSession ? 'true' : 'false';
38+
39+
echo "
40+
<!DOCTYPE html>
41+
<html>
42+
<head>
43+
<meta charset='utf-8'/>
44+
<title>Geolocation for IP: $requestedIp</title>
45+
</head>
46+
47+
<body>
48+
<h1>For IP $requestedIp:</h1>
49+
<ul>
50+
<li>Country: $country</li>
51+
<li>Not Found message: $notFound</li>
52+
<li>Error message: $error</li>
53+
<li>Session save: $sessionMessage</li>
54+
</ul>
55+
</body>
56+
</html>
57+
";
58+
} else {
59+
die('You must pass an "ip" param' . PHP_EOL);
60+
}
61+
62+
63+
64+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-undef */
2+
const { JAPAN_IP, FRANCE_IP } = require("../utils/constants");
3+
4+
const { removeAllDecisions, runGeolocationTest } = require("../utils/helpers");
5+
6+
describe(`Geolocation standalone run`, () => {
7+
beforeAll(async () => {
8+
await removeAllDecisions();
9+
});
10+
11+
it("Should get JP", async () => {
12+
await runGeolocationTest(JAPAN_IP, false);
13+
await expect(page).toMatchText(/Country: JP/);
14+
});
15+
16+
it("Should get FR", async () => {
17+
await runGeolocationTest(FRANCE_IP, false);
18+
await expect(page).toMatchText(/Country: FR/);
19+
});
20+
21+
it("Should get FR and FR as it will be saved in session", async () => {
22+
await runGeolocationTest(FRANCE_IP, true);
23+
await expect(page).toMatchText(/Country: FR/);
24+
await runGeolocationTest(JAPAN_IP, true);
25+
await expect(page).toMatchText(/Country: FR/);
26+
});
27+
});

tests/end-to-end/utils/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const FORCED_TEST_IP =
88
const GEOLOC_ENABLED = process.env.GEOLOC_ENABLED === "true";
99
const STREAM_MODE = process.env.STREAM_MODE === "true";
1010
const GEOLOC_BAD_COUNTRY = "JP";
11+
const JAPAN_IP = "210.249.74.42";
12+
const FRANCE_IP = "78.119.253.85";
1113
const { LAPI_URL_FROM_PLAYWRIGHT } = process.env;
1214
const { BOUNCER_KEY } = process.env;
1315
const WATCHER_LOGIN = "watcherLogin";
@@ -33,4 +35,6 @@ module.exports = {
3335
GEOLOC_ENABLED,
3436
GEOLOC_BAD_COUNTRY,
3537
STREAM_MODE,
38+
JAPAN_IP,
39+
FRANCE_IP,
3640
};

tests/end-to-end/utils/helpers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ const runCacheAction = async (actionType = "refresh") => {
2121
await expect(page).toMatchTitle(`Cache action: ${actionType}`);
2222
};
2323

24+
const runGeolocationTest = async (ip, saveInSession) => {
25+
let url = `/my-own-modules/crowdsec-php-lib/examples/auto-prepend/scripts/geolocation-test.php?ip=${ip}`;
26+
if (saveInSession) {
27+
url += `&session-save=1`;
28+
}
29+
await goToPublicPage(`${url}`);
30+
await page.waitForLoadState("networkidle");
31+
await expect(page).not.toMatchTitle(/404/);
32+
await expect(page).toMatchTitle(`Geolocation for IP: ${ip}`);
33+
};
34+
2435
const computeCurrentPageRemediation = async (
2536
accessibleTextInTitle = "Home page",
2637
) => {
@@ -120,4 +131,5 @@ module.exports = {
120131
getFileContent,
121132
deleteFileContent,
122133
runCacheAction,
134+
runGeolocationTest,
123135
};

0 commit comments

Comments
 (0)