tgbot_draft_render_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package tgbot
  2. import (
  3. "encoding/json"
  4. "html"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/locale"
  8. "github.com/mymmrac/telego"
  9. "github.com/nicksnyder/go-i18n/v2/i18n"
  10. "golang.org/x/text/language"
  11. )
  12. // clientDraftTestChatID is a chat id no other test drives, so the draft this
  13. // test fills cannot leak into them.
  14. const clientDraftTestChatID = -9001
  15. // Regression test: the draft is sent with ParseMode HTML, so Markdown markers
  16. // were rendered literally and an unescaped value could break the whole message.
  17. func TestClientDraftMessageRendersHTML(t *testing.T) {
  18. draft := addClientDrafts.forChat(clientDraftTestChatID)
  19. t.Cleanup(func() { addClientDrafts.reset(clientDraftTestChatID) })
  20. draft.email = "[email protected]"
  21. draft.comment = "<b>promo</b> & <10 GB>"
  22. draft.tgID = "42"
  23. draft.totalGB, draft.limitIP, draft.expiryTime = 0, 0, 0
  24. draft.receiverInboundIDs = nil
  25. out := (&Tgbot{}).BuildClientDraftMessage(draft)
  26. if !strings.Contains(out, "<b>New client draft</b>") {
  27. t.Errorf("draft title is not HTML markup: %q", out)
  28. }
  29. if strings.Contains(out, "*New client draft*") || strings.Contains(out, "`") {
  30. t.Errorf("draft still carries Markdown markers: %q", out)
  31. }
  32. if strings.Contains(out, "<b>promo</b>") {
  33. t.Errorf("raw comment markup reached the message: %q", out)
  34. }
  35. if !strings.Contains(out, html.EscapeString(draft.comment)) {
  36. t.Errorf("comment is not HTML-escaped: %q", out)
  37. }
  38. }
  39. // draftLocalizer registers only the messages a wizard test drives, with the
  40. // templates the translation files carry; without it I18n returns the bare key.
  41. func draftLocalizer(t *testing.T, msgs ...*i18n.Message) {
  42. t.Helper()
  43. bundle := i18n.NewBundle(language.MustParse("en-US"))
  44. bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
  45. _ = bundle.AddMessages(language.MustParse("en-US"), msgs...)
  46. orig := locale.LocalizerBot
  47. t.Cleanup(func() { locale.LocalizerBot = orig })
  48. locale.LocalizerBot = i18n.NewLocalizer(bundle, "en-US")
  49. }
  50. // Regression test: the wizard's own prompts are HTML-parsed as well, so the
  51. // draft value they echo has to be escaped exactly like the draft card.
  52. func TestAddClientPromptsEscapeDraftValues(t *testing.T) {
  53. draftLocalizer(t,
  54. &i18n.Message{ID: "tgbot.messages.email_prompt", Other: "📧 Default Email: {{ .ClientEmail }}\n\nEnter your email."},
  55. &i18n.Message{ID: "tgbot.messages.comment_prompt", Other: "💬 Default Comment: {{ .ClientComment }}\n\nEnter your comment."},
  56. )
  57. url, texts := draftTexts(t)
  58. swapTestBot(t, url)
  59. draft := addClientDrafts.forChat(1)
  60. origRunning := isRunning
  61. t.Cleanup(func() {
  62. addClientDrafts.reset(1)
  63. isRunning = origRunning
  64. })
  65. isRunning = true
  66. cases := []struct {
  67. name string
  68. data string
  69. value string
  70. }{
  71. {"email prompt", "add_client_ch_default_email", "long<name>@example.com"},
  72. {"comment prompt", "add_client_ch_default_comment", "promo <b>tag</b>"},
  73. }
  74. tb := &Tgbot{}
  75. for _, tc := range cases {
  76. t.Run(tc.name, func(t *testing.T) {
  77. draft.email, draft.comment = tc.value, tc.value
  78. tb.answerCallback(&telego.CallbackQuery{
  79. ID: "q1",
  80. From: telego.User{ID: 1},
  81. Data: tc.data,
  82. Message: &telego.Message{Chat: telego.Chat{ID: 1}},
  83. }, true) // admin
  84. sent := texts(1)
  85. if len(sent) == 0 {
  86. t.Fatalf("no prompt was sent for %s", tc.data)
  87. }
  88. got := sent[len(sent)-1]
  89. if strings.Contains(got, tc.value) {
  90. t.Errorf("prompt = %q, want the draft value escaped", got)
  91. }
  92. if !strings.Contains(got, html.EscapeString(tc.value)) {
  93. t.Errorf("prompt = %q, want it to contain %q", got, html.EscapeString(tc.value))
  94. }
  95. })
  96. }
  97. }