From 1fa2444daf283a3ff1ae0de4d714f98c079e3e38 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Wed, 27 May 2026 11:52:53 -0400 Subject: [PATCH] fix: resolve failing getApplicationDefault unit tests in gcloud SDK environment --- src/utils/crypto-signer.ts | 2 +- test/unit/app/credential-internal.spec.ts | 28 ++++++++++++++++++----- test/unit/utils/crypto-signer.spec.ts | 1 - 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/utils/crypto-signer.ts b/src/utils/crypto-signer.ts index ffa23d26a8..7ff00d5297 100644 --- a/src/utils/crypto-signer.ts +++ b/src/utils/crypto-signer.ts @@ -79,7 +79,7 @@ export class ServiceAccountSigner implements CryptoSigner { * @inheritDoc */ public sign(buffer: Buffer): Promise { - const crypto = require('node:crypto'); // eslint-disable-line @typescript-eslint/no-var-requires + const crypto = require('node:crypto'); const sign = crypto.createSign('RSA-SHA256'); sign.update(buffer); return Promise.resolve(sign.sign(this.credential.privateKey)); diff --git a/test/unit/app/credential-internal.spec.ts b/test/unit/app/credential-internal.spec.ts index 2d56a74dd0..b269735921 100644 --- a/test/unit/app/credential-internal.spec.ts +++ b/test/unit/app/credential-internal.spec.ts @@ -26,6 +26,7 @@ import * as chai from 'chai'; import * as sinon from 'sinon'; import * as sinonChai from 'sinon-chai'; import * as chaiAsPromised from 'chai-as-promised'; +import { UserRefreshClient } from 'google-auth-library'; import * as mocks from '../../resources/mocks'; @@ -68,11 +69,16 @@ describe('Credential', () => { beforeEach(() => { mockCertificateObject = _.clone(mocks.certificateObject); - oldProcessEnv = process.env; + oldProcessEnv = { ...process.env }; }); afterEach(() => { - process.env = oldProcessEnv; + for (const key of Object.keys(process.env)) { + if (!(key in oldProcessEnv)) { + delete process.env[key]; + } + } + Object.assign(process.env, oldProcessEnv); }); describe('ServiceAccountCredential', () => { @@ -310,7 +316,7 @@ describe('Credential', () => { expect(c).to.be.an.instanceof(ApplicationDefaultCredential); }); - it('should return a RefreshTokenCredential with gcloud login', () => { + it('should return a RefreshTokenCredential with gcloud login', async () => { if (!fs.existsSync(GCLOUD_CREDENTIAL_PATH)) { // tslint:disable-next-line:no-console console.log( @@ -319,7 +325,12 @@ describe('Credential', () => { return; } delete process.env.GOOGLE_APPLICATION_CREDENTIALS; - expect((getApplicationDefault())).to.be.an.instanceof(RefreshTokenCredential); + const c = getApplicationDefault(); + expect(c).to.be.an.instanceof(ApplicationDefaultCredential); + + process.env.GOOGLE_CLOUD_PROJECT = 'mock-project'; + const client = await (c as any).googleAuth.getClient(); + expect(client).to.be.an.instanceof(UserRefreshClient); }); it('should return a MetadataServiceCredential as a last resort', () => { @@ -345,7 +356,7 @@ describe('Credential', () => { expect(isApplicationDefault(c)).to.be.true; }); - it('should return true for credential loaded from gcloud SDK', () => { + it('should return true for credential loaded from gcloud SDK', async () => { if (!fs.existsSync(GCLOUD_CREDENTIAL_PATH)) { // tslint:disable-next-line:no-console console.log( @@ -355,7 +366,12 @@ describe('Credential', () => { } delete process.env.GOOGLE_APPLICATION_CREDENTIALS; const c = getApplicationDefault(); - expect(c).to.be.an.instanceof(RefreshTokenCredential); + expect(c).to.be.an.instanceof(ApplicationDefaultCredential); + + process.env.GOOGLE_CLOUD_PROJECT = 'mock-project'; + const client = await (c as any).googleAuth.getClient(); + expect(client).to.be.an.instanceof(UserRefreshClient); + expect(isApplicationDefault(c)).to.be.true; }); diff --git a/test/unit/utils/crypto-signer.spec.ts b/test/unit/utils/crypto-signer.spec.ts index 26bfc7cb38..5c3a8999fc 100644 --- a/test/unit/utils/crypto-signer.spec.ts +++ b/test/unit/utils/crypto-signer.spec.ts @@ -56,7 +56,6 @@ describe('CryptoSigner', () => { const payload = Buffer.from('test'); const cert = new ServiceAccountCredential(mocks.certificateObject); - // eslint-disable-next-line @typescript-eslint/no-var-requires const crypto = require('node:crypto'); const rsa = crypto.createSign('RSA-SHA256'); rsa.update(payload);