





























@@ -0,0 +1,64 @@
1+import { describe, expect, it, vi } from "vitest";
2+import type { SlackMonitorContext } from "./context.js";
3+import { authorizeSlackDirectMessage } from "./dm-auth.js";
4+5+function makeCtx(dmPolicy: SlackMonitorContext["dmPolicy"]): SlackMonitorContext {
6+return {
7+allowNameMatching: false,
8+dmEnabled: true,
9+ dmPolicy,
10+} as SlackMonitorContext;
11+}
12+13+function makeParams(
14+dmPolicy: SlackMonitorContext["dmPolicy"],
15+): Parameters<typeof authorizeSlackDirectMessage>[0] {
16+return {
17+ctx: makeCtx(dmPolicy),
18+accountId: "workspace",
19+senderId: "U123",
20+allowFromLower: [],
21+resolveSenderName: vi.fn(async () => ({ name: "Alice" })),
22+sendPairingReply: vi.fn(),
23+onDisabled: vi.fn(),
24+onUnauthorized: vi.fn(),
25+log: vi.fn(),
26+};
27+}
28+29+describe("authorizeSlackDirectMessage", () => {
30+it("allows open DM policy when effective allowFrom includes wildcard", async () => {
31+const params = makeParams("open");
32+params.allowFromLower = ["*"];
33+params.resolveSenderName = vi.fn(async () => {
34+throw new Error("users.info failed");
35+});
36+37+await expect(authorizeSlackDirectMessage(params)).resolves.toBe(true);
38+39+expect(params.onUnauthorized).not.toHaveBeenCalled();
40+expect(params.resolveSenderName).not.toHaveBeenCalled();
41+});
42+43+it("rejects open DM policy when effective allowFrom lacks wildcard", async () => {
44+const params = makeParams("open");
45+46+await expect(authorizeSlackDirectMessage(params)).resolves.toBe(false);
47+48+expect(params.onUnauthorized).toHaveBeenCalledWith({
49+allowMatchMeta: "matchKey=none matchSource=none",
50+senderName: "Alice",
51+});
52+});
53+54+it("keeps allowlist DM policy gated by allowFrom", async () => {
55+const params = makeParams("allowlist");
56+57+await expect(authorizeSlackDirectMessage(params)).resolves.toBe(false);
58+59+expect(params.onUnauthorized).toHaveBeenCalledWith({
60+allowMatchMeta: "matchKey=none matchSource=none",
61+senderName: "Alice",
62+});
63+});
64+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。