Skip to content

Commit 65cd64f

Browse files
author
Jonathan Visser
committed
Add support for creating whitelist entries (POST /v2/whitelist/{app}/)
1 parent 19a360e commit 65cd64f

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/Service/Whitelist.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,26 @@ public function getList(string $app, array $params = []): array
3333

3434
return $this->client->getJsonFromResponse($response);
3535
}
36+
37+
/**
38+
* Add a whitelist entry for the Hypernode.
39+
*
40+
* @param string $app Name of the Hypernode
41+
* @param array $data Whitelist entry data, requires at least the key `ip`.
42+
* Optional keys are `type` (database, ftp, phpmyadmin, ssh or waf,
43+
* defaults to database) and `description`.
44+
* @return array The created whitelist entry
45+
* @throws HypernodeApiClientException
46+
* @throws HypernodeApiServerException
47+
*/
48+
public function create(string $app, array $data): array
49+
{
50+
$url = sprintf(self::V2_WHITELIST_URL, $app);
51+
52+
$response = $this->client->api->post($url, [], json_encode($data));
53+
54+
$this->client->maybeThrowApiExceptions($response);
55+
56+
return $this->client->getJsonFromResponse($response);
57+
}
3658
}

tests/unit/Service/WhitelistTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,54 @@ public function testGetListRaisesServerExceptions()
7272

7373
$this->client->whitelist->getList('johndoe');
7474
}
75+
76+
public function testCreate()
77+
{
78+
$entryData = [
79+
'ip' => '1.2.3.4',
80+
'type' => 'waf',
81+
'description' => 'Office IP',
82+
];
83+
$createdEntry = array_merge($entryData, [
84+
'id' => 1234,
85+
'created' => '2026-07-15T12:00:00Z',
86+
'app' => 'johndoe',
87+
]);
88+
$this->responses->append(
89+
new Response(200, [], json_encode($createdEntry)),
90+
);
91+
92+
$result = $this->client->whitelist->create('johndoe', $entryData);
93+
94+
$request = $this->responses->getLastRequest();
95+
$this->assertEquals('POST', $request->getMethod());
96+
$this->assertEquals('/v2/whitelist/johndoe/', $request->getUri());
97+
$this->assertJson((string)$request->getBody());
98+
$this->assertEquals($entryData, json_decode((string)$request->getBody(), true));
99+
$this->assertEquals($createdEntry, $result);
100+
}
101+
102+
public function testCreateRaisesClientExceptions()
103+
{
104+
$badRequestResponse = new Response(400, [], json_encode([
105+
'non_field_errors' => ['Your request was invalid.']
106+
]));
107+
$this->responses->append($badRequestResponse);
108+
109+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
110+
111+
$this->client->whitelist->create('johndoe', ['ip' => '1.2.3.4']);
112+
}
113+
114+
public function testCreateRaisesServerExceptions()
115+
{
116+
$badRequestResponse = new Response(500, [], json_encode([
117+
'non_field_errors' => ['Something went wrong processing your request.']
118+
]));
119+
$this->responses->append($badRequestResponse);
120+
121+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
122+
123+
$this->client->whitelist->create('johndoe', ['ip' => '1.2.3.4']);
124+
}
75125
}

0 commit comments

Comments
 (0)