Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirumaramba committed Dec 11, 2024
1 parent dcda638 commit 650735e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/utils/crypto-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions test/unit/utils/crypto-signer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand All @@ -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.');
Expand All @@ -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');
});
});

Expand Down

0 comments on commit 650735e

Please sign in to comment.