Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirumaramba committed Nov 7, 2024
1 parent 9fc3448 commit c10036f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
16 changes: 11 additions & 5 deletions src/utils/api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,13 +1079,10 @@ export class AuthorizedHttpClient extends HttpClient {
requestCopy.headers[authHeader] = `Bearer ${token}`;

let quotaProjectId: string | undefined;
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
}
else if (this.app.options.credential instanceof ApplicationDefaultCredential){
if (this.app.options.credential instanceof ApplicationDefaultCredential) {
quotaProjectId = this.app.options.credential.getQuotaProjectId();
}

quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT || quotaProjectId;
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
}
Expand Down Expand Up @@ -1119,6 +1116,15 @@ export class AuthorizedHttp2Client extends Http2Client {
const authHeader = 'Authorization';
requestCopy.headers[authHeader] = `Bearer ${token}`;

let quotaProjectId: string | undefined;
if (this.app.options.credential instanceof ApplicationDefaultCredential) {
quotaProjectId = this.app.options.credential.getQuotaProjectId();
}
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT || quotaProjectId;
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
}

requestCopy.headers['X-Goog-Api-Client'] = getMetricsHeader()

return super.send(requestCopy);
Expand Down
5 changes: 2 additions & 3 deletions test/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import minimist = require('minimist');
import path = require('path');
import { random } from 'lodash';
import {
App, Credential, GoogleOAuthAccessToken, applicationDefault, cert, deleteApp, initializeApp,
App, Credential, GoogleOAuthAccessToken, cert, deleteApp, initializeApp,
} from '../../lib/app/index'

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -87,8 +87,7 @@ before(() => {
storageBucket = projectId + '.appspot.com';

defaultApp = initializeApp({
//...getCredential(),
credential: applicationDefault(),
...getCredential(),
projectId,
databaseURL: databaseUrl,
storageBucket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ describe('Extension API client', () => {
const EXPECTED_HEADERS = {
'Authorization': 'Bearer mock-token',
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
'x-goog-user-project': 'test-project',
'X-Goog-Api-Client': getMetricsHeader(),
}

Expand Down
44 changes: 41 additions & 3 deletions test/unit/utils/api-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

'use strict';

import * as _ from 'lodash';
import * as chai from 'chai';
import * as nock from 'nock';
import * as sinon from 'sinon';
Expand All @@ -35,6 +36,7 @@ import {
import { deepCopy } from '../../../src/utils/deep-copy';
import { Agent } from 'http';
import * as zlib from 'zlib';
import { getMetricsHeader } from '../../../src/utils';

chai.should();
chai.use(sinonChai);
Expand Down Expand Up @@ -2569,9 +2571,6 @@ describe('AuthorizedHttpClient', () => {
afterEach(() => {
transportSpy!.restore();
transportSpy = null;
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
delete process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
}
return mockAppWithAgent.delete();
});

Expand Down Expand Up @@ -2651,6 +2650,45 @@ describe('AuthorizedHttpClient', () => {
});
});

describe('Quota Project', () => {
let stubs: sinon.SinonStub[] = [];

afterEach(() => {
_.forEach(stubs, (stub) => stub.restore());
stubs = [];
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
delete process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
}
});

it('should include quota project id in headers when GOOGLE_CLOUD_QUOTA_PROJECT is set', () => {
const reqData = { request: 'data' };
const stub = sinon
.stub(HttpClient.prototype, 'send')
.resolves(utils.responseFrom({}, 200));
stubs.push(stub);
process.env.GOOGLE_CLOUD_QUOTA_PROJECT = 'test-project-id';
const client = new AuthorizedHttpClient(mockApp);
return client.send({
method: 'POST',
url: mockUrl,
data: reqData,
})
.then(() => {
expect(stub).to.have.been.calledOnce.and.calledWith({
method: 'POST',
url: mockUrl,
headers: {
...requestHeaders.reqheaders,
'x-goog-user-project': 'test-project-id',
'X-Goog-Api-Client': getMetricsHeader(),
},
data: reqData
});
});
});
});

it('should not mutate the arguments', () => {
const reqData = { request: 'data' };
const options = {
Expand Down

0 comments on commit c10036f

Please sign in to comment.