From 7f235e7b81c5aa4dc1b2c3639d890e0438285640 Mon Sep 17 00:00:00 2001 From: batopa Date: Wed, 3 Jun 2020 11:04:20 +0200 Subject: [PATCH] test: improve coverage for ApiProxyTrait --- src/Controller/ApiProxyTrait.php | 20 ++-- .../TestCase/Controller/ApiProxyTraitTest.php | 111 +++++++++++++++++- 2 files changed, 120 insertions(+), 11 deletions(-) diff --git a/src/Controller/ApiProxyTrait.php b/src/Controller/ApiProxyTrait.php index 1cf3323..6d447da 100644 --- a/src/Controller/ApiProxyTrait.php +++ b/src/Controller/ApiProxyTrait.php @@ -123,15 +123,15 @@ protected function request(array $options): void case 'get': $response = $this->apiClient->get($options['path'], $options['query'], $options['headers']); break; - case 'post': - $response = $this->apiClient->post($options['path'], $options['body'], $options['headers']); - break; - case 'patch': - $response = $this->apiClient->patch($options['path'], $options['body'], $options['headers']); - break; - case 'delete': - $response = $this->apiClient->delete($options['path'], $options['body'], $options['headers']); - break; + // case 'post': + // $response = $this->apiClient->post($options['path'], $options['body'], $options['headers']); + // break; + // case 'patch': + // $response = $this->apiClient->patch($options['path'], $options['body'], $options['headers']); + // break; + // case 'delete': + // $response = $this->apiClient->delete($options['path'], $options['body'], $options['headers']); + // break; default: throw new MethodNotAllowedException(); } @@ -162,7 +162,7 @@ protected function handleError(\Throwable $error): void } $this->response = $this->response->withStatus($status); $errorData = [ - 'status' => $status, + 'status' => (string)$status, 'title' => $error->getMessage(), ]; $this->set('error', $errorData); diff --git a/tests/TestCase/Controller/ApiProxyTraitTest.php b/tests/TestCase/Controller/ApiProxyTraitTest.php index ea7cc84..4afd9a4 100644 --- a/tests/TestCase/Controller/ApiProxyTraitTest.php +++ b/tests/TestCase/Controller/ApiProxyTraitTest.php @@ -14,7 +14,11 @@ */ namespace BEdita\WebTools\Test\TestCase\Controller; +use BEdita\SDK\BEditaClient; use BEdita\WebTools\ApiClientProvider; +use BEdita\WebTools\Controller\ApiProxyTrait; +use Cake\Controller\Controller; +use Cake\Http\ServerRequest; use Cake\Routing\Router; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; @@ -60,6 +64,16 @@ public function tearDown(): void $this->apiClient = null; } + /** + * Get base URL. + * + * @return string + */ + protected function getBaseUrl(): string + { + return Router::url('/', true); + } + /** * Test get() method * @@ -91,7 +105,7 @@ public function testGet(): void static::assertArrayHasKey('links', $response); static::assertArrayHasKey('meta', $response); - $baseUrl = Router::url('/', true); + $baseUrl = $this->getBaseUrl(); foreach ($response['links'] as $link) { static::assertStringContainsString($baseUrl, $link); } @@ -123,4 +137,99 @@ public function testNotFoundError(): void static::assertArrayHasKey('status', $response['error']); static::assertArrayHasKey('title', $response['error']); } + + /** + * Test that masking links with value searched equal to string works. + * + * @return void + * + * @covers ::maskLinks() + */ + public function testMaskLinksString(): void + { + $this->get('/api/model/schema/users'); + $this->assertResponseOk(); + $this->assertContentType('application/json'); + $response = json_decode((string)$this->_response, true); + static::assertStringContainsString($this->getBaseUrl(), Hash::get($response, '$id')); + } + + /** + * Test that getting a list of objects the relationships links are masked. + * + * @return void + * + * @covers ::maskResponseLinks() + */ + public function testMaskRelationshipsLinksGettingList(): void + { + $this->get('/api/users'); + $this->assertResponseOk(); + $this->assertContentType('application/json'); + $response = json_decode((string)$this->_response, true); + + foreach (Hash::extract($response, 'data.{n}.relationships.{s}.links.{s}') as $link) { + static::assertStringContainsString($this->getBaseUrl(), $link); + } + } + + /** + * Test that getting /home the resources links are masked. + * + * @return void + * + * @covers ::maskResponseLinks() + */ + public function testMaskResourcesGettingHome(): void + { + $this->get('/api/home'); + $this->assertResponseOk(); + $this->assertContentType('application/json'); + $response = json_decode((string)$this->_response, true); + + foreach (Hash::extract($response, 'meta.resources.{s}.href') as $link) { + static::assertStringContainsString($this->getBaseUrl(), $link); + } + } + + /** + * Test that an exception different from BEditaClientException throws in BEditaClient request + * is correctly handled + * + * @return void + * + * @covers ::handleError() + */ + public function testNotBEditaClientException(): void + { + $controller = new class (new ServerRequest()) extends Controller { + use ApiProxyTrait; + + public function setApiCLient($apiClient) + { + $this->apiClient = $apiClient; + } + + protected function setBaseUrl($path): void + { + $this->baseUrl = '/'; + } + }; + + $apiClientMock = $this->getMockBuilder(BEditaClient::class) + ->disableOriginalConstructor() + ->setMethods(['get']) + ->getMock(); + + $apiClientMock->method('get')->willThrowException(new \LogicException('Broken')); + + $controller->setApiCLient($apiClientMock); + $controller->get('/gustavo'); + $error = $controller->viewBuilder()->getVar('error'); + + static::assertArrayHasKey('status', $error); + static::assertArrayHasKey('title', $error); + static::assertEquals('500', $error['status']); + static::assertEquals('Broken', $error['title']); + } }