From e96f6d6eb67a3de99f5c4ee78440224c04c70215 Mon Sep 17 00:00:00 2001 From: Silas Sewell Date: Mon, 3 Jun 2024 20:15:29 -0400 Subject: [PATCH] Add user not found error --- src/connectionsv1.ts | 10 ++-- src/mod.ts | 1 + src/webhook/errors.ts | 15 ++++++ test/node/webhook.test.ts | 99 +++++++++++++++++++++++++++++++++++---- 4 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 src/webhook/errors.ts diff --git a/src/connectionsv1.ts b/src/connectionsv1.ts index 24a053e..cd7c8aa 100644 --- a/src/connectionsv1.ts +++ b/src/connectionsv1.ts @@ -8,7 +8,7 @@ export interface Challenge { /** * The challenge string. */ - challenge?: string; + challenge: string; } /** @@ -62,7 +62,7 @@ export interface DeleteCustomUserRequest { /** * The external identifier for the user. */ - id?: string; + id: string; } /** @@ -76,7 +76,7 @@ export interface ErrorResponse { /** * A user-facing error message. */ - message?: string; + message: string; } /** @@ -86,7 +86,7 @@ export interface GetCustomUserRequest { /** * The external identifier for the user. */ - id?: string; + id: string; } /** @@ -97,7 +97,7 @@ export interface ListCustomUsersRequest { * The maximum number of users to return. The webhook is allowed to * return fewer than this value, but it should never return more. */ - pageSize?: number; + pageSize: number; /** * A page token, this is from the response of the previous list * request. diff --git a/src/mod.ts b/src/mod.ts index 04b434e..698815e 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -13,6 +13,7 @@ export type * as userv1 from "./userv1.ts"; export { AdminApi } from "./clients.ts"; export { UserApi } from "./clients.ts"; export { Webhook } from "./webhook/actions.ts"; +export { WebhookUserNotFound } from "./webhook/errors.ts"; export { WebhookResponse, WebhookRequest } from "./webhook/http.ts"; // misc diff --git a/src/webhook/errors.ts b/src/webhook/errors.ts new file mode 100644 index 0000000..6809856 --- /dev/null +++ b/src/webhook/errors.ts @@ -0,0 +1,15 @@ +import { Code } from "../code.ts"; +import { UserHubError } from "../error.ts"; + +/** + * WebhookUserNotFound is an error which can be used to indicate a user was + * not found in the onGetUser, onUpdateUser, and onDeleteUser methods. + */ +export class WebhookUserNotFound extends UserHubError { + constructor() { + super({ + message: "User not found", + apiCode: Code.NotFound, + }); + } +} diff --git a/test/node/webhook.test.ts b/test/node/webhook.test.ts index 9570641..c2c0bce 100644 --- a/test/node/webhook.test.ts +++ b/test/node/webhook.test.ts @@ -1,13 +1,17 @@ // Code generated. DO NOT EDIT. import * as constants from "../../src/internal/constants.ts"; -import { Code, UserHubError, type eventsv1 } from "../../src/mod.ts"; -import { Webhook } from "../../src/webhook/actions.ts"; -import { concatArrays, loadCrypto } from "../../src/webhook/base.ts"; import { + Code, + UserHubError, WebhookRequest, WebhookResponse, - getHeader, -} from "../../src/webhook/http.ts"; + WebhookUserNotFound, + type connectionsv1, + type eventsv1, +} from "../../src/mod.ts"; +import { Webhook } from "../../src/webhook/actions.ts"; +import { concatArrays, loadCrypto } from "../../src/webhook/base.ts"; +import { getHeader } from "../../src/webhook/http.ts"; import { expect, test } from "vitest"; test.each([ @@ -268,18 +272,97 @@ test.each([ setTimestamp: true, addSignature: true, }, + { + name: "List users", + secret: "test", + request: new WebhookRequest({ + headers: { + "UserHub-Action": "users.list", + }, + body: '{"pageSize":100}', + }), + response: new WebhookResponse({ + statusCode: 200, + body: '{"nextPageToken":"","users":[]}', + }), + setTimestamp: true, + addSignature: true, + }, + { + name: "Get user", + secret: "test", + request: new WebhookRequest({ + headers: { + "UserHub-Action": "users.get", + }, + body: '{"id": "1"}', + }), + response: new WebhookResponse({ + statusCode: 200, + body: '{"id":"1","displayName":"","email":"","emailVerified":false,"phoneNumber":"","phoneNumberVerified":false,"imageUrl":"","disabled":false}', + }), + setTimestamp: true, + addSignature: true, + }, + { + name: "Get user not found", + secret: "test", + request: new WebhookRequest({ + headers: { + "UserHub-Action": "users.get", + }, + body: '{"id": "not-found"}', + }), + response: new WebhookResponse({ + statusCode: 404, + body: '{"message":"User not found","code":"NOT_FOUND"}', + }), + setTimestamp: true, + addSignature: true, + }, ])("handler: $name", async (test) => { const webhook = new Webhook(test.secret); - webhook.onEvent((event: eventsv1.Event) => { - if (event.type !== "ok") { + webhook.onEvent((input: eventsv1.Event) => { + if (input.type !== "ok") { throw new UserHubError({ - message: `Event failed: ${event.type}`, + message: `Event failed: ${input.type}`, apiCode: Code.InvalidArgument, }); } }); + webhook.onListUsers( + ( + input: connectionsv1.ListCustomUsersRequest, + ): connectionsv1.ListCustomUsersResponse => { + if (input.pageSize !== 100) { + throw new Error(`unexpected page size: ${input.pageSize}`); + } + + return { nextPageToken: "", users: [] }; + }, + ); + + webhook.onGetUser( + (input: connectionsv1.GetCustomUserRequest): connectionsv1.CustomUser => { + if (input.id === "not-found") { + throw new WebhookUserNotFound(); + } + + return { + id: input.id, + displayName: "", + email: "", + emailVerified: false, + phoneNumber: "", + phoneNumberVerified: false, + imageUrl: "", + disabled: false, + }; + }, + ); + const encoder = new TextEncoder(); if (test.setTimestamp) {