refactor: share zalouser group auth setup

This commit is contained in:
Peter Steinberger 2026-03-13 19:00:05 +00:00
parent 44e1c6cc21
commit a5f0f66427
1 changed files with 29 additions and 26 deletions

View File

@ -187,6 +187,31 @@ function installRuntime(params: {
}; };
} }
function installGroupCommandAuthRuntime() {
return installRuntime({
resolveCommandAuthorizedFromAuthorizers: ({ useAccessGroups, authorizers }) =>
useAccessGroups && authorizers.some((entry) => entry.configured && entry.allowed),
});
}
async function processGroupControlCommand(params: {
account: ResolvedZalouserAccount;
content?: string;
commandContent?: string;
}) {
await __testing.processMessage({
message: createGroupMessage({
content: params.content ?? "/new",
commandContent: params.commandContent ?? "/new",
hasAnyMention: true,
wasExplicitlyMentioned: true,
}),
account: params.account,
config: createConfig(),
runtime: createRuntimeEnv(),
});
}
function createGroupMessage(overrides: Partial<ZaloInboundMessage> = {}): ZaloInboundMessage { function createGroupMessage(overrides: Partial<ZaloInboundMessage> = {}): ZaloInboundMessage {
return { return {
threadId: "g-1", threadId: "g-1",
@ -369,17 +394,8 @@ describe("zalouser monitor group mention gating", () => {
it("allows group control commands when only allowFrom is configured", async () => { it("allows group control commands when only allowFrom is configured", async () => {
const { dispatchReplyWithBufferedBlockDispatcher, resolveCommandAuthorizedFromAuthorizers } = const { dispatchReplyWithBufferedBlockDispatcher, resolveCommandAuthorizedFromAuthorizers } =
installRuntime({ installGroupCommandAuthRuntime();
resolveCommandAuthorizedFromAuthorizers: ({ useAccessGroups, authorizers }) => await processGroupControlCommand({
useAccessGroups && authorizers.some((entry) => entry.configured && entry.allowed),
});
await __testing.processMessage({
message: createGroupMessage({
content: "/new",
commandContent: "/new",
hasAnyMention: true,
wasExplicitlyMentioned: true,
}),
account: { account: {
...createAccount(), ...createAccount(),
config: { config: {
@ -387,8 +403,6 @@ describe("zalouser monitor group mention gating", () => {
allowFrom: ["123"], allowFrom: ["123"],
}, },
}, },
config: createConfig(),
runtime: createRuntimeEnv(),
}); });
expect(dispatchReplyWithBufferedBlockDispatcher).toHaveBeenCalledTimes(1); expect(dispatchReplyWithBufferedBlockDispatcher).toHaveBeenCalledTimes(1);
@ -493,17 +507,8 @@ describe("zalouser monitor group mention gating", () => {
it("allows group control commands when sender is in groupAllowFrom", async () => { it("allows group control commands when sender is in groupAllowFrom", async () => {
const { dispatchReplyWithBufferedBlockDispatcher, resolveCommandAuthorizedFromAuthorizers } = const { dispatchReplyWithBufferedBlockDispatcher, resolveCommandAuthorizedFromAuthorizers } =
installRuntime({ installGroupCommandAuthRuntime();
resolveCommandAuthorizedFromAuthorizers: ({ useAccessGroups, authorizers }) => await processGroupControlCommand({
useAccessGroups && authorizers.some((entry) => entry.configured && entry.allowed),
});
await __testing.processMessage({
message: createGroupMessage({
content: "/new",
commandContent: "/new",
hasAnyMention: true,
wasExplicitlyMentioned: true,
}),
account: { account: {
...createAccount(), ...createAccount(),
config: { config: {
@ -512,8 +517,6 @@ describe("zalouser monitor group mention gating", () => {
groupAllowFrom: ["123"], groupAllowFrom: ["123"],
}, },
}, },
config: createConfig(),
runtime: createRuntimeEnv(),
}); });
expect(dispatchReplyWithBufferedBlockDispatcher).toHaveBeenCalledTimes(1); expect(dispatchReplyWithBufferedBlockDispatcher).toHaveBeenCalledTimes(1);