Skip to content

Commit ea04070

Browse files
committed
refacto watcher client
1 parent c820d37 commit ea04070

File tree

5 files changed

+126
-150
lines changed

5 files changed

+126
-150
lines changed

src/RestClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function request(
9898
'type' => 'HTTP CALL',
9999
'method' => $method,
100100
'uri' => $this->baseUri.$endpoint,
101+
'content' => 'POST' === $method ? $config['http']['content'] : null,
101102
]);
102103

103104
$response = file_get_contents($this->baseUri.$endpoint, false, $context);

tests/TestHelpers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TestHelpers
1616
const BAD_IP = '1.2.3.4';
1717
const CLEAN_IP = '2.3.4.5';
1818
const NEWLY_BAD_IP = '3.4.5.6';
19+
const IP_RANGE = '30';
1920

2021
const FS_CACHE_ADAPTER_DIR = __DIR__.'/../var/fs.cache';
2122
const PHP_FILES_CACHE_ADAPTER_DIR = __DIR__.'/../var/phpFiles.cache';

tests/WatcherClient.php

Lines changed: 124 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public function configure(): void
4646
$this->logger->info('Watcher client initialized');
4747
}
4848

49-
/** Set the initial watcher state using the alert_base_state.json file */
49+
/** Set the initial watcher state */
5050
public function setInitialState(): void
5151
{
5252
$this->logger->info('Set initial state');
5353
$this->deleteAllDecisions();
5454
$this->addBaseDecisions();
5555
}
5656

