diff --git a/tests/src/Unit/ApiAuthorizationTest.php b/tests/src/Unit/ApiAuthorizationTest.php deleted file mode 100644 index ce7c6e1..0000000 --- a/tests/src/Unit/ApiAuthorizationTest.php +++ /dev/null @@ -1,60 +0,0 @@ -getConfigFactoryStub([]), - $vaultManager, - ); - $this->assertEquals('123', $sut->getAuthorization()); - } - - /** - * @covers ::__construct - * @covers ::getAuthorization - */ - public function testEmptyAuthorization() : void { - $sut = new ApiAuthorization( - $this->getConfigFactoryStub([]), - new VaultManager([]), - ); - $this->assertNull($sut->getAuthorization()); - } - - /** - * @covers ::__construct - * @covers ::getAuthorization - */ - public function testFallbackConfigAuthorization() : void { - $sut = new ApiAuthorization( - $this->getConfigFactoryStub(['helfi_navigation.api' => ['key' => '123']]), - new VaultManager([]), - ); - $this->assertEquals('123', $sut->getAuthorization()); - } - -} diff --git a/tests/src/Unit/ApiManagerTest.php b/tests/src/Unit/ApiManagerTest.php index 1b68ff5..192ca96 100644 --- a/tests/src/Unit/ApiManagerTest.php +++ b/tests/src/Unit/ApiManagerTest.php @@ -8,6 +8,9 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\MemoryBackend; use Drupal\Core\File\Exception\FileNotExistsException; +use Drupal\helfi_api_base\ApiClient\ApiResponse; +use Drupal\helfi_api_base\ApiClient\CacheValue; +use Drupal\helfi_api_base\ApiClient\VaultAuthorizer; use Drupal\helfi_api_base\Environment\EnvironmentResolver; use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; use Drupal\helfi_api_base\Environment\Project; @@ -104,7 +107,6 @@ private function getSut( ClientInterface $client, TimeInterface $time = NULL, LoggerInterface $logger = NULL, - string $apiKey = NULL, EnvironmentResolverInterface $environmentResolver = NULL, ) : ApiManager { @@ -121,18 +123,18 @@ private function getSut( ])); } return new ApiManager( - $time, - $this->cache, $client, + $this->cache, + $time, $environmentResolver, $logger, - new ApiAuthorization( + new VaultAuthorizer( $this->getConfigFactoryStub([]), new VaultManager([ - new AuthorizationToken(ApiAuthorization::VAULT_MANAGER_KEY, '123'), - ]) + new AuthorizationToken('test_vault_key', '123'), + ]), + 'test_vault_key' ), - 1, ); } @@ -140,81 +142,29 @@ private function getSut( * Tests updateMainMenu(). * * @covers ::update - * @covers ::__construct - * @covers ::makeRequest - * @covers ::getRequestOptions * @covers ::getUrl - * @covers ::hasAuthorization - * @covers ::getAuthorization - * @covers \Drupal\helfi_navigation\ApiResponse::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization */ public function testUpdateMainMenu() : void { $requests = []; $client = $this->createMockHistoryMiddlewareHttpClient($requests, [ new Response(200, body: json_encode(['key' => 'value'])), ]); - $sut = $this->getSut( - $client, - apiKey: '123' - ); + $sut = $this->getSut($client); $sut->update('fi', ['key' => 'value']); $this->assertCount(1, $requests); // Make sure SSL verification is disabled on local. $this->assertFalse($requests[0]['options']['verify']); // Make sure Authorization header was set. - $this->assertEquals('Basic 123', $requests[0]['request']->getHeader('Authorization')[0]); - } - - /** - * Tests getExternalMenu(). - * - * @covers ::get - * @covers ::__construct - * @covers ::makeRequest - * @covers ::cache - * @covers ::getRequestOptions - * @covers ::getUrl - * @covers \Drupal\helfi_navigation\CacheValue::hasExpired - * @covers \Drupal\helfi_navigation\CacheValue::__construct - * @covers \Drupal\helfi_navigation\ApiResponse::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization - */ - public function testGetExternalMenu() : void { - $requests = []; - $client = $this->createMockHistoryMiddlewareHttpClient($requests, [ - new Response(200, body: json_encode([])), - new Response(200, body: json_encode(['key' => 'value'])), - ]); - $sut = $this->getSut($client); - - // Test empty and non-empty response. - for ($i = 0; $i < 2; $i++) { - $response = $sut->get('fi', 'main'); - $this->assertInstanceOf(ApiResponse::class, $response); - $this->assertInstanceOf(RequestInterface::class, $requests[0]['request']); - } - // Make sure cache is used (request queue should be empty). - $sut->get('fi', 'main'); + $this->assertNotEmpty($requests[0]['request']->getHeader('Authorization')[0]); } /** * Tests main menu. * * @covers ::get - * @covers ::__construct - * @covers ::makeRequest - * @covers ::cache - * @covers ::getRequestOptions + * @covers ::doGet * @covers ::getUrl - * @covers \Drupal\helfi_navigation\CacheValue::hasExpired - * @covers \Drupal\helfi_navigation\CacheValue::__construct - * @covers \Drupal\helfi_navigation\ApiResponse::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization */ public function testGetMainMenu() : void { $requests = []; @@ -236,17 +186,9 @@ public function testGetMainMenu() : void { /** * Tests that stale cache will be returned in case request fails. * - * @covers ::makeRequest * @covers ::get - * @covers ::__construct - * @covers ::cache - * @covers ::getRequestOptions + * @covers ::doGet * @covers ::getUrl - * @covers \Drupal\helfi_navigation\CacheValue::hasExpired - * @covers \Drupal\helfi_navigation\CacheValue::__construct - * @covers \Drupal\helfi_navigation\ApiResponse::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization */ public function testStaleCacheOnRequestFailure() : void { $requests = []; @@ -274,17 +216,9 @@ public function testStaleCacheOnRequestFailure() : void { /** * Tests that stale cache can be updated. * - * @covers ::makeRequest * @covers ::get - * @covers ::__construct - * @covers ::cache - * @covers ::getRequestOptions + * @covers ::doGet * @covers ::getUrl - * @covers \Drupal\helfi_navigation\CacheValue::hasExpired - * @covers \Drupal\helfi_navigation\CacheValue::__construct - * @covers \Drupal\helfi_navigation\ApiResponse::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization */ public function testStaleCacheUpdate() : void { $time = time(); @@ -318,178 +252,4 @@ public function testStaleCacheUpdate() : void { $this->assertEquals('value', $response->data->value); } - /** - * Make sure we log the exception and then re-throw the same exception. - * - * @covers ::makeRequest - * @covers ::get - * @covers ::__construct - * @covers ::cache - * @covers ::getRequestOptions - * @covers ::getUrl - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization - */ - public function testRequestLoggingException() : void { - $this->expectException(GuzzleException::class); - - $client = $this->createMockHttpClient([ - new RequestException('Test', $this->prophesize(RequestInterface::class)->reveal()), - ]); - $logger = $this->prophesize(LoggerInterface::class); - $logger->error(Argument::any()) - ->shouldBeCalled(); - - $sut = $this->getSut($client, logger: $logger->reveal()); - $sut->get('fi', 'footer'); - } - - /** - * Tests that file not found exception is thrown when no mock file exists. - * - * @covers ::makeRequest - * @covers ::get - * @covers ::__construct - * @covers ::cache - * @covers ::getRequestOptions - * @covers ::getUrl - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization - */ - public function testMockFallbackException() : void { - $this->expectException(FileNotExistsException::class); - $response = $this->prophesize(ResponseInterface::class); - $response->getStatusCode()->willReturn(403); - $client = $this->createMockHttpClient([ - new ClientException( - 'Test', - $this->prophesize(RequestInterface::class)->reveal(), - $response->reveal(), - ), - ]); - $sut = $this->getSut($client); - // Test with non-existent menu to make sure no mock file exist. - $sut->get('fi', 'footer'); - } - - /** - * Tests that mock file used on local environment when GET request fails. - * - * @covers ::makeRequest - * @covers ::get - * @covers ::__construct - * @covers ::cache - * @covers ::getRequestOptions - * @covers ::getUrl - * @covers \Drupal\helfi_navigation\CacheValue::__construct - * @covers \Drupal\helfi_navigation\ApiResponse::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization - */ - public function testMockFallback() : void { - // Use logger to verify that mock file is actually used. - $logger = $this->prophesize(LoggerInterface::class); - $logger->warning(Argument::containingString('Mock data is used instead.')) - ->shouldBeCalled(); - $client = $this->createMockHttpClient([ - new ConnectException( - 'Test', - $this->prophesize(RequestInterface::class)->reveal(), - ), - ]); - $sut = $this->getSut( - $client, - logger: $logger->reveal(), - ); - $response = $sut->get('fi', 'footer-bottom-navigation'); - $this->assertInstanceOf(ApiResponse::class, $response); - } - - /** - * Make sure subsequent requests are failed after one failed request. - * - * @covers ::makeRequest - * @covers ::get - * @covers ::__construct - * @covers ::cache - * @covers ::getRequestOptions - * @covers ::getUrl - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization - */ - public function testFastRequestFailure() : void { - // Override environment name so we don't fallback to mock responses. - $this->environmentResolverConfiguration[EnvironmentResolver::ENVIRONMENT_NAME_KEY] = 'test'; - - $client = $this->createMockHttpClient([ - new ConnectException( - 'Test', - $this->prophesize(RequestInterface::class)->reveal(), - ), - ]); - $sut = $this->getSut($client); - - $attempts = 0; - // Make sure only one request is sent if the first request fails. - // This should fail to \OutOfBoundsException from guzzle MockHandler - // if more than one request is sent. - for ($i = 0; $i < 50; $i++) { - try { - $sut->get('fi', 'footer-bottom-navigation'); - } - catch (ConnectException) { - $attempts++; - } - } - $this->assertEquals(50, $attempts); - } - - /** - * Make sure cache can be bypassed when configured so. - * - * @covers ::makeRequest - * @covers ::get - * @covers ::__construct - * @covers ::cache - * @covers ::getRequestOptions - * @covers ::withBypassCache - * @covers ::getUrl - * @covers \Drupal\helfi_navigation\CacheValue::hasExpired - * @covers \Drupal\helfi_navigation\CacheValue::__construct - * @covers \Drupal\helfi_navigation\ApiResponse::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::__construct - * @covers \Drupal\helfi_navigation\ApiAuthorization::getAuthorization - */ - public function testCacheBypass() : void { - $requests = []; - $client = $this->createMockHistoryMiddlewareHttpClient($requests, [ - new Response(200, body: json_encode(['value' => 1])), - new Response(200, body: json_encode(['value' => 2])), - ]); - $sut = $this->getSut( - $client, - ); - // Make sure cache is used for all requests. - for ($i = 0; $i < 3; $i++) { - $response = $sut->get('en', 'main'); - $this->assertInstanceOf(\stdClass::class, $response->data); - $this->assertEquals(1, $response->data->value); - } - // Make sure cache is bypassed when configured so and the cached content - // is updated. - $response = $sut->withBypassCache()->get('en', 'main'); - $this->assertInstanceOf(\stdClass::class, $response->data); - $this->assertEquals(2, $response->data->value); - - // withBypassCache() method creates a clone of ApiManager instance to ensure - // cache is only bypassed when explicitly told so. - // We defined only two responses, so this should fail to OutOfBoundException - // if cache was bypassed here. - for ($i = 0; $i < 3; $i++) { - $response = $sut->get('en', 'main'); - $this->assertInstanceOf(\stdClass::class, $response->data); - $this->assertEquals(2, $response->data->value); - } - } - }