@@ -24,7 +24,15 @@ import { expect } from 'chai';
24
24
import * as functions from 'firebase-functions' ;
25
25
import { set } from 'lodash' ;
26
26
27
- import { mockConfig , makeChange , _makeResourceName , wrap } from '../src/main' ;
27
+ import {
28
+ mockConfig ,
29
+ makeChange ,
30
+ _makeResourceName ,
31
+ _extractParams ,
32
+ wrap ,
33
+ } from '../src/main' ;
34
+ import { features } from '../src/features' ;
35
+ import { FirebaseFunctionsTest } from '../src/lifecycle' ;
28
36
29
37
describe ( 'main' , ( ) => {
30
38
describe ( '#wrap' , ( ) => {
@@ -54,9 +62,9 @@ describe('main', () => {
54
62
expect ( typeof context . eventId ) . to . equal ( 'string' ) ;
55
63
expect ( context . resource . service ) . to . equal ( 'service' ) ;
56
64
expect (
57
- / r e f \/ w i l d c a r d [ 1 - 9 ] \/ n e s t e d \/ a n o t h e r W i l d c a r d [ 1 - 9 ] / . test (
58
- context . resource . name
59
- )
65
+ / r e f \/ w i l d c a r d [ 1 - 9 ] \/ n e s t e d \/ a n o t h e r W i l d c a r d [ 1 - 9 ] / . test (
66
+ context . resource . name
67
+ )
60
68
) . to . be . true ;
61
69
expect ( context . eventType ) . to . equal ( 'event' ) ;
62
70
expect ( Date . parse ( context . timestamp ) ) . to . be . greaterThan ( 0 ) ;
@@ -75,31 +83,49 @@ describe('main', () => {
75
83
expect ( context . timestamp ) . to . equal ( '2018-03-28T18:58:50.370Z' ) ;
76
84
} ) ;
77
85
78
- it ( 'should generate auth and authType for database functions' , ( ) => {
79
- const context = wrap ( constructBackgroundCF ( 'google.firebase.database.ref.write' ) ) (
80
- 'data'
81
- ) . context ;
82
- expect ( context . auth ) . to . equal ( null ) ;
83
- expect ( context . authType ) . to . equal ( 'UNAUTHENTICATED' ) ;
84
- } ) ;
86
+ describe ( 'database functions' , ( ) => {
87
+ let test ;
88
+ let change ;
85
89
86
- it ( 'should allow auth and authType to be specified for database functions' , ( ) => {
87
- const wrapped = wrap ( constructBackgroundCF ( 'google.firebase.database.ref.write' ) ) ;
88
- const context = wrapped ( 'data' , {
89
- auth : { uid : 'abc' } ,
90
- authType : 'USER' ,
91
- } ) . context ;
92
- expect ( context . auth ) . to . deep . equal ( { uid : 'abc' } ) ;
93
- expect ( context . authType ) . to . equal ( 'USER' ) ;
90
+ beforeEach ( ( ) => {
91
+ test = new FirebaseFunctionsTest ( ) ;
92
+ test . init ( ) ;
93
+ change = features . database . exampleDataSnapshotChange ( ) ;
94
+ } ) ;
95
+
96
+ afterEach ( ( ) => {
97
+ test . cleanup ( ) ;
98
+ } ) ;
99
+
100
+ it ( 'should generate auth and authType' , ( ) => {
101
+ const wrapped = wrap (
102
+ constructBackgroundCF ( 'google.firebase.database.ref.write' )
103
+ ) ;
104
+ const context = wrapped ( change ) . context ;
105
+ expect ( context . auth ) . to . equal ( null ) ;
106
+ expect ( context . authType ) . to . equal ( 'UNAUTHENTICATED' ) ;
107
+ } ) ;
108
+
109
+ it ( 'should allow auth and authType to be specified' , ( ) => {
110
+ const wrapped = wrap (
111
+ constructBackgroundCF ( 'google.firebase.database.ref.write' )
112
+ ) ;
113
+ const context = wrapped ( change , {
114
+ auth : { uid : 'abc' } ,
115
+ authType : 'USER' ,
116
+ } ) . context ;
117
+ expect ( context . auth ) . to . deep . equal ( { uid : 'abc' } ) ;
118
+ expect ( context . authType ) . to . equal ( 'USER' ) ;
119
+ } ) ;
94
120
} ) ;
95
121
96
122
it ( 'should throw when passed invalid options' , ( ) => {
97
123
const wrapped = wrap ( constructBackgroundCF ( ) ) ;
98
124
expect ( ( ) =>
99
- wrapped ( 'data' , {
100
- auth : { uid : 'abc' } ,
101
- isInvalid : true ,
102
- } as any )
125
+ wrapped ( 'data' , {
126
+ auth : { uid : 'abc' } ,
127
+ isInvalid : true ,
128
+ } as any )
103
129
) . to . throw ( ) ;
104
130
} ) ;
105
131
@@ -113,6 +139,91 @@ describe('main', () => {
113
139
expect ( context . params ) . to . deep . equal ( params ) ;
114
140
expect ( context . resource . name ) . to . equal ( 'ref/a/nested/b' ) ;
115
141
} ) ;
142
+
143
+ describe ( 'Params extraction' , ( ) => {
144
+ let test ;
145
+
146
+ beforeEach ( ( ) => {
147
+ test = new FirebaseFunctionsTest ( ) ;
148
+ test . init ( ) ;
149
+ } ) ;
150
+
151
+ afterEach ( ( ) => {
152
+ test . cleanup ( ) ;
153
+ } ) ;
154
+
155
+ it ( 'should extract the appropriate params for database function trigger' , ( ) => {
156
+ const cf = constructBackgroundCF (
157
+ 'google.firebase.database.ref.create'
158
+ ) ;
159
+ cf . __trigger . eventTrigger . resource =
160
+ 'companies/{company}/users/{user}' ;
161
+ const wrapped = wrap ( cf ) ;
162
+ const context = wrapped (
163
+ features . database . makeDataSnapshot (
164
+ { foo : 'bar' } ,
165
+ 'companies/Google/users/Lauren'
166
+ )
167
+ ) . context ;
168
+ expect ( context . params ) . to . deep . equal ( {
169
+ company : 'Google' ,
170
+ user : 'Lauren' ,
171
+ } ) ;
172
+ expect ( context . resource . name ) . to . equal (
173
+ 'companies/Google/users/Lauren'
174
+ ) ;
175
+ } ) ;
176
+
177
+ it ( 'should extract the appropriate params for Firestore function trigger' , ( ) => {
178
+ const cf = constructBackgroundCF ( 'google.firestore.document.create' ) ;
179
+ cf . __trigger . eventTrigger . resource =
180
+ 'databases/(default)/documents/companies/{company}/users/{user}' ;
181
+ const wrapped = wrap ( cf ) ;
182
+ const context = wrapped (
183
+ features . firestore . makeDocumentSnapshot (
184
+ { foo : 'bar' } ,
185
+ 'companies/Google/users/Lauren'
186
+ )
187
+ ) . context ;
188
+ expect ( context . params ) . to . deep . equal ( {
189
+ company : 'Google' ,
190
+ user : 'Lauren' ,
191
+ } ) ;
192
+ expect ( context . resource . name ) . to . equal (
193
+ 'databases/(default)/documents/companies/Google/users/Lauren'
194
+ ) ;
195
+ } ) ;
196
+
197
+ it ( 'should prefer provided context.params over the extracted params' , ( ) => {
198
+ const cf = constructBackgroundCF (
199
+ 'google.firebase.database.ref.create'
200
+ ) ;
201
+ cf . __trigger . eventTrigger . resource =
202
+ 'companies/{company}/users/{user}' ;
203
+ const wrapped = wrap ( cf ) ;
204
+ const context = wrapped (
205
+ features . database . makeDataSnapshot (
206
+ { foo : 'bar' } ,
207
+ 'companies/Google/users/Lauren'
208
+ ) ,
209
+ {
210
+ params : {
211
+ company : 'Alphabet' ,
212
+ user : 'Lauren' ,
213
+ foo : 'bar' ,
214
+ } ,
215
+ }
216
+ ) . context ;
217
+ expect ( context . params ) . to . deep . equal ( {
218
+ company : 'Alphabet' ,
219
+ user : 'Lauren' ,
220
+ foo : 'bar' ,
221
+ } ) ;
222
+ expect ( context . resource . name ) . to . equal (
223
+ 'companies/Alphabet/users/Lauren'
224
+ ) ;
225
+ } ) ;
226
+ } ) ;
116
227
} ) ;
117
228
118
229
describe ( 'callable functions' , ( ) => {
@@ -141,23 +252,22 @@ describe('main', () => {
141
252
auth : { uid : 'abc' } ,
142
253
app : { appId : 'efg' } ,
143
254
instanceIdToken : '123' ,
144
- rawRequest : { body : 'hello' }
255
+ rawRequest : { body : 'hello' } ,
145
256
} ) . context ;
146
257
expect ( context . auth ) . to . deep . equal ( { uid : 'abc' } ) ;
147
258
expect ( context . app ) . to . deep . equal ( { appId : 'efg' } ) ;
148
259
expect ( context . instanceIdToken ) . to . equal ( '123' ) ;
149
- expect ( context . rawRequest ) . to . deep . equal ( { body : 'hello' } ) ;
260
+ expect ( context . rawRequest ) . to . deep . equal ( { body : 'hello' } ) ;
150
261
} ) ;
151
262
152
263
it ( 'should throw when passed invalid options' , ( ) => {
153
264
expect ( ( ) =>
154
- wrappedCF ( 'data' , {
155
- auth : { uid : 'abc' } ,
156
- isInvalid : true ,
157
- } as any )
265
+ wrappedCF ( 'data' , {
266
+ auth : { uid : 'abc' } ,
267
+ isInvalid : true ,
268
+ } as any )
158
269
) . to . throw ( ) ;
159
270
} ) ;
160
-
161
271
} ) ;
162
272
} ) ;
163
273
@@ -171,6 +281,24 @@ describe('main', () => {
171
281
} ) ;
172
282
} ) ;
173
283
284
+ describe ( '#_extractParams' , ( ) => {
285
+ it ( 'should not extract any params' , ( ) => {
286
+ const params = _extractParams ( 'users/foo' , 'users/foo' ) ;
287
+ expect ( params ) . to . deep . equal ( { } ) ;
288
+ } ) ;
289
+
290
+ it ( 'should extract params' , ( ) => {
291
+ const params = _extractParams (
292
+ 'companies/{company}/users/{user}' ,
293
+ 'companies/Google/users/Lauren'
294
+ ) ;
295
+ expect ( params ) . to . deep . equal ( {
296
+ company : 'Google' ,
297
+ user : 'Lauren' ,
298
+ } ) ;
299
+ } ) ;
300
+ } ) ;
301
+
174
302
describe ( '#makeChange' , ( ) => {
175
303
it ( 'should make a Change object with the correct before and after' , ( ) => {
176
304
const change = makeChange ( 'before' , 'after' ) ;
0 commit comments