Skip to content

Commit 5e79548

Browse files
test(geolocation): Add tests for geolocation
1 parent bac5068 commit 5e79548

File tree

11 files changed

+825
-533
lines changed

11 files changed

+825
-533
lines changed

.github/workflows/end-to-end-auto-prepend-test-suite.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ jobs:
9696

9797
- name: Prepare for playwright test
9898
run: |
99+
cd ${{ github.workspace }}
100+
ddev maxmind-download DEFAULT GeoLite2-City /var/www/html/my-own-modules/${{ env.EXTENSION_PATH }}/geolocation
101+
ddev maxmind-download DEFAULT GeoLite2-Country /var/www/html/my-own-modules/${{ env.EXTENSION_PATH }}/geolocation
102+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/geolocation
103+
sha256sum -c GeoLite2-Country.tar.gz.sha256.txt
104+
sha256sum -c GeoLite2-City.tar.gz.sha256.txt
105+
tar -xf GeoLite2-Country.tar.gz
106+
tar -xf GeoLite2-City.tar.gz
107+
rm GeoLite2-Country.tar.gz GeoLite2-Country.tar.gz.sha256.txt GeoLite2-City.tar.gz GeoLite2-City.tar.gz.sha256.txt
99108
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
100109
chmod +x test-init.sh
101110
./test-init.sh
@@ -157,4 +166,9 @@ jobs:
157166
- name: Run Memcached tests
158167
run: |
159168
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
160-
./run-tests.sh ci "./__tests__/7-memcached.js"
169+
./run-tests.sh ci "./__tests__/7-memcached.js"
170+
171+
- name: Run Geolocation tests
172+
run: |
173+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
174+
./run-tests.sh ci "./__tests__/8-geolocation.js"
Lines changed: 147 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,154 @@
11
name: End-to-end test suite
22
on:
3-
push:
4-
branches:
5-
- main
6-
paths-ignore:
7-
- '**.md'
8-
workflow_dispatch:
3+
push:
4+
branches:
5+
- main
6+
paths-ignore:
7+
- '**.md'
8+
workflow_dispatch:
99

1010
permissions:
11-
contents: read
11+
contents: read
1212

