Skip to content

Commit 07dd43a

Browse files
author
Jonathan Visser
committed
Add support for deleting whitelist entries (DELETE /v2/whitelist/{app}/)
1 parent 65cd64f commit 07dd43a

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/Service/Whitelist.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,23 @@ public function create(string $app, array $data): array
5555

5656
return $this->client->getJsonFromResponse($response);
5757
}
58+
59+
/**
60+
* Remove a whitelist entry for the Hypernode.
61+
*
62+
* @param string $app Name of the Hypernode
63+
* @param array $data Data identifying the entry to remove, e.g.
64+
* ['ip' => '1.2.3.4', 'type' => 'waf']
65+
* @return void
66+
* @throws HypernodeApiClientException
67+
* @throws HypernodeApiServerException
68+
*/
69+
public function delete(string $app, array $data): void
70+
{
71+
$url = sprintf(self::V2_WHITELIST_URL, $app);
72+
73+
$response = $this->client->api->delete($url, [], json_encode($data));
74+
75+
$this->client->maybeThrowApiExceptions($response);
76+
}
5877
}

tests/unit/Service/WhitelistTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,44 @@ public function testCreateRaisesServerExceptions()
122122

123123
$this->client->whitelist->create('johndoe', ['ip' => '1.2.3.4']);
124124
}
125+
126+
public function testDelete()
127+
{
128+
$this->responses->append(
129+
new Response(204, [], null),
130+
);
131+
132+
$entryData = ['ip' => '1.2.3.4', 'type' => 'waf'];
133+
$this->client->whitelist->delete('johndoe', $entryData);
134+
135+
$request = $this->responses->getLastRequest();
136+
$this->assertEquals('DELETE', $request->getMethod());
137+
$this->assertEquals('/v2/whitelist/johndoe/', $request->getUri());
138+
$this->assertJson((string)$request->getBody());
139+
$this->assertEquals($entryData, json_decode((string)$request->getBody(), true));
140+
}
141+
142+
public function testDeleteRaisesClientExceptions()
143+
{
144+
$badRequestResponse = new Response(400, [], json_encode([
145+
'non_field_errors' => ['Your request was invalid.']
146+
]));
147+
$this->responses->append($badRequestResponse);
148+
149+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
150+
151+
$this->client->whitelist->delete('johndoe', ['ip' => '1.2.3.4']);
152+
}
153+
154+
public function testDeleteRaisesServerExceptions()
155+
{
156+
$badRequestResponse = new Response(500, [], json_encode([
157+
'non_field_errors' => ['Something went wrong processing your request.']
158+
]));
159+
$this->responses->append($badRequestResponse);
160+
161+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
162+
163+
$this->client->whitelist->delete('johndoe', ['ip' => '1.2.3.4']);
164+
}
125165
}

0 commit comments

Comments
 (0)