mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 10:47:27 +00:00
test(discord): cover DM command decision flow
This commit is contained in:
@@ -38,6 +38,22 @@ describe("resolveDiscordDmCommandAccess", () => {
|
||||
expect(result.commandAuthorized).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps command auth enabled for open DMs when configured allowlist does not match", async () => {
|
||||
const result = await resolveDiscordDmCommandAccess({
|
||||
accountId: "default",
|
||||
dmPolicy: "open",
|
||||
configuredAllowFrom: ["discord:999"],
|
||||
sender,
|
||||
allowNameMatching: false,
|
||||
useAccessGroups: true,
|
||||
readStoreAllowFrom: async () => [],
|
||||
});
|
||||
|
||||
expect(result.decision).toBe("allow");
|
||||
expect(result.allowMatch.allowed).toBe(false);
|
||||
expect(result.commandAuthorized).toBe(true);
|
||||
});
|
||||
|
||||
it("returns pairing decision and unauthorized command auth for unknown senders", async () => {
|
||||
const result = await resolveDiscordDmCommandAccess({
|
||||
accountId: "default",
|
||||
|
||||
114
src/discord/monitor/dm-command-decision.test.ts
Normal file
114
src/discord/monitor/dm-command-decision.test.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { DiscordDmCommandAccess } from "./dm-command-auth.js";
|
||||
import { handleDiscordDmCommandDecision } from "./dm-command-decision.js";
|
||||
|
||||
function buildDmAccess(overrides: Partial<DiscordDmCommandAccess>): DiscordDmCommandAccess {
|
||||
return {
|
||||
decision: "allow",
|
||||
reason: "ok",
|
||||
commandAuthorized: true,
|
||||
allowMatch: { allowed: true, matchKey: "123", matchSource: "id" },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("handleDiscordDmCommandDecision", () => {
|
||||
it("returns true for allowed DM access", async () => {
|
||||
const onPairingCreated = vi.fn(async () => {});
|
||||
const onUnauthorized = vi.fn(async () => {});
|
||||
const upsertPairingRequest = vi.fn(async () => ({ code: "PAIR-1", created: true }));
|
||||
|
||||
const allowed = await handleDiscordDmCommandDecision({
|
||||
dmAccess: buildDmAccess({ decision: "allow" }),
|
||||
accountId: "default",
|
||||
sender: { id: "123", tag: "alice#0001", name: "alice" },
|
||||
onPairingCreated,
|
||||
onUnauthorized,
|
||||
upsertPairingRequest,
|
||||
});
|
||||
|
||||
expect(allowed).toBe(true);
|
||||
expect(upsertPairingRequest).not.toHaveBeenCalled();
|
||||
expect(onPairingCreated).not.toHaveBeenCalled();
|
||||
expect(onUnauthorized).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("creates pairing reply for new pairing requests", async () => {
|
||||
const onPairingCreated = vi.fn(async () => {});
|
||||
const onUnauthorized = vi.fn(async () => {});
|
||||
const upsertPairingRequest = vi.fn(async () => ({ code: "PAIR-1", created: true }));
|
||||
|
||||
const allowed = await handleDiscordDmCommandDecision({
|
||||
dmAccess: buildDmAccess({
|
||||
decision: "pairing",
|
||||
commandAuthorized: false,
|
||||
allowMatch: { allowed: false },
|
||||
}),
|
||||
accountId: "default",
|
||||
sender: { id: "123", tag: "alice#0001", name: "alice" },
|
||||
onPairingCreated,
|
||||
onUnauthorized,
|
||||
upsertPairingRequest,
|
||||
});
|
||||
|
||||
expect(allowed).toBe(false);
|
||||
expect(upsertPairingRequest).toHaveBeenCalledWith({
|
||||
channel: "discord",
|
||||
id: "123",
|
||||
accountId: "default",
|
||||
meta: {
|
||||
tag: "alice#0001",
|
||||
name: "alice",
|
||||
},
|
||||
});
|
||||
expect(onPairingCreated).toHaveBeenCalledWith("PAIR-1");
|
||||
expect(onUnauthorized).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips pairing reply when pairing request already exists", async () => {
|
||||
const onPairingCreated = vi.fn(async () => {});
|
||||
const onUnauthorized = vi.fn(async () => {});
|
||||
const upsertPairingRequest = vi.fn(async () => ({ code: "PAIR-1", created: false }));
|
||||
|
||||
const allowed = await handleDiscordDmCommandDecision({
|
||||
dmAccess: buildDmAccess({
|
||||
decision: "pairing",
|
||||
commandAuthorized: false,
|
||||
allowMatch: { allowed: false },
|
||||
}),
|
||||
accountId: "default",
|
||||
sender: { id: "123", tag: "alice#0001", name: "alice" },
|
||||
onPairingCreated,
|
||||
onUnauthorized,
|
||||
upsertPairingRequest,
|
||||
});
|
||||
|
||||
expect(allowed).toBe(false);
|
||||
expect(onPairingCreated).not.toHaveBeenCalled();
|
||||
expect(onUnauthorized).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("runs unauthorized handler for blocked DM access", async () => {
|
||||
const onPairingCreated = vi.fn(async () => {});
|
||||
const onUnauthorized = vi.fn(async () => {});
|
||||
const upsertPairingRequest = vi.fn(async () => ({ code: "PAIR-1", created: true }));
|
||||
|
||||
const allowed = await handleDiscordDmCommandDecision({
|
||||
dmAccess: buildDmAccess({
|
||||
decision: "block",
|
||||
commandAuthorized: false,
|
||||
allowMatch: { allowed: false },
|
||||
}),
|
||||
accountId: "default",
|
||||
sender: { id: "123", tag: "alice#0001", name: "alice" },
|
||||
onPairingCreated,
|
||||
onUnauthorized,
|
||||
upsertPairingRequest,
|
||||
});
|
||||
|
||||
expect(allowed).toBe(false);
|
||||
expect(onUnauthorized).toHaveBeenCalledTimes(1);
|
||||
expect(upsertPairingRequest).not.toHaveBeenCalled();
|
||||
expect(onPairingCreated).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user