|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypernode\Api\Service; |
| 6 | + |
| 7 | +use GuzzleHttp\Psr7\Response; |
| 8 | +use Hypernode\Api\Exception\HypernodeApiClientException; |
| 9 | +use Hypernode\Api\Exception\HypernodeApiServerException; |
| 10 | +use Hypernode\Api\HypernodeClientTestCase; |
| 11 | + |
| 12 | +class WhitelistTest extends HypernodeClientTestCase |
| 13 | +{ |
| 14 | + public function testGetList() |
| 15 | + { |
| 16 | + $entries = [ |
| 17 | + [ |
| 18 | + 'id' => 1234, |
| 19 | + 'created' => '2026-07-14T14:49:06Z', |
| 20 | + 'type' => 'waf', |
| 21 | + 'description' => 'Office IP', |
| 22 | + 'ip' => '1.2.3.4', |
| 23 | + 'app' => 'johndoe', |
| 24 | + ], |
| 25 | + ]; |
| 26 | + $this->responses->append( |
| 27 | + new Response(200, [], json_encode($entries)), |
| 28 | + ); |
| 29 | + |
| 30 | + $result = $this->client->whitelist->getList('johndoe'); |
| 31 | + |
| 32 | + $request = $this->responses->getLastRequest(); |
| 33 | + $this->assertEquals('GET', $request->getMethod()); |
| 34 | + $this->assertEquals('/v2/whitelist/johndoe/', $request->getUri()); |
| 35 | + $this->assertEquals($entries, $result); |
| 36 | + } |
| 37 | + |
| 38 | + public function testGetListAcceptsParams() |
| 39 | + { |
| 40 | + $this->responses->append( |
| 41 | + new Response(200, [], json_encode([])), |
| 42 | + ); |
| 43 | + |
| 44 | + $result = $this->client->whitelist->getList('johndoe', ['type' => 'waf']); |
| 45 | + |
| 46 | + $request = $this->responses->getLastRequest(); |
| 47 | + $this->assertEquals('GET', $request->getMethod()); |
| 48 | + $this->assertEquals('/v2/whitelist/johndoe/?type=waf', $request->getUri()); |
| 49 | + $this->assertEquals([], $result); |
| 50 | + } |
| 51 | + |
| 52 | + public function testGetListRaisesClientExceptions() |
| 53 | + { |
| 54 | + $badRequestResponse = new Response(400, [], json_encode([ |
| 55 | + 'non_field_errors' => ['Your request was invalid.'] |
| 56 | + ])); |
| 57 | + $this->responses->append($badRequestResponse); |
| 58 | + |
| 59 | + $this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse)); |
| 60 | + |
| 61 | + $this->client->whitelist->getList('johndoe'); |
| 62 | + } |
| 63 | + |
| 64 | + public function testGetListRaisesServerExceptions() |
| 65 | + { |
| 66 | + $badRequestResponse = new Response(500, [], json_encode([ |
| 67 | + 'non_field_errors' => ['Something went wrong processing your request.'] |
| 68 | + ])); |
| 69 | + $this->responses->append($badRequestResponse); |
| 70 | + |
| 71 | + $this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse)); |
| 72 | + |
| 73 | + $this->client->whitelist->getList('johndoe'); |
| 74 | + } |
| 75 | +} |
0 commit comments