Skip to content

Commit df00e0d

Browse files
authored
Running Unit Tests without gcloud SDK in Automated Environments (#29)
* Adding Travis config * Making it possible to run application default credential tests without having to install and authorize gcloud SDK * Updated variable names * Fix for read-only property test in Nodejs 5 and below * Fixing a type check in test cases
1 parent 33d3216 commit df00e0d

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- "7"
4+
- "6"
5+
- "5"
6+
- "4"

test/resources/mocks.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import * as jwt from 'jsonwebtoken';
2727
import {FirebaseNamespace} from '../../src/firebase-namespace';
2828
import {FirebaseServiceInterface} from '../../src/firebase-service';
2929
import {FirebaseApp, FirebaseAppOptions} from '../../src/firebase-app';
30-
import {CertCredential, ApplicationDefaultCredential} from '../../src/auth/credential';
30+
import {Certificate, Credential, CertCredential, GoogleOAuthAccessToken} from '../../src/auth/credential';
3131

3232
const ALGORITHM = 'RS256';
3333
const ONE_HOUR_IN_SECONDS = 60 * 60;
@@ -60,15 +60,28 @@ export let appOptionsNoDatabaseUrl: FirebaseAppOptions = {
6060
credential,
6161
};
6262

63+
export class MockCredential implements Credential {
64+
public getAccessToken(): Promise<GoogleOAuthAccessToken> {
65+
return Promise.resolve({
66+
access_token: 'mock-token',
67+
expires_in: 3600,
68+
});
69+
}
70+
71+
public getCertificate(): Certificate {
72+
return null;
73+
}
74+
}
75+
6376
export function app(): FirebaseApp {
6477
const namespaceInternals = new FirebaseNamespace().INTERNAL;
6578
namespaceInternals.removeApp = _.noop;
6679
return new FirebaseApp(appOptions, appName, namespaceInternals);
6780
}
6881

69-
export function applicationDefaultApp(): FirebaseApp {
82+
export function mockCredentialApp(): FirebaseApp {
7083
return new FirebaseApp({
71-
credential: new ApplicationDefaultCredential(),
84+
credential: new MockCredential(),
7285
databaseURL,
7386
}, appName, new FirebaseNamespace().INTERNAL);
7487
}

test/unit/auth/auth.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ describe('Auth', () => {
169169
});
170170

171171
it('should throw if a cert credential is not specified', () => {
172-
const applicationDefaultCredentialAuth = new Auth(mocks.applicationDefaultApp());
172+
const mockCredentialAuth = new Auth(mocks.mockCredentialApp());
173173

174174
expect(() => {
175-
applicationDefaultCredentialAuth.createCustomToken(mocks.uid, mocks.developerClaims);
175+
mockCredentialAuth.createCustomToken(mocks.uid, mocks.developerClaims);
176176
}).to.throw('Must initialize app with a cert credential');
177177
});
178178

@@ -215,10 +215,10 @@ describe('Auth', () => {
215215
afterEach(() => stub.restore());
216216

217217
it('should throw if a cert credential is not specified', () => {
218-
const applicationDefaultCredentialAuth = new Auth(mocks.applicationDefaultApp());
218+
const mockCredentialAuth = new Auth(mocks.mockCredentialApp());
219219

220220
expect(() => {
221-
applicationDefaultCredentialAuth.verifyIdToken(mockIdToken);
221+
mockCredentialAuth.verifyIdToken(mockIdToken);
222222
}).to.throw('Must initialize app with a cert credential');
223223
});
224224

@@ -231,9 +231,9 @@ describe('Auth', () => {
231231
it('should work with a non-cert credential when the GCLOUD_PROJECT environment variable is present', () => {
232232
process.env.GCLOUD_PROJECT = mocks.projectId;
233233

234-
const applicationDefaultCredentialAuth = new Auth(mocks.applicationDefaultApp());
234+
const mockCredentialAuth = new Auth(mocks.mockCredentialApp());
235235

236-
return applicationDefaultCredentialAuth.verifyIdToken(mockIdToken).then(() => {
236+
return mockCredentialAuth.verifyIdToken(mockIdToken).then(() => {
237237
expect(stub).to.have.been.calledOnce.and.calledWith(mockIdToken);
238238
});
239239
});

test/unit/firebase.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,21 @@ describe('Firebase', () => {
136136
});
137137

138138
it('should initialize SDK given an application default credential', () => {
139+
let credPath: string;
140+
credPath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
141+
process.env.GOOGLE_APPLICATION_CREDENTIALS = path.resolve(__dirname, '../resources/mock.key.json');
139142
firebaseAdmin.initializeApp({
140143
credential: firebaseAdmin.credential.applicationDefault(),
141144
});
142145

143-
return firebaseAdmin.app().INTERNAL.getToken()
144-
.should.eventually.have.keys(['accessToken', 'expirationTime']);
146+
return firebaseAdmin.app().INTERNAL.getToken().then(token => {
147+
if (typeof credPath === 'undefined') {
148+
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
149+
} else {
150+
process.env.GOOGLE_APPLICATION_CREDENTIALS = credPath;
151+
}
152+
return token;
153+
}).should.eventually.have.keys(['accessToken', 'expirationTime']);
145154
});
146155

147156
// TODO(jwenger): mock out the refresh token endpoint so this test will work

test/unit/utils/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('addReadonlyGetter()', () => {
3636

3737
expect(() => {
3838
obj.foo = false;
39-
}).to.throw('Cannot assign to read only property \'foo\' of object \'#<Object>\'');
39+
}).to.throw(/Cannot assign to read only property \'foo\' of/);
4040
});
4141

4242
it('should make the new property enumerable', () => {

0 commit comments

Comments
 (0)