test: dedupe fixtures and test harness setup

This commit is contained in:
Peter Steinberger
2026-02-23 05:43:30 +00:00
parent 8af19ddc5b
commit 1c753ea786
75 changed files with 1886 additions and 2136 deletions

View File

@@ -23,14 +23,14 @@ async function settleTimers<T>(promise: Promise<T>): Promise<T> {
return promise;
}
function mockSuccessResponse() {
function mockResponse(statusCode: number, body: string) {
const httpsRequest = vi.mocked(https.request);
httpsRequest.mockImplementation((_url: any, _opts: any, callback: any) => {
const res = new EventEmitter() as any;
res.statusCode = 200;
res.statusCode = statusCode;
process.nextTick(() => {
callback(res);
res.emit("data", Buffer.from('{"success":true}'));
res.emit("data", Buffer.from(body));
res.emit("end");
});
const req = new EventEmitter() as any;
@@ -41,22 +41,12 @@ function mockSuccessResponse() {
});
}
function mockSuccessResponse() {
mockResponse(200, '{"success":true}');
}
function mockFailureResponse(statusCode = 500) {
const httpsRequest = vi.mocked(https.request);
httpsRequest.mockImplementation((_url: any, _opts: any, callback: any) => {
const res = new EventEmitter() as any;
res.statusCode = statusCode;
process.nextTick(() => {
callback(res);
res.emit("data", Buffer.from("error"));
res.emit("end");
});
const req = new EventEmitter() as any;
req.write = vi.fn();
req.end = vi.fn();
req.destroy = vi.fn();
return req;
});
mockResponse(statusCode, "error");
}
describe("sendMessage", () => {

View File

@@ -80,6 +80,24 @@ describe("createWebhookHandler", () => {
};
});
async function expectForbiddenByPolicy(params: {
account: Partial<ResolvedSynologyChatAccount>;
bodyContains: string;
}) {
const handler = createWebhookHandler({
account: makeAccount(params.account),
deliver: vi.fn(),
log,
});
const req = makeReq("POST", validBody);
const res = makeRes();
await handler(req, res);
expect(res._status).toBe(403);
expect(res._body).toContain(params.bodyContains);
}
it("rejects non-POST methods with 405", async () => {
const handler = createWebhookHandler({
account: makeAccount(),
@@ -129,36 +147,20 @@ describe("createWebhookHandler", () => {
});
it("returns 403 for unauthorized user with allowlist policy", async () => {
const handler = createWebhookHandler({
account: makeAccount({
await expectForbiddenByPolicy({
account: {
dmPolicy: "allowlist",
allowedUserIds: ["456"],
}),
deliver: vi.fn(),
log,
},
bodyContains: "not authorized",
});
const req = makeReq("POST", validBody);
const res = makeRes();
await handler(req, res);
expect(res._status).toBe(403);
expect(res._body).toContain("not authorized");
});
it("returns 403 when DMs are disabled", async () => {
const handler = createWebhookHandler({
account: makeAccount({ dmPolicy: "disabled" }),
deliver: vi.fn(),
log,
await expectForbiddenByPolicy({
account: { dmPolicy: "disabled" },
bodyContains: "disabled",
});
const req = makeReq("POST", validBody);
const res = makeRes();
await handler(req, res);
expect(res._status).toBe(403);
expect(res._body).toContain("disabled");
});
it("returns 429 when rate limited", async () => {