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