Initial public release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
387 lines
13 KiB
Go
387 lines
13 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func baseRequest(action string) Request {
|
|
return Request{
|
|
Action: action,
|
|
ChatType: "supergroup",
|
|
BotUsername: "iishkogatewaybot",
|
|
ActorAllowed: true,
|
|
BotAllowed: true,
|
|
}
|
|
}
|
|
|
|
func requestWith(action string, args ...string) Request {
|
|
request := baseRequest(action)
|
|
request.Args = args
|
|
return request
|
|
}
|
|
|
|
func TestPlanPermissions(t *testing.T) {
|
|
requests := []Request{
|
|
requestWith(ActionBan, "101"),
|
|
requestWith(ActionUnban, "101"),
|
|
requestWith(ActionKick, "101"),
|
|
requestWith(ActionMute, "101"),
|
|
requestWith(ActionUnmute, "101"),
|
|
requestWith(ActionSetup),
|
|
requestWith(ActionChatMode, "quiet"),
|
|
}
|
|
flags := []struct {
|
|
name string
|
|
actorAllowed bool
|
|
botAllowed bool
|
|
}{
|
|
{"actor denied", false, true},
|
|
{"bot denied", true, false},
|
|
{"both denied", false, false},
|
|
}
|
|
for _, request := range requests {
|
|
for _, flag := range flags {
|
|
t.Run(request.Action+"/"+flag.name, func(t *testing.T) {
|
|
request.ActorAllowed = flag.actorAllowed
|
|
request.BotAllowed = flag.botAllowed
|
|
_, err := Plan(request)
|
|
assertPolicyStatus(t, err, http.StatusForbidden)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPlanResolveWithoutRights(t *testing.T) {
|
|
request := requestWith(ActionResolve)
|
|
request.ReplyUID = 101
|
|
request.ActorAllowed = false
|
|
request.BotAllowed = false
|
|
request.ChatType = "private"
|
|
|
|
response, err := Plan(request)
|
|
if err != nil {
|
|
t.Fatalf("resolve without rights: %v", err)
|
|
}
|
|
if response.Action != ActionResolve || response.TargetUID != 101 {
|
|
t.Fatalf("resolve response = %+v", response)
|
|
}
|
|
}
|
|
|
|
func TestPlanResolveTargets(t *testing.T) {
|
|
candidates := []Candidate{{UID: 101, Username: "Alice"}, {UID: 202, Username: "bob"}}
|
|
ambiguous := []Candidate{{UID: 1, Username: "alice"}, {UID: 2, Username: "ALICE"}}
|
|
|
|
named := requestWith(ActionResolve, "@alice")
|
|
named.Candidates = candidates
|
|
bareName := requestWith(ActionResolve, "ALICE")
|
|
bareName.Candidates = candidates
|
|
plainNumeric := requestWith(ActionResolve, "101")
|
|
plusNumeric := requestWith(ActionResolve, "+101")
|
|
reply := requestWith(ActionResolve)
|
|
reply.ReplyUID = 303
|
|
unknownName := requestWith(ActionResolve, "@carol")
|
|
unknownName.Candidates = candidates
|
|
ambiguousName := requestWith(ActionResolve, "alice")
|
|
ambiguousName.Candidates = ambiguous
|
|
|
|
tests := []struct {
|
|
name string
|
|
request Request
|
|
wantTarget int64
|
|
wantStatus int
|
|
}{
|
|
{"numeric", plainNumeric, 101, http.StatusOK},
|
|
{"signed numeric", plusNumeric, 101, http.StatusOK},
|
|
{"at username", named, 101, http.StatusOK},
|
|
{"bare username", bareName, 101, http.StatusOK},
|
|
{"reply only", reply, 303, http.StatusOK},
|
|
{"unknown username", unknownName, 0, http.StatusBadRequest},
|
|
{"ambiguous username", ambiguousName, 0, http.StatusBadRequest},
|
|
{"missing target", requestWith(ActionResolve), 0, http.StatusBadRequest},
|
|
{"zero id", requestWith(ActionResolve, "0"), 0, http.StatusBadRequest},
|
|
{"negative id", requestWith(ActionResolve, "-101"), 0, http.StatusBadRequest},
|
|
{"overflow id", requestWith(ActionResolve, "9223372036854775808"), 0, http.StatusBadRequest},
|
|
{"empty argument", requestWith(ActionResolve, ""), 0, http.StatusBadRequest},
|
|
{"empty username", requestWith(ActionResolve, "@"), 0, http.StatusBadRequest},
|
|
{"too many arguments", requestWith(ActionResolve, "101", "202"), 0, http.StatusBadRequest},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
response, err := Plan(test.request)
|
|
if test.wantStatus == http.StatusOK {
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if response.Action != ActionResolve || response.TargetUID != test.wantTarget {
|
|
t.Fatalf("response = %+v, want target %d", response, test.wantTarget)
|
|
}
|
|
return
|
|
}
|
|
assertPolicyStatus(t, err, test.wantStatus)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlanTargetConflict(t *testing.T) {
|
|
cases := []Request{
|
|
func() Request {
|
|
request := requestWith(ActionResolve, "101")
|
|
request.ReplyUID = 202
|
|
return request
|
|
}(),
|
|
func() Request {
|
|
request := requestWith(ActionResolve, "@alice")
|
|
request.ReplyUID = 202
|
|
request.Candidates = []Candidate{{UID: 101, Username: "alice"}}
|
|
return request
|
|
}(),
|
|
func() Request {
|
|
request := requestWith(ActionBan, "101")
|
|
request.ReplyUID = 202
|
|
return request
|
|
}(),
|
|
}
|
|
for _, request := range cases {
|
|
_, err := Plan(request)
|
|
assertPolicyStatus(t, err, http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
func TestPlanValidatesSuppliedIDs(t *testing.T) {
|
|
badCandidate := requestWith(ActionResolve, "alice")
|
|
badCandidate.Candidates = []Candidate{{UID: 0, Username: "alice"}}
|
|
badProtected := requestWith(ActionResolve, "101")
|
|
badProtected.ProtectedUIDs = []int64{-1}
|
|
badReply := requestWith(ActionResolve)
|
|
badReply.ReplyUID = -7
|
|
|
|
for _, request := range []Request{badCandidate, badProtected, badReply} {
|
|
_, err := Plan(request)
|
|
assertPolicyStatus(t, err, http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
func TestPlanProtectedTargets(t *testing.T) {
|
|
protected := []int64{101}
|
|
|
|
for _, action := range []string{ActionBan, ActionKick} {
|
|
request := requestWith(action, "101")
|
|
request.ProtectedUIDs = protected
|
|
_, err := Plan(request)
|
|
assertPolicyStatus(t, err, http.StatusForbidden)
|
|
}
|
|
|
|
for _, action := range []string{ActionUnban, ActionUnmute} {
|
|
request := requestWith(action, "101")
|
|
request.ProtectedUIDs = protected
|
|
response, err := Plan(request)
|
|
if err != nil {
|
|
t.Fatalf("%s protected target should be allowed: %v", action, err)
|
|
}
|
|
if response.TargetUID != 101 {
|
|
t.Fatalf("%s target = %d, want 101", action, response.TargetUID)
|
|
}
|
|
}
|
|
|
|
muteRequest := requestWith(ActionMute, "101", "2h")
|
|
muteRequest.ProtectedUIDs = protected
|
|
_, err := Plan(muteRequest)
|
|
assertPolicyStatus(t, err, http.StatusForbidden)
|
|
|
|
unprotected := requestWith(ActionBan, "102")
|
|
unprotected.ProtectedUIDs = protected
|
|
response, err := Plan(unprotected)
|
|
if err != nil {
|
|
t.Fatalf("unprotected target: %v", err)
|
|
}
|
|
if response.TargetUID != 102 {
|
|
t.Fatalf("unprotected target = %d, want 102", response.TargetUID)
|
|
}
|
|
}
|
|
|
|
func TestPlanMuteRequiresSupergroup(t *testing.T) {
|
|
for _, action := range []string{ActionMute, ActionUnmute} {
|
|
for _, chatType := range []string{"group", "private", "channel", ""} {
|
|
request := requestWith(action, "101")
|
|
request.ChatType = chatType
|
|
_, err := Plan(request)
|
|
assertPolicyStatus(t, err, http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
request := requestWith(ActionMute, "101")
|
|
request.ChatType = "SUPERGROUP"
|
|
response, err := Plan(request)
|
|
if err != nil {
|
|
t.Fatalf("case-insensitive supergroup: %v", err)
|
|
}
|
|
if response.DurationSeconds != DefaultMuteDuration {
|
|
t.Fatalf("duration = %d, want %d", response.DurationSeconds, DefaultMuteDuration)
|
|
}
|
|
}
|
|
|
|
func TestPlanMuteDurations(t *testing.T) {
|
|
muteReply := func(uid int64, args ...string) Request {
|
|
request := requestWith(ActionMute, args...)
|
|
request.ReplyUID = uid
|
|
return request
|
|
}
|
|
candidates := []Candidate{{UID: 101, Username: "Alice"}, {UID: 202, Username: "bob"}}
|
|
|
|
namedExplicit := requestWith(ActionMute, "@alice", "2h")
|
|
namedExplicit.Candidates = candidates
|
|
bareNamed := requestWith(ActionMute, "alice")
|
|
bareNamed.Candidates = candidates
|
|
namedReplyDuration := muteReply(303, "1h30m")
|
|
|
|
tests := []struct {
|
|
name string
|
|
request Request
|
|
wantTarget int64
|
|
wantDur int64
|
|
wantStatus int
|
|
}{
|
|
{"reply default", muteReply(101), 101, DefaultMuteDuration, http.StatusOK},
|
|
{"reply duration", muteReply(101, "2h"), 101, 7200, http.StatusOK},
|
|
{"reply bare minutes", muteReply(101, "90"), 101, 5400, http.StatusOK},
|
|
{"reply compound", namedReplyDuration, 303, 5400, http.StatusOK},
|
|
{"reply two minutes", muteReply(101, "2"), 101, 120, http.StatusOK},
|
|
{"explicit duration", requestWith(ActionMute, "101", "45m"), 101, 2700, http.StatusOK},
|
|
{"explicit compound", requestWith(ActionMute, "101", "1h30m"), 101, 5400, http.StatusOK},
|
|
{"explicit bare minutes", requestWith(ActionMute, "101", "5"), 101, 300, http.StatusOK},
|
|
{"explicit username", namedExplicit, 101, 7200, http.StatusOK},
|
|
{"bare username default", bareNamed, 101, DefaultMuteDuration, http.StatusOK},
|
|
{"numeric target default", requestWith(ActionMute, "202"), 202, DefaultMuteDuration, http.StatusOK},
|
|
{"max duration", requestWith(ActionMute, "101", "365d"), 101, MaxDuration, http.StatusOK},
|
|
{"min duration", requestWith(ActionMute, "101", "30s"), 101, 30, http.StatusOK},
|
|
{"duration only", requestWith(ActionMute, "2h"), 0, 0, http.StatusBadRequest},
|
|
{"missing everything", requestWith(ActionMute), 0, 0, http.StatusBadRequest},
|
|
{"reply plus name", muteReply(101, "@alice"), 0, 0, http.StatusBadRequest},
|
|
{"reply plus numeric duration", muteReply(101, "202"), 101, 12120, http.StatusOK},
|
|
{"reply plus bad name", muteReply(101, "alice"), 0, 0, http.StatusBadRequest},
|
|
{"two numbers explicit", requestWith(ActionMute, "101", "202"), 101, 12120, http.StatusOK},
|
|
{"two targets with reply", muteReply(101, "202", "2h"), 0, 0, http.StatusBadRequest},
|
|
{"too many", requestWith(ActionMute, "101", "2h", "extra"), 0, 0, http.StatusBadRequest},
|
|
{"malformed duration", requestWith(ActionMute, "101", "1x"), 0, 0, http.StatusBadRequest},
|
|
{"malformed reply duration", muteReply(101, "1x"), 0, 0, http.StatusBadRequest},
|
|
{"zero reply duration", muteReply(101, "0"), 0, 0, http.StatusBadRequest},
|
|
{"zero target", requestWith(ActionMute, "0"), 0, 0, http.StatusBadRequest},
|
|
{"negative target", requestWith(ActionMute, "-101"), 0, 0, http.StatusBadRequest},
|
|
{"zero duration", requestWith(ActionMute, "101", "0s"), 0, 0, http.StatusBadRequest},
|
|
{"short duration", requestWith(ActionMute, "101", "29s"), 0, 0, http.StatusBadRequest},
|
|
{"long duration", requestWith(ActionMute, "101", "366d"), 0, 0, http.StatusBadRequest},
|
|
{"unknown username", requestWith(ActionMute, "@carol", "1h"), 0, 0, http.StatusBadRequest},
|
|
{"empty argument", requestWith(ActionMute, ""), 0, 0, http.StatusBadRequest},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
response, err := Plan(test.request)
|
|
if test.wantStatus != http.StatusOK {
|
|
assertPolicyStatus(t, err, test.wantStatus)
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if response.Action != ActionMute || response.TargetUID != test.wantTarget || response.DurationSeconds != test.wantDur {
|
|
t.Fatalf("response = %+v, want target %d duration %d", response, test.wantTarget, test.wantDur)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlanSetup(t *testing.T) {
|
|
response, err := Plan(requestWith(ActionSetup))
|
|
if err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
if response.Action != ActionSetup || response.TargetUID != 0 || response.DurationSeconds != 0 || response.Mode != "" {
|
|
t.Fatalf("setup response = %+v", response)
|
|
}
|
|
|
|
_, err = Plan(requestWith(ActionSetup, "extra"))
|
|
assertPolicyStatus(t, err, http.StatusBadRequest)
|
|
}
|
|
|
|
func TestPlanChatMode(t *testing.T) {
|
|
for _, mode := range []string{"quiet", "mentions", "replies", "QUIET", " Mentions "} {
|
|
response, err := Plan(requestWith(ActionChatMode, mode))
|
|
if err != nil {
|
|
t.Fatalf("chatmode %q: %v", mode, err)
|
|
}
|
|
want := asciiLower(strings.TrimSpace(mode))
|
|
if response.Action != ActionChatMode || response.Mode != want || response.TargetUID != 0 {
|
|
t.Fatalf("chatmode %q response = %+v, want %q", mode, response, want)
|
|
}
|
|
}
|
|
|
|
for _, request := range []Request{
|
|
requestWith(ActionChatMode),
|
|
requestWith(ActionChatMode, "silent"),
|
|
requestWith(ActionChatMode, "quiet", "mentions"),
|
|
requestWith(ActionChatMode, ""),
|
|
} {
|
|
_, err := Plan(request)
|
|
assertPolicyStatus(t, err, http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
func TestPlanActionNormalization(t *testing.T) {
|
|
request := requestWith(" BAN ", "101")
|
|
response, err := Plan(request)
|
|
if err != nil {
|
|
t.Fatalf("uppercase action: %v", err)
|
|
}
|
|
if response.Action != ActionBan || response.TargetUID != 101 {
|
|
t.Fatalf("uppercase action response = %+v", response)
|
|
}
|
|
|
|
for _, action := range []string{"", "nope", "mute "} {
|
|
if action == "mute " {
|
|
request = requestWith(action, "101")
|
|
response, err = Plan(request)
|
|
if err != nil {
|
|
t.Fatalf("padded mute: %v", err)
|
|
}
|
|
if response.Action != ActionMute {
|
|
t.Fatalf("padded mute response = %+v", response)
|
|
}
|
|
continue
|
|
}
|
|
_, err = Plan(requestWith(action, "101"))
|
|
assertPolicyStatus(t, err, http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
func TestPlanBanAndUnbanResponses(t *testing.T) {
|
|
for _, action := range []string{ActionBan, ActionUnban, ActionKick, ActionUnmute} {
|
|
response, err := Plan(requestWith(action, "101"))
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", action, err)
|
|
}
|
|
if response.Action != action || response.TargetUID != 101 || response.DurationSeconds != 0 || response.Mode != "" || response.Reason != "" {
|
|
t.Fatalf("%s response = %+v", action, response)
|
|
}
|
|
}
|
|
}
|
|
|
|
func assertPolicyStatus(t *testing.T, err error, status int) {
|
|
t.Helper()
|
|
if err == nil {
|
|
t.Fatalf("error = nil, want status %d", status)
|
|
}
|
|
var policyErr *PolicyError
|
|
if !errors.As(err, &policyErr) {
|
|
t.Fatalf("error type = %T, want *PolicyError (%v)", err, err)
|
|
}
|
|
if policyErr.Status != status {
|
|
t.Fatalf("status = %d, want %d (message %q)", policyErr.Status, status, policyErr.Message)
|
|
}
|
|
if policyErr.Message == "" {
|
|
t.Fatal("empty Russian error message")
|
|
}
|
|
}
|