|
1 | 1 | // Code generated. DO NOT EDIT.
|
2 | 2 | import * as constants from "../../src/internal/constants.ts";
|
3 |
| -import { Code, UserHubError, type eventsv1 } from "../../src/mod.ts"; |
4 |
| -import { Webhook } from "../../src/webhook/actions.ts"; |
5 |
| -import { concatArrays, loadCrypto } from "../../src/webhook/base.ts"; |
6 | 3 | import {
|
| 4 | + Code, |
| 5 | + UserHubError, |
7 | 6 | WebhookRequest,
|
8 | 7 | WebhookResponse,
|
9 |
| - getHeader, |
10 |
| -} from "../../src/webhook/http.ts"; |
| 8 | + WebhookUserNotFound, |
| 9 | + type connectionsv1, |
| 10 | + type eventsv1, |
| 11 | +} from "../../src/mod.ts"; |
| 12 | +import { Webhook } from "../../src/webhook/actions.ts"; |
| 13 | +import { concatArrays, loadCrypto } from "../../src/webhook/base.ts"; |
| 14 | +import { getHeader } from "../../src/webhook/http.ts"; |
11 | 15 | import { expect, test } from "vitest";
|
12 | 16 |
|
13 | 17 | test.each<WebhookTest>([
|
@@ -268,18 +272,97 @@ test.each<WebhookTest>([
|
268 | 272 | setTimestamp: true,
|
269 | 273 | addSignature: true,
|
270 | 274 | },
|
| 275 | + { |
| 276 | + name: "List users", |
| 277 | + secret: "test", |
| 278 | + request: new WebhookRequest({ |
| 279 | + headers: { |
| 280 | + "UserHub-Action": "users.list", |
| 281 | + }, |
| 282 | + body: '{"pageSize":100}', |
| 283 | + }), |
| 284 | + response: new WebhookResponse({ |
| 285 | + statusCode: 200, |
| 286 | + body: '{"nextPageToken":"","users":[]}', |
| 287 | + }), |
| 288 | + setTimestamp: true, |
| 289 | + addSignature: true, |
| 290 | + }, |
| 291 | + { |
| 292 | + name: "Get user", |
| 293 | + secret: "test", |
| 294 | + request: new WebhookRequest({ |
| 295 | + headers: { |
| 296 | + "UserHub-Action": "users.get", |
| 297 | + }, |
| 298 | + body: '{"id": "1"}', |
| 299 | + }), |
| 300 | + response: new WebhookResponse({ |
| 301 | + statusCode: 200, |
| 302 | + body: '{"id":"1","displayName":"","email":"","emailVerified":false,"phoneNumber":"","phoneNumberVerified":false,"imageUrl":"","disabled":false}', |
| 303 | + }), |
| 304 | + setTimestamp: true, |
| 305 | + addSignature: true, |
| 306 | + }, |
| 307 | + { |
| 308 | + name: "Get user not found", |
| 309 | + secret: "test", |
| 310 | + request: new WebhookRequest({ |
| 311 | + headers: { |
| 312 | + "UserHub-Action": "users.get", |
| 313 | + }, |
| 314 | + body: '{"id": "not-found"}', |
| 315 | + }), |
| 316 | + response: new WebhookResponse({ |
| 317 | + statusCode: 404, |
| 318 | + body: '{"message":"User not found","code":"NOT_FOUND"}', |
| 319 | + }), |
| 320 | + setTimestamp: true, |
| 321 | + addSignature: true, |
| 322 | + }, |
271 | 323 | ])("handler: $name", async (test) => {
|
272 | 324 | const webhook = new Webhook(test.secret);
|
273 | 325 |
|
274 |
| - webhook.onEvent((event: eventsv1.Event) => { |
275 |
| - if (event.type !== "ok") { |
| 326 | + webhook.onEvent((input: eventsv1.Event) => { |
| 327 | + if (input.type !== "ok") { |
276 | 328 | throw new UserHubError({
|
277 |
| - message: `Event failed: ${event.type}`, |
| 329 | + message: `Event failed: ${input.type}`, |
278 | 330 | apiCode: Code.InvalidArgument,
|
279 | 331 | });
|
280 | 332 | }
|
281 | 333 | });
|
282 | 334 |
|
| 335 | + webhook.onListUsers( |
| 336 | + ( |
| 337 | + input: connectionsv1.ListCustomUsersRequest, |
| 338 | + ): connectionsv1.ListCustomUsersResponse => { |
| 339 | + if (input.pageSize !== 100) { |
| 340 | + throw new Error(`unexpected page size: ${input.pageSize}`); |
| 341 | + } |
| 342 | + |
| 343 | + return { nextPageToken: "", users: [] }; |
| 344 | + }, |
| 345 | + ); |
| 346 | + |
| 347 | + webhook.onGetUser( |
| 348 | + (input: connectionsv1.GetCustomUserRequest): connectionsv1.CustomUser => { |
| 349 | + if (input.id === "not-found") { |
| 350 | + throw new WebhookUserNotFound(); |
| 351 | + } |
| 352 | + |
| 353 | + return { |
| 354 | + id: input.id, |
| 355 | + displayName: "", |
| 356 | + email: "", |
| 357 | + emailVerified: false, |
| 358 | + phoneNumber: "", |
| 359 | + phoneNumberVerified: false, |
| 360 | + imageUrl: "", |
| 361 | + disabled: false, |
| 362 | + }; |
| 363 | + }, |
| 364 | + ); |
| 365 | + |
283 | 366 | const encoder = new TextEncoder();
|
284 | 367 |
|
285 | 368 | if (test.setTimestamp) {
|
|
0 commit comments