inbound_import_legacy_fields_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. // Exports from v2.x store tgId as a string ("" when unset); the stored copies
  9. // are healed by a startup migration, but an imported export never passes it.
  10. func TestAddInboundAcceptsLegacyStringClientFields(t *testing.T) {
  11. setupConflictDB(t)
  12. settings := `{"clients":[` +
  13. `{"id":"11111111-1111-1111-1111-111111111111","email":"legacy-a","tgId":"","subId":"s-a","enable":true,"limitIp":0,"totalGB":0,"expiryTime":0,"reset":0},` +
  14. `{"id":"22222222-2222-2222-2222-222222222222","email":"legacy-b","tgId":"123456","subId":"s-b","enable":true,"limitIp":0,"totalGB":0,"expiryTime":0,"reset":0}` +
  15. `],"decryption":"none","fallbacks":[]}`
  16. in := makeImportInbound("in-9201-tcp", 9201, settings, nil)
  17. saved, _, err := (&InboundService{}).AddInbound(in)
  18. if err != nil {
  19. t.Fatalf("AddInbound: %v", err)
  20. }
  21. var rec model.ClientRecord
  22. if err := database.GetDB().Where("email = ?", "legacy-b").First(&rec).Error; err != nil {
  23. t.Fatalf("read client: %v", err)
  24. }
  25. if rec.TgID != 123456 {
  26. t.Fatalf("client tgId = %d, want 123456 parsed from the string", rec.TgID)
  27. }
  28. var stored struct {
  29. Clients []map[string]any `json:"clients"`
  30. }
  31. if err := json.Unmarshal([]byte(saved.Settings), &stored); err != nil {
  32. t.Fatalf("stored settings: %v", err)
  33. }
  34. for _, c := range stored.Clients {
  35. if _, isString := c["tgId"].(string); isString {
  36. t.Fatalf("stored settings keep a string tgId for %v: %v", c["email"], c["tgId"])
  37. }
  38. }
  39. }