Files
3x-ui/internal/web/service/tgbot/tgbot_add_client_expiry_test.go
T
sdhfsl ee2ff48c81 fix(tgbot): scope the add-client wizard to the admin, not the chat (#6604)
Two admins in one group chat shared one draft and one wizard step: clientDrafts and userStateStore were keyed by chat id alone, so the second admin's wizard opened on the first one's email and limits, and whichever of them tapped a control last decided what the other created.

Key both stores by (chat, user) instead. A private chat is unaffected: its two ids are equal, so the key matches what the chat alone used to be. A message with no sender (a channel post) keys to user 0, which no admin holds.

Fixes #6593.
2026-09-26 20:31:39 +02:00

62 lines
1.9 KiB
Go

package tgbot
import (
"strings"
"testing"
"github.com/mymmrac/telego"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
// Pins the decision the dead accumulate branch hid: a preset tap chooses the term
// instead of adding to it, and 0 is Unlimited. The custom keypad shares this case.
func TestAddClientExpiryPresetReplacesTheTerm(t *testing.T) {
const chatID = int64(7505)
draftLocalizer(t,
&i18n.Message{ID: "tgbot.days", Other: "Days"},
&i18n.Message{ID: "tgbot.unlimited", Other: "Unlimited"},
)
url, textsFor := draftTexts(t)
swapTestBot(t, url)
// A fresh draft attaches no inbound, so the card never looks one up by remark.
draft := addClientDrafts.forActor(chatUser{chatID: chatID, userID: 1})
origRunning := isRunning
t.Cleanup(func() {
addClientDrafts.reset(chatUser{chatID: chatID, userID: 1})
isRunning = origRunning
})
isRunning = true
tb := &Tgbot{}
for _, tc := range []struct {
days string
want string
}{
{"30", "Expire: 30 Days"}, // not 37: a second tap replaces the first
{"90", "Expire: 90 Days"}, // not 97, which accumulating would show
{"0", "Expire: Unlimited"}, // the Unlimited button clears the term
} {
t.Run(tc.days, func(t *testing.T) {
// A term left by an earlier preset; every row has to fail on its own
// under the accumulate semantics this change rejected.
draft.expiryTime = -7 * 86400000
tb.answerCallback(&telego.CallbackQuery{
ID: "q1",
From: telego.User{ID: 1},
Data: "add_client_reset_exp_c " + tc.days,
Message: &telego.Message{MessageID: 7, Chat: telego.Chat{ID: chatID}},
}, true) // admin
sent := textsFor(chatID)
if len(sent) == 0 {
t.Fatalf("add_client_reset_exp_c %s rendered no card", tc.days)
}
if got := sent[len(sent)-1]; !strings.Contains(got, tc.want) {
t.Errorf("card after add_client_reset_exp_c %s = %q, want it to contain %q", tc.days, got, tc.want)
}
})
}
}