From 650735ee589be1210e6703b5cb61d404837f01e8 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Mon, 9 Dec 2024 16:38:58 -0500 Subject: [PATCH] fix tests --- src/utils/crypto-signer.ts | 14 ++++++++------ test/unit/utils/crypto-signer.spec.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/utils/crypto-signer.ts b/src/utils/crypto-signer.ts index 3c64be7e96..cbb5891a0d 100644 --- a/src/utils/crypto-signer.ts +++ b/src/utils/crypto-signer.ts @@ -108,14 +108,14 @@ export class IAMSigner implements CryptoSigner { private serviceAccountId?: string; private app?: App; - constructor(httpClient: AuthorizedHttpClient, app: App) { + constructor(httpClient: AuthorizedHttpClient, app?: App) { if (!httpClient) { throw new CryptoSignerError({ code: CryptoSignerErrorCode.INVALID_ARGUMENT, message: 'INTERNAL ASSERT: Must provide a HTTP client to initialize IAMSigner.', }); } - if (typeof app !== 'object' || app === null || !('options' in app)) { + if (app && (typeof app !== 'object' || app === null || !('options' in app))) { throw new CryptoSignerError({ code: CryptoSignerErrorCode.INVALID_ARGUMENT, message: 'INTERNAL ASSERT: Must provide a valid Firebase app instance.', @@ -158,10 +158,12 @@ export class IAMSigner implements CryptoSigner { if (validator.isNonEmptyString(this.serviceAccountId)) { return Promise.resolve(this.serviceAccountId); } - const accountId = await utils.findServiceAccountEmail(this.app!) - if (accountId) { - this.serviceAccountId = accountId; - return Promise.resolve(accountId); + if (this.app) { + const accountId = await utils.findServiceAccountEmail(this.app!) + if (accountId) { + this.serviceAccountId = accountId; + return Promise.resolve(accountId); + } } const request: HttpRequestConfig = { method: 'GET', diff --git a/test/unit/utils/crypto-signer.spec.ts b/test/unit/utils/crypto-signer.spec.ts index 024be3ecbe..cf38160a2f 100644 --- a/test/unit/utils/crypto-signer.spec.ts +++ b/test/unit/utils/crypto-signer.spec.ts @@ -103,7 +103,7 @@ describe('CryptoSigner', () => { const input = Buffer.from('input'); const signRequest = { method: 'POST', - url: 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/test-service-account:signBlob', + url: 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/foo@project_id.iam.gserviceaccount.com:signBlob', headers: { Authorization: `Bearer ${mockAccessToken}`, 'X-Goog-Api-Client': getMetricsHeader() @@ -120,7 +120,7 @@ describe('CryptoSigner', () => { const expectedResult = utils.responseFrom(response); stub = sinon.stub(HttpClient.prototype, 'send').resolves(expectedResult); const requestHandler = new AuthorizedHttpClient(mockApp); - const signer = new IAMSigner(requestHandler, 'test-service-account'); + const signer = new IAMSigner(requestHandler, mockApp); return signer.sign(input).then((signature) => { expect(signature.toString('base64')).to.equal(response.signedBlob); expect(stub).to.have.been.calledOnce.and.calledWith(signRequest); @@ -136,7 +136,7 @@ describe('CryptoSigner', () => { }); stub = sinon.stub(HttpClient.prototype, 'send').rejects(expectedResult); const requestHandler = new AuthorizedHttpClient(mockApp); - const signer = new IAMSigner(requestHandler, 'test-service-account'); + const signer = new IAMSigner(requestHandler, mockApp); return signer.sign(input).catch((err) => { expect(err).to.be.instanceOf(CryptoSignerError); expect(err.message).to.equal('Server responded with status 500.'); @@ -145,9 +145,9 @@ describe('CryptoSigner', () => { }); }); - it('should return the explicitly specified service account', () => { - const signer = new IAMSigner(new AuthorizedHttpClient(mockApp), 'test-service-account'); - return signer.getAccountId().should.eventually.equal('test-service-account'); + it('should return the service account from the app', () => { + const signer = new IAMSigner(new AuthorizedHttpClient(mockApp), mockApp); + return signer.getAccountId().should.eventually.equal('foo@project_id.iam.gserviceaccount.com'); }); });