test: dedupe synology webhook request helpers

This commit is contained in:
Peter Steinberger 2026-03-13 21:48:24 +00:00
parent 8896a477df
commit d964c15040
1 changed files with 18 additions and 29 deletions

View File

@ -37,21 +37,7 @@ function makeReq(
body: string,
opts: { headers?: Record<string, string>; url?: string } = {},
): IncomingMessage {
const req = new EventEmitter() as IncomingMessage & {
destroyed: boolean;
};
req.method = method;
req.headers = opts.headers ?? {};
req.url = opts.url ?? "/webhook/synology";
req.socket = { remoteAddress: "127.0.0.1" } as any;
req.destroyed = false;
req.destroy = ((_: Error | undefined) => {
if (req.destroyed) {
return req;
}
req.destroyed = true;
return req;
}) as IncomingMessage["destroy"];
const req = makeBaseReq(method, opts);
// Simulate body delivery
process.nextTick(() => {
@ -65,11 +51,19 @@ function makeReq(
return req;
}
function makeStalledReq(method: string): IncomingMessage {
return makeBaseReq(method);
}
function makeBaseReq(
method: string,
opts: { headers?: Record<string, string>; url?: string } = {},
): IncomingMessage & { destroyed: boolean } {
const req = new EventEmitter() as IncomingMessage & {
destroyed: boolean;
};
req.method = method;
req.headers = {};
req.headers = opts.headers ?? {};
req.url = opts.url ?? "/webhook/synology";
req.socket = { remoteAddress: "127.0.0.1" } as any;
req.destroyed = false;
req.destroy = ((_: Error | undefined) => {
@ -124,10 +118,12 @@ describe("createWebhookHandler", () => {
async function expectForbiddenByPolicy(params: {
account: Partial<ResolvedSynologyChatAccount>;
bodyContains: string;
deliver?: ReturnType<typeof vi.fn>;
}) {
const deliver = params.deliver ?? vi.fn();
const handler = createWebhookHandler({
account: makeAccount(params.account),
deliver: vi.fn(),
deliver,
log,
});
@ -137,6 +133,7 @@ describe("createWebhookHandler", () => {
expect(res._status).toBe(403);
expect(res._body).toContain(params.bodyContains);
expect(deliver).not.toHaveBeenCalled();
}
it("rejects non-POST methods with 405", async () => {
@ -302,22 +299,14 @@ describe("createWebhookHandler", () => {
it("returns 403 when allowlist policy is set with empty allowedUserIds", async () => {
const deliver = vi.fn();
const handler = createWebhookHandler({
account: makeAccount({
await expectForbiddenByPolicy({
account: {
dmPolicy: "allowlist",
allowedUserIds: [],
}),
},
bodyContains: "Allowlist is empty",
deliver,
log,
});
const req = makeReq("POST", validBody);
const res = makeRes();
await handler(req, res);
expect(res._status).toBe(403);
expect(res._body).toContain("Allowlist is empty");
expect(deliver).not.toHaveBeenCalled();
});
it("returns 403 when DMs are disabled", async () => {