|
| 1 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { |
| 4 | + resolveWhatsAppConversationGroupAllowFrom, |
| 5 | + resolveWhatsAppInboundPolicy, |
| 6 | +} from "./inbound-policy.js"; |
| 7 | + |
| 8 | +function buildPolicyForCfg(cfg: OpenClawConfig) { |
| 9 | + return resolveWhatsAppInboundPolicy({ cfg, accountId: null, selfE164: null }); |
| 10 | +} |
| 11 | + |
| 12 | +describe("resolveWhatsAppConversationGroupAllowFrom (#69926)", () => { |
| 13 | + it("returns the account-level groupAllowFrom when no per-group allowFrom is configured", () => { |
| 14 | + const policy = buildPolicyForCfg({ |
| 15 | + channels: { |
| 16 | + whatsapp: { |
| 17 | + enabled: true, |
| 18 | + groupPolicy: "allowlist", |
| 19 | + groupAllowFrom: ["+15550001111", "+15550002222"], |
| 20 | + }, |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + expect( |
| 25 | + resolveWhatsAppConversationGroupAllowFrom({ |
| 26 | + policy, |
| 27 | + conversationId: "[email protected]", |
| 28 | + }), |
| 29 | + ).toEqual(["+15550001111", "+15550002222"]); |
| 30 | + }); |
| 31 | + |
| 32 | + it("uses per-group allowFrom when defined for the conversation JID", () => { |
| 33 | + const policy = buildPolicyForCfg({ |
| 34 | + channels: { |
| 35 | + whatsapp: { |
| 36 | + enabled: true, |
| 37 | + groupPolicy: "allowlist", |
| 38 | + groupAllowFrom: ["+15550001111"], |
| 39 | + groups: { |
| 40 | + |
| 41 | + allowFrom: ["+15550003333"], |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + expect( |
| 49 | + resolveWhatsAppConversationGroupAllowFrom({ |
| 50 | + policy, |
| 51 | + conversationId: "[email protected]", |
| 52 | + }), |
| 53 | + ).toEqual(["+15550003333"]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("falls back to the account-level list when the per-group allowFrom is empty", () => { |
| 57 | + const policy = buildPolicyForCfg({ |
| 58 | + channels: { |
| 59 | + whatsapp: { |
| 60 | + enabled: true, |
| 61 | + groupPolicy: "allowlist", |
| 62 | + groupAllowFrom: ["+15550001111"], |
| 63 | + groups: { |
| 64 | + |
| 65 | + allowFrom: [], |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + expect( |
| 73 | + resolveWhatsAppConversationGroupAllowFrom({ |
| 74 | + policy, |
| 75 | + conversationId: "[email protected]", |
| 76 | + }), |
| 77 | + ).toEqual(["+15550001111"]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("leaves other group JIDs on the account-level allowlist when one group has an override", () => { |
| 81 | + const policy = buildPolicyForCfg({ |
| 82 | + channels: { |
| 83 | + whatsapp: { |
| 84 | + enabled: true, |
| 85 | + groupPolicy: "allowlist", |
| 86 | + groupAllowFrom: ["+15550001111"], |
| 87 | + groups: { |
| 88 | + |
| 89 | + allowFrom: ["+15550003333"], |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + expect( |
| 97 | + resolveWhatsAppConversationGroupAllowFrom({ |
| 98 | + policy, |
| 99 | + conversationId: "[email protected]", |
| 100 | + }), |
| 101 | + ).toEqual(["+15550001111"]); |
| 102 | + }); |
| 103 | + |
| 104 | + it("honours an account-scoped groups[<jid>].allowFrom over a root-scope account default", () => { |
| 105 | + const policy = resolveWhatsAppInboundPolicy({ |
| 106 | + cfg: { |
| 107 | + channels: { |
| 108 | + whatsapp: { |
| 109 | + enabled: true, |
| 110 | + groupPolicy: "allowlist", |
| 111 | + groupAllowFrom: ["+15550001111"], |
| 112 | + groups: { |
| 113 | + |
| 114 | + allowFrom: ["+15550009999"], |
| 115 | + }, |
| 116 | + }, |
| 117 | + accounts: { |
| 118 | + work: { |
| 119 | + groupAllowFrom: ["+15550008888"], |
| 120 | + groups: { |
| 121 | + |
| 122 | + allowFrom: ["+15550007777"], |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } as OpenClawConfig, |
| 130 | + accountId: "work", |
| 131 | + selfE164: null, |
| 132 | + }); |
| 133 | + |
| 134 | + expect( |
| 135 | + resolveWhatsAppConversationGroupAllowFrom({ |
| 136 | + policy, |
| 137 | + conversationId: "[email protected]", |
| 138 | + }), |
| 139 | + ).toEqual(["+15550007777"]); |
| 140 | + }); |
| 141 | + |
| 142 | + it("returns the account-level list when the policy has no groups map at all", () => { |
| 143 | + const policy = buildPolicyForCfg({ |
| 144 | + channels: { |
| 145 | + whatsapp: { |
| 146 | + enabled: true, |
| 147 | + groupPolicy: "allowlist", |
| 148 | + groupAllowFrom: ["+15550001111"], |
| 149 | + }, |
| 150 | + }, |
| 151 | + }); |
| 152 | + |
| 153 | + expect( |
| 154 | + resolveWhatsAppConversationGroupAllowFrom({ |
| 155 | + policy, |
| 156 | + conversationId: "[email protected]", |
| 157 | + }), |
| 158 | + ).toEqual(["+15550001111"]); |
| 159 | + }); |
| 160 | +}); |
0 commit comments