client_group_node_sync_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/database"
  6. "github.com/mhsanaei/3x-ui/v3/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/web/runtime"
  8. )
  9. func TestSetRemoteTraffic_PreservesPanelLocalGroupAndComment(t *testing.T) {
  10. dbDir := t.TempDir()
  11. t.Setenv("XUI_DB_FOLDER", dbDir)
  12. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  13. t.Fatalf("InitDB: %v", err)
  14. }
  15. t.Cleanup(func() { _ = database.CloseDB() })
  16. db := database.GetDB()
  17. const nodeID = 1
  18. const email = "[email protected]"
  19. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c003"
  20. const wantGroup = "vip"
  21. const wantComment = "renewed manually"
  22. id := nodeID
  23. central := &model.Inbound{
  24. UserId: 1,
  25. NodeID: &id,
  26. Tag: "n1-vless",
  27. Enable: true,
  28. Port: 20001,
  29. Protocol: model.VLESS,
  30. Settings: `{"clients":[{"email":"` + email + `","id":"` + uid + `","enable":true,"group":"` + wantGroup + `","comment":"` + wantComment + `"}]}`,
  31. }
  32. if err := db.Create(central).Error; err != nil {
  33. t.Fatalf("create node inbound: %v", err)
  34. }
  35. if err := db.Create(&model.ClientRecord{
  36. Email: email,
  37. UUID: uid,
  38. Enable: true,
  39. Group: wantGroup,
  40. Comment: wantComment,
  41. }).Error; err != nil {
  42. t.Fatalf("create client record: %v", err)
  43. }
  44. snap := &runtime.TrafficSnapshot{
  45. Inbounds: []*model.Inbound{
  46. {
  47. Tag: "n1-vless",
  48. Enable: true,
  49. Port: 20001,
  50. Protocol: model.VLESS,
  51. Settings: `{"clients":[{"email":"` + email + `","id":"` + uid + `","enable":true}]}`,
  52. },
  53. },
  54. }
  55. svc := InboundService{}
  56. if _, err := svc.setRemoteTrafficLocked(nodeID, snap); err != nil {
  57. t.Fatalf("setRemoteTrafficLocked: %v", err)
  58. }
  59. var row model.ClientRecord
  60. if err := db.Where("email = ?", email).First(&row).Error; err != nil {
  61. t.Fatalf("lookup client row after sync: %v", err)
  62. }
  63. if row.Group != wantGroup {
  64. t.Errorf("group was wiped by node snapshot sync: got %q, want %q", row.Group, wantGroup)
  65. }
  66. if row.Comment != wantComment {
  67. t.Errorf("comment was wiped by node snapshot sync: got %q, want %q", row.Comment, wantComment)
  68. }
  69. }
  70. func TestSyncInbound_KeepsGroupWhenIncomingEmpty(t *testing.T) {
  71. dbDir := t.TempDir()
  72. t.Setenv("XUI_DB_FOLDER", dbDir)
  73. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  74. t.Fatalf("InitDB: %v", err)
  75. }
  76. t.Cleanup(func() { _ = database.CloseDB() })
  77. db := database.GetDB()
  78. ib := &model.Inbound{Tag: "vless-grp", Enable: true, Port: 20002, Protocol: model.VLESS}
  79. if err := db.Create(ib).Error; err != nil {
  80. t.Fatalf("create inbound: %v", err)
  81. }
  82. svc := ClientService{}
  83. const email = "[email protected]"
  84. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c004"
  85. const wantGroup = "vip"
  86. withGroup := model.Client{Email: email, ID: uid, Enable: true, Group: wantGroup}
  87. if err := svc.SyncInbound(nil, ib.Id, []model.Client{withGroup}); err != nil {
  88. t.Fatalf("SyncInbound (set group): %v", err)
  89. }
  90. noGroup := model.Client{Email: email, ID: uid, Enable: true, Group: ""}
  91. if err := svc.SyncInbound(nil, ib.Id, []model.Client{noGroup}); err != nil {
  92. t.Fatalf("SyncInbound (group-less rebuild): %v", err)
  93. }
  94. var row model.ClientRecord
  95. if err := db.Where("email = ?", email).First(&row).Error; err != nil {
  96. t.Fatalf("lookup client row: %v", err)
  97. }
  98. if row.Group != wantGroup {
  99. t.Errorf("group must survive a group-less settings rebuild (it is managed via the Groups page, not Xray settings): got %q, want %q", row.Group, wantGroup)
  100. }
  101. }