diff --git a/composer.json b/composer.json index 4ab5323..8e29892 100644 --- a/composer.json +++ b/composer.json @@ -2,17 +2,17 @@ "name": "mittwald/vault-php", "type": "library", "license": "MIT", - "version": "0.0.1", "homepage": "https://www.mittwald.de/", "description": "PHP library for Vault", "require": { "ext-json": "*", - "guzzlehttp/psr7": ">=1.6", + "guzzlehttp/psr7": "^1.6", "php": ">=5.6", "php-http/httplug": ">=1.1.0" }, "suggest": { - "php-http/curl-client": "CURL Client Adapter" + "php-http/curl-client": "CURL Client Adapter", + "tuupola/http-factory": "HTTP Factory" }, "require-dev": { "phpunit/phpunit": ">=5.0.0" diff --git a/examples/BulkOperations.php b/examples/BulkOperations.php index 7a4d7bb..1c31d6c 100644 --- a/examples/BulkOperations.php +++ b/examples/BulkOperations.php @@ -10,6 +10,7 @@ use VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData\EncryptData; use VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData\EncryptDataBulkRequest; use VaultPHP\SecretEngines\Engines\Transit\Request\UpdateKeyConfigRequest; +use VaultPHP\SecretEngines\Engines\Transit\Response\EncryptDataResponse; use VaultPHP\SecretEngines\Engines\Transit\Transit; use VaultPHP\SecretEngines\Engines\Transit\EncryptionType; use VaultPHP\VaultClient; @@ -52,11 +53,15 @@ ]); $encryptBulkResponse = $transitApi->encryptDataBulk($encryptRequest); + // indicates that one or more bulk results contains errors + var_dump($encryptBulkResponse->hasErrors()); + + /** @var EncryptDataResponse $bulkResult */ foreach($encryptBulkResponse as $bulkResult) { // BULK REQUEST WON'T THROW INVALID DATA EXCEPTIONS // SO YOU ARE RESPONSABLE TO CHECK IF EVERY BULK WAS // SUCCESSFULLY PROCESSED - if (!$bulkResult->getBasicMetaResponse()->hasErrors()) { + if (!$bulkResult->getMetaData()->hasErrors()) { var_dump($bulkResult->getCiphertext()); } } diff --git a/src/VaultPHP/Authentication/Provider/Kubernetes.php b/src/VaultPHP/Authentication/Provider/Kubernetes.php index ecbf5a1..53c1fd4 100644 --- a/src/VaultPHP/Authentication/Provider/Kubernetes.php +++ b/src/VaultPHP/Authentication/Provider/Kubernetes.php @@ -57,7 +57,7 @@ public function authenticate() false ); - if ($auth = $response->getBasicMetaResponse()->getAuth()) { + if ($auth = $response->getMetaData()->getAuth()) { return new AuthenticationMetaData($auth); } diff --git a/src/VaultPHP/Authentication/Provider/UserPassword.php b/src/VaultPHP/Authentication/Provider/UserPassword.php index 4b4f056..d5aebb1 100644 --- a/src/VaultPHP/Authentication/Provider/UserPassword.php +++ b/src/VaultPHP/Authentication/Provider/UserPassword.php @@ -56,7 +56,7 @@ public function authenticate() false ); - if ($auth = $response->getBasicMetaResponse()->getAuth()) { + if ($auth = $response->getMetaData()->getAuth()) { return new AuthenticationMetaData($auth); } diff --git a/src/VaultPHP/Exceptions/VaultResponseException.php b/src/VaultPHP/Exceptions/VaultResponseException.php index a7131ab..0ecf6f2 100644 --- a/src/VaultPHP/Exceptions/VaultResponseException.php +++ b/src/VaultPHP/Exceptions/VaultResponseException.php @@ -30,7 +30,7 @@ public function __construct(ResponseInterface $response, RequestInterface $reque $this->request = $request; $parsedResponse = EndpointResponse::fromResponse($response); - $returnedErrors = $parsedResponse->getBasicMetaResponse()->getErrors(); + $returnedErrors = $parsedResponse->getMetaData()->getErrors(); $errors = implode(', ', is_array($returnedErrors) ? $returnedErrors : []); parent::__construct($errors, $response->getStatusCode(), $prevException); diff --git a/src/VaultPHP/Response/BulkEndpointResponse.php b/src/VaultPHP/Response/BulkEndpointResponse.php new file mode 100644 index 0000000..b285919 --- /dev/null +++ b/src/VaultPHP/Response/BulkEndpointResponse.php @@ -0,0 +1,134 @@ +getMetaData()->hasErrors(); + + if (!$errorOccurred) { + /** @var EndpointResponse $batchResult */ + foreach ($this as $batchResult) { + $errorOccurred = $batchResult->getMetaData()->hasErrors(); + if ($errorOccurred) { + return true; + } + } + } + + return $errorOccurred; + } + + /** + * @return array + */ + public function getBatchResults() { + return $this->batch_results; + } + + /** + * @return mixed + */ + public function current() + { + return $this->batch_results[$this->iteratorPosition]; + } + + /** + * @return void + */ + public function next() + { + ++$this->iteratorPosition; + } + + /** + * @return integer + */ + public function key() + { + return (int) $this->iteratorPosition; + } + + /** + * @return bool + */ + public function valid() + { + return isset($this->batch_results[$this->iteratorPosition]); + } + + /** + * @return void + */ + public function rewind() + { + $this->iteratorPosition = 0; + } + + /** + * @return int + */ + public function count() + { + return count($this->batch_results); + } + + /** + * @param integer $offset + * @return bool + */ + public function offsetExists($offset) + { + return isset($this->batch_results[$offset]); + } + + /** + * @param integer $offset + * @return mixed + */ + public function offsetGet($offset) + { + return $this->batch_results[$offset]; + } + + /** + * @param mixed $offset + * @param mixed $value + * @throws VaultException + */ + public function offsetSet($offset, $value) + { + throw new VaultException('readonly'); + } + + /** + * @param mixed $offset + * @throws VaultException + */ + public function offsetUnset($offset) + { + throw new VaultException('readonly'); + } +} diff --git a/src/VaultPHP/Response/EndpointResponse.php b/src/VaultPHP/Response/EndpointResponse.php index 5e7c168..94fb98b 100644 --- a/src/VaultPHP/Response/EndpointResponse.php +++ b/src/VaultPHP/Response/EndpointResponse.php @@ -10,16 +10,17 @@ */ class EndpointResponse implements EndpointResponseInterface { - /** @var BasicMetaResponse */ - private $basicMetaResponse; + /** @var MetaData */ + protected $metaData; /** * EndpointResponse constructor. * @param array|object $data + * @param array $meta */ - public function __construct($data = []) + public function __construct($data = [], $meta = []) { - $this->basicMetaResponse = new BasicMetaResponse(); + $this->metaData = new MetaData($meta); $this->populateData($data); } @@ -27,7 +28,7 @@ public function __construct($data = []) * @param $response * @return array */ - private static function getResponseContent(ResponseInterface $response) { + protected static function getResponseContent(ResponseInterface $response) { $responseBody = $response->getBody(); $responseBody->rewind(); $responseBodyContents = $responseBody->getContents(); @@ -41,7 +42,7 @@ private static function getResponseContent(ResponseInterface $response) { * @param ResponseInterface $response * @return static */ - static function fromResponse(ResponseInterface $response) + public static function fromResponse(ResponseInterface $response) { $metaData = static::getResponseContent($response); @@ -49,46 +50,32 @@ static function fromResponse(ResponseInterface $response) $domainData = isset($metaData['data']) ? $metaData['data'] : []; unset($metaData['data']); - $responseDTO = new static($domainData); - $responseDTO->basicMetaResponse = new BasicMetaResponse($metaData); - - return $responseDTO; + return new static($domainData, $metaData); } /** * @param ResponseInterface $response - * @return static[] + * @return BulkEndpointResponse */ static function fromBulkResponse(ResponseInterface $response) { - $resultArray = []; $metaData = static::getResponseContent($response); - /** @var object $domainData */ + /** @var object|array $domainData */ $domainData = isset($metaData['data']) ? $metaData['data'] : []; unset($metaData['data']); - if ($domainData && is_array($domainData->batch_results)) { + $responseDTO = new BulkEndpointResponse($domainData, $metaData); + $responseDTO->batch_results = array_map(function($batchResult) { /** @var object $batchResult */ - foreach($domainData->batch_results as $batchResult) { - /** @var array $batchMetaData */ - $batchMetaData = $metaData; - - if (isset($batchResult->error)) { - /** @var array $currentErrors */ - $currentErrors = isset($metaData['errors']) ? $metaData['errors'] : []; - array_push($currentErrors, $batchResult->error); - $batchMetaData['errors'] = $currentErrors; - } - - $responseDTO = new static($batchResult); - $responseDTO->basicMetaResponse = new BasicMetaResponse($batchMetaData); - $resultArray[] = $responseDTO; - } - } + $errors = isset($batchResult->error) && $batchResult->error ? explode(', ', (string) $batchResult->error) : []; + return new static($batchResult, [ + 'errors' => $errors, + ]); + }, $responseDTO->batch_results); - return $resultArray; + return $responseDTO; } /** @@ -107,10 +94,18 @@ private function populateData($data) } /** - * @return BasicMetaResponse + * @return MetaData + */ + public function getMetaData() + { + return $this->metaData; + } + + /** + * @return bool */ - public function getBasicMetaResponse() + public function hasErrors() { - return $this->basicMetaResponse; + return (bool) $this->getMetaData()->hasErrors(); } } diff --git a/src/VaultPHP/Response/EndpointResponseInterface.php b/src/VaultPHP/Response/EndpointResponseInterface.php index 25073cf..dbd5313 100644 --- a/src/VaultPHP/Response/EndpointResponseInterface.php +++ b/src/VaultPHP/Response/EndpointResponseInterface.php @@ -12,12 +12,24 @@ interface EndpointResponseInterface { /** * @param ResponseInterface $response - * @return mixed + * @return static */ static function fromResponse(ResponseInterface $response); /** - * @return BasicMetaResponse + * @param ResponseInterface $response + * @return BulkEndpointResponse + */ + static function fromBulkResponse(ResponseInterface $response); + + + /** + * @return MetaData + */ + public function getMetaData(); + + /** + * @return bool */ - public function getBasicMetaResponse(); + public function hasErrors(); } diff --git a/src/VaultPHP/Response/BasicMetaResponse.php b/src/VaultPHP/Response/MetaData.php similarity index 94% rename from src/VaultPHP/Response/BasicMetaResponse.php rename to src/VaultPHP/Response/MetaData.php index 8a9e697..4f95b07 100644 --- a/src/VaultPHP/Response/BasicMetaResponse.php +++ b/src/VaultPHP/Response/MetaData.php @@ -3,10 +3,10 @@ namespace VaultPHP\Response; /** - * Class BasicMetaResponse + * Class MetaData * @package VaultPHP\Response */ -class BasicMetaResponse implements BasicMetaResponseInterface +class MetaData implements MetaDataInterface { /** @var string|null */ private $request_id; @@ -33,7 +33,7 @@ class BasicMetaResponse implements BasicMetaResponseInterface private $errors; /** - * BasicMetaResponse constructor. + * MetaData constructor. * @param array|object $data */ public function __construct($data = []) diff --git a/src/VaultPHP/Response/BasicMetaResponseInterface.php b/src/VaultPHP/Response/MetaDataInterface.php similarity index 92% rename from src/VaultPHP/Response/BasicMetaResponseInterface.php rename to src/VaultPHP/Response/MetaDataInterface.php index fb67f97..135d041 100644 --- a/src/VaultPHP/Response/BasicMetaResponseInterface.php +++ b/src/VaultPHP/Response/MetaDataInterface.php @@ -3,10 +3,10 @@ namespace VaultPHP\Response; /** - * Interface BasicMetaResponseInterface + * Interface MetaDataInterface * @package VaultPHP\Response */ -interface BasicMetaResponseInterface +interface MetaDataInterface { /** * GenericEndpointResponse constructor. diff --git a/src/VaultPHP/SecretEngines/Engines/Transit/Transit.php b/src/VaultPHP/SecretEngines/Engines/Transit/Transit.php index 71df5f5..eb5f61d 100644 --- a/src/VaultPHP/SecretEngines/Engines/Transit/Transit.php +++ b/src/VaultPHP/SecretEngines/Engines/Transit/Transit.php @@ -7,6 +7,7 @@ use VaultPHP\Exceptions\VaultAuthenticationException; use VaultPHP\Exceptions\VaultException; use VaultPHP\Exceptions\VaultHttpException; +use VaultPHP\Response\BulkEndpointResponse; use VaultPHP\Response\EndpointResponse; use VaultPHP\SecretEngines\AbstractSecretEngine; use VaultPHP\SecretEngines\Engines\Transit\Request\CreateKeyRequest; @@ -66,14 +67,14 @@ public function encryptData(EncryptDataRequest $encryptDataRequest) /** * @param EncryptDataBulkRequest $encryptDataBulkRequest - * @return EncryptDataResponse[] + * @return BulkEndpointResponse * @throws InvalidDataException * @throws InvalidRouteException * @throws VaultException */ public function encryptDataBulk(EncryptDataBulkRequest $encryptDataBulkRequest) { - /** @var EncryptDataResponse[] */ + /** @var BulkEndpointResponse */ return $this->vaultClient->sendApiRequest( 'POST', sprintf('/v1/transit/encrypt/%s', urlencode($encryptDataBulkRequest->getName())), @@ -102,7 +103,7 @@ public function decryptData(DecryptDataRequest $decryptDataRequest) /** * @param DecryptDataBulkRequest $decryptDataBulkRequest - * @return EndpointResponse|EndpointResponse[] + * @return BulkEndpointResponse * @throws InvalidDataException * @throws InvalidRouteException * @throws VaultException @@ -111,7 +112,7 @@ public function decryptData(DecryptDataRequest $decryptDataRequest) */ public function decryptDataBulk(DecryptDataBulkRequest $decryptDataBulkRequest) { - /** @var DecryptDataResponse[] */ + /** @var BulkEndpointResponse */ return $this->vaultClient->sendApiRequest( 'POST', sprintf('/v1/transit/decrypt/%s', urlencode($decryptDataBulkRequest->getName())), diff --git a/src/VaultPHP/SecretEngines/Traits/ArrayExportTrait.php b/src/VaultPHP/SecretEngines/Traits/ArrayExportTrait.php index 268cf0c..6d1f104 100644 --- a/src/VaultPHP/SecretEngines/Traits/ArrayExportTrait.php +++ b/src/VaultPHP/SecretEngines/Traits/ArrayExportTrait.php @@ -27,6 +27,7 @@ private function array_map_r($callback, $input) if (is_array($data)) { $output[$key] = $this->array_map_r($callback, $data); } else { + /** @psalm-suppress MixedAssignment */ $output[$key] = $callback($data); } } @@ -40,7 +41,7 @@ private function array_map_r($callback, $input) public function toArray() { $data = get_object_vars($this); - return $this->array_map_r( + $result = $this->array_map_r( /** @psalm-suppress MissingClosureParamType */ function ($v) { if ($v instanceof ArrayExportInterface) { @@ -50,5 +51,7 @@ function ($v) { }, $data ); + + return array_filter($result); } } diff --git a/src/VaultPHP/VaultClient.php b/src/VaultPHP/VaultClient.php index 6231b98..82acb0f 100644 --- a/src/VaultPHP/VaultClient.php +++ b/src/VaultPHP/VaultClient.php @@ -97,7 +97,7 @@ private function extractPayload($data) * @param string $returnClass * @param array|ResourceRequestInterface $data * @param bool $authRequired - * @return mixed|mixed[] + * @return mixed * @throws InvalidDataException * @throws InvalidRouteException * @throws VaultAuthenticationException @@ -132,7 +132,7 @@ public function sendApiRequest($method, $endpoint, $returnClass, $data = [], $au * @param ResponseInterface $response * @param string $returnClass * @param boolean $isBulkRequest - * @return mixed|mixed[] + * @return mixed * @throws InvalidDataException * @throws InvalidRouteException * @throws VaultAuthenticationException @@ -153,7 +153,7 @@ private function parseResponse( */ if (!$isBulkRequest) { /** @psalm-suppress ArgumentTypeCoercion */ - if (!$returnClass || !method_exists($returnClass, 'fromResponse')) { + if (!method_exists($returnClass, 'fromResponse')) { throw new VaultException('Return Class declaration lacks static::fromResponse'); } @@ -161,7 +161,7 @@ private function parseResponse( $responseDataDTO = $returnClass::fromResponse($response); } else { /** @psalm-suppress ArgumentTypeCoercion */ - if (!$returnClass || !method_exists($returnClass, 'fromBulkResponse')) { + if (!method_exists($returnClass, 'fromBulkResponse')) { throw new VaultException('Return Class declaration lacks static::fromBulkResponse'); } @@ -169,7 +169,7 @@ private function parseResponse( $responseDataDTO = $returnClass::fromBulkResponse($response); } - if (!is_array($responseDataDTO) && !$responseDataDTO instanceof EndpointResponseInterface) { + if (!$responseDataDTO instanceof EndpointResponseInterface) { throw new VaultException('Result from "fromResponse/fromBulkResponse" isn\'t an instance of EndpointResponse or Array'); } @@ -184,7 +184,7 @@ private function parseResponse( } elseif ($status === 404) { // if 404 and no error this indicates no data for e.g. List // makes no sense but hey - the vault rest is a magical unicorn - if (!is_array($responseDataDTO) && !$responseDataDTO->getBasicMetaResponse()->hasErrors()) { + if (!is_array($responseDataDTO) && !$responseDataDTO->getMetaData()->hasErrors()) { return $responseDataDTO; } diff --git a/tests/VaultPHP/Mocks/EndpointResponseMock.php b/tests/VaultPHP/Mocks/EndpointResponseMock.php deleted file mode 100644 index 1c1c578..0000000 --- a/tests/VaultPHP/Mocks/EndpointResponseMock.php +++ /dev/null @@ -1,23 +0,0 @@ - [ + 'batch_results' => [ + [], + [], + [], + ], + ], + ])); + $bulkResponses = DecryptDataResponse::fromBulkResponse($response); + $this->assertInstanceOf(BulkEndpointResponse::class, $bulkResponses); + $this->assertTrue(is_array($bulkResponses->getBatchResults())); + + // test foreach + foreach ($bulkResponses as $batchResponse) { + $this->assertInstanceOf(DecryptDataResponse::class, $batchResponse); + } + + // can count + $this->assertCount(3, $bulkResponses); + + $bulkResponses->rewind(); + $this->assertTrue($bulkResponses->valid()); + + // can interact with index + $this->assertInstanceOf(DecryptDataResponse::class, $bulkResponses[2]); + + // can iterate + $this->assertSame($bulkResponses[0], $bulkResponses->current()); + + $bulkResponses->next(); + $this->assertSame($bulkResponses[1], $bulkResponses->current()); + + $bulkResponses->next(); + $this->assertSame($bulkResponses[2], $bulkResponses->current()); + + $bulkResponses->next(); + $this->assertFalse($bulkResponses->valid()); + + $this->assertEquals(3, $bulkResponses->key()); + $this->assertTrue(isset($bulkResponses[0])); + $this->assertFalse(isset($bulkResponses[3])); + } + + public function testCantWriteToArrayStyleObject() { + $this->expectException(VaultException::class); + $this->expectExceptionMessage('readonly'); + + $response = new Response(200, [], json_encode([ + 'data' => [ + 'batch_results' => [ + [], + [], + [], + ], + ], + ])); + $bulkResponses = DecryptDataResponse::fromBulkResponse($response); + $bulkResponses[1] = "foo"; + } + + public function testCantDeleteFromArrayStyleObject() { + $this->expectException(VaultException::class); + $this->expectExceptionMessage('readonly'); + + $response = new Response(200, [], json_encode([ + 'data' => [ + 'batch_results' => [ + [], + [], + [], + ], + ], + ])); + $bulkResponses = DecryptDataResponse::fromBulkResponse($response); + unset($bulkResponses[1]); + } + + public function testHasErrors() { + $response = new Response(200, [], json_encode([ + 'errors' => [], + 'data' => [ + 'batch_results' => [ + [], + ], + ], + ])); + $bulkResponses = DecryptDataResponse::fromBulkResponse($response); + $this->assertFalse($bulkResponses->hasErrors()); + + $response = new Response(200, [], json_encode([ + 'errors' => [ + 'oh no' + ], + 'data' => [ + 'batch_results' => [ + [], + ], + ], + ])); + $bulkResponses = DecryptDataResponse::fromBulkResponse($response); + $this->assertTrue($bulkResponses->hasErrors()); + + $response = new Response(200, [], json_encode([ + 'errors' => [ + ], + 'data' => [ + 'batch_results' => [ + [], + [ + 'error' => 'oh no' + ], + [], + ], + ], + ])); + $bulkResponses = DecryptDataResponse::fromBulkResponse($response); + $this->assertTrue($bulkResponses->hasErrors()); + } + + public function testGetErrors() { + $response = new Response(200, [], json_encode([ + 'errors' => [ + 'foo', + 'bar', + ], + 'data' => [ + 'batch_results' => [ + [], + ['error' => 'baz, buz'], + [], + ['error' => 'bam'], + ], + ], + ])); + $bulkResponses = DecryptDataResponse::fromBulkResponse($response); + $this->assertEquals([], $bulkResponses[0]->getMetaData()->getErrors()); + $this->assertEquals(['baz', 'buz'], $bulkResponses[1]->getMetaData()->getErrors()); + $this->assertEquals([], $bulkResponses[2]->getMetaData()->getErrors()); + $this->assertEquals(['bam'], $bulkResponses[3]->getMetaData()->getErrors()); + + $this->assertEquals(['foo', 'bar'], $bulkResponses->getMetaData()->getErrors()); + } + + public function testCanGetPopulateMetaDataFromBulkResponse() + { + $response = new Response(200, [], json_encode([ + 'errors' => [ + 'metaDataError', + 'metaDataError2', + ], + 'data' => [ + 'batch_results' => [ + [ + 'error' => 'batchError' + ], + [], + [ + 'error' => 'batchError2' + ], + ], + ], + ])); + $arrayEndpointResponse = DecryptDataResponse::fromBulkResponse($response); + $this->assertSame(3, count($arrayEndpointResponse)); + + $basicMeta = $arrayEndpointResponse->getMetaData(); + $this->assertEquals(['metaDataError', 'metaDataError2'], $basicMeta->getErrors()); + $this->assertTrue($arrayEndpointResponse->hasErrors()); + + /** @var DecryptDataResponse $batchResponse */ + foreach($arrayEndpointResponse as $batchResponse) { + $this->assertInstanceOf(DecryptDataResponse::class, $batchResponse); + $this->assertInstanceOf(MetaData::class, $batchResponse->getMetaData()); + } + + $this->assertEquals(['batchError'], $arrayEndpointResponse[0]->getMetaData()->getErrors()); + $this->assertEquals([], $arrayEndpointResponse[1]->getMetaData()->getErrors()); + $this->assertEquals(['batchError2'], $arrayEndpointResponse[2]->getMetaData()->getErrors()); + } + + public function testBulkPayloadWillBePopulatedToResponseClass() + { + $batchResponse = [ + ['plaintext' => base64_encode('OH NO')], + ['plaintext' => base64_encode('WHHAAT')], + ]; + + $response = new Response(200, [], json_encode([ + 'data' => [ + 'batch_results' => $batchResponse, + ], + ])); + + $arrayEndpointResponse = DecryptDataResponse::fromBulkResponse($response); + foreach($arrayEndpointResponse as $bulkResponse) { + $expected = array_map('base64_decode', current($batchResponse)); + $this->assertEquals(current($expected), $bulkResponse->getPlaintext()); + next($batchResponse); + } + } +} diff --git a/tests/VaultPHP/Response/EndpointResponseTest.php b/tests/VaultPHP/Response/EndpointResponseTest.php index 158e1f8..8b32e47 100644 --- a/tests/VaultPHP/Response/EndpointResponseTest.php +++ b/tests/VaultPHP/Response/EndpointResponseTest.php @@ -4,7 +4,7 @@ use GuzzleHttp\Psr7\Response; use PHPUnit\Framework\TestCase; -use VaultPHP\Response\BasicMetaResponse; +use VaultPHP\Response\MetaData; use VaultPHP\Response\EndpointResponse; use VaultPHP\SecretEngines\Engines\Transit\Response\DecryptDataResponse; @@ -16,21 +16,39 @@ final class EndpointResponseTest extends TestCase { public function testCanGetPopulateMetaDataFromResponse() { - $response = new Response(200, [], json_encode([ + $testMeta = [ + 'request_id' => 1337, + 'lease_id' => 1338, + 'renewable' => true, + 'lease_duration' => 1339, + 'wrap_info' => 'foo', + 'warnings' => [ + 'fooWarning', + 'fooWarning2', + ], + 'auth' => [ + 'token' => 'fooToken', + ], 'errors' => [ 'metaDataError', 'metaDataError2', ], - ])); + ]; + $response = new Response(200, [], json_encode($testMeta)); $endpointResponse = EndpointResponse::fromResponse($response); - $basicMeta = $endpointResponse->getBasicMetaResponse(); + $basicMeta = $endpointResponse->getMetaData(); $this->assertInstanceOf(EndpointResponse::class, $endpointResponse); - $this->assertInstanceOf(BasicMetaResponse::class, $basicMeta); - $this->assertEquals( - ['metaDataError', 'metaDataError2'], - $basicMeta->getErrors() - ); + $this->assertInstanceOf(MetaData::class, $basicMeta); + + $this->assertEquals($testMeta['request_id'], $basicMeta->getRequestId()); + $this->assertEquals($testMeta['lease_id'], $basicMeta->getLeaseId()); + $this->assertEquals($testMeta['renewable'], $basicMeta->getRenewable()); + $this->assertEquals($testMeta['lease_duration'], $basicMeta->getLeaseDuration()); + $this->assertEquals($testMeta['wrap_info'], $basicMeta->getWrapInfo()); + $this->assertEquals($testMeta['warnings'], $basicMeta->getWarnings()); + $this->assertEquals((object) $testMeta['auth'], $basicMeta->getAuth()); + $this->assertEquals($testMeta['errors'], $basicMeta->getErrors()); } public function testCanGetPopulatePayloadDataFromResponse() @@ -46,86 +64,36 @@ public function testCanGetPopulatePayloadDataFromResponse() $this->assertEquals('fooPlaintext', $endpointResponse->getPlaintext()); } - public function testCanGetPopulateMetaDataFromBulkResponse() + public function testHasErrors() { $response = new Response(200, [], json_encode([ - 'errors' => [ - 'metaDataError', - 'metaDataError2', - ], - 'data' => [ - 'batch_results' => [ - [], - [], - ], - ], + 'errors' => [], + 'data' => [], ])); - $arrayEndpointResponse = EndpointResponse::fromBulkResponse($response); - $this->assertSame(2, count($arrayEndpointResponse)); - - foreach($arrayEndpointResponse as $response) { - $basicMeta = $response->getBasicMetaResponse(); - - $this->assertInstanceOf(EndpointResponse::class, $response); - $this->assertInstanceOf(BasicMetaResponse::class, $basicMeta); - $this->assertEquals( - ['metaDataError', 'metaDataError2'], - $basicMeta->getErrors() - ); - } - } - - public function testBulkErrorsWillBeMergedInMetaDataErrors() - { - $batchErrors = [ - ['error' => 'OH NO'], - ['error' => 'WHHAAT'], - [], - ]; + $endpointResponse = DecryptDataResponse::fromResponse($response); + $this->assertFalse($endpointResponse->hasErrors()); $response = new Response(200, [], json_encode([ 'errors' => [ - 'metaDataError', - 'metaDataError2', - ], - 'data' => [ - 'batch_results' => $batchErrors, + 'foo', + 'bar', ], + 'data' => [], ])); - - $arrayEndpointResponse = EndpointResponse::fromBulkResponse($response); - foreach($arrayEndpointResponse as $response) { - $basicMeta = $response->getBasicMetaResponse(); - - $this->assertEquals( - array_merge( - ['metaDataError', 'metaDataError2'], - array_values(current($batchErrors)) - ), - $basicMeta->getErrors() - ); - next($batchErrors); - } + $endpointResponse = DecryptDataResponse::fromResponse($response); + $this->assertTrue($endpointResponse->hasErrors()); } - public function testBulkPayloadWillBePopulatedToResponseClass() + public function testGetErrors() { - $batchResponse = [ - ['plaintext' => base64_encode('OH NO')], - ['plaintext' => base64_encode('WHHAAT')], - ]; - $response = new Response(200, [], json_encode([ - 'data' => [ - 'batch_results' => $batchResponse, + 'errors' => [ + 'foo', + 'bar' ], + 'data' => [], ])); - - $arrayEndpointResponse = DecryptDataResponse::fromBulkResponse($response); - foreach($arrayEndpointResponse as $bulkResponse) { - $expected = array_map('base64_decode', current($batchResponse)); - $this->assertEquals(current($expected), $bulkResponse->getPlaintext()); - next($batchResponse); - } + $endpointResponse = DecryptDataResponse::fromResponse($response); + $this->assertEquals(['foo', 'bar'], $endpointResponse->getMetaData()->getErrors()); } } diff --git a/tests/VaultPHP/Response/BasicMetaResponseTest.php b/tests/VaultPHP/Response/MetaDataResponseTest.php similarity index 81% rename from tests/VaultPHP/Response/BasicMetaResponseTest.php rename to tests/VaultPHP/Response/MetaDataResponseTest.php index cefe337..7a08ace 100644 --- a/tests/VaultPHP/Response/BasicMetaResponseTest.php +++ b/tests/VaultPHP/Response/MetaDataResponseTest.php @@ -4,17 +4,17 @@ use PHPUnit\Framework\TestCase; use ReflectionClass; -use VaultPHP\Response\BasicMetaResponse; +use VaultPHP\Response\MetaData; /** * Class BasicMetaResponseTest * @package Test\VaultPHP\Response */ -final class BasicMetaResponseTest extends TestCase +final class MetaDataResponseTest extends TestCase { private function createTestData() { - $reflectionClass = new ReflectionClass(BasicMetaResponse::class); + $reflectionClass = new ReflectionClass(MetaData::class); $classPropertyNames = array_map(function ($property) { return $property->getName(); @@ -42,26 +42,26 @@ private function checkDtoData($testData, $basicMetaData) public function testCanPopulateArrayDataToSelf() { $testData = $this->createTestData(); - $basicMetaData = new BasicMetaResponse((array)$testData); + $basicMetaData = new MetaData((array)$testData); $this->checkDtoData($testData, $basicMetaData); } public function testCanPopulateObjectDataToSelf() { $testData = $this->createTestData(); - $basicMetaData = new BasicMetaResponse((object)$testData); + $basicMetaData = new MetaData((object)$testData); $this->checkDtoData($testData, $basicMetaData); } public function testCheckForErrors() { $error = ["foo"]; - $basicMetaData = new BasicMetaResponse(['errors' => $error]); + $basicMetaData = new MetaData(['errors' => $error]); $this->assertTrue($basicMetaData->hasErrors()); $this->assertEquals($error, $basicMetaData->getErrors()); - $basicMetaData = new BasicMetaResponse(['errors' => []]); + $basicMetaData = new MetaData(['errors' => []]); $this->assertFalse($basicMetaData->hasErrors()); $this->assertEquals([], $basicMetaData->getErrors()); diff --git a/tests/VaultPHP/SecretEngines/Traits/TraitTest.php b/tests/VaultPHP/SecretEngines/Traits/TraitTest.php index b519cbd..34c33e9 100644 --- a/tests/VaultPHP/SecretEngines/Traits/TraitTest.php +++ b/tests/VaultPHP/SecretEngines/Traits/TraitTest.php @@ -12,10 +12,10 @@ use VaultPHP\SecretEngines\Engines\Transit\Request\DecryptData\DecryptDataBulkRequest; /** - * Class ArrayExportTraitTest + * Class TraitTest * @package Test\VaultPHP\SecretEngines\Traits */ -class ArrayExportTraitTest extends TestCase +class TraitTest extends TestCase { public function testArrayExtractionFromRequest() { diff --git a/tests/VaultPHP/VaultClientTest.php b/tests/VaultPHP/VaultClientTest.php index 9d164a6..eb0575b 100644 --- a/tests/VaultPHP/VaultClientTest.php +++ b/tests/VaultPHP/VaultClientTest.php @@ -9,6 +9,7 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Test\VaultPHP\Mocks\EndpointResponseMock; +use Test\VaultPHP\Mocks\InvalidEndpointResponseMock; use VaultPHP\Authentication\AuthenticationMetaData; use VaultPHP\Authentication\AuthenticationProviderInterface; use VaultPHP\Authentication\Provider\Token; @@ -18,16 +19,16 @@ use VaultPHP\Exceptions\VaultException; use VaultPHP\Exceptions\VaultHttpException; use VaultPHP\Exceptions\VaultResponseException; -use VaultPHP\Response\BasicMetaResponse; +use VaultPHP\Response\MetaData; use VaultPHP\Response\EndpointResponse; use VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData\EncryptDataBulkRequest; use VaultPHP\VaultClient; /** - * Class VaultPHPTest + * Class VaultClientTest * @package Test\VaultPHP */ -final class VaultPHPTest extends TestCase +final class VaultClientTest extends TestCase { public function testAuthProviderGetsClientInjected() { @@ -166,7 +167,7 @@ public function testWillThrowWhenReturnClassDeclarationIsInvalid() { ])); $client = new VaultClient($httpClient, $auth, TEST_VAULT_ENDPOINT); - $client->sendApiRequest('GET', '/foo', [], BasicMetaResponse::class); + $client->sendApiRequest('GET', '/foo', [], MetaData::class); } public function testWillThrowWhenReturnClassDeclarationIsInvalidForBulk() { @@ -187,7 +188,7 @@ public function testWillThrowWhenReturnClassDeclarationIsInvalidForBulk() { ])); $client = new VaultClient($httpClient, $auth, TEST_VAULT_ENDPOINT); - $client->sendApiRequest('GET', '/foo', BasicMetaResponse::class, new EncryptDataBulkRequest('foo')); + $client->sendApiRequest('GET', '/foo', MetaData::class, new EncryptDataBulkRequest('foo')); } public function testWillThrowWhenResultOfReturnClassDeclarationIsInvalid() { @@ -208,7 +209,7 @@ public function testWillThrowWhenResultOfReturnClassDeclarationIsInvalid() { ])); $client = new VaultClient($httpClient, $auth, TEST_VAULT_ENDPOINT); - $client->sendApiRequest('GET', '/foo', EndpointResponseMock::class, []); + $client->sendApiRequest('GET', '/foo', InvalidEndpointResponseMock::class, []); } private function simulateApiResponse($responseStatus, $responseBody = '', $responseHeader = []) {