@@ -33,6 +33,7 @@ import {
33
33
} from './interfaces/installation-entry' ;
34
34
import { getFakeApp } from './testing/get-fake-app' ;
35
35
import './testing/setup' ;
36
+ import { extractAppConfig } from './util/extract-app-config' ;
36
37
import { sleep } from './util/sleep' ;
37
38
38
39
const FID = 'dont-talk-to-strangers' ;
@@ -43,10 +44,13 @@ const ONE_WEEK_MS = 7 * 24 * 60 * 60 * 1000;
43
44
* A map of different states of the database and a function that creates the
44
45
* said state.
45
46
*/
46
- const setupInstallationEntryMap : Map < string , ( ) => Promise < void > > = new Map ( [
47
+ const setupInstallationEntryMap : Map <
48
+ string ,
49
+ ( appConfig : AppConfig ) => Promise < void >
50
+ > = new Map ( [
47
51
[
48
52
'existing and valid auth token' ,
49
- async ( ) => {
53
+ async ( appConfig : AppConfig ) => {
50
54
const entry : RegisteredInstallationEntry = {
51
55
fid : FID ,
52
56
registrationStatus : RequestStatus . COMPLETED ,
@@ -58,12 +62,12 @@ const setupInstallationEntryMap: Map<string, () => Promise<void>> = new Map([
58
62
creationTime : Date . now ( )
59
63
}
60
64
} ;
61
- set ( 'appId' , entry ) ;
65
+ set ( appConfig , entry ) ;
62
66
}
63
67
] ,
64
68
[
65
69
'expired auth token' ,
66
- async ( ) => {
70
+ async ( appConfig : AppConfig ) => {
67
71
const entry : RegisteredInstallationEntry = {
68
72
fid : FID ,
69
73
registrationStatus : RequestStatus . COMPLETED ,
@@ -75,12 +79,12 @@ const setupInstallationEntryMap: Map<string, () => Promise<void>> = new Map([
75
79
creationTime : Date . now ( ) - 2 * ONE_WEEK_MS
76
80
}
77
81
} ;
78
- set ( 'appId' , entry ) ;
82
+ set ( appConfig , entry ) ;
79
83
}
80
84
] ,
81
85
[
82
86
'pending auth token' ,
83
- async ( ) => {
87
+ async ( appConfig : AppConfig ) => {
84
88
const entry : RegisteredInstallationEntry = {
85
89
fid : FID ,
86
90
registrationStatus : RequestStatus . COMPLETED ,
@@ -91,7 +95,7 @@ const setupInstallationEntryMap: Map<string, () => Promise<void>> = new Map([
91
95
}
92
96
} ;
93
97
94
- set ( 'appId' , entry ) ;
98
+ set ( appConfig , entry ) ;
95
99
96
100
// Finish pending request in 10 ms
97
101
sleep ( 50 ) . then ( ( ) => {
@@ -104,13 +108,13 @@ const setupInstallationEntryMap: Map<string, () => Promise<void>> = new Map([
104
108
creationTime : Date . now ( )
105
109
}
106
110
} ;
107
- set ( 'appId' , updatedEntry ) ;
111
+ set ( appConfig , updatedEntry ) ;
108
112
} ) ;
109
113
}
110
114
] ,
111
115
[
112
116
'no auth token' ,
113
- async ( ) => {
117
+ async ( appConfig : AppConfig ) => {
114
118
const entry : RegisteredInstallationEntry = {
115
119
fid : FID ,
116
120
registrationStatus : RequestStatus . COMPLETED ,
@@ -119,19 +123,19 @@ const setupInstallationEntryMap: Map<string, () => Promise<void>> = new Map([
119
123
requestStatus : RequestStatus . NOT_STARTED
120
124
}
121
125
} ;
122
- set ( 'appId' , entry ) ;
126
+ set ( appConfig , entry ) ;
123
127
}
124
128
] ,
125
129
[
126
130
'pending fid registration' ,
127
- async ( ) => {
131
+ async ( appConfig : AppConfig ) => {
128
132
const entry : InProgressInstallationEntry = {
129
133
fid : FID ,
130
134
registrationStatus : RequestStatus . IN_PROGRESS ,
131
135
registrationTime : Date . now ( ) - 3 * 1000
132
136
} ;
133
137
134
- set ( 'appId' , entry ) ;
138
+ set ( appConfig , entry ) ;
135
139
136
140
// Finish pending request in 10 ms
137
141
sleep ( 50 ) . then ( async ( ) => {
@@ -146,25 +150,26 @@ const setupInstallationEntryMap: Map<string, () => Promise<void>> = new Map([
146
150
creationTime : Date . now ( )
147
151
}
148
152
} ;
149
- set ( 'appId' , updatedEntry ) ;
153
+ set ( appConfig , updatedEntry ) ;
150
154
} ) ;
151
155
}
152
156
] ,
153
157
[
154
158
'unregistered fid' ,
155
- async ( ) => {
159
+ async ( appConfig : AppConfig ) => {
156
160
const entry : UnregisteredInstallationEntry = {
157
161
fid : FID ,
158
162
registrationStatus : RequestStatus . NOT_STARTED
159
163
} ;
160
164
161
- set ( 'appId' , entry ) ;
165
+ set ( appConfig , entry ) ;
162
166
}
163
167
]
164
168
] ) ;
165
169
166
170
describe ( 'getAuthToken' , ( ) => {
167
- const app : FirebaseApp = getFakeApp ( ) ;
171
+ let app : FirebaseApp ;
172
+ let appConfig : AppConfig ;
168
173
let createInstallationSpy : SinonStub <
169
174
[ AppConfig , InProgressInstallationEntry ] ,
170
175
Promise < RegisteredInstallationEntry >
@@ -175,6 +180,9 @@ describe('getAuthToken', () => {
175
180
> ;
176
181
177
182
beforeEach ( ( ) => {
183
+ app = getFakeApp ( ) ;
184
+ appConfig = extractAppConfig ( app ) ;
185
+
178
186
createInstallationSpy = stub ( api , 'createInstallation' ) . callsFake (
179
187
async ( _ , installationEntry ) => {
180
188
await sleep ( 50 ) ; // Request would take some time
@@ -216,7 +224,9 @@ describe('getAuthToken', () => {
216
224
describe ( 'basic functionality' , ( ) => {
217
225
for ( const [ title , setup ] of setupInstallationEntryMap . entries ( ) ) {
218
226
describe ( `when ${ title } in the DB` , ( ) => {
219
- beforeEach ( setup ) ;
227
+ beforeEach ( ( ) => {
228
+ setup ( appConfig ) ;
229
+ } ) ;
220
230
221
231
it ( 'resolves with an auth token' , async ( ) => {
222
232
const token = await getAuthToken ( app ) ;
@@ -226,7 +236,7 @@ describe('getAuthToken', () => {
226
236
it ( 'saves the token in the DB' , async ( ) => {
227
237
const token = await getAuthToken ( app ) ;
228
238
const installationEntry = await get < RegisteredInstallationEntry > (
229
- app . options . appId
239
+ appConfig
230
240
) ;
231
241
expect ( installationEntry ) . not . to . be . undefined ;
232
242
expect ( installationEntry ! . registrationStatus ) . to . equal (
@@ -281,7 +291,7 @@ describe('getAuthToken', () => {
281
291
requestStatus : RequestStatus . NOT_STARTED
282
292
}
283
293
} ;
284
- await set ( app . options . appId , installationEntry ) ;
294
+ await set ( appConfig , installationEntry ) ;
285
295
} ) ;
286
296
287
297
it ( 'gets the token by calling generateAuthToken' , async ( ) => {
@@ -314,7 +324,7 @@ describe('getAuthToken', () => {
314
324
} ) ;
315
325
316
326
await expect ( getAuthToken ( app ) ) . to . eventually . be . rejected ;
317
- await expect ( get ( app . options . appId ) ) . to . eventually . be . undefined ;
327
+ await expect ( get ( appConfig ) ) . to . eventually . be . undefined ;
318
328
} ) ;
319
329
320
330
it ( 'removes the FID from the DB if the server returns a 404 response' , async ( ) => {
@@ -328,7 +338,7 @@ describe('getAuthToken', () => {
328
338
} ) ;
329
339
330
340
await expect ( getAuthToken ( app ) ) . to . eventually . be . rejected ;
331
- await expect ( get ( app . options . appId ) ) . to . eventually . be . undefined ;
341
+ await expect ( get ( appConfig ) ) . to . eventually . be . undefined ;
332
342
} ) ;
333
343
334
344
it ( 'does not remove the FID from the DB if the server returns any other response' , async ( ) => {
@@ -342,7 +352,7 @@ describe('getAuthToken', () => {
342
352
} ) ;
343
353
344
354
await expect ( getAuthToken ( app ) ) . to . eventually . be . rejected ;
345
- await expect ( get ( app . options . appId ) ) . to . eventually . deep . equal (
355
+ await expect ( get ( appConfig ) ) . to . eventually . deep . equal (
346
356
installationEntry
347
357
) ;
348
358
} ) ;
@@ -362,7 +372,7 @@ describe('getAuthToken', () => {
362
372
creationTime : Date . now ( )
363
373
}
364
374
} ;
365
- await set ( app . options . appId , installationEntry ) ;
375
+ await set ( appConfig , installationEntry ) ;
366
376
} ) ;
367
377
368
378
it ( 'does not call any server APIs' , async ( ) => {
@@ -394,7 +404,7 @@ describe('getAuthToken', () => {
394
404
creationTime : Date . now ( ) - ONE_WEEK_MS + TOKEN_EXPIRATION_BUFFER + 10
395
405
}
396
406
} ;
397
- await set ( app . options . appId , installationEntry ) ;
407
+ await set ( appConfig , installationEntry ) ;
398
408
} ) ;
399
409
400
410
it ( 'returns a different token after expiration' , async ( ) => {
@@ -424,7 +434,7 @@ describe('getAuthToken', () => {
424
434
creationTime : Date . now ( ) - 2 * ONE_WEEK_MS
425
435
}
426
436
} ;
427
- await set ( app . options . appId , installationEntry ) ;
437
+ await set ( appConfig , installationEntry ) ;
428
438
} ) ;
429
439
430
440
it ( 'throws if the app is offline' , async ( ) => {
0 commit comments