1313
jobs:
14-
end-to-end-test-suite:
15-
strategy:
16-
fail-fast: false
17-
matrix:
18-
wp-version: [ "4.9", "5.0", "5.1", "5.2", "5.3", "5.4", "5.5", "5.6", "5.7", "5.8", "5.9", "6.0" ]
19-
php-version: [ "7.2", "7.4", "8.0" ]
20-
exclude:
21-
- { php-version: "7.4", wp-version: "4.9" }
22-
- { php-version: "7.4", wp-version: "5.0" }
23-
- { php-version: "7.4", wp-version: "5.1" }
24-
- { php-version: "7.4", wp-version: "5.2" }
25-
- { php-version: "8.0", wp-version: "4.9" }
26-
- { php-version: "8.0", wp-version: "5.0" }
27-
- { php-version: "8.0", wp-version: "5.1" }
28-
- { php-version: "8.0", wp-version: "5.2" }
29-
- { php-version: "8.0", wp-version: "5.3" }
30-
- { php-version: "8.0", wp-version: "5.4" }
31-
- { php-version: "8.0", wp-version: "5.5" }
32-
33-
name: End-to-end test suite
34-
runs-on: ubuntu-latest
35-
if: ${{ !contains(github.event.head_commit.message, 'chore(') }}
36-
14+
end-to-end-test-suite:
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
wp-version: [ "4.9", "5.0", "5.1", "5.2", "5.3", "5.4", "5.5", "5.6", "5.7", "5.8", "5.9", "6.0" ]
19+
php-version: [ "7.2", "7.4", "8.0" ]
20+
exclude:
21+
- { php-version: "7.4", wp-version: "4.9" }
22+
- { php-version: "7.4", wp-version: "5.0" }
23+
- { php-version: "7.4", wp-version: "5.1" }
24+
- { php-version: "7.4", wp-version: "5.2" }
25+
- { php-version: "8.0", wp-version: "4.9" }
26+
- { php-version: "8.0", wp-version: "5.0" }
27+
- { php-version: "8.0", wp-version: "5.1" }
28+
- { php-version: "8.0", wp-version: "5.2" }
29+
- { php-version: "8.0", wp-version: "5.3" }
30+
- { php-version: "8.0", wp-version: "5.4" }
31+
- { php-version: "8.0", wp-version: "5.5" }
32+
33+
name: End-to-end test suite
34+
runs-on: ubuntu-latest
35+
if: ${{ !contains(github.event.head_commit.message, 'chore(') }}
36+
37+
env:
38+
EXTENSION_NAME: "CrowdSec_Bouncer"
39+
EXTENSION_PATH: "crowdsec-bouncer"
40+
41+
steps:
42+
43+
- name: Clone DDEV files
44+
uses: actions/checkout@v2
45+
with:
46+
path: .ddev
47+
repository: julienloizelet/ddev-wp
48+
49+
- name: Install DDEV
3750
env:
38-
EXTENSION_NAME: "CrowdSec_Bouncer"
39-
EXTENSION_PATH: "crowdsec-bouncer"
40-
41-
steps:
42-
43-
- name: Clone DDEV files
44-
uses: actions/checkout@v2
45-
with:
46-
path: .ddev
47-
repository: julienloizelet/ddev-wp
48-
49-
- name: Install DDEV
50-
env:
51-
DDEV_VERSION: v1.19.3
52-
run: |
53-
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
54-
sudo apt-get -qq update
55-
sudo apt-get -qq -y install libnss3-tools
56-
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
57-
bash install_ddev.sh ${{ env.DDEV_VERSION }}
58-
ddev config global --instrumentation-opt-in=false --omit-containers=dba,ddev-ssh-agent
59-
rm install_ddev.sh
60-
61-
- name: Set WP_VERSION_CODE env
62-
# used in some directory path and conventional file naming
63-
# Example : 5.6.5 => wp565
64-
run: |
65-
echo "WP_VERSION_CODE=$(echo wp${{ matrix.wp-version }} | sed 's/\.//g' )" >> $GITHUB_ENV
66-
67-
68-
- name: Start DDEV for ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
69-
run: |
70-
cp .ddev/config_overrides/config.${{ env.WP_VERSION_CODE }}.yaml .ddev/config.${{ env.WP_VERSION_CODE }}.yaml
71-
cp .ddev/additional_docker_compose/docker-compose.crowdsec.yaml .ddev/docker-compose.crowdsec.yaml
72-
cp .ddev/additional_docker_compose/docker-compose.playwright.yaml .ddev/docker-compose.playwright.yaml
73-
sed -i -e 's/^php_version:.*/php_version: ${{ matrix.php-version }}/g' .ddev/config.${{ env.WP_VERSION_CODE }}.yaml
74-
mkdir wp-content
75-
ddev start
76-
sudo chmod -R 777 ${{ github.workspace }}/wp-content
77-
sudo chmod -R 777 ${{ github.workspace }}/my-own-modules
78-
79-
- name: Install WordPress ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
80-
run: |
81-
wget https://wordpress.org/wordpress-${{ matrix.wp-version }}.tar.gz
82-
tar -xf wordpress-${{ matrix.wp-version }}.tar.gz wordpress
83-
cp -r wordpress/. ${{ github.workspace }}
84-
rm -rf wordpress
85-
rm wordpress-${{ matrix.wp-version }}.tar.gz
86-
87-
- name: Setup WordPress ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
88-
run: |
89-
ddev exec wp core install --url='https://${{ env.WP_VERSION_CODE }}.ddev.site' --title='WordPress' --admin_user='admin' --admin_password='admin123' --admin_email='admin@admin.com'
90-
91-
92-
- name: Clone ${{ env.EXTENSION_NAME }} files
93-
uses: actions/checkout@v2
94-
with:
95-
path: my-own-modules/${{ env.EXTENSION_PATH }}
96-
97-
- name: Prepare for playwright test
98-
run: |
99-
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
100-
chmod +x test-init.sh
101-
./test-init.sh
102-
chmod +x run-tests.sh
103-
104-
- name: Run Plugin activation tests
105-
run: |
106-
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
107-
./run-tests.sh ci "./__tests__/1-activate-plugin.js"
108-
109-
- name: Configure CrowdSec and Wordpress bouncer plugin
110-
run: |
111-
ddev crowdsec-config
112-
113-
- name: Run Live mode remediation tests
114-
run: |
115-
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
116-
./run-tests.sh ci "./__tests__/2-live-mode-remediations.js ./__tests__/3-live-mode-more.js"
117-
118-
- name: Run Live mode cache tests
119-
run: |
120-
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
121-
./run-tests.sh ci "./__tests__/4-live-mode-cache.js"
122-
123-
- name: Prepare cron usage
124-
run: |
125-
sed -i 's/fastcgi_finish_request/\/\/fastcgi_finish_request/g' wp-cron.php
126-
127-
- name: Run Stream mode tests
128-
run: |
129-
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
130-
./run-tests.sh ci "./__tests__/5-stream-mode.js"
131-
132-
- name: Run Redis tests
133-
run: |
134-
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
135-
./run-tests.sh ci "./__tests__/6-redis.js"
136-
137-
- name: Run Memcached tests
138-
run: |
139-
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
140-
./run-tests.sh ci "./__tests__/7-memcached.js"
51+
DDEV_VERSION: v1.19.3
52+
run: |
53+
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
54+
sudo apt-get -qq update
55+
sudo apt-get -qq -y install libnss3-tools
56+
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
57+
bash install_ddev.sh ${{ env.DDEV_VERSION }}
58+
ddev config global --instrumentation-opt-in=false --omit-containers=dba,ddev-ssh-agent
59+
rm install_ddev.sh
60+
61+
- name: Set WP_VERSION_CODE env
62+
# used in some directory path and conventional file naming
63+
# Example : 5.6.5 => wp565
64+
run: |
65+
echo "WP_VERSION_CODE=$(echo wp${{ matrix.wp-version }} | sed 's/\.//g' )" >> $GITHUB_ENV
66+
67+
68+
- name: Start DDEV for ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
69+
run: |
70+
cp .ddev/config_overrides/config.${{ env.WP_VERSION_CODE }}.yaml .ddev/config.${{ env.WP_VERSION_CODE }}.yaml
71+
cp .ddev/additional_docker_compose/docker-compose.crowdsec.yaml .ddev/docker-compose.crowdsec.yaml
72+
cp .ddev/additional_docker_compose/docker-compose.playwright.yaml .ddev/docker-compose.playwright.yaml
73+
sed -i -e 's/^php_version:.*/php_version: ${{ matrix.php-version }}/g' .ddev/config.${{ env.WP_VERSION_CODE }}.yaml
74+
mkdir wp-content
75+
ddev start
76+
sudo chmod -R 777 ${{ github.workspace }}/wp-content
77+
sudo chmod -R 777 ${{ github.workspace }}/my-own-modules
78+
79+
- name: Install WordPress ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
80+
run: |
81+
wget https://wordpress.org/wordpress-${{ matrix.wp-version }}.tar.gz
82+
tar -xf wordpress-${{ matrix.wp-version }}.tar.gz wordpress
83+
cp -r wordpress/. ${{ github.workspace }}
84+
rm -rf wordpress
85+
rm wordpress-${{ matrix.wp-version }}.tar.gz
86+
87+
- name: Setup WordPress ${{ matrix.wp-version }} with PHP ${{ matrix.php-version }}
88+
run: |
89+
ddev exec wp core install --url='https://${{ env.WP_VERSION_CODE }}.ddev.site' --title='WordPress' --admin_user='admin' --admin_password='admin123' --admin_email='admin@admin.com'
90+
91+
92+
- name: Clone ${{ env.EXTENSION_NAME }} files
93+
uses: actions/checkout@v2
94+
with:
95+
path: my-own-modules/${{ env.EXTENSION_PATH }}
96+
97+
- name: Prepare for playwright test
98+
run: |
99+
cd ${{ github.workspace }}
100+
ddev maxmind-download DEFAULT GeoLite2-City /var/www/html/my-own-modules/${{ env.EXTENSION_PATH }}/geolocation
101+
ddev maxmind-download DEFAULT GeoLite2-Country /var/www/html/my-own-modules/${{ env.EXTENSION_PATH }}/geolocation
102+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/geolocation
103+
sha256sum -c GeoLite2-Country.tar.gz.sha256.txt
104+
sha256sum -c GeoLite2-City.tar.gz.sha256.txt
105+
tar -xf GeoLite2-Country.tar.gz
106+
tar -xf GeoLite2-City.tar.gz
107+
rm GeoLite2-Country.tar.gz GeoLite2-Country.tar.gz.sha256.txt GeoLite2-City.tar.gz GeoLite2-City.tar.gz.sha256.txt
108+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
109+
chmod +x test-init.sh
110+
./test-init.sh
111+
chmod +x run-tests.sh
112+
113+
- name: Run Plugin activation tests
114+
run: |
115+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
116+
./run-tests.sh ci "./__tests__/1-activate-plugin.js"
117+
118+
- name: Configure CrowdSec and Wordpress bouncer plugin
119+
run: |
120+
ddev crowdsec-config
121+
122+
- name: Run Live mode remediation tests
123+
run: |
124+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
125+
./run-tests.sh ci "./__tests__/2-live-mode-remediations.js ./__tests__/3-live-mode-more.js"
126+
127+
- name: Run Live mode cache tests
128+
run: |
129+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
130+
./run-tests.sh ci "./__tests__/4-live-mode-cache.js"
131+
132+
- name: Prepare cron usage
133+
run: |
134+
sed -i 's/fastcgi_finish_request/\/\/fastcgi_finish_request/g' wp-cron.php
135+
136+
- name: Run Stream mode tests
137+
run: |
138+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
139+
./run-tests.sh ci "./__tests__/5-stream-mode.js"
140+
141+
- name: Run Redis tests
142+
run: |
143+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
144+
./run-tests.sh ci "./__tests__/6-redis.js"
145+
146+
- name: Run Memcached tests
147+
run: |
148+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
149+
./run-tests.sh ci "./__tests__/7-memcached.js"
150+
151+
- name: Run Geolocation tests
152+
run: |
153+
cd ${{ github.workspace }}/my-own-modules/${{ env.EXTENSION_PATH }}/tests/e2e-ddev/__scripts__
154+
./run-tests.sh ci "./__tests__/8-geolocation.js"

