mirror of https://github.com/openclaw/openclaw.git
test: share gateway route auth helpers
This commit is contained in:
parent
727fc79ed2
commit
4523260dda
|
|
@ -30,9 +30,8 @@ async function invokeSingleRunAbort({
|
|||
});
|
||||
}
|
||||
|
||||
describe("chat.abort authorization", () => {
|
||||
it("rejects explicit run aborts from other clients", async () => {
|
||||
const context = createChatAbortContext({
|
||||
function createSingleAbortContext() {
|
||||
return createChatAbortContext({
|
||||
chatAbortControllers: new Map([
|
||||
[
|
||||
"run-1",
|
||||
|
|
@ -40,6 +39,11 @@ describe("chat.abort authorization", () => {
|
|||
],
|
||||
]),
|
||||
});
|
||||
}
|
||||
|
||||
describe("chat.abort authorization", () => {
|
||||
it("rejects explicit run aborts from other clients", async () => {
|
||||
const context = createSingleAbortContext();
|
||||
|
||||
const respond = await invokeSingleRunAbort({
|
||||
context,
|
||||
|
|
@ -104,14 +108,7 @@ describe("chat.abort authorization", () => {
|
|||
});
|
||||
|
||||
it("allows operator.admin clients to bypass owner checks", async () => {
|
||||
const context = createChatAbortContext({
|
||||
chatAbortControllers: new Map([
|
||||
[
|
||||
"run-1",
|
||||
createActiveRun("main", { owner: { connId: "conn-owner", deviceId: "dev-owner" } }),
|
||||
],
|
||||
]),
|
||||
});
|
||||
const context = createSingleAbortContext();
|
||||
|
||||
const respond = await invokeSingleRunAbort({
|
||||
context,
|
||||
|
|
|
|||
|
|
@ -111,6 +111,23 @@ function createSecurePluginRouteHandler(params: {
|
|||
});
|
||||
}
|
||||
|
||||
async function invokeSecureGatewayRoute(params: { gatewayAuthSatisfied: boolean }) {
|
||||
const exactPluginHandler = vi.fn(async () => false);
|
||||
const prefixGatewayHandler = vi.fn(async () => true);
|
||||
const handler = createSecurePluginRouteHandler({
|
||||
exactPluginHandler,
|
||||
prefixGatewayHandler,
|
||||
});
|
||||
const { res } = makeMockHttpResponse();
|
||||
const handled = await handler(
|
||||
{ url: "/plugin/secure/report" } as IncomingMessage,
|
||||
res,
|
||||
undefined,
|
||||
{ gatewayAuthSatisfied: params.gatewayAuthSatisfied },
|
||||
);
|
||||
return { handled, exactPluginHandler, prefixGatewayHandler };
|
||||
}
|
||||
|
||||
describe("createGatewayPluginRequestHandler", () => {
|
||||
it("caps unauthenticated plugin routes to non-admin subagent scopes", async () => {
|
||||
loadOpenClawPlugins.mockReset();
|
||||
|
|
@ -232,44 +249,18 @@ describe("createGatewayPluginRequestHandler", () => {
|
|||
});
|
||||
|
||||
it("fails closed when a matched gateway route reaches dispatch without auth", async () => {
|
||||
const exactPluginHandler = vi.fn(async () => false);
|
||||
const prefixGatewayHandler = vi.fn(async () => true);
|
||||
const handler = createSecurePluginRouteHandler({
|
||||
exactPluginHandler,
|
||||
prefixGatewayHandler,
|
||||
});
|
||||
|
||||
const { res } = makeMockHttpResponse();
|
||||
const handled = await handler(
|
||||
{ url: "/plugin/secure/report" } as IncomingMessage,
|
||||
res,
|
||||
undefined,
|
||||
{
|
||||
const { handled, exactPluginHandler, prefixGatewayHandler } = await invokeSecureGatewayRoute({
|
||||
gatewayAuthSatisfied: false,
|
||||
},
|
||||
);
|
||||
});
|
||||
expect(handled).toBe(false);
|
||||
expect(exactPluginHandler).not.toHaveBeenCalled();
|
||||
expect(prefixGatewayHandler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows gateway route fallthrough only after gateway auth succeeds", async () => {
|
||||
const exactPluginHandler = vi.fn(async () => false);
|
||||
const prefixGatewayHandler = vi.fn(async () => true);
|
||||
const handler = createSecurePluginRouteHandler({
|
||||
exactPluginHandler,
|
||||
prefixGatewayHandler,
|
||||
});
|
||||
|
||||
const { res } = makeMockHttpResponse();
|
||||
const handled = await handler(
|
||||
{ url: "/plugin/secure/report" } as IncomingMessage,
|
||||
res,
|
||||
undefined,
|
||||
{
|
||||
const { handled, exactPluginHandler, prefixGatewayHandler } = await invokeSecureGatewayRoute({
|
||||
gatewayAuthSatisfied: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
expect(handled).toBe(true);
|
||||
expect(exactPluginHandler).toHaveBeenCalledTimes(1);
|
||||
expect(prefixGatewayHandler).toHaveBeenCalledTimes(1);
|
||||
|
|
|
|||
Loading…
Reference in New Issue