test: dedupe twitch access control checks

This commit is contained in:
Peter Steinberger 2026-03-13 21:54:23 +00:00
parent 0530d1c530
commit 110eeec5b8

View File

@ -64,6 +64,26 @@ describe("checkTwitchAccessControl", () => {
return result; return result;
} }
function expectAllowFromBlocked(params: {
allowFrom: string[];
allowedRoles?: NonNullable<TwitchAccountConfig["allowedRoles"]>;
message?: Partial<TwitchChatMessage>;
reason: string;
}) {
const result = runAccessCheck({
account: {
allowFrom: params.allowFrom,
allowedRoles: params.allowedRoles,
},
message: {
message: "@testbot hello",
...params.message,
},
});
expect(result.allowed).toBe(false);
expect(result.reason).toContain(params.reason);
}
describe("when no restrictions are configured", () => { describe("when no restrictions are configured", () => {
it("allows messages that mention the bot (default requireMention)", () => { it("allows messages that mention the bot (default requireMention)", () => {
const result = runAccessCheck({ const result = runAccessCheck({
@ -134,42 +154,18 @@ describe("checkTwitchAccessControl", () => {
}); });
it("blocks users not in allowlist when allowFrom is set", () => { it("blocks users not in allowlist when allowFrom is set", () => {
const account: TwitchAccountConfig = { expectAllowFromBlocked({
...mockAccount,
allowFrom: ["789012"], allowFrom: ["789012"],
}; reason: "allowFrom",
const message: TwitchChatMessage = {
...mockMessage,
message: "@testbot hello",
};
const result = checkTwitchAccessControl({
message,
account,
botUsername: "testbot",
}); });
expect(result.allowed).toBe(false);
expect(result.reason).toContain("allowFrom");
}); });
it("blocks messages without userId", () => { it("blocks messages without userId", () => {
const account: TwitchAccountConfig = { expectAllowFromBlocked({
...mockAccount,
allowFrom: ["123456"], allowFrom: ["123456"],
}; message: { userId: undefined },
const message: TwitchChatMessage = { reason: "user ID not available",
...mockMessage,
message: "@testbot hello",
userId: undefined,
};
const result = checkTwitchAccessControl({
message,
account,
botUsername: "testbot",
}); });
expect(result.allowed).toBe(false);
expect(result.reason).toContain("user ID not available");
}); });
it("bypasses role checks when user is in allowlist", () => { it("bypasses role checks when user is in allowlist", () => {
@ -193,47 +189,21 @@ describe("checkTwitchAccessControl", () => {
}); });
it("blocks user with role when not in allowlist", () => { it("blocks user with role when not in allowlist", () => {
const account: TwitchAccountConfig = { expectAllowFromBlocked({
...mockAccount,
allowFrom: ["789012"], allowFrom: ["789012"],
allowedRoles: ["moderator"], allowedRoles: ["moderator"],
}; message: { userId: "123456", isMod: true },
const message: TwitchChatMessage = { reason: "allowFrom",
...mockMessage,
message: "@testbot hello",
userId: "123456",
isMod: true,
};
const result = checkTwitchAccessControl({
message,
account,
botUsername: "testbot",
}); });
expect(result.allowed).toBe(false);
expect(result.reason).toContain("allowFrom");
}); });
it("blocks user not in allowlist even when roles configured", () => { it("blocks user not in allowlist even when roles configured", () => {
const account: TwitchAccountConfig = { expectAllowFromBlocked({
...mockAccount,
allowFrom: ["789012"], allowFrom: ["789012"],
allowedRoles: ["moderator"], allowedRoles: ["moderator"],
}; message: { userId: "123456", isMod: false },
const message: TwitchChatMessage = { reason: "allowFrom",
...mockMessage,
message: "@testbot hello",
userId: "123456",
isMod: false,
};
const result = checkTwitchAccessControl({
message,
account,
botUsername: "testbot",
}); });
expect(result.allowed).toBe(false);
expect(result.reason).toContain("allowFrom");
}); });
}); });