Skip to content

Commit f0c8606

Browse files
committed
refacto to easily add more decisions
1 parent d8d38db commit f0c8606

File tree

1 file changed

+20
-124
lines changed

1 file changed

+20
-124
lines changed

tests/WatcherClient.php

Lines changed: 20 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,19 @@ public function setInitialState(): void
5151
{
5252
$this->logger->info('Set initial state');
5353
$this->deleteAllDecisions();
54-
$this->addBaseDecisions();
54+
$now = new DateTime();
55+
$this->addDecision($now, '12h', '+12 hours', TestHelpers::BAD_IP, 'captcha');
56+
$this->addDecision($now, '24h', '+24 hours', TestHelpers::BAD_IP.'/'.TestHelpers::IP_RANGE, 'ban');
5557
}
5658

5759
/** Set the initial watcher state */
5860
public function setSecondState(): void
5961
{
6062
$this->logger->info('Set second state');
6163
$this->deleteAllDecisions();
62-
$this->addNewDecisions();
64+
$now = new DateTime();
65+
$this->addDecision($now, '36h', '+36 hours', TestHelpers::NEWLY_BAD_IP, 'ban');
66+
$this->addDecision($now, '48h', '+48 hours', TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE, 'captcha');
6367
}
6468

6569
/**
@@ -96,130 +100,22 @@ private function deleteAllDecisions(): void
96100
$this->request('/v1/decisions', null, null, 'DELETE');
97101
}
98102

99-
private function addBaseDecisions(): void
103+
private function addDecision(\DateTime $now, string $durationString, string $dateTimeDurationString, string $ipOrRange, string $type)
100104
{
101-
$now = new DateTime();
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');
105+
$isRange = (2 === count(explode('/', $ipOrRange)));
106+
$stopAt = (clone $now)->modify($dateTimeDurationString)->format('Y-m-d\TH:i:s.000\Z');
104107
$startAt = $now->format('Y-m-d\TH:i:s.000\Z');
105108

106-
$ipCaptcha12h = [
109+
$body = [
107110
'capacity' => 0,
108111
'decisions' => [
109112
[
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-
];
135-
$result = $this->request('/v1/alerts', null, [$ipCaptcha12h], 'POST');
136-
$this->logger->info('Decision '.$result[0].' added: '.$ipCaptcha12h['decisions'][0]['scenario'].'');
137-
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-
];
167-
$result = $this->request('/v1/alerts', null, [$rangeBan24h], 'POST');
168-
$this->logger->info('Decision '.$result[0].' added: '.$rangeBan24h['decisions'][0]['scenario'].'');
169-
}
170-
171-
/**
172-
* Add new decisions (captcha TestHelpers::NEWLY_BAD_IP for 36h + ban TestHelpers::NEWLY_BAD_IP/TestHelpers::IP_RANGE for 48h).
173-
*/
174-
public function addNewDecisions(): void
175-
{
176-
$now = new DateTime();
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');
179-
$startAt = $now->format('Y-m-d\TH:i:s.000\Z');
180-
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-
];
210-
$result = $this->request('/v1/alerts', null, [$ipBan36h], 'POST');
211-
$this->logger->info('Decision '.$result[0].' added: '.$ipBan36h['decisions'][0]['scenario'].'');
212-
213-
$rangeCaptcha48h = [
214-
'capacity' => 0,
215-
'decisions' => [
216-
0 => [
217-
'duration' => '48h',
113+
'duration' => $durationString,
218114
'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,
115+
'scenario' => 'captcha for '.$ipOrRange.' for '.$durationString.' for PHPUnit tests',
116+
'scope' => $isRange ? 'Range' : 'Ip',
117+
'type' => $type,
118+
'value' => $ipOrRange,
223119
],
224120
],
225121
'events' => [
@@ -233,13 +129,13 @@ public function addNewDecisions(): void
233129
'scenario_version' => '',
234130
'simulated' => false,
235131
'source' => [
236-
'scope' => 'Range',
237-
'value' => TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE,
132+
'scope' => $isRange ? 'Range' : 'Ip',
133+
'value' => $ipOrRange,
238134
],
239135
'start_at' => $startAt,
240-
'stop_at' => $stopAt48h,
136+
'stop_at' => $stopAt,
241137
];
242-
$result = $this->request('/v1/alerts', null, [$rangeCaptcha48h], 'POST');
243-
$this->logger->info('Decision '.$result[0].' added: '.$rangeCaptcha48h['decisions'][0]['scenario']);
138+
$result = $this->request('/v1/alerts', null, [$body], 'POST');
139+
$this->logger->info('Decision '.$result[0].' added: '.$body['decisions'][0]['scenario'].'');
244140
}
245141
}

0 commit comments

Comments
 (0)