Skip to content

Commit

Permalink
fix: getCredential(): load credentials with getClient() (#648)
Browse files Browse the repository at this point in the history
* fix: getCredential(): load credentials with getClient()

* npm run fix
  • Loading branch information
jkwlui authored Mar 22, 2019
1 parent ce4f99c commit 5cad41a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ export class GoogleAuth {
}

private async getCredentialsAsync(): Promise<CredentialBody> {
await this.getClient();

if (this.jsonContent) {
const credential: CredentialBody = {
client_email: this.jsonContent.client_email,
Expand Down
22 changes: 22 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,28 @@ describe('googleauth', () => {
assert.strictEqual(jwt.key, body!.private_key);
});

it('getCredentials should call getClient to load credentials', async () => {
// Set up a mock to return path to a valid credentials file.
blockGoogleApplicationCredentialEnvironmentVariable();
mockEnvVar(
'GOOGLE_APPLICATION_CREDENTIALS', './test/fixtures/private.json');

const spy = sinon.spy(auth, 'getClient');
const body = await auth.getCredentials();

const result =
await auth._tryGetApplicationCredentialsFromEnvironmentVariable();
if (!(result instanceof JWT)) {
throw new assert.AssertionError(
{message: 'Credentials are not a JWT object'});
}

assert.notEqual(null, body);
assert(spy.calledOnce);
assert.strictEqual(result.email, body!.client_email);
assert.strictEqual(result.key, body!.private_key);
});

it('getCredentials should handle valid file path', async () => {
// Set up a mock to return path to a valid credentials file.
blockGoogleApplicationCredentialEnvironmentVariable();
Expand Down

0 comments on commit 5cad41a

Please sign in to comment.