-
Notifications
You must be signed in to change notification settings - Fork 356
/
firebase.spec.ts
234 lines (197 loc) · 7.81 KB
/
firebase.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*!
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// Use untyped import syntax for Node built-ins
import path = require('path');
import * as _ from 'lodash';
import * as sinon from 'sinon';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as mocks from '../resources/mocks';
import * as firebaseAdmin from '../../src/index';
import {RefreshTokenCredential, ServiceAccountCredential, isApplicationDefault} from '../../src/auth/credential';
chai.should();
chai.use(chaiAsPromised);
const expect = chai.expect;
describe('Firebase', () => {
let getTokenStub: sinon.SinonStub;
before(() => {
getTokenStub = sinon.stub(ServiceAccountCredential.prototype, 'getAccessToken').resolves({
access_token: 'mock-access-token',
expires_in: 3600,
});
});
after(() => {
getTokenStub.restore();
});
afterEach(() => {
const deletePromises: Array<Promise<void>> = [];
firebaseAdmin.apps.forEach((app) => {
deletePromises.push(app.delete());
});
return Promise.all(deletePromises);
});
describe('#initializeApp()', () => {
const invalidOptions: any[] = [null, NaN, 0, 1, true, false, '', 'a', [], _.noop];
invalidOptions.forEach((invalidOption: any) => {
it('should throw given invalid options object: ' + JSON.stringify(invalidOption), () => {
expect(() => {
firebaseAdmin.initializeApp(invalidOption);
}).to.throw('Invalid Firebase app options');
});
});
it('should use application default credentials when no credentials are explicitly specified', () => {
const app = firebaseAdmin.initializeApp(mocks.appOptionsNoAuth);
expect(app.options).to.have.property('credential');
expect(app.options.credential).to.not.be.undefined;
});
it('should not modify the provided options object', () => {
const optionsClone = _.clone(mocks.appOptions);
firebaseAdmin.initializeApp(mocks.appOptions);
expect(optionsClone).to.deep.equal(mocks.appOptions);
});
const invalidCredentials = [undefined, null, NaN, 0, 1, '', 'a', true, false, '', _.noop];
invalidCredentials.forEach((invalidCredential) => {
it('should throw given non-object credential: ' + JSON.stringify(invalidCredential), () => {
expect(() => {
firebaseAdmin.initializeApp({
credential: invalidCredential as any,
});
}).to.throw('Invalid Firebase app options');
});
});
it('should throw given a credential which doesn\'t implement the Credential interface', () => {
expect(() => {
firebaseAdmin.initializeApp({
credential: {},
} as any);
}).to.throw('Invalid Firebase app options');
expect(() => {
firebaseAdmin.initializeApp({
credential: {
getAccessToken: true,
},
} as any);
}).to.throw('Invalid Firebase app options');
});
it('should initialize SDK given a cert credential with a certificate object', () => {
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(mocks.certificateObject),
});
expect(isApplicationDefault(firebaseAdmin.app().options.credential)).to.be.false;
return firebaseAdmin.app().INTERNAL.getToken()
.should.eventually.have.keys(['accessToken', 'expirationTime']);
});
it('should initialize SDK given a cert credential with a valid path to a certificate key file', () => {
const keyPath = path.resolve(__dirname, '../resources/mock.key.json');
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(keyPath),
});
expect(isApplicationDefault(firebaseAdmin.app().options.credential)).to.be.false;
return firebaseAdmin.app().INTERNAL.getToken()
.should.eventually.have.keys(['accessToken', 'expirationTime']);
});
it('should initialize SDK given an application default credential', () => {
let credPath: string | undefined;
credPath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
process.env.GOOGLE_APPLICATION_CREDENTIALS = path.resolve(__dirname, '../resources/mock.key.json');
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.applicationDefault(),
});
expect(isApplicationDefault(firebaseAdmin.app().options.credential)).to.be.true;
return firebaseAdmin.app().INTERNAL.getToken().then((token) => {
if (typeof credPath === 'undefined') {
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
} else {
process.env.GOOGLE_APPLICATION_CREDENTIALS = credPath;
}
return token;
}).should.eventually.have.keys(['accessToken', 'expirationTime']);
});
it('should initialize SDK given a refresh token credential', () => {
getTokenStub.restore();
getTokenStub = sinon.stub(RefreshTokenCredential.prototype, 'getAccessToken')
.resolves({
access_token: 'mock-access-token',
expires_in: 3600,
});
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.refreshToken(mocks.refreshToken),
});
expect(isApplicationDefault(firebaseAdmin.app().options.credential)).to.be.false;
return firebaseAdmin.app().INTERNAL.getToken()
.should.eventually.have.keys(['accessToken', 'expirationTime']);
});
});
describe('#database()', () => {
it('should throw if the app has not be initialized', () => {
expect(() => {
return firebaseAdmin.database();
}).to.throw('The default Firebase app does not exist.');
});
it('should throw given no databaseURL key when initializing the app', () => {
firebaseAdmin.initializeApp(mocks.appOptionsNoDatabaseUrl);
expect(() => {
firebaseAdmin.database();
}).to.throw('Can\'t determine Firebase Database URL');
});
it('should return the database service', () => {
firebaseAdmin.initializeApp(mocks.appOptions);
expect(() => {
return firebaseAdmin.database();
}).not.to.throw();
});
});
describe('#auth', () => {
it('should throw if the app has not be initialized', () => {
expect(() => {
return firebaseAdmin.auth();
}).to.throw('The default Firebase app does not exist.');
});
it('should return the auth service', () => {
firebaseAdmin.initializeApp(mocks.appOptions);
expect(() => {
return firebaseAdmin.auth();
}).not.to.throw();
});
});
describe('#messaging', () => {
it('should throw if the app has not be initialized', () => {
expect(() => {
return firebaseAdmin.messaging();
}).to.throw('The default Firebase app does not exist.');
});
it('should return the messaging service', () => {
firebaseAdmin.initializeApp(mocks.appOptions);
expect(() => {
return firebaseAdmin.messaging();
}).not.to.throw();
});
});
describe('#storage', () => {
it('should throw if the app has not be initialized', () => {
expect(() => {
return firebaseAdmin.storage();
}).to.throw('The default Firebase app does not exist.');
});
it('should return the storage service', () => {
firebaseAdmin.initializeApp(mocks.appOptions);
expect(() => {
return firebaseAdmin.storage();
}).not.to.throw();
});
});
});