mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 03:32:43 +00:00
refactor(test): dedupe gateway hooks server setup
This commit is contained in:
@@ -14,15 +14,23 @@ installGatewayTestHooks({ scope: "suite" });
|
|||||||
|
|
||||||
const resolveMainKey = () => resolveMainSessionKeyFromConfig();
|
const resolveMainKey = () => resolveMainSessionKeyFromConfig();
|
||||||
|
|
||||||
|
async function withGatewayServer<T>(fn: (ctx: { port: number }) => Promise<T>): Promise<T> {
|
||||||
|
const port = await getFreePort();
|
||||||
|
const server = await startGatewayServer(port);
|
||||||
|
try {
|
||||||
|
return await fn({ port });
|
||||||
|
} finally {
|
||||||
|
await server.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe("gateway server hooks", () => {
|
describe("gateway server hooks", () => {
|
||||||
test("handles auth, wake, and agent flows", async () => {
|
test("handles auth, wake, and agent flows", async () => {
|
||||||
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
||||||
testState.agentsConfig = {
|
testState.agentsConfig = {
|
||||||
list: [{ id: "main", default: true }, { id: "hooks" }],
|
list: [{ id: "main", default: true }, { id: "hooks" }],
|
||||||
};
|
};
|
||||||
const port = await getFreePort();
|
await withGatewayServer(async ({ port }) => {
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
try {
|
|
||||||
const resNoAuth = await fetch(`http://127.0.0.1:${port}/hooks/wake`, {
|
const resNoAuth = await fetch(`http://127.0.0.1:${port}/hooks/wake`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
@@ -194,16 +202,12 @@ describe("gateway server hooks", () => {
|
|||||||
body: "{",
|
body: "{",
|
||||||
});
|
});
|
||||||
expect(resBadJson.status).toBe(400);
|
expect(resBadJson.status).toBe(400);
|
||||||
} finally {
|
});
|
||||||
await server.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("rejects request sessionKey unless hooks.allowRequestSessionKey is enabled", async () => {
|
test("rejects request sessionKey unless hooks.allowRequestSessionKey is enabled", async () => {
|
||||||
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
||||||
const port = await getFreePort();
|
await withGatewayServer(async ({ port }) => {
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
try {
|
|
||||||
const denied = await fetch(`http://127.0.0.1:${port}/hooks/agent`, {
|
const denied = await fetch(`http://127.0.0.1:${port}/hooks/agent`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -218,9 +222,7 @@ describe("gateway server hooks", () => {
|
|||||||
expect(denied.status).toBe(400);
|
expect(denied.status).toBe(400);
|
||||||
const deniedBody = (await denied.json()) as { error?: string };
|
const deniedBody = (await denied.json()) as { error?: string };
|
||||||
expect(deniedBody.error).toContain("hooks.allowRequestSessionKey");
|
expect(deniedBody.error).toContain("hooks.allowRequestSessionKey");
|
||||||
} finally {
|
});
|
||||||
await server.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("respects hooks session policy for request + mapping session keys", async () => {
|
test("respects hooks session policy for request + mapping session keys", async () => {
|
||||||
@@ -245,9 +247,7 @@ describe("gateway server hooks", () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
const port = await getFreePort();
|
await withGatewayServer(async ({ port }) => {
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
try {
|
|
||||||
cronIsolatedRun.mockReset();
|
cronIsolatedRun.mockReset();
|
||||||
cronIsolatedRun.mockResolvedValue({ status: "ok", summary: "done" });
|
cronIsolatedRun.mockResolvedValue({ status: "ok", summary: "done" });
|
||||||
|
|
||||||
@@ -303,9 +303,7 @@ describe("gateway server hooks", () => {
|
|||||||
body: JSON.stringify({ subject: "hello" }),
|
body: JSON.stringify({ subject: "hello" }),
|
||||||
});
|
});
|
||||||
expect(mappedBadPrefix.status).toBe(400);
|
expect(mappedBadPrefix.status).toBe(400);
|
||||||
} finally {
|
});
|
||||||
await server.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("enforces hooks.allowedAgentIds for explicit agent routing", async () => {
|
test("enforces hooks.allowedAgentIds for explicit agent routing", async () => {
|
||||||
@@ -325,9 +323,7 @@ describe("gateway server hooks", () => {
|
|||||||
testState.agentsConfig = {
|
testState.agentsConfig = {
|
||||||
list: [{ id: "main", default: true }, { id: "hooks" }],
|
list: [{ id: "main", default: true }, { id: "hooks" }],
|
||||||
};
|
};
|
||||||
const port = await getFreePort();
|
await withGatewayServer(async ({ port }) => {
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
try {
|
|
||||||
cronIsolatedRun.mockReset();
|
cronIsolatedRun.mockReset();
|
||||||
cronIsolatedRun.mockResolvedValueOnce({
|
cronIsolatedRun.mockResolvedValueOnce({
|
||||||
status: "ok",
|
status: "ok",
|
||||||
@@ -394,9 +390,7 @@ describe("gateway server hooks", () => {
|
|||||||
const mappedDeniedBody = (await resMappedDenied.json()) as { error?: string };
|
const mappedDeniedBody = (await resMappedDenied.json()) as { error?: string };
|
||||||
expect(mappedDeniedBody.error).toContain("hooks.allowedAgentIds");
|
expect(mappedDeniedBody.error).toContain("hooks.allowedAgentIds");
|
||||||
expect(peekSystemEvents(resolveMainKey()).length).toBe(0);
|
expect(peekSystemEvents(resolveMainKey()).length).toBe(0);
|
||||||
} finally {
|
});
|
||||||
await server.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("denies explicit agentId when hooks.allowedAgentIds is empty", async () => {
|
test("denies explicit agentId when hooks.allowedAgentIds is empty", async () => {
|
||||||
@@ -408,9 +402,7 @@ describe("gateway server hooks", () => {
|
|||||||
testState.agentsConfig = {
|
testState.agentsConfig = {
|
||||||
list: [{ id: "main", default: true }, { id: "hooks" }],
|
list: [{ id: "main", default: true }, { id: "hooks" }],
|
||||||
};
|
};
|
||||||
const port = await getFreePort();
|
await withGatewayServer(async ({ port }) => {
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
try {
|
|
||||||
const resDenied = await fetch(`http://127.0.0.1:${port}/hooks/agent`, {
|
const resDenied = await fetch(`http://127.0.0.1:${port}/hooks/agent`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -423,16 +415,12 @@ describe("gateway server hooks", () => {
|
|||||||
const deniedBody = (await resDenied.json()) as { error?: string };
|
const deniedBody = (await resDenied.json()) as { error?: string };
|
||||||
expect(deniedBody.error).toContain("hooks.allowedAgentIds");
|
expect(deniedBody.error).toContain("hooks.allowedAgentIds");
|
||||||
expect(peekSystemEvents(resolveMainKey()).length).toBe(0);
|
expect(peekSystemEvents(resolveMainKey()).length).toBe(0);
|
||||||
} finally {
|
});
|
||||||
await server.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("throttles repeated hook auth failures and resets after success", async () => {
|
test("throttles repeated hook auth failures and resets after success", async () => {
|
||||||
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
testState.hooksConfig = { enabled: true, token: "hook-secret" };
|
||||||
const port = await getFreePort();
|
await withGatewayServer(async ({ port }) => {
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
try {
|
|
||||||
const firstFail = await fetch(`http://127.0.0.1:${port}/hooks/wake`, {
|
const firstFail = await fetch(`http://127.0.0.1:${port}/hooks/wake`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -478,8 +466,6 @@ describe("gateway server hooks", () => {
|
|||||||
body: JSON.stringify({ text: "blocked" }),
|
body: JSON.stringify({ text: "blocked" }),
|
||||||
});
|
});
|
||||||
expect(failAfterSuccess.status).toBe(401);
|
expect(failAfterSuccess.status).toBe(401);
|
||||||
} finally {
|
});
|
||||||
await server.close();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user