Skip to content

Commit

Permalink
test: add ApplicationDefaultCredentials tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnorris committed Sep 26, 2020
1 parent b2dae18 commit 2c6b66e
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions tests/Unit/Credentials/ApplicationDefaultCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

use ericnorris\GCPAuthContrib\Contracts\Credentials;
use ericnorris\GCPAuthContrib\Credentials\ApplicationDefaultCredentials;
use ericnorris\GCPAuthContrib\Credentials\AuthorizedUserCredentials;
use ericnorris\GCPAuthContrib\Credentials\MetadataServerCredentials;
Expand Down Expand Up @@ -40,13 +41,13 @@ public function setUp(): void {

$this->vfs = vfsStream::setup('root', null, $structure);

$this->mockCredentialsFactory = $this->createStub(CredentialsFactory::class);
$this->mockCredentialsFactory = $this->createMock(CredentialsFactory::class);
}

/**
* @dataProvider envVarPathProvider
*/
public function testLazyLoad(array $envVars, string $expectedClass) {
public function testLazyLoad(array $envVars, string $expectedClass): void {
foreach ($envVars as $name => $path) {
$vfsPath = $this->vfs->getChild($path)->url();

Expand All @@ -71,6 +72,52 @@ public function testLazyLoad(array $envVars, string $expectedClass) {
}
}

public function testPassthrough(): void {
$mockCredentials = $this->createMock(MetadataServerCredentials::class);

$this->mockCredentialsFactory
->method("makeMetadataServerCredentials")
->willReturn($mockCredentials);

$adc = new ApplicationDefaultCredentials($this->mockCredentialsFactory);

$mockCredentials
->expects($this->once())
->method("fetchAccessToken");

$adc->fetchAccessToken();

$mockCredentials
->expects($this->once())
->method("fetchIdentityToken");

$adc->fetchIdentityToken("https://example.com");

$mockCredentials
->expects($this->once())
->method("fetchProjectID");

$adc->fetchProjectID();

$mockCredentials
->expects($this->once())
->method("fetchServiceAccountEmail");

$adc->fetchServiceAccountEmail();

$mockCredentials
->expects($this->once())
->method("generateSignature");

$adc->generateSignature("toSign");

$mockCredentials
->expects($this->once())
->method("supportsCapability");

$adc->supportsCapability(Credentials::CAN_FETCH_PROJECT_ID);
}

public function envVarPathProvider(): array {
$serviceAccountAtWellKnownEnvVarSetup = [
ApplicationDefaultCredentials::WELL_KNOWN_ENV_VAR => "service-account-file",
Expand Down

0 comments on commit 2c6b66e

Please sign in to comment.