test: merge command allowlist add cases

This commit is contained in:
Peter Steinberger 2026-03-17 09:38:33 +00:00
parent 7c24aab954
commit 59eaeaccfe

View File

@ -1083,78 +1083,92 @@ describe("handleCommands /allowlist", () => {
expect(result.reply?.text).toContain("Paired allowFrom (store): 456"); expect(result.reply?.text).toContain("Paired allowFrom (store): 456");
}); });
it("adds entries to config and pairing store", async () => { it("adds allowlist entries to config and pairing stores", async () => {
await withTempConfigPath(
{
channels: { telegram: { allowFrom: ["123"] } },
},
async (configPath) => {
readConfigFileSnapshotMock.mockResolvedValueOnce({
valid: true,
parsed: {
channels: { telegram: { allowFrom: ["123"] } },
},
});
validateConfigObjectWithPluginsMock.mockImplementation((config: unknown) => ({
ok: true,
config,
}));
addChannelAllowFromStoreEntryMock.mockResolvedValueOnce({
changed: true,
allowFrom: ["123", "789"],
});
const cfg = {
commands: { text: true, config: true },
channels: { telegram: { allowFrom: ["123"] } },
} as OpenClawConfig;
const params = buildPolicyParams("/allowlist add dm 789", cfg);
const result = await handleCommands(params);
expect(result.shouldContinue).toBe(false);
const written = await readJsonFile<OpenClawConfig>(configPath);
expect(written.channels?.telegram?.allowFrom).toEqual(["123", "789"]);
expect(addChannelAllowFromStoreEntryMock).toHaveBeenCalledWith({
channel: "telegram",
entry: "789",
accountId: "default",
});
expect(result.reply?.text).toContain("DM allowlist added");
},
);
});
it("writes store entries to the selected account scope", async () => {
readConfigFileSnapshotMock.mockResolvedValueOnce({
valid: true,
parsed: {
channels: { telegram: { accounts: { work: { allowFrom: ["123"] } } } },
},
});
validateConfigObjectWithPluginsMock.mockImplementation((config: unknown) => ({ validateConfigObjectWithPluginsMock.mockImplementation((config: unknown) => ({
ok: true, ok: true,
config, config,
})); }));
addChannelAllowFromStoreEntryMock.mockResolvedValueOnce({ const cases = [
changed: true, {
allowFrom: ["123", "789"], name: "default account",
}); run: async () => {
await withTempConfigPath(
{
channels: { telegram: { allowFrom: ["123"] } },
},
async (configPath) => {
readConfigFileSnapshotMock.mockResolvedValueOnce({
valid: true,
parsed: {
channels: { telegram: { allowFrom: ["123"] } },
},
});
addChannelAllowFromStoreEntryMock.mockResolvedValueOnce({
changed: true,
allowFrom: ["123", "789"],
});
const cfg = { const params = buildPolicyParams("/allowlist add dm 789", {
commands: { text: true, config: true }, commands: { text: true, config: true },
channels: { telegram: { accounts: { work: { allowFrom: ["123"] } } } }, channels: { telegram: { allowFrom: ["123"] } },
} as OpenClawConfig; } as OpenClawConfig);
const params = buildPolicyParams("/allowlist add dm --account work 789", cfg, { const result = await handleCommands(params);
AccountId: "work",
});
const result = await handleCommands(params);
expect(result.shouldContinue).toBe(false); expect(result.shouldContinue).toBe(false);
expect(addChannelAllowFromStoreEntryMock).toHaveBeenCalledWith({ const written = await readJsonFile<OpenClawConfig>(configPath);
channel: "telegram", expect(written.channels?.telegram?.allowFrom, "default account").toEqual([
entry: "789", "123",
accountId: "work", "789",
}); ]);
expect(addChannelAllowFromStoreEntryMock, "default account").toHaveBeenCalledWith({
channel: "telegram",
entry: "789",
accountId: "default",
});
expect(result.reply?.text, "default account").toContain("DM allowlist added");
},
);
},
},
{
name: "selected account scope",
run: async () => {
readConfigFileSnapshotMock.mockResolvedValueOnce({
valid: true,
parsed: {
channels: { telegram: { accounts: { work: { allowFrom: ["123"] } } } },
},
});
addChannelAllowFromStoreEntryMock.mockResolvedValueOnce({
changed: true,
allowFrom: ["123", "789"],
});
const params = buildPolicyParams(
"/allowlist add dm --account work 789",
{
commands: { text: true, config: true },
channels: { telegram: { accounts: { work: { allowFrom: ["123"] } } } },
} as OpenClawConfig,
{
AccountId: "work",
},
);
const result = await handleCommands(params);
expect(result.shouldContinue, "selected account scope").toBe(false);
expect(addChannelAllowFromStoreEntryMock, "selected account scope").toHaveBeenCalledWith({
channel: "telegram",
entry: "789",
accountId: "work",
});
},
},
] as const;
for (const testCase of cases) {
await testCase.run();
}
}); });
it("blocks config-targeted /allowlist edits when the target account disables writes", async () => { it("blocks config-targeted /allowlist edits when the target account disables writes", async () => {