-
Notifications
You must be signed in to change notification settings - Fork 891
/
remote_config.test.ts
521 lines (413 loc) · 16.3 KB
/
remote_config.test.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* @license
* Copyright 2019 Google LLC
*
* 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.
*/
import { FirebaseApp } from '@firebase/app';
import {
RemoteConfig as RemoteConfigType,
LogLevel as RemoteConfigLogLevel
} from '../src/public_types';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { StorageCache } from '../src/storage/storage_cache';
import { Storage } from '../src/storage/storage';
import { RemoteConfig } from '../src/remote_config';
import {
RemoteConfigFetchClient,
FetchResponse
} from '../src/client/remote_config_fetch_client';
import { Value } from '../src/value';
import './setup';
import { ERROR_FACTORY, ErrorCode } from '../src/errors';
import { Logger, LogLevel as FirebaseLogLevel } from '@firebase/logger';
import {
activate,
ensureInitialized,
getAll,
getBoolean,
getNumber,
getString,
getValue,
setLogLevel,
fetchConfig
} from '../src/api';
import * as api from '../src/api';
import { fetchAndActivate } from '../src';
import { restore } from 'sinon';
describe('RemoteConfig', () => {
const ACTIVE_CONFIG = {
key1: 'active_config_value_1',
key2: 'active_config_value_2',
key3: 'true',
key4: '123'
};
const DEFAULT_CONFIG = {
key1: 'default_config_value_1',
key2: 'default_config_value_2',
key3: 'false',
key4: '345',
test: 'test'
};
let app: FirebaseApp;
let client: RemoteConfigFetchClient;
let storageCache: StorageCache;
let storage: Storage;
let logger: Logger;
let rc: RemoteConfigType;
let getActiveConfigStub: sinon.SinonStub;
let loggerDebugSpy: sinon.SinonSpy;
let loggerLogLevelSpy: any;
beforeEach(() => {
// Clears stubbed behavior between each test.
app = {} as FirebaseApp;
client = {} as RemoteConfigFetchClient;
storageCache = {} as StorageCache;
storage = {} as Storage;
logger = new Logger('package-name');
getActiveConfigStub = sinon.stub().returns(undefined);
storageCache.getActiveConfig = getActiveConfigStub;
loggerDebugSpy = sinon.spy(logger, 'debug');
loggerLogLevelSpy = sinon.spy(logger, 'logLevel', ['set']);
rc = new RemoteConfig(app, client, storageCache, storage, logger);
});
afterEach(() => {
loggerDebugSpy.restore();
loggerLogLevelSpy.restore();
});
// Adapts getUserLanguage tests from packages/auth/test/utils_test.js for TypeScript.
describe('setLogLevel', () => {
it('proxies to the FirebaseLogger instance', () => {
setLogLevel(rc, 'debug');
// Casts spy to any because property setters aren't defined on the SinonSpy type.
expect(loggerLogLevelSpy.set).to.have.been.calledWith(
FirebaseLogLevel.DEBUG
);
});
it('normalizes levels other than DEBUG and SILENT to ERROR', () => {
for (const logLevel of ['info', 'verbose', 'error', 'severe']) {
setLogLevel(rc, logLevel as RemoteConfigLogLevel);
// Casts spy to any because property setters aren't defined on the SinonSpy type.
expect(loggerLogLevelSpy.set).to.have.been.calledWith(
FirebaseLogLevel.ERROR
);
}
});
});
describe('ensureInitialized', () => {
it('warms cache', async () => {
storageCache.loadFromStorage = sinon.stub().returns(Promise.resolve());
await ensureInitialized(rc);
expect(storageCache.loadFromStorage).to.have.been.calledOnce;
});
it('de-duplicates repeated calls', async () => {
storageCache.loadFromStorage = sinon.stub().returns(Promise.resolve());
await ensureInitialized(rc);
await ensureInitialized(rc);
expect(storageCache.loadFromStorage).to.have.been.calledOnce;
});
});
describe('fetchTimeMillis', () => {
it('normalizes undefined values', async () => {
storageCache.getLastSuccessfulFetchTimestampMillis = sinon
.stub()
.returns(undefined);
expect(rc.fetchTimeMillis).to.eq(-1);
});
it('reads from cache', async () => {
const lastFetchTimeMillis = 123;
storageCache.getLastSuccessfulFetchTimestampMillis = sinon
.stub()
.returns(lastFetchTimeMillis);
expect(rc.fetchTimeMillis).to.eq(lastFetchTimeMillis);
});
});
describe('lastFetchStatus', () => {
it('normalizes undefined values', async () => {
storageCache.getLastFetchStatus = sinon.stub().returns(undefined);
expect(rc.lastFetchStatus).to.eq('no-fetch-yet');
});
it('reads from cache', async () => {
const lastFetchStatus = 'success';
storageCache.getLastFetchStatus = sinon.stub().returns(lastFetchStatus);
expect(rc.lastFetchStatus).to.eq(lastFetchStatus);
});
});
describe('getValue', () => {
it('returns the active value if available', () => {
getActiveConfigStub.returns(ACTIVE_CONFIG);
rc.defaultConfig = DEFAULT_CONFIG;
expect(getValue(rc, 'key1')).to.deep.eq(
new Value('remote', ACTIVE_CONFIG.key1)
);
});
it('returns the default value if active is not available', () => {
rc.defaultConfig = DEFAULT_CONFIG;
expect(getValue(rc, 'key1')).to.deep.eq(
new Value('default', DEFAULT_CONFIG.key1)
);
});
it('returns the stringified default boolean values if active is not available', () => {
const DEFAULTS = { trueVal: true, falseVal: false };
rc.defaultConfig = DEFAULTS;
expect(getValue(rc, 'trueVal')).to.deep.eq(
new Value('default', String(DEFAULTS.trueVal))
);
expect(getValue(rc, 'falseVal')).to.deep.eq(
new Value('default', String(DEFAULTS.falseVal))
);
});
it('returns the stringified default numeric values if active is not available', () => {
const DEFAULTS = { negative: -1, zero: 0, positive: 11 };
rc.defaultConfig = DEFAULTS;
expect(getValue(rc, 'negative')).to.deep.eq(
new Value('default', String(DEFAULTS.negative))
);
expect(getValue(rc, 'zero')).to.deep.eq(
new Value('default', String(DEFAULTS.zero))
);
expect(getValue(rc, 'positive')).to.deep.eq(
new Value('default', String(DEFAULTS.positive))
);
});
it('returns the static value if active and default are not available', () => {
expect(getValue(rc, 'key1')).to.deep.eq(new Value('static'));
// Asserts debug message logged if static value is returned, per EAP feedback.
expect(logger.debug).to.have.been.called;
});
it('logs if initialization is incomplete', async () => {
// Defines default value to isolate initialization logging from static value logging.
rc.defaultConfig = { key1: 'val' };
// Gets value before initialization.
getValue(rc, 'key1');
// Asserts getValue logs.
expect(logger.debug).to.have.been.called;
// Enables initialization to complete.
storageCache.loadFromStorage = sinon.stub().returns(Promise.resolve());
// Ensures initialization completes.
await ensureInitialized(rc);
// Gets value after initialization.
getValue(rc, 'key1');
// Asserts getValue doesn't log after initialization is complete.
expect(logger.debug).to.have.been.calledOnce;
});
});
describe('getBoolean', () => {
it('returns the active value if available', () => {
getActiveConfigStub.returns(ACTIVE_CONFIG);
rc.defaultConfig = DEFAULT_CONFIG;
expect(getBoolean(rc, 'key3')).to.be.true;
});
it('returns the default value if active is not available', () => {
rc.defaultConfig = DEFAULT_CONFIG;
expect(getBoolean(rc, 'key3')).to.be.false;
});
it('returns the static value if active and default are not available', () => {
expect(getBoolean(rc, 'key3')).to.be.false;
});
});
describe('getString', () => {
it('returns the active value if available', () => {
getActiveConfigStub.returns(ACTIVE_CONFIG);
rc.defaultConfig = DEFAULT_CONFIG;
expect(getString(rc, 'key1')).to.eq(ACTIVE_CONFIG.key1);
});
it('returns the default value if active is not available', () => {
rc.defaultConfig = DEFAULT_CONFIG;
expect(getString(rc, 'key2')).to.eq(DEFAULT_CONFIG.key2);
});
it('returns the static value if active and default are not available', () => {
expect(getString(rc, 'key1')).to.eq('');
});
});
describe('getNumber', () => {
it('returns the active value if available', () => {
getActiveConfigStub.returns(ACTIVE_CONFIG);
rc.defaultConfig = DEFAULT_CONFIG;
expect(getNumber(rc, 'key4')).to.eq(Number(ACTIVE_CONFIG.key4));
});
it('returns the default value if active is not available', () => {
rc.defaultConfig = DEFAULT_CONFIG;
expect(getNumber(rc, 'key4')).to.eq(Number(DEFAULT_CONFIG.key4));
});
it('returns the static value if active and default are not available', () => {
expect(getNumber(rc, 'key1')).to.eq(0);
});
});
describe('getAll', () => {
it('returns values for all keys included in active and default configs', () => {
getActiveConfigStub.returns(ACTIVE_CONFIG);
rc.defaultConfig = DEFAULT_CONFIG;
expect(getAll(rc)).to.deep.eq({
key1: new Value('remote', ACTIVE_CONFIG.key1),
key2: new Value('remote', ACTIVE_CONFIG.key2),
key3: new Value('remote', ACTIVE_CONFIG.key3),
key4: new Value('remote', ACTIVE_CONFIG.key4),
test: new Value('default', DEFAULT_CONFIG.test)
});
});
it('returns values in default if active is not available', () => {
rc.defaultConfig = DEFAULT_CONFIG;
expect(getAll(rc)).to.deep.eq({
key1: new Value('default', DEFAULT_CONFIG.key1),
key2: new Value('default', DEFAULT_CONFIG.key2),
key3: new Value('default', DEFAULT_CONFIG.key3),
key4: new Value('default', DEFAULT_CONFIG.key4),
test: new Value('default', DEFAULT_CONFIG.test)
});
});
it('returns empty object if both active and default configs are not defined', () => {
expect(getAll(rc)).to.deep.eq({});
});
});
describe('activate', () => {
const ETAG = 'etag';
const CONFIG = { key: 'val' };
const NEW_ETAG = 'new_etag';
let getLastSuccessfulFetchResponseStub: sinon.SinonStub;
let getActiveConfigEtagStub: sinon.SinonStub;
let setActiveConfigEtagStub: sinon.SinonStub;
let setActiveConfigStub: sinon.SinonStub;
beforeEach(() => {
getLastSuccessfulFetchResponseStub = sinon.stub();
getActiveConfigEtagStub = sinon.stub();
setActiveConfigEtagStub = sinon.stub();
setActiveConfigStub = sinon.stub();
storage.getLastSuccessfulFetchResponse =
getLastSuccessfulFetchResponseStub;
storage.getActiveConfigEtag = getActiveConfigEtagStub;
storage.setActiveConfigEtag = setActiveConfigEtagStub;
storageCache.setActiveConfig = setActiveConfigStub;
});
it('does not activate if last successful fetch response is undefined', async () => {
getLastSuccessfulFetchResponseStub.returns(Promise.resolve());
getActiveConfigEtagStub.returns(Promise.resolve(ETAG));
const activateResponse = await activate(rc);
expect(activateResponse).to.be.false;
expect(storage.setActiveConfigEtag).to.not.have.been.called;
expect(storageCache.setActiveConfig).to.not.have.been.called;
});
it('does not activate if fetched and active etags are the same', async () => {
getLastSuccessfulFetchResponseStub.returns(
Promise.resolve({ config: {}, etag: ETAG })
);
getActiveConfigEtagStub.returns(Promise.resolve(ETAG));
const activateResponse = await activate(rc);
expect(activateResponse).to.be.false;
expect(storage.setActiveConfigEtag).to.not.have.been.called;
expect(storageCache.setActiveConfig).to.not.have.been.called;
});
it('activates if fetched and active etags are different', async () => {
getLastSuccessfulFetchResponseStub.returns(
Promise.resolve({ config: CONFIG, eTag: NEW_ETAG })
);
getActiveConfigEtagStub.returns(Promise.resolve(ETAG));
const activateResponse = await activate(rc);
expect(activateResponse).to.be.true;
expect(storage.setActiveConfigEtag).to.have.been.calledWith(NEW_ETAG);
expect(storageCache.setActiveConfig).to.have.been.calledWith(CONFIG);
});
it('activates if fetched is defined but active config is not', async () => {
getLastSuccessfulFetchResponseStub.returns(
Promise.resolve({ config: CONFIG, eTag: NEW_ETAG })
);
getActiveConfigEtagStub.returns(Promise.resolve());
const activateResponse = await activate(rc);
expect(activateResponse).to.be.true;
expect(storage.setActiveConfigEtag).to.have.been.calledWith(NEW_ETAG);
expect(storageCache.setActiveConfig).to.have.been.calledWith(CONFIG);
});
});
describe('fetchAndActivate', () => {
let rcActivateStub: sinon.SinonStub<[RemoteConfigType], Promise<boolean>>;
beforeEach(() => {
sinon.stub(api, 'fetchConfig').returns(Promise.resolve());
rcActivateStub = sinon.stub(api, 'activate');
});
afterEach(() => restore());
it('calls fetch and activate and returns activation boolean if true', async () => {
rcActivateStub.returns(Promise.resolve(true));
const response = await fetchAndActivate(rc);
expect(response).to.be.true;
expect(api.fetchConfig).to.have.been.calledWith(rc);
expect(api.activate).to.have.been.calledWith(rc);
});
it('calls fetch and activate and returns activation boolean if false', async () => {
rcActivateStub.returns(Promise.resolve(false));
const response = await fetchAndActivate(rc);
expect(response).to.be.false;
expect(api.fetchConfig).to.have.been.calledWith(rc);
expect(api.activate).to.have.been.calledWith(rc);
});
});
describe('fetch', () => {
let timeoutStub: sinon.SinonStub<
[(...args: any[]) => void, number, ...any[]]
>;
beforeEach(() => {
client.fetch = sinon
.stub()
.returns(Promise.resolve({ status: 200 } as FetchResponse));
storageCache.setLastFetchStatus = sinon.stub();
timeoutStub = sinon.stub(window, 'setTimeout');
});
afterEach(() => {
timeoutStub.restore();
});
it('defines a default timeout', async () => {
await fetchConfig(rc);
expect(timeoutStub).to.have.been.calledWith(sinon.match.any, 60000);
});
it('honors a custom timeout', async () => {
rc.settings.fetchTimeoutMillis = 1000;
await fetchConfig(rc);
expect(timeoutStub).to.have.been.calledWith(sinon.match.any, 1000);
});
it('sets success status', async () => {
for (const status of [200, 304]) {
client.fetch = sinon
.stub()
.returns(Promise.resolve({ status } as FetchResponse));
await fetchConfig(rc);
expect(storageCache.setLastFetchStatus).to.have.been.calledWith(
'success'
);
}
});
it('sets throttle status', async () => {
storage.getThrottleMetadata = sinon.stub().returns(Promise.resolve({}));
const error = ERROR_FACTORY.create(ErrorCode.FETCH_THROTTLE, {
throttleEndTimeMillis: 123
});
client.fetch = sinon.stub().returns(Promise.reject(error));
const fetchPromise = fetchConfig(rc);
await expect(fetchPromise).to.eventually.be.rejectedWith(error);
expect(storageCache.setLastFetchStatus).to.have.been.calledWith(
'throttle'
);
});
it('defaults to failure status', async () => {
storage.getThrottleMetadata = sinon.stub().returns(Promise.resolve());
const error = ERROR_FACTORY.create(ErrorCode.FETCH_STATUS, {
httpStatus: 400
});
client.fetch = sinon.stub().returns(Promise.reject(error));
const fetchPromise = fetchConfig(rc);
await expect(fetchPromise).to.eventually.be.rejectedWith(error);
expect(storageCache.setLastFetchStatus).to.have.been.calledWith(
'failure'
);
});
});
});