1
0

placeholders_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package sub
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. func TestRenderSubPlaceholders(t *testing.T) {
  8. data := subPlaceholderData{
  9. SubID: "sub-123",
  10. Context: remarkContext{client: model.Client{
  11. Email: "Ilnur",
  12. ID: "abcdef12-3456-7890-abcd-ef1234567890",
  13. SubID: "sub-123",
  14. TgID: 42,
  15. Enable: true,
  16. }},
  17. HasCtx: true,
  18. }
  19. tests := []struct {
  20. name string
  21. tmpl string
  22. data subPlaceholderData
  23. want string
  24. }{
  25. {
  26. name: "identity tokens",
  27. tmpl: "{{EMAIL}}/{{ID}}/{{SHORT_ID}}/{{SUB_ID}}/{{TELEGRAM_ID}}",
  28. data: data,
  29. want: "Ilnur/abcdef12-3456-7890-abcd-ef1234567890/abcdef12/sub-123/42",
  30. },
  31. {
  32. name: "no template",
  33. tmpl: "isVPN",
  34. data: subPlaceholderData{SubID: "sub-123"},
  35. want: "isVPN",
  36. },
  37. {
  38. name: "unsupported tokens stay literal",
  39. tmpl: "{{SUB_ID}}/{{INBOUND}}/{{TRAFFIC_LEFT}}/{{PROTOCOL}}/{EMAIL}",
  40. data: subPlaceholderData{SubID: "sub-123"},
  41. want: "sub-123/{{INBOUND}}/{{TRAFFIC_LEFT}}/{{PROTOCOL}}/{EMAIL}",
  42. },
  43. {
  44. name: "URL values are escaped",
  45. tmpl: "https://support.example/?email={{EMAIL}}&sub={{SUB_ID}}",
  46. data: subPlaceholderData{
  47. SubID: "sub id",
  48. Context: remarkContext{client: model.Client{
  49. Email: "john [email protected]",
  50. SubID: "sub id",
  51. }},
  52. HasCtx: true,
  53. Escape: true,
  54. },
  55. want: "https://support.example/?email=john+doe%40example.com&sub=sub+id",
  56. },
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.name, func(t *testing.T) {
  60. if got := renderSubPlaceholders(tt.tmpl, tt.data); got != tt.want {
  61. t.Fatalf("renderSubPlaceholders() = %q, want %q", got, tt.want)
  62. }
  63. })
  64. }
  65. }
  66. func TestMetadataForSubRequestDoesNotExpandFallbackProfileURL(t *testing.T) {
  67. a := &SUBController{
  68. subTitle: "isVPN",
  69. subSupportUrl: "https://support.example/",
  70. }
  71. fallback := "https://sub.example.com/sub/sub-123?x={{EMAIL}}"
  72. metadata := a.metadataForSubRequest(func() *SubService {
  73. t.Fatal("metadataForSubRequest loaded a subscription context without configured placeholders")
  74. return nil
  75. }, "sub-123", fallback)
  76. if metadata.ProfileURL != fallback {
  77. t.Fatalf("ProfileURL = %q, want untouched fallback %q", metadata.ProfileURL, fallback)
  78. }
  79. }
  80. func TestMetadataForSubRequestUsesStableClientIdentity(t *testing.T) {
  81. initSubDB(t)
  82. db := database.GetDB()
  83. first := model.ClientRecord{
  84. Email: "john [email protected]",
  85. SubID: "sub-123",
  86. UUID: "abcdef12-3456-7890-abcd-ef1234567890",
  87. TgID: 42,
  88. Enable: true,
  89. }
  90. second := model.ClientRecord{
  91. Email: "[email protected]",
  92. SubID: "sub-123",
  93. UUID: "fedcba98-3456-7890-abcd-ef1234567890",
  94. TgID: 99,
  95. Enable: true,
  96. }
  97. if err := db.Create(&first).Error; err != nil {
  98. t.Fatalf("seed first client: %v", err)
  99. }
  100. if err := db.Create(&second).Error; err != nil {
  101. t.Fatalf("seed second client: %v", err)
  102. }
  103. a := &SUBController{
  104. subTitle: "isVPN — {{EMAIL}}",
  105. subSupportUrl: "https://support.example/?email={{EMAIL}}&tg={{TELEGRAM_ID}}",
  106. subProfileUrl: "https://profile.example/account/{{ID}}",
  107. subAnnounce: "Subscription {{SUB_ID}}",
  108. }
  109. metadata := a.metadataForSubRequest(func() *SubService { return &SubService{} }, "sub-123", "https://fallback.example/{{EMAIL}}")
  110. if metadata.Title != "isVPN — john [email protected]" {
  111. t.Fatalf("Title = %q", metadata.Title)
  112. }
  113. if metadata.SupportURL != "https://support.example/?email=john+doe%40example.com&tg=42" {
  114. t.Fatalf("SupportURL = %q", metadata.SupportURL)
  115. }
  116. if metadata.ProfileURL != "https://profile.example/account/abcdef12-3456-7890-abcd-ef1234567890" {
  117. t.Fatalf("ProfileURL = %q", metadata.ProfileURL)
  118. }
  119. if metadata.Announce != "Subscription sub-123" {
  120. t.Fatalf("Announce = %q", metadata.Announce)
  121. }
  122. }