Skip to content

Commit 9f97343

Browse files
committed
update tests
1 parent 760e34b commit 9f97343

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

src/app/credential-internal.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ export class RefreshTokenCredential implements Credential {
260260

261261
class RefreshToken {
262262

263-
// public readonly clientId: string;
264-
// public readonly clientSecret: string;
265-
// public readonly refreshToken: string;
266-
// public readonly type: string;
263+
public readonly clientId: string;
264+
public readonly clientSecret: string;
265+
public readonly refreshToken: string;
266+
public readonly type: string;
267267

268268
/*
269269
* Tries to load a RefreshToken from a path. Throws if the path doesn't exist or the
@@ -283,18 +283,21 @@ class RefreshToken {
283283

284284
public static validateFromJSON(json: object): void {
285285

286-
const {
287-
client_id: clientId, client_secret: clientSecret, refresh_token: refreshToken, type
288-
} = (json as { [key: string]: any });
286+
const creds = { clientId: '', clientSecret: '', refreshToken: '', type: '' };
287+
288+
copyAttr(creds, json, 'clientId', 'client_id');
289+
copyAttr(creds, json, 'clientSecret', 'client_secret');
290+
copyAttr(creds, json, 'refreshToken', 'refresh_token');
291+
copyAttr(creds, json, 'type', 'type');
289292

290293
let errorMessage;
291-
if (!util.isNonEmptyString(clientId)) {
294+
if (!util.isNonEmptyString(creds.clientId)) {
292295
errorMessage = 'Refresh token must contain a "client_id" property.';
293-
} else if (!util.isNonEmptyString(clientSecret)) {
296+
} else if (!util.isNonEmptyString(creds.clientSecret)) {
294297
errorMessage = 'Refresh token must contain a "client_secret" property.';
295-
} else if (!util.isNonEmptyString(refreshToken)) {
298+
} else if (!util.isNonEmptyString(creds.refreshToken)) {
296299
errorMessage = 'Refresh token must contain a "refresh_token" property.';
297-
} else if (!util.isNonEmptyString(type)) {
300+
} else if (!util.isNonEmptyString(creds.type)) {
298301
errorMessage = 'Refresh token must contain a "type" property.';
299302
}
300303

test/resources/mocks.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as jwt from 'jsonwebtoken';
2828
import { AppOptions } from '../../src/firebase-namespace-api';
2929
import { FirebaseApp } from '../../src/app/firebase-app';
3030
import { Credential, GoogleOAuthAccessToken, cert } from '../../src/app/index';
31-
import { ComputeEngineCredential } from '../../src/app/credential-internal';
31+
import { ApplicationDefaultCredential } from '../../src/app/credential-internal';
3232

3333
const ALGORITHM = 'RS256' as const;
3434
const ONE_HOUR_IN_SECONDS = 60 * 60;
@@ -91,7 +91,7 @@ export class MockCredential implements Credential {
9191
}
9292
}
9393

94-
export class MockComputeEngineCredential extends ComputeEngineCredential {
94+
export class MockComputeEngineCredential extends ApplicationDefaultCredential {
9595
public getAccessToken(): Promise<GoogleOAuthAccessToken> {
9696
return Promise.resolve({
9797
access_token: 'mock-token',
@@ -102,6 +102,10 @@ export class MockComputeEngineCredential extends ComputeEngineCredential {
102102
public getIDToken(): Promise<string> {
103103
return Promise.resolve('mockIdToken');
104104
}
105+
106+
public isComputeEngineCredential(): Promise<boolean> {
107+
return Promise.resolve(true);
108+
}
105109
}
106110

107111
export function app(altName?: string): FirebaseApp {

test/unit/app/credential-internal.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from '../../../src/app/index';
3737
import {
3838
RefreshTokenCredential, ServiceAccountCredential,
39-
ComputeEngineCredential, getApplicationDefault, isApplicationDefault, ImpersonatedServiceAccountCredential
39+
getApplicationDefault, isApplicationDefault, ImpersonatedServiceAccountCredential, ApplicationDefaultCredential
4040
} from '../../../src/app/credential-internal';
4141
import { HttpClient } from '../../../src/utils/api-request';
4242
import { Agent } from 'https';
@@ -332,7 +332,7 @@ describe('Credential', () => {
332332
const response = utils.responseFrom(expected);
333333
httpStub.resolves(response);
334334

335-
const c = new ComputeEngineCredential();
335+
const c = new ApplicationDefaultCredential();
336336
return c.getAccessToken().then((token) => {
337337
expect(token.access_token).to.equal('anAccessToken');
338338
expect(token.expires_in).to.equal(42);
@@ -350,7 +350,7 @@ describe('Credential', () => {
350350
const response = utils.responseFrom(expected);
351351
httpStub.resolves(response);
352352

353-
const c = new ComputeEngineCredential();
353+
const c = new ApplicationDefaultCredential();
354354
return c.getIDToken('my-audience.cloudfunctions.net').then((token) => {
355355
expect(token).to.equal(expected);
356356
expect(httpStub).to.have.been.calledOnce.and.calledWith({
@@ -367,7 +367,7 @@ describe('Credential', () => {
367367
const response = utils.responseFrom(expectedProjectId);
368368
httpStub.resolves(response);
369369

370-
const c = new ComputeEngineCredential();
370+
const c = new ApplicationDefaultCredential();
371371
return c.getProjectId().then((projectId) => {
372372
expect(projectId).to.equal(expectedProjectId);
373373
expect(httpStub).to.have.been.calledOnce.and.calledWith({
@@ -384,7 +384,7 @@ describe('Credential', () => {
384384
const response = utils.responseFrom(expectedProjectId);
385385
httpStub.resolves(response);
386386

387-
const c = new ComputeEngineCredential();
387+
const c = new ApplicationDefaultCredential();
388388
return c.getProjectId()
389389
.then((projectId) => {
390390
expect(projectId).to.equal(expectedProjectId);
@@ -404,7 +404,7 @@ describe('Credential', () => {
404404
it('should reject when the metadata service is not available', () => {
405405
httpStub.rejects(new FirebaseAppError('network-error', 'Failed to connect'));
406406

407-
const c = new ComputeEngineCredential();
407+
const c = new ApplicationDefaultCredential();
408408
return c.getProjectId().should.eventually
409409
.rejectedWith('Failed to determine project ID: Failed to connect')
410410
.and.have.property('code', 'app/invalid-credential');
@@ -414,7 +414,7 @@ describe('Credential', () => {
414414
const response = utils.errorFrom('Unexpected error');
415415
httpStub.rejects(response);
416416

417-
const c = new ComputeEngineCredential();
417+
const c = new ApplicationDefaultCredential();
418418
return c.getProjectId().should.eventually
419419
.rejectedWith('Failed to determine project ID: Unexpected error')
420420
.and.have.property('code', 'app/invalid-credential');
@@ -562,7 +562,7 @@ describe('Credential', () => {
562562
it('should return a MetadataServiceCredential as a last resort', () => {
563563
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
564564
fsStub = sinon.stub(fs, 'readFileSync').throws(new Error('no gcloud credential file'));
565-
expect(getApplicationDefault()).to.be.an.instanceof(ComputeEngineCredential);
565+
expect(getApplicationDefault()).to.be.an.instanceof(ApplicationDefaultCredential);
566566
});
567567

568568
it('should create access tokens', () => {
@@ -653,7 +653,7 @@ describe('Credential', () => {
653653
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
654654
fsStub = sinon.stub(fs, 'readFileSync').throws(new Error('no gcloud credential file'));
655655
const c = getApplicationDefault();
656-
expect(c).to.be.an.instanceof(ComputeEngineCredential);
656+
expect(c).to.be.an.instanceof(ApplicationDefaultCredential);
657657
expect(isApplicationDefault(c)).to.be.true;
658658
});
659659

@@ -720,7 +720,7 @@ describe('Credential', () => {
720720

721721
it('ComputeEngineCredential should use the provided HTTP Agent', () => {
722722
const agent = new Agent();
723-
const c = new ComputeEngineCredential(agent);
723+
const c = new ApplicationDefaultCredential(agent);
724724
return c.getAccessToken().then((token) => {
725725
expect(token.access_token).to.equal(expectedToken);
726726
expect(stub).to.have.been.calledOnce;

test/unit/firestore/firestore.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { expect } from 'chai';
2323
import * as mocks from '../../resources/mocks';
2424
import { FirebaseApp } from '../../../src/app/firebase-app';
2525
import {
26-
ComputeEngineCredential, RefreshTokenCredential
26+
ApplicationDefaultCredential,
27+
RefreshTokenCredential
2728
} from '../../../src/app/credential-internal';
2829
import { FirestoreService, getFirestoreOptions } from '../../../src/firestore/firestore-internal';
2930
import { DEFAULT_DATABASE_ID } from '@google-cloud/firestore/build/src/path';
@@ -48,7 +49,7 @@ describe('Firestore', () => {
4849
{
4950
name: 'ComputeEngineCredentials',
5051
app: mocks.appWithOptions({
51-
credential: new ComputeEngineCredential(),
52+
credential: new ApplicationDefaultCredential(),
5253
}),
5354
},
5455
{

test/unit/utils/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import {
2626
} from '../../../src/utils/index';
2727
import { isNonEmptyString } from '../../../src/utils/validator';
2828
import { FirebaseApp } from '../../../src/app/firebase-app';
29-
import { ComputeEngineCredential } from '../../../src/app/credential-internal';
3029
import { HttpClient } from '../../../src/utils/api-request';
3130
import * as utils from '../utils';
3231
import { FirebaseAppError } from '../../../src/utils/error';
3332
import { getSdkVersion } from '../../../src/utils/index';
33+
import { ApplicationDefaultCredential } from '../../../src/app/credential-internal';
3434

3535
interface Obj {
3636
[key: string]: any;
@@ -203,15 +203,15 @@ describe('findProjectId()', () => {
203203
const response = utils.responseFrom(expectedProjectId);
204204
httpStub.resolves(response);
205205
const app: FirebaseApp = mocks.appWithOptions({
206-
credential: new ComputeEngineCredential(),
206+
credential: new ApplicationDefaultCredential(),
207207
});
208208
return findProjectId(app).should.eventually.equal(expectedProjectId);
209209
});
210210

211211
it('should reject when the metadata service is not available', () => {
212212
httpStub.rejects(new FirebaseAppError('network-error', 'Failed to connect'));
213213
const app: FirebaseApp = mocks.appWithOptions({
214-
credential: new ComputeEngineCredential(),
214+
credential: new ApplicationDefaultCredential(),
215215
});
216216
return findProjectId(app).should.eventually
217217
.rejectedWith('Failed to determine project ID: Failed to connect');

0 commit comments

Comments
 (0)