forked from firebase/firebase-admin-node
/
firestore.spec.ts
215 lines (189 loc) · 7.71 KB
/
firestore.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
/*!
* @license
* 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';
import * as _ from 'lodash';
import { expect } from 'chai';
import * as mocks from '../../resources/mocks';
import { FirebaseApp } from '../../../src/app/firebase-app';
import {
ComputeEngineCredential, RefreshTokenCredential
} from '../../../src/app/credential-internal';
import { FirestoreService, getFirestoreOptions } from '../../../src/firestore/firestore-internal';
import { DEFAULT_DATABASE_ID } from '@google-cloud/firestore/build/src/path';
describe('Firestore', () => {
let mockApp: FirebaseApp;
let mockCredentialApp: FirebaseApp;
let projectIdApp: FirebaseApp;
let firestore: any;
let appCredentials: string | undefined;
let googleCloudProject: string | undefined;
let gcloudProject: string | undefined;
const invalidCredError = 'Failed to initialize Google Cloud Firestore client with the available '
+ 'credentials. Must initialize the SDK with a certificate credential or application default '
+ 'credentials to use Cloud Firestore API.';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: firebaseVersion } = require('../../../package.json');
const defaultCredentialApps = [
{
name: 'ComputeEngineCredentials',
app: mocks.appWithOptions({
credential: new ComputeEngineCredential(),
}),
},
{
name: 'RefreshTokenCredentials',
app: mocks.appWithOptions({
credential: new RefreshTokenCredential(mocks.refreshToken, undefined, true),
}),
},
];
beforeEach(() => {
appCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT;
gcloudProject = process.env.GCLOUD_PROJECT;
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
mockApp = mocks.app();
mockCredentialApp = mocks.mockCredentialApp();
projectIdApp = mocks.appWithOptions({
credential: mocks.credential,
projectId: 'explicit-project-id',
});
firestore = new FirestoreService(mockApp);
});
afterEach(() => {
if (appCredentials) {
process.env.GOOGLE_APPLICATION_CREDENTIALS = appCredentials;
} else {
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
}
if (googleCloudProject) {
process.env.GOOGLE_CLOUD_PROJECT = googleCloudProject;
} else {
delete process.env.GOOGLE_CLOUD_PROJECT;
}
if (gcloudProject) {
process.env.GCLOUD_PROJECT = gcloudProject;
} else {
delete process.env.GCLOUD_PROJECT;
}
return mockApp.delete();
});
describe('Initializer', () => {
const invalidApps = [null, NaN, 0, 1, true, false, '', 'a', [], [1, 'a'], {}, { a: 1 }, _.noop];
invalidApps.forEach((invalidApp) => {
it(`should throw given invalid app: ${ JSON.stringify(invalidApp) }`, () => {
expect(() => {
const firestoreAny: any = FirestoreService;
const firestoreService: FirestoreService = new firestoreAny(invalidApp);
return firestoreService.getDatabase(DEFAULT_DATABASE_ID)
}).to.throw('First argument passed to admin.firestore() must be a valid Firebase app instance.');
});
});
it('should throw given no app', () => {
expect(() => {
const firestoreAny: any = FirestoreService;
const firestoreService: FirestoreService = new firestoreAny();
return firestoreService.getDatabase(DEFAULT_DATABASE_ID)
}).to.throw('First argument passed to admin.firestore() must be a valid Firebase app instance.');
});
it('should throw given an invalid credential with project ID', () => {
// Project ID is read from the environment variable, but the credential is unsupported.
process.env.GOOGLE_CLOUD_PROJECT = 'project_id';
expect(() => {
return new FirestoreService(mockCredentialApp).getDatabase(DEFAULT_DATABASE_ID);
}).to.throw(invalidCredError);
});
it('should throw given an invalid credential without project ID', () => {
// Project ID not set in the environment.
delete process.env.GOOGLE_CLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
expect(() => {
return new FirestoreService(mockCredentialApp).getDatabase(DEFAULT_DATABASE_ID);
}).to.throw(invalidCredError);
});
it('should not throw given a valid app', () => {
expect(() => {
return new FirestoreService(mockApp).getDatabase(DEFAULT_DATABASE_ID);
}).not.to.throw();
});
defaultCredentialApps.forEach((config) => {
it(`should not throw given default ${config.name} without project ID`, () => {
// Project ID not set in the environment.
delete process.env.GOOGLE_CLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
expect(() => {
return new FirestoreService(config.app).getDatabase(DEFAULT_DATABASE_ID);
}).not.to.throw();
});
});
});
describe('app', () => {
it('returns the app from the constructor', () => {
// We expect referential equality here
expect(firestore.app).to.equal(mockApp);
});
it('is read-only', () => {
expect(() => {
firestore.app = mockApp;
}).to.throw('Cannot set property app of #<FirestoreService> which has only a getter');
});
});
describe('options.projectId', () => {
it('should return a string when project ID is present in credential', () => {
const options = getFirestoreOptions(mockApp);
expect(options.projectId).to.equal('project_id');
});
it('should return a string when project ID is present in app options', () => {
const options = getFirestoreOptions(projectIdApp);
expect(options.projectId).to.equal('explicit-project-id');
});
defaultCredentialApps.forEach((config) => {
it(`should return a string when GOOGLE_CLOUD_PROJECT is set with ${config.name}`, () => {
process.env.GOOGLE_CLOUD_PROJECT = 'env-project-id';
const options = getFirestoreOptions(config.app);
expect(options.projectId).to.equal('env-project-id');
});
it(`should return a string when GCLOUD_PROJECT is set with ${config.name}`, () => {
process.env.GCLOUD_PROJECT = 'env-project-id';
const options = getFirestoreOptions(config.app);
expect(options.projectId).to.equal('env-project-id');
});
});
});
describe('options.firebaseVersion', () => {
it('should return firebaseVersion when using credential with service account certificate', () => {
const options = getFirestoreOptions(mockApp);
expect(options.firebaseVersion).to.equal(firebaseVersion);
});
defaultCredentialApps.forEach((config) => {
it(`should return firebaseVersion when using default ${config.name}`, () => {
const options = getFirestoreOptions(config.app);
expect(options.firebaseVersion).to.equal(firebaseVersion);
});
});
});
describe('options.preferRest', () => {
it('should not enable preferRest by default', () => {
const options = getFirestoreOptions(mockApp);
expect(options.preferRest).to.be.undefined;
});
it('should enable preferRest if provided', () => {
const options = getFirestoreOptions(mockApp, { preferRest: true });
expect(options.preferRest).to.be.true;
});
});
});