From 0fb4d77a88d4782e5caa9bd6564103dee256d813 Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Tue, 9 May 2017 14:53:18 -0400 Subject: [PATCH 1/4] Modify exceptions when a whitelist may be present --- src/Core/Exception/NotFoundException.php | 12 ++- src/Core/WhitelistTrait.php | 49 +++++++++++++ src/PubSub/Connection/Grpc.php | 40 ++++++---- src/PubSub/Connection/Rest.php | 26 ++++++- .../Core/Exception/NotFoundExceptionTest.php | 36 +++++++++ tests/unit/Core/WhitelistTraitTest.php | 73 +++++++++++++++++++ 6 files changed, 218 insertions(+), 18 deletions(-) create mode 100644 src/Core/WhitelistTrait.php create mode 100644 tests/unit/Core/Exception/NotFoundExceptionTest.php create mode 100644 tests/unit/Core/WhitelistTraitTest.php diff --git a/src/Core/Exception/NotFoundException.php b/src/Core/Exception/NotFoundException.php index f0a1f3139ed6..e6ba2c5cbe59 100644 --- a/src/Core/Exception/NotFoundException.php +++ b/src/Core/Exception/NotFoundException.php @@ -22,5 +22,15 @@ */ class NotFoundException extends ServiceException { - + /** + * Allows overriding message for injection of Whitelist Notice. + * + * @param string $message the new message + * @return void + * @access private + */ + public function setMessage($message) + { + $this->message = $message; + } } diff --git a/src/Core/WhitelistTrait.php b/src/Core/WhitelistTrait.php new file mode 100644 index 000000000000..cf908461e667 --- /dev/null +++ b/src/Core/WhitelistTrait.php @@ -0,0 +1,49 @@ +setMessage('NOTE: Error may be due to Whitelist Restriction. ' . $e->getMessage()); + + throw $e; + } catch (\Exception $e) { + throw $e; + } + } +} diff --git a/src/PubSub/Connection/Grpc.php b/src/PubSub/Connection/Grpc.php index 445c7a6be946..2d3d405ded2e 100644 --- a/src/PubSub/Connection/Grpc.php +++ b/src/PubSub/Connection/Grpc.php @@ -22,6 +22,7 @@ use Google\Cloud\Core\GrpcRequestWrapper; use Google\Cloud\Core\GrpcTrait; use Google\Cloud\Core\PhpArray; +use Google\Cloud\Core\WhitelistTrait; use Google\Cloud\PubSub\PubSubClient; use Google\Cloud\PubSub\V1\PublisherClient; use Google\Cloud\PubSub\V1\SubscriberClient; @@ -40,6 +41,7 @@ class Grpc implements ConnectionInterface { use EmulatorTrait; use GrpcTrait; + use WhitelistTrait; const BASE_URI = 'https://pubsub.googleapis.com/'; @@ -288,9 +290,12 @@ public function acknowledge(array $args) */ public function listSnapshots(array $args) { - return $this->send([$this->subscriberClient, 'listSnapshots'], [ - $this->pluck('project', $args), - $args + return $this->whitelist([$this, 'send'], [ + [$this->subscriberClient, 'listSnapshots'], + [ + $this->pluck('project', $args), + $args + ] ]); } @@ -299,10 +304,13 @@ public function listSnapshots(array $args) */ public function createSnapshot(array $args) { - return $this->send([$this->subscriberClient, 'createSnapshot'], [ - $this->pluck('name', $args), - $this->pluck('subscription', $args), - $args + return $this->whitelist([$this, 'send'], [ + [$this->subscriberClient, 'createSnapshot'], + [ + $this->pluck('name', $args), + $this->pluck('subscription', $args), + $args + ] ]); } @@ -311,9 +319,12 @@ public function createSnapshot(array $args) */ public function deleteSnapshot(array $args) { - return $this->send([$this->subscriberClient, 'deleteSnapshot'], [ - $this->pluck('snapshot', $args), - $args + return $this->whitelist([$this, 'send'], [ + [$this->subscriberClient, 'deleteSnapshot'], + [ + $this->pluck('snapshot', $args), + $args + ] ]); } @@ -327,9 +338,12 @@ public function seek(array $args) $args['time'] = (new protobuf\Timestamp)->deserialize($time, $this->codec); } - return $this->send([$this->subscriberClient, 'seek'], [ - $this->pluck('subscription', $args), - $args + return $this->whitelist([$this, 'send'], [ + [$this->subscriberClient, 'seek'], + [ + $this->pluck('subscription', $args), + $args + ] ]); } diff --git a/src/PubSub/Connection/Rest.php b/src/PubSub/Connection/Rest.php index 44bbb65e7fcc..f8ec8d207e97 100644 --- a/src/PubSub/Connection/Rest.php +++ b/src/PubSub/Connection/Rest.php @@ -22,6 +22,7 @@ use Google\Cloud\Core\RequestWrapper; use Google\Cloud\Core\RestTrait; use Google\Cloud\Core\UriTrait; +use Google\Cloud\Core\WhitelistTrait; use Google\Cloud\PubSub\PubSubClient; /** @@ -36,6 +37,7 @@ class Rest implements ConnectionInterface use EmulatorTrait; use RestTrait; use UriTrait; + use WhitelistTrait; const BASE_URI = 'https://pubsub.googleapis.com/'; @@ -214,7 +216,11 @@ public function acknowledge(array $args) */ public function listSnapshots(array $args) { - return $this->send('snapshots', 'list', $args); + return $this->whitelist([$this, 'send'], [ + 'snapshots', + 'list', + $args + ]); } /** @@ -222,7 +228,11 @@ public function listSnapshots(array $args) */ public function createSnapshot(array $args) { - return $this->send('snapshots', 'create', $args); + return $this->whitelist([$this, 'send'], [ + 'snapshots', + 'create', + $args + ]); } /** @@ -230,7 +240,11 @@ public function createSnapshot(array $args) */ public function deleteSnapshot(array $args) { - return $this->send('snapshots', 'delete', $args); + return $this->whitelist([$this, 'send'], [ + 'snapshots', + 'delete', + $args + ]); } /** @@ -238,7 +252,11 @@ public function deleteSnapshot(array $args) */ public function seek(array $args) { - return $this->send('subscriptions', 'seek', $args); + return $this->whitelist([$this, 'send'], [ + 'subscriptions', + 'seek', + $args + ]); } /** diff --git a/tests/unit/Core/Exception/NotFoundExceptionTest.php b/tests/unit/Core/Exception/NotFoundExceptionTest.php new file mode 100644 index 000000000000..72dd944a5f67 --- /dev/null +++ b/tests/unit/Core/Exception/NotFoundExceptionTest.php @@ -0,0 +1,36 @@ +assertEquals($ex->getMessage(), 'hello'); + + $ex->setMessage('world'); + $this->assertEquals($ex->getMessage(), 'world'); + } +} diff --git a/tests/unit/Core/WhitelistTraitTest.php b/tests/unit/Core/WhitelistTraitTest.php new file mode 100644 index 000000000000..a4055d14b626 --- /dev/null +++ b/tests/unit/Core/WhitelistTraitTest.php @@ -0,0 +1,73 @@ +trait = new WhitelistTraitStub; + } + + public function testWhitelistWithNotFoundException() + { + $callable = function() { + throw new NotFoundException('hello world'); + }; + + try { + $res = $this->trait->call('whitelist', [$callable, []]); + } catch (\Exception $e) { + $this->assertInstanceOf(NotFoundException::class, $e); + $this->assertTrue(strpos($e->getMessage(), 'hello world') !== false); + } + } + + public function testWhitelistWithNotOtherException() + { + $callable = function() { + throw new ServiceException('hello world'); + }; + + try { + $res = $this->trait->call('whitelist', [$callable, []]); + } catch (\Exception $e) { + $this->assertInstanceOf(ServiceException::class, $e); + $this->assertEquals('hello world', $e->getMessage()); + } + } +} + +class WhitelistTraitStub +{ + use WhitelistTrait; + + public function call($method, array $args) + { + return call_user_func_array([$this, $method], $args); + } +} From ffd3b22db7eac8743e48f9ed19fb87035aed8756 Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Fri, 19 May 2017 10:51:44 -0400 Subject: [PATCH 2/4] Add Whitelist system tests --- src/Core/GrpcRequestWrapper.php | 1 + src/Core/WhitelistTrait.php | 2 +- .../system/ServiceWhitelist/WhitelistTest.php | 182 ++++++++++++++++++ tests/system/bootstrap.php | 6 + 4 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 tests/system/ServiceWhitelist/WhitelistTest.php diff --git a/src/Core/GrpcRequestWrapper.php b/src/Core/GrpcRequestWrapper.php index 3d89d417a9e0..51cb3a3f1a07 100644 --- a/src/Core/GrpcRequestWrapper.php +++ b/src/Core/GrpcRequestWrapper.php @@ -216,6 +216,7 @@ private function convertToGoogleException(ApiException $ex) break; case Grpc\STATUS_NOT_FOUND: + case Grpc\STATUS_UNIMPLEMENTED: $exception = Exception\NotFoundException::class; break; diff --git a/src/Core/WhitelistTrait.php b/src/Core/WhitelistTrait.php index cf908461e667..e8b6e221c71b 100644 --- a/src/Core/WhitelistTrait.php +++ b/src/Core/WhitelistTrait.php @@ -1,6 +1,6 @@ markTestSkipped('Missing whitelist keyfile path for whitelist system tests.'); + } + + $this->keyFilePath = GOOGLE_CLOUD_WHITELIST_KEY_PATH; + } + + public function testPubSubListSnapshotsRest() + { + $client = new PubSubClient([ + 'keyFilePath' => $this->keyFilePath, + 'transport' => 'rest' + ]); + + $this->checkException(function() use ($client) { + iterator_to_array($client->snapshots()); + }); + } + + public function testPubSubListSnapshotsGrpc() + { + $client = new PubSubClient([ + 'keyFilePath' => $this->keyFilePath, + 'transport' => 'grpc' + ]); + + $this->checkException(function() use ($client) { + iterator_to_array($client->snapshots()); + }); + } + + public function testPubSubCreateSnapshotRest() + { + $client = new PubSubClient([ + 'keyFilePath' => $this->keyFilePath, + 'transport' => 'rest' + ]); + + $topic = $client->createTopic(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($topic) { + $topic->delete(); + }; + + $sub = $topic->subscribe(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($sub) { + $sub->delete(); + }; + + $this->checkException(function () use ($client, $sub) { + $client->createSnapshot(uniqid(self::TESTING_PREFIX), $sub); + }); + } + + public function testPubSubCreateSnapshotGrpc() + { + $client = new PubSubClient([ + 'keyFilePath' => $this->keyFilePath, + 'transport' => 'grpc' + ]); + + $topic = $client->createTopic(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($topic) { + $topic->delete(); + }; + + $sub = $topic->subscribe(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($sub) { + $sub->delete(); + }; + + $this->checkException(function () use ($client, $sub) { + $client->createSnapshot(uniqid(self::TESTING_PREFIX), $sub); + }); + } + + public function testPubSubSeekRest() + { + $client = new PubSubClient([ + 'keyFilePath' => $this->keyFilePath, + 'transport' => 'rest' + ]); + + $topic = $client->createTopic(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($topic) { + $topic->delete(); + }; + + $sub = $topic->subscribe(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($sub) { + $sub->delete(); + }; + + $this->checkException(function () use ($sub) { + $sub->seekToTime(new Timestamp(new \DateTime)); + }); + } + + public function testPubSubSeekGrpc() + { + $client = new PubSubClient([ + 'keyFilePath' => $this->keyFilePath, + 'transport' => 'grpc' + ]); + + $topic = $client->createTopic(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($topic) { + $topic->delete(); + }; + + $sub = $topic->subscribe(uniqid(self::TESTING_PREFIX)); + self::$deletionQueue[] = function () use ($sub) { + $sub->delete(); + }; + + $this->checkException(function () use ($sub) { + $sub->seekToTime(new Timestamp(new \DateTime)); + }); + } + + private function checkException(callable $call) + { + $thrown = false; + $ex = null; + try { + $call(); + } catch (\Exception $e) { + $thrown = true; + $ex = $e; + } + + $this->assertTrue($thrown); + $this->assertInstanceOf(NotFoundException::class, $ex); + $this->assertTrue(strpos($ex->getMessage(), self::MESSAGE) !== false); + } + + public static function tearDownFixtures() + { + foreach (self::$deletionQueue as $toDelete) { + if (!is_callable($toDelete)) { + throw new \Exception('fixtures must be callables'); + } + + call_user_func($toDelete); + } + } +} diff --git a/tests/system/bootstrap.php b/tests/system/bootstrap.php index f9e2b36a997c..528369062539 100644 --- a/tests/system/bootstrap.php +++ b/tests/system/bootstrap.php @@ -8,6 +8,7 @@ use Google\Cloud\Tests\System\PubSub\PubSubTestCase; use Google\Cloud\Tests\System\Spanner\SpannerTestCase; use Google\Cloud\Tests\System\Storage\StorageTestCase; +use Google\Cloud\Tests\System\Whitelist\WhitelistTest; if (!getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH')) { throw new \Exception( @@ -15,6 +16,10 @@ ); } +if (getenv('GOOGLE_CLOUD_PHP_TESTS_WHITELIST_KEY_PATH')) { + define('GOOGLE_CLOUD_WHITELIST_KEY_PATH', getenv('GOOGLE_CLOUD_PHP_TESTS_WHITELIST_KEY_PATH')); +} + register_shutdown_function(function () { PubSubTestCase::tearDownFixtures(); DatastoreTestCase::tearDownFixtures(); @@ -22,4 +27,5 @@ LoggingTestCase::tearDownFixtures(); BigQueryTestCase::tearDownFixtures(); SpannerTestCase::tearDownFixtures(); + WhitelistTest::tearDownFixtures(); }); From f51fc5c473274e1c3331950885f785275cd4bbc0 Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Mon, 22 May 2017 09:37:24 -0400 Subject: [PATCH 3/4] Code review fixes --- src/Core/WhitelistTrait.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Core/WhitelistTrait.php b/src/Core/WhitelistTrait.php index e8b6e221c71b..b725b267ecee 100644 --- a/src/Core/WhitelistTrait.php +++ b/src/Core/WhitelistTrait.php @@ -31,7 +31,7 @@ trait WhitelistTrait * @param array $callable The request callable. * @param array $args Request arguments. * @return array - * @throws WhitelistException + * @throws NotFoundException * @throws ServiceException */ private function whitelist(callable $callable, array $args) @@ -41,8 +41,6 @@ private function whitelist(callable $callable, array $args) } catch (NotFoundException $e) { $e->setMessage('NOTE: Error may be due to Whitelist Restriction. ' . $e->getMessage()); - throw $e; - } catch (\Exception $e) { throw $e; } } From 139a9392a78c8dd7e0c0941467156407f5f320ba Mon Sep 17 00:00:00 2001 From: jdpedrie Date: Mon, 22 May 2017 11:16:48 -0400 Subject: [PATCH 4/4] Refactor out additional call_user_func --- src/Core/GrpcTrait.php | 17 +++++++-- src/Core/RestTrait.php | 28 ++++++++++---- src/Core/WhitelistTrait.php | 20 +++------- src/PubSub/Connection/Grpc.php | 52 +++++++++++--------------- src/PubSub/Connection/Rest.php | 30 ++++----------- tests/unit/Core/WhitelistTraitTest.php | 31 ++++----------- 6 files changed, 77 insertions(+), 101 deletions(-) diff --git a/src/Core/GrpcTrait.php b/src/Core/GrpcTrait.php index 46313151a373..01a655e3e5d9 100644 --- a/src/Core/GrpcTrait.php +++ b/src/Core/GrpcTrait.php @@ -19,9 +19,10 @@ use DateTime; use DateTimeZone; -use Google\Auth\FetchAuthTokenCache; use Google\Auth\Cache\MemoryCacheItemPool; +use Google\Auth\FetchAuthTokenCache; use Google\Cloud\Core\ArrayTrait; +use Google\Cloud\Core\Exception\NotFoundException; use Google\Cloud\Core\GrpcRequestWrapper; use google\protobuf; @@ -31,6 +32,7 @@ trait GrpcTrait { use ArrayTrait; + use WhitelistTrait; /** * @var GrpcRequestWrapper Wrapper used to handle sending requests to the @@ -53,9 +55,10 @@ public function setRequestWrapper(GrpcRequestWrapper $requestWrapper) * * @param callable $request * @param array $args + * @param bool $whitelisted * @return \Generator|array */ - public function send(callable $request, array $args) + public function send(callable $request, array $args, $whitelisted = false) { $requestOptions = $this->pluckArray([ 'grpcOptions', @@ -63,7 +66,15 @@ public function send(callable $request, array $args) 'requestTimeout' ], $args[count($args) - 1]); - return $this->requestWrapper->send($request, $args, $requestOptions); + try { + return $this->requestWrapper->send($request, $args, $requestOptions); + } catch (NotFoundException $e) { + if ($whitelisted) { + throw $this->modifyWhitelistedError($e); + } + + throw $e; + } } /** diff --git a/src/Core/RestTrait.php b/src/Core/RestTrait.php index 663687a807e9..1640e5baad55 100644 --- a/src/Core/RestTrait.php +++ b/src/Core/RestTrait.php @@ -17,6 +17,8 @@ namespace Google\Cloud\Core; +use Google\Cloud\Core\Exception\NotFoundException; + /** * Provides shared functionality for REST service implementations. */ @@ -24,6 +26,7 @@ trait RestTrait { use ArrayTrait; use JsonTrait; + use WhitelistTrait; /** * @var RequestBuilder Builds PSR7 requests from a service definition. @@ -64,9 +67,10 @@ public function setRequestWrapper(RequestWrapper $requestWrapper) * @param string $resource The resource type used for the request. * @param string $method The method used for the request. * @param array $options [optional] Options used to build out the request. + * @param array $whitelisted [optional] * @return array */ - public function send($resource, $method, array $options = []) + public function send($resource, $method, array $options = [], $whitelisted = false) { $requestOptions = $this->pluckArray([ 'restOptions', @@ -74,12 +78,20 @@ public function send($resource, $method, array $options = []) 'requestTimeout' ], $options); - return json_decode( - $this->requestWrapper->send( - $this->requestBuilder->build($resource, $method, $options), - $requestOptions - )->getBody(), - true - ); + try { + return json_decode( + $this->requestWrapper->send( + $this->requestBuilder->build($resource, $method, $options), + $requestOptions + )->getBody(), + true + ); + } catch (NotFoundException $e) { + if ($whitelisted) { + throw $this->modifyWhitelistedError($e); + } + + throw $e; + } } } diff --git a/src/Core/WhitelistTrait.php b/src/Core/WhitelistTrait.php index b725b267ecee..5e40e6bef7a3 100644 --- a/src/Core/WhitelistTrait.php +++ b/src/Core/WhitelistTrait.php @@ -25,23 +25,15 @@ trait WhitelistTrait { /** - * Check if the error is due to a whitelist restriction, modify the message - * and throw the updated exception, otherwise pass through with no changes. + * Modify the error message of a whitelisted exception. * - * @param array $callable The request callable. - * @param array $args Request arguments. - * @return array - * @throws NotFoundException - * @throws ServiceException + * @param NotFoundException $e The exception. + * @return NotFoundException */ - private function whitelist(callable $callable, array $args) + private function modifyWhitelistedError(NotFoundException $e) { - try { - return call_user_func_array($callable, $args); - } catch (NotFoundException $e) { - $e->setMessage('NOTE: Error may be due to Whitelist Restriction. ' . $e->getMessage()); + $e->setMessage('NOTE: Error may be due to Whitelist Restriction. ' . $e->getMessage()); - throw $e; - } + return $e; } } diff --git a/src/PubSub/Connection/Grpc.php b/src/PubSub/Connection/Grpc.php index 2d3d405ded2e..903de3fc860e 100644 --- a/src/PubSub/Connection/Grpc.php +++ b/src/PubSub/Connection/Grpc.php @@ -22,7 +22,6 @@ use Google\Cloud\Core\GrpcRequestWrapper; use Google\Cloud\Core\GrpcTrait; use Google\Cloud\Core\PhpArray; -use Google\Cloud\Core\WhitelistTrait; use Google\Cloud\PubSub\PubSubClient; use Google\Cloud\PubSub\V1\PublisherClient; use Google\Cloud\PubSub\V1\SubscriberClient; @@ -41,7 +40,6 @@ class Grpc implements ConnectionInterface { use EmulatorTrait; use GrpcTrait; - use WhitelistTrait; const BASE_URI = 'https://pubsub.googleapis.com/'; @@ -290,13 +288,11 @@ public function acknowledge(array $args) */ public function listSnapshots(array $args) { - return $this->whitelist([$this, 'send'], [ - [$this->subscriberClient, 'listSnapshots'], - [ - $this->pluck('project', $args), - $args - ] - ]); + $whitelisted = true; + return $this->send([$this->subscriberClient, 'listSnapshots'], [ + $this->pluck('project', $args), + $args + ], $whitelisted); } /** @@ -304,14 +300,12 @@ public function listSnapshots(array $args) */ public function createSnapshot(array $args) { - return $this->whitelist([$this, 'send'], [ - [$this->subscriberClient, 'createSnapshot'], - [ - $this->pluck('name', $args), - $this->pluck('subscription', $args), - $args - ] - ]); + $whitelisted = true; + return $this->send([$this->subscriberClient, 'createSnapshot'], [ + $this->pluck('name', $args), + $this->pluck('subscription', $args), + $args + ], $whitelisted); } /** @@ -319,13 +313,11 @@ public function createSnapshot(array $args) */ public function deleteSnapshot(array $args) { - return $this->whitelist([$this, 'send'], [ - [$this->subscriberClient, 'deleteSnapshot'], - [ - $this->pluck('snapshot', $args), - $args - ] - ]); + $whitelisted = true; + return $this->send([$this->subscriberClient, 'deleteSnapshot'], [ + $this->pluck('snapshot', $args), + $args + ], $whitelisted); } /** @@ -338,13 +330,11 @@ public function seek(array $args) $args['time'] = (new protobuf\Timestamp)->deserialize($time, $this->codec); } - return $this->whitelist([$this, 'send'], [ - [$this->subscriberClient, 'seek'], - [ - $this->pluck('subscription', $args), - $args - ] - ]); + $whitelisted = true; + return $this->send([$this->subscriberClient, 'seek'], [ + $this->pluck('subscription', $args), + $args + ], $whitelisted); } /** diff --git a/src/PubSub/Connection/Rest.php b/src/PubSub/Connection/Rest.php index f8ec8d207e97..d45e9b4b2d15 100644 --- a/src/PubSub/Connection/Rest.php +++ b/src/PubSub/Connection/Rest.php @@ -22,7 +22,6 @@ use Google\Cloud\Core\RequestWrapper; use Google\Cloud\Core\RestTrait; use Google\Cloud\Core\UriTrait; -use Google\Cloud\Core\WhitelistTrait; use Google\Cloud\PubSub\PubSubClient; /** @@ -37,7 +36,6 @@ class Rest implements ConnectionInterface use EmulatorTrait; use RestTrait; use UriTrait; - use WhitelistTrait; const BASE_URI = 'https://pubsub.googleapis.com/'; @@ -216,11 +214,8 @@ public function acknowledge(array $args) */ public function listSnapshots(array $args) { - return $this->whitelist([$this, 'send'], [ - 'snapshots', - 'list', - $args - ]); + $whitelisted = true; + return $this->send('snapshots', 'list', $args, $whitelisted); } /** @@ -228,11 +223,8 @@ public function listSnapshots(array $args) */ public function createSnapshot(array $args) { - return $this->whitelist([$this, 'send'], [ - 'snapshots', - 'create', - $args - ]); + $whitelisted = true; + return $this->send('snapshots', 'create', $args, $whitelisted); } /** @@ -240,11 +232,8 @@ public function createSnapshot(array $args) */ public function deleteSnapshot(array $args) { - return $this->whitelist([$this, 'send'], [ - 'snapshots', - 'delete', - $args - ]); + $whitelisted = true; + return $this->send('snapshots', 'delete', $args, $whitelisted); } /** @@ -252,11 +241,8 @@ public function deleteSnapshot(array $args) */ public function seek(array $args) { - return $this->whitelist([$this, 'send'], [ - 'subscriptions', - 'seek', - $args - ]); + $whitelisted = true; + return $this->send('subscriptions', 'seek', $args, $whitelisted); } /** diff --git a/tests/unit/Core/WhitelistTraitTest.php b/tests/unit/Core/WhitelistTraitTest.php index a4055d14b626..dd654c205919 100644 --- a/tests/unit/Core/WhitelistTraitTest.php +++ b/tests/unit/Core/WhitelistTraitTest.php @@ -33,32 +33,17 @@ public function setUp() $this->trait = new WhitelistTraitStub; } - public function testWhitelistWithNotFoundException() + public function testModifyWhitelistedError() { - $callable = function() { - throw new NotFoundException('hello world'); - }; + $ex = new NotFoundException('hello world'); - try { - $res = $this->trait->call('whitelist', [$callable, []]); - } catch (\Exception $e) { - $this->assertInstanceOf(NotFoundException::class, $e); - $this->assertTrue(strpos($e->getMessage(), 'hello world') !== false); - } - } - - public function testWhitelistWithNotOtherException() - { - $callable = function() { - throw new ServiceException('hello world'); - }; + $res = $this->trait->call('modifyWhitelistedError', [$ex]); - try { - $res = $this->trait->call('whitelist', [$callable, []]); - } catch (\Exception $e) { - $this->assertInstanceOf(ServiceException::class, $e); - $this->assertEquals('hello world', $e->getMessage()); - } + $this->assertInstanceOf(NotFoundException::class, $res); + $this->assertEquals( + $res->getMessage(), + 'NOTE: Error may be due to Whitelist Restriction. hello world' + ); } }