57-
/** Set the initial watcher state using the alert_updated_state.json file */
57+
/** Set the initial watcher state */
5858
public function setSecondState(): void
5959
{
6060
$this->logger->info('Set second state');
@@ -98,50 +98,148 @@ private function deleteAllDecisions(): void
9898

9999
private function addBaseDecisions(): void
100100
{
101-
/** @var string */
102-
$jsonString = file_get_contents(__DIR__.'/data/alerts_base_state.json');
103-
$data = json_decode($jsonString, true);
104-
105101
$now = new DateTime();
106-
$stopAt = (clone $now)->modify('+1 day')->format('Y-m-d\TH:i:s.000\Z');
102+
$stopAt12h = (clone $now)->modify('+12 hours')->format('Y-m-d\TH:i:s.000\Z');
103+
$stopAt24h = (clone $now)->modify('+24 hours')->format('Y-m-d\TH:i:s.000\Z');
107104
$startAt = $now->format('Y-m-d\TH:i:s.000\Z');
108105

109-
$ipCaptcha12h = $data[0];
110-
$ipCaptcha12h['start_at'] = $startAt;
111-
$ipCaptcha12h['stop_at'] = $stopAt;
106+
$ipCaptcha12h = [
107+
'capacity' => 0,
108+
'decisions' => [
109+
[
110+
'duration' => '12h',
111+
'origin' => 'cscli',
112+
'scenario' => 'captcha single IP '.TestHelpers::BAD_IP.' for 12h for PHPUnit tests',
113+
'scope' => 'Ip',
114+
'type' => 'captcha',
115+
'value' => TestHelpers::BAD_IP,
116+
],
117+
],
118+
'events' => [
119+
],
120+
'events_count' => 1,
121+
'labels' => null,
122+
'leakspeed' => '0',
123+
'message' => 'setup for PHPUnit tests',
124+
'scenario' => 'setup for PHPUnit tests',
125+
'scenario_hash' => '',
126+
'scenario_version' => '',
127+
'simulated' => false,
128+
'source' => [
129+
'scope' => 'Ip',
130+
'value' => TestHelpers::BAD_IP,
131+
],
132+
'start_at' => $startAt,
133+
'stop_at' => $stopAt12h,
134+
];
112135
$result = $this->request('/v1/alerts', null, [$ipCaptcha12h], 'POST');
113136
$this->logger->info('Decision '.$result[0].' added: '.$ipCaptcha12h['decisions'][0]['scenario'].'');
114137

115-
$rangeBan24h = $data[1];
116-
$rangeBan24h['start_at'] = $startAt;
117-
$rangeBan24h['stop_at'] = $stopAt;
138+
$rangeBan24h = [
139+
'capacity' => 0,
140+
'decisions' => [
141+
[
142+
'duration' => '24h',
143+
'origin' => 'cscli',
144+
'scenario' => 'ban range '.TestHelpers::BAD_IP.'/'.TestHelpers::IP_RANGE.' for 24h for PHPUnit tests',
145+
'scope' => 'Range',
146+
'type' => 'ban',
147+
'value' => TestHelpers::BAD_IP.'/'.TestHelpers::IP_RANGE,
148+
],
149+
],
150+
'events' => [
151+
],
152+
'events_count' => 1,
153+
'labels' => null,
154+
'leakspeed' => '0',
155+
'message' => 'setup for PHPUnit tests',
156+
'scenario' => 'setup for PHPUnit tests',
157+
'scenario_hash' => '',
158+
'scenario_version' => '',
159+
'simulated' => false,
160+
'source' => [
161+
'scope' => 'Range',
162+
'value' => TestHelpers::BAD_IP.'/'.TestHelpers::IP_RANGE,
163+
],
164+
'start_at' => $startAt,
165+
'stop_at' => $stopAt24h,
166+
];
118167
$result = $this->request('/v1/alerts', null, [$rangeBan24h], 'POST');
119168
$this->logger->info('Decision '.$result[0].' added: '.$rangeBan24h['decisions'][0]['scenario'].'');
120169
}
121170

122171
/**
123-
* Add new decisions (captcha 3.4.5.6 for 36h + ban 3.4.5.6/30 for 48h).
172+
* Add new decisions (captcha TestHelpers::NEWLY_BAD_IP for 36h + ban TestHelpers::NEWLY_BAD_IP/TestHelpers::IP_RANGE for 48h).
124173
*/
125174
public function addNewDecisions(): void
126175
{
127-
/** @var string */
128-
$jsonString = file_get_contents(__DIR__.'/data/alerts_updated_state.json');
129-
$data = json_decode($jsonString, true);
130-
131176
$now = new DateTime();
132-
$stopAt = (clone $now)->modify('+1 day')->format('Y-m-d\TH:i:s.000\Z');
177+
$stopAt48h = (clone $now)->modify('+48 hours')->format('Y-m-d\TH:i:s.000\Z');
178+
$stopAt36h = (clone $now)->modify('+36 hours')->format('Y-m-d\TH:i:s.000\Z');
133179
$startAt = $now->format('Y-m-d\TH:i:s.000\Z');
134180

135-
$ipBan36h = $data[0];
136-
$ipBan36h['start_at'] = $startAt;
137-
$ipBan36h['stop_at'] = $stopAt;
181+
$ipBan36h = [
182+
'capacity' => 0,
183+
'decisions' => [
184+
0 => [
185+
'duration' => '36h',
186+
'origin' => 'cscli',
187+
'scenario' => 'ban single IP '.TestHelpers::NEWLY_BAD_IP.' for 36h for PHPUnit tests',
188+
'scope' => 'Ip',
189+
'type' => 'ban',
190+
'value' => TestHelpers::NEWLY_BAD_IP,
191+
],
192+
],
193+
'events' => [
194+
],
195+
'events_count' => 1,
196+
'labels' => null,
197+
'leakspeed' => '0',
198+
'message' => 'updated state for PHPUnit tests',
199+
'scenario' => 'updated state for PHPUnit tests',
200+
'scenario_hash' => '',
201+
'scenario_version' => '',
202+
'simulated' => false,
203+
'source' => [
204+
'scope' => 'Ip',
205+
'value' => TestHelpers::NEWLY_BAD_IP,
206+
],
207+
'start_at' => $startAt,
208+
'stop_at' => $stopAt36h,
209+
];
138210
$result = $this->request('/v1/alerts', null, [$ipBan36h], 'POST');
139211
$this->logger->info('Decision '.$result[0].' added: '.$ipBan36h['decisions'][0]['scenario'].'');
140212

141-
$rangeCaptcha48h = $data[1];
142-
$rangeCaptcha48h['start_at'] = $startAt;
143-
$rangeCaptcha48h['stop_at'] = $stopAt;
213+
$rangeCaptcha48h = [
214+
'capacity' => 0,
215+
'decisions' => [
216+
0 => [
217+
'duration' => '48h',
218+
'origin' => 'cscli',
219+
'scenario' => 'captcha range '.TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE.' for 48h for PHPUnit tests',
220+
'scope' => 'Range',
221+
'type' => 'captcha',
222+
'value' => TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE,
223+
],
224+
],
225+
'events' => [
226+
],
227+
'events_count' => 1,
228+
'labels' => null,
229+
'leakspeed' => '0',
230+
'message' => 'setup for PHPUnit tests',
231+
'scenario' => 'setup for PHPUnit tests',
232+
'scenario_hash' => '',
233+
'scenario_version' => '',
234+
'simulated' => false,
235+
'source' => [
236+
'scope' => 'Range',
237+
'value' => TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE,
238+
],
239+
'start_at' => $startAt,
240+
'stop_at' => $stopAt48h,
241+
];
144242
$result = $this->request('/v1/alerts', null, [$rangeCaptcha48h], 'POST');
145-
$this->logger->info('Decision '.$result[0].' added: '.$rangeCaptcha48h['decisions'][0]['scenario'].'');
243+
$this->logger->info('Decision '.$result[0].' added: '.$rangeCaptcha48h['decisions'][0]['scenario']);
146244
}
147245
}

tests/data/alerts_base_state.json

Lines changed: 0 additions & 62 deletions
This file was deleted.

tests/data/alerts_updated_state.json

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)