inc/admin/advanced-settings.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ function convertInlineIpRangesToComparableIpBounds(string $inlineIpRanges): arra
282282
'<br><strong>Comma (,)</strong> separated ips or ips ranges. Example: 1.2.3.4/24, 2.3.4.5, 3.4.5.6/27.<br><br>Some common CDN IP list: <a href="https://www.cloudflare.com/fr-fr/ips/" target="_blank">Cloudflare</a>, <a href="https://api.fastly.com/public-ip-list" target="_blank">Fastly</a>',
283283
'fill the IPs or IPs ranges here...', '');
284284

285-
285+
// Field "crowdsec_hide_mentions"
286+
addFieldCheckbox('crowdsec_hide_mentions', 'Hide CrowdSec mentions', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_remediations', function () {}, function () {}, '
287+
<p>Enable if you want to hide CrowdSec mentions on the Ban and Captcha pages</p>');
286288

287289
/***************************
288290
** Section "Geolocation" **

inc/admin/init.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,42 @@ function testBouncerConnexionInAdminPage($ip)
125125
}
126126
}
127127

128+
function testGeolocationInAdminPage($ip)
129+
{
130+
try {
131+
$settings = getDatabaseSettings();
132+
$bouncer = getBouncerInstance($settings);
133+
$geolocationConfig = $settings['geolocation'];
134+
$apiCache = $bouncer->getApiCache();
135+
$geolocation = new CrowdSecBouncer\Geolocation();
136+
$countryResult = $geolocation->getCountryResult($geolocationConfig, $ip, $apiCache);
137+
if (!empty($countryResult['country'])) {
138+
$countryMessage = $countryResult['country'];
139+
} elseif (!empty($countryResult['not_found'])) {
140+
$countryMessage = $countryResult['not_found'];
141+
} elseif (!empty($countryResult['error'])) {
142+
$countryMessage = $countryResult['error'];
143+
}
144+
else{
145+
$countryMessage = __('Something went wrong.');
146+
}
147+
148+
149+
$message = __("Geolocation has been tested for IP: $ip. <br>Result is: $countryMessage");
150+
151+
AdminNotice::displaySuccess($message);
152+
} catch (BouncerException $e) {
153+
getCrowdSecLoggerInstance()->error('', [
154+
'type' => 'WP_EXCEPTION_WHILE_TESTING_GEOLOCATION',
155+
'message' => $e->getMessage(),
156+
'code' => $e->getCode(),
157+
'file' => $e->getFile(),
158+
'line' => $e->getLine(),
159+
]);
160+
AdminNotice::displayError('Technical error while testing geolocation: '.$e->getMessage());
161+
}
162+
}
163+
128164
// ACTIONS
129165
add_action('admin_post_crowdsec_clear_cache', function () {
130166
if (
@@ -164,6 +200,17 @@ function testBouncerConnexionInAdminPage($ip)
164200
exit(0);
165201
});
166202

203+
add_action('admin_post_crowdsec_test_geolocation', function () {
204+
if (
205+
!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'crowdsec_test_geolocation')) {
206+
die('This link expired.');
207+
}
208+
$ip = isset($_POST['crowdsec_test_geolocation_ip']) ? $_POST['crowdsec_test_geolocation_ip'] : $_SERVER['REMOTE_ADDR'];
209+
testGeolocationInAdminPage($ip);
210+
header("Location: {$_SERVER['HTTP_REFERER']}");
211+
exit(0);
212+
});
213+
167214
// THEME
168215
add_action('admin_enqueue_scripts', function () {
169216
// enqueue all our scripts

0 commit comments

Comments
 (0)