-
Notifications
You must be signed in to change notification settings - Fork 200
/
tasks.spec.ts
262 lines (234 loc) · 7.74 KB
/
tasks.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
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
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import { expect } from 'chai';
import * as firebase from 'firebase-admin';
import { apps as appsNamespace } from '../../../src/apps';
import * as https from '../../../src/common/providers/https';
import {
onDispatchHandler,
Request,
TaskContext,
} from '../../../src/common/providers/tasks';
import * as mocks from '../../fixtures/credential/key.json';
import {
generateIdToken,
generateUnsignedIdToken,
mockRequest,
} from '../../fixtures/mockrequest';
import { checkAuthContext, runHandler } from '../../helper';
/** Represents a test case for a Task Queue Function */
interface TaskTest {
// An http request, mocking a subset of https.Request.
httpRequest: any;
// The expected format of the request passed to the handler.
expectedData: any;
taskFunction?: (data: any, context: TaskContext) => void | Promise<void>;
taskFunction2?: (request: Request<any>) => void | Promise<void>;
// The expected shape of the http response returned to the callable SDK.
expectedStatus: number;
}
// Runs a TaskTest test.
export async function runTaskTest(test: TaskTest): Promise<any> {
const taskQueueFunctionV1 = onDispatchHandler((data, context) => {
expect(data).to.deep.equal(test.expectedData);
if (test.taskFunction) {
test.taskFunction(data, context);
}
});
const responseV1 = await runHandler(taskQueueFunctionV1, test.httpRequest);
expect(responseV1.status).to.equal(test.expectedStatus);
const taskQueueFunctionV2 = onDispatchHandler((request) => {
expect(request.data).to.deep.equal(test.expectedData);
if (test.taskFunction2) {
test.taskFunction2(request);
}
});
const responseV2 = await runHandler(taskQueueFunctionV2, test.httpRequest);
expect(responseV2.status).to.equal(test.expectedStatus);
}
describe('onEnqueueHandler', () => {
let app: firebase.app.App;
function mockEnqueueRequest(
data: unknown,
contentType: string = 'application/json',
context: { authorization?: string } = { authorization: 'Bearer abc' }
): ReturnType<typeof mockRequest> {
return mockRequest(data, contentType, context);
}
before(() => {
const credential = {
getAccessToken: () => {
return Promise.resolve({
expires_in: 1000,
access_token: 'fake',
});
},
getCertificate: () => {
return {
projectId: 'aProjectId',
};
},
};
app = firebase.initializeApp({
projectId: 'aProjectId',
credential,
});
Object.defineProperty(appsNamespace(), 'admin', { get: () => app });
});
after(() => {
app.delete();
delete appsNamespace.singleton;
});
it('should handle success', () => {
return runTaskTest({
httpRequest: mockEnqueueRequest({ foo: 'bar' }),
expectedData: { foo: 'bar' },
expectedStatus: 204,
});
});
it('should reject bad method', () => {
const req = mockRequest(null);
req.method = 'GET';
return runTaskTest({
httpRequest: req,
expectedData: null,
expectedStatus: 400,
});
});
it('should ignore charset', () => {
return runTaskTest({
httpRequest: mockEnqueueRequest(null, 'application/json; charset=utf-8'),
expectedData: null,
expectedStatus: 204,
});
});
it('should reject bad content type', () => {
return runTaskTest({
httpRequest: mockEnqueueRequest(null, 'text/plain'),
expectedData: null,
expectedStatus: 400,
});
});
it('should reject extra body fields', () => {
const req = mockEnqueueRequest(null);
req.body.extra = 'bad';
return runTaskTest({
httpRequest: req,
expectedData: null,
expectedStatus: 400,
});
});
it('should handle unhandled error', () => {
return runTaskTest({
httpRequest: mockEnqueueRequest(null),
expectedData: null,
taskFunction: (data, context) => {
throw new Error(`ceci n'est pas une error`);
},
taskFunction2: (request) => {
throw new Error(`cece n'est pas une error`);
},
expectedStatus: 500,
});
});
it('should handle unknown error status', () => {
return runTaskTest({
httpRequest: mockEnqueueRequest(null),
expectedData: null,
taskFunction: (data, context) => {
throw new https.HttpsError('THIS_IS_NOT_VALID' as any, 'nope');
},
taskFunction2: (request) => {
throw new https.HttpsError('THIS_IS_NOT_VALID' as any, 'nope');
},
expectedStatus: 500,
});
});
it('should handle well-formed error', () => {
return runTaskTest({
httpRequest: mockEnqueueRequest(null),
expectedData: null,
taskFunction: (data, context) => {
throw new https.HttpsError('not-found', 'i am error');
},
taskFunction2: (request) => {
throw new https.HttpsError('not-found', 'i am error');
},
expectedStatus: 404,
});
});
it('should handle auth', async () => {
const projectId = appsNamespace().admin.options.projectId;
const idToken = generateIdToken(projectId);
await runTaskTest({
httpRequest: mockEnqueueRequest(null, 'application/json', {
authorization: 'Bearer ' + idToken,
}),
expectedData: null,
taskFunction: (data, context) => {
checkAuthContext(context, projectId, mocks.user_id);
return null;
},
taskFunction2: (request) => {
checkAuthContext(request, projectId, mocks.user_id);
return null;
},
expectedStatus: 204,
});
});
it('should accept unsigned auth too', async () => {
const projectId = appsNamespace().admin.options.projectId;
const idToken = generateUnsignedIdToken(projectId);
await runTaskTest({
httpRequest: mockEnqueueRequest(null, 'application/json', {
authorization: 'Bearer ' + idToken,
}),
expectedData: null,
taskFunction: (data, context) => {
checkAuthContext(context, projectId, mocks.user_id);
return null;
},
taskFunction2: (request) => {
checkAuthContext(request, projectId, mocks.user_id);
return null;
},
expectedStatus: 204,
});
});
it('should skip auth in emulated environment', async () => {
const restore = process.env.FUNCTIONS_EMULATOR;
process.env.FUNCTIONS_EMULATOR = 'true';
await runTaskTest({
httpRequest: mockEnqueueRequest(null, 'application/json', {}),
expectedData: null,
taskFunction: (data, context) => {
expect(context.auth).to.be.undefined;
return null;
},
taskFunction2: (request) => {
expect(request.auth).to.be.undefined;
},
expectedStatus: 204,
});
process.env.FUNCTIONS_EMULATOR = restore;
});
});