21
21
// SOFTWARE.
22
22
23
23
import { expect } from 'chai' ;
24
- import * as sinon from 'sinon' ;
25
24
import * as firebase from 'firebase-admin' ;
26
25
27
26
import { checkAuthContext , runHandler } from '../../helper' ;
28
27
import {
29
28
generateIdToken ,
30
29
generateUnsignedIdToken ,
31
- mockFetchPublicKeys ,
32
30
mockRequest ,
33
31
} from '../../fixtures/mockrequest' ;
34
32
import {
@@ -39,7 +37,6 @@ import {
39
37
import { apps as appsNamespace } from '../../../src/apps' ;
40
38
import * as mocks from '../../fixtures/credential/key.json' ;
41
39
import * as https from '../../../src/common/providers/https' ;
42
- import * as debug from '../../../src/common/debug' ;
43
40
44
41
/** Represents a test case for a Task Queue Function */
45
42
interface TaskTest {
@@ -83,6 +80,14 @@ export async function runTaskTest(test: TaskTest): Promise<any> {
83
80
describe ( 'onEnqueueHandler' , ( ) => {
84
81
let app : firebase . app . App ;
85
82
83
+ function mockEnqueueRequest (
84
+ data : unknown ,
85
+ contentType : string = 'application/json' ,
86
+ context : { authorization : string } = { authorization : 'Bearer abc' }
87
+ ) : ReturnType < typeof mockRequest > {
88
+ return mockRequest ( data , contentType , context ) ;
89
+ }
90
+
86
91
before ( ( ) => {
87
92
const credential = {
88
93
getAccessToken : ( ) => {
@@ -111,7 +116,7 @@ describe('onEnqueueHandler', () => {
111
116
112
117
it ( 'should handle success' , ( ) => {
113
118
return runTaskTest ( {
114
- httpRequest : mockRequest ( { foo : 'bar' } ) ,
119
+ httpRequest : mockEnqueueRequest ( { foo : 'bar' } ) ,
115
120
expectedData : { foo : 'bar' } ,
116
121
expectedStatus : 204 ,
117
122
} ) ;
@@ -129,22 +134,22 @@ describe('onEnqueueHandler', () => {
129
134
130
135
it ( 'should ignore charset' , ( ) => {
131
136
return runTaskTest ( {
132
- httpRequest : mockRequest ( null , 'application/json; charset=utf-8' ) ,
137
+ httpRequest : mockEnqueueRequest ( null , 'application/json; charset=utf-8' ) ,
133
138
expectedData : null ,
134
139
expectedStatus : 204 ,
135
140
} ) ;
136
141
} ) ;
137
142
138
143
it ( 'should reject bad content type' , ( ) => {
139
144
return runTaskTest ( {
140
- httpRequest : mockRequest ( null , 'text/plain' ) ,
145
+ httpRequest : mockEnqueueRequest ( null , 'text/plain' ) ,
141
146
expectedData : null ,
142
147
expectedStatus : 400 ,
143
148
} ) ;
144
149
} ) ;
145
150
146
151
it ( 'should reject extra body fields' , ( ) => {
147
- const req = mockRequest ( null ) ;
152
+ const req = mockEnqueueRequest ( null ) ;
148
153
req . body . extra = 'bad' ;
149
154
return runTaskTest ( {
150
155
httpRequest : req ,
@@ -155,7 +160,7 @@ describe('onEnqueueHandler', () => {
155
160
156
161
it ( 'should handle unhandled error' , ( ) => {
157
162
return runTaskTest ( {
158
- httpRequest : mockRequest ( null ) ,
163
+ httpRequest : mockEnqueueRequest ( null ) ,
159
164
expectedData : null ,
160
165
taskFunction : ( data , context ) => {
161
166
throw new Error ( `ceci n'est pas une error` ) ;
@@ -169,7 +174,7 @@ describe('onEnqueueHandler', () => {
169
174
170
175
it ( 'should handle unknown error status' , ( ) => {
171
176
return runTaskTest ( {
172
- httpRequest : mockRequest ( null ) ,
177
+ httpRequest : mockEnqueueRequest ( null ) ,
173
178
expectedData : null ,
174
179
taskFunction : ( data , context ) => {
175
180
throw new https . HttpsError ( 'THIS_IS_NOT_VALID' as any , 'nope' ) ;
@@ -183,7 +188,7 @@ describe('onEnqueueHandler', () => {
183
188
184
189
it ( 'should handle well-formed error' , ( ) => {
185
190
return runTaskTest ( {
186
- httpRequest : mockRequest ( null ) ,
191
+ httpRequest : mockEnqueueRequest ( null ) ,
187
192
expectedData : null ,
188
193
taskFunction : ( data , context ) => {
189
194
throw new https . HttpsError ( 'not-found' , 'i am error' ) ;
@@ -196,11 +201,10 @@ describe('onEnqueueHandler', () => {
196
201
} ) ;
197
202
198
203
it ( 'should handle auth' , async ( ) => {
199
- const mock = mockFetchPublicKeys ( ) ;
200
204
const projectId = appsNamespace ( ) . admin . options . projectId ;
201
205
const idToken = generateIdToken ( projectId ) ;
202
206
await runTaskTest ( {
203
- httpRequest : mockRequest ( null , 'application/json' , {
207
+ httpRequest : mockEnqueueRequest ( null , 'application/json' , {
204
208
authorization : 'Bearer ' + idToken ,
205
209
} ) ,
206
210
expectedData : null ,
@@ -214,49 +218,25 @@ describe('onEnqueueHandler', () => {
214
218
} ,
215
219
expectedStatus : 204 ,
216
220
} ) ;
217
- mock . done ( ) ;
218
221
} ) ;
219
222
220
- it ( 'should reject bad auth' , async ( ) => {
223
+ it ( 'should accept unsigned auth too ' , async ( ) => {
221
224
const projectId = appsNamespace ( ) . admin . options . projectId ;
222
225
const idToken = generateUnsignedIdToken ( projectId ) ;
223
226
await runTaskTest ( {
224
- httpRequest : mockRequest ( null , 'application/json' , {
227
+ httpRequest : mockEnqueueRequest ( null , 'application/json' , {
225
228
authorization : 'Bearer ' + idToken ,
226
229
} ) ,
227
230
expectedData : null ,
228
- expectedStatus : 401 ,
229
- } ) ;
230
- } ) ;
231
-
232
- describe ( 'skip token verification debug mode support' , ( ) => {
233
- before ( ( ) => {
234
- sinon
235
- . stub ( debug , 'isDebugFeatureEnabled' )
236
- . withArgs ( 'skipTokenVerification' )
237
- . returns ( true ) ;
238
- } ) ;
239
-
240
- after ( ( ) => {
241
- sinon . verifyAndRestore ( ) ;
242
- } ) ;
243
-
244
- it ( 'should skip auth token verification' , async ( ) => {
245
- const projectId = appsNamespace ( ) . admin . options . projectId ;
246
- const idToken = generateUnsignedIdToken ( projectId ) ;
247
- await runTaskTest ( {
248
- httpRequest : mockRequest ( null , 'application/json' , {
249
- authorization : 'Bearer ' + idToken ,
250
- } ) ,
251
- expectedData : null ,
252
- taskFunction : ( data , context ) => {
253
- checkAuthContext ( context , projectId , mocks . user_id ) ;
254
- } ,
255
- taskFunction2 : ( request ) => {
256
- checkAuthContext ( request , projectId , mocks . user_id ) ;
257
- } ,
258
- expectedStatus : 204 ,
259
- } ) ;
231
+ taskFunction : ( data , context ) => {
232
+ checkAuthContext ( context , projectId , mocks . user_id ) ;
233
+ return null ;
234
+ } ,
235
+ taskFunction2 : ( request ) => {
236
+ checkAuthContext ( request , projectId , mocks . user_id ) ;
237
+ return null ;
238
+ } ,
239
+ expectedStatus : 204 ,
260
240
} ) ;
261
241
} ) ;
262
242
} ) ;
0 commit comments