mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 20:51:23 +00:00
test: add per-account action gating tests for Discord and Telegram handlers
This commit is contained in:
committed by
Peter Steinberger
parent
a03fec2a3f
commit
4640999e77
@@ -1,8 +1,9 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { DiscordActionConfig } from "../../config/config.js";
|
||||
import type { DiscordActionConfig, OpenClawConfig } from "../../config/config.js";
|
||||
import { handleDiscordGuildAction } from "./discord-actions-guild.js";
|
||||
import { handleDiscordMessagingAction } from "./discord-actions-messaging.js";
|
||||
import { handleDiscordModerationAction } from "./discord-actions-moderation.js";
|
||||
import { handleDiscordAction } from "./discord-actions.js";
|
||||
|
||||
const createChannelDiscord = vi.fn(async () => ({
|
||||
id: "new-channel",
|
||||
@@ -596,3 +597,65 @@ describe("handleDiscordModerationAction", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleDiscordAction per-account gating", () => {
|
||||
it("allows moderation when account config enables it", async () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
discord: {
|
||||
accounts: {
|
||||
ops: { token: "tok-ops", actions: { moderation: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
await handleDiscordAction(
|
||||
{ action: "timeout", guildId: "G1", userId: "U1", durationMinutes: 5, accountId: "ops" },
|
||||
cfg,
|
||||
);
|
||||
expect(timeoutMemberDiscord).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ guildId: "G1", userId: "U1" }),
|
||||
{ accountId: "ops" },
|
||||
);
|
||||
});
|
||||
|
||||
it("blocks moderation when account omits it", async () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
discord: {
|
||||
accounts: {
|
||||
chat: { token: "tok-chat" },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
await expect(
|
||||
handleDiscordAction(
|
||||
{ action: "timeout", guildId: "G1", userId: "U1", durationMinutes: 5, accountId: "chat" },
|
||||
cfg,
|
||||
),
|
||||
).rejects.toThrow(/Discord moderation is disabled/);
|
||||
});
|
||||
|
||||
it("uses account-merged config, not top-level config", async () => {
|
||||
// Top-level has no moderation, but the account does
|
||||
const cfg = {
|
||||
channels: {
|
||||
discord: {
|
||||
token: "tok-base",
|
||||
accounts: {
|
||||
ops: { token: "tok-ops", actions: { moderation: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
await handleDiscordAction(
|
||||
{ action: "kick", guildId: "G1", userId: "U1", accountId: "ops" },
|
||||
cfg,
|
||||
);
|
||||
expect(kickMemberDiscord).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -589,3 +589,70 @@ describe("readTelegramButtons", () => {
|
||||
).toThrow(/style must be one of danger, success, primary/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleTelegramAction per-account gating", () => {
|
||||
it("allows sticker when account config enables it", async () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
telegram: {
|
||||
accounts: {
|
||||
media: { botToken: "tok-media", actions: { sticker: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
await handleTelegramAction(
|
||||
{ action: "sendSticker", to: "123", fileId: "sticker-id", accountId: "media" },
|
||||
cfg,
|
||||
);
|
||||
expect(sendStickerTelegram).toHaveBeenCalledWith(
|
||||
"123",
|
||||
"sticker-id",
|
||||
expect.objectContaining({ token: "tok-media" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("blocks sticker when account omits it", async () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
telegram: {
|
||||
accounts: {
|
||||
chat: { botToken: "tok-chat" },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
await expect(
|
||||
handleTelegramAction(
|
||||
{ action: "sendSticker", to: "123", fileId: "sticker-id", accountId: "chat" },
|
||||
cfg,
|
||||
),
|
||||
).rejects.toThrow(/sticker actions are disabled/i);
|
||||
});
|
||||
|
||||
it("uses account-merged config, not top-level config", async () => {
|
||||
// Top-level has no sticker enabled, but the account does
|
||||
const cfg = {
|
||||
channels: {
|
||||
telegram: {
|
||||
botToken: "tok-base",
|
||||
accounts: {
|
||||
media: { botToken: "tok-media", actions: { sticker: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
await handleTelegramAction(
|
||||
{ action: "sendSticker", to: "123", fileId: "sticker-id", accountId: "media" },
|
||||
cfg,
|
||||
);
|
||||
expect(sendStickerTelegram).toHaveBeenCalledWith(
|
||||
"123",
|
||||
"sticker-id",
|
||||
expect.objectContaining({ token: "tok-media" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { resolveTelegramAccount } from "../../telegram/accounts.js";
|
||||
import type { TelegramButtonStyle, TelegramInlineButtons } from "../../telegram/button-types.js";
|
||||
import { resolveTelegramAccount } from "../../telegram/accounts.js";
|
||||
import {
|
||||
resolveTelegramInlineButtonsScope,
|
||||
resolveTelegramTargetChatType,
|
||||
|
||||
Reference in New Issue
Block a user