inbound_clients_stale_uuid_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  7. )
  8. // GetInbounds must enrich ClientStats.UUID from the clients table, not the
  9. // embedded inbound settings JSON, when the two diverge (#6436).
  10. func TestEnrichClientStats_UsesClientsTableUUIDWhenSettingsStale(t *testing.T) {
  11. setupBulkDB(t)
  12. db := database.GetDB()
  13. stale := "11111111-1111-1111-1111-111111111111"
  14. fresh := "22222222-2222-2222-2222-222222222222"
  15. ib := &model.Inbound{
  16. UserId: 1, Tag: "stale-uuid-list", Enable: true, Listen: "0.0.0.0", Port: 8443,
  17. Protocol: model.VLESS, Remark: "stale",
  18. Settings: `{"clients":[{"id":"` + stale + `","email":"stale@e","subId":"sub1","enable":true}],"decryption":"none"}`,
  19. StreamSettings: `{"network":"tcp","security":"none"}`,
  20. }
  21. if err := db.Create(ib).Error; err != nil {
  22. t.Fatalf("create inbound: %v", err)
  23. }
  24. rec := &model.ClientRecord{Email: "stale@e", SubID: "sub1", UUID: fresh, Enable: true}
  25. if err := db.Create(rec).Error; err != nil {
  26. t.Fatalf("create client: %v", err)
  27. }
  28. if err := db.Create(&model.ClientInbound{ClientId: rec.Id, InboundId: ib.Id}).Error; err != nil {
  29. t.Fatalf("create client_inbound: %v", err)
  30. }
  31. if err := db.Create(&xray.ClientTraffic{InboundId: ib.Id, Email: "stale@e", Enable: true}).Error; err != nil {
  32. t.Fatalf("create client_traffics: %v", err)
  33. }
  34. svc := &InboundService{}
  35. inbounds, err := svc.GetInbounds(1)
  36. if err != nil {
  37. t.Fatalf("GetInbounds: %v", err)
  38. }
  39. if len(inbounds) != 1 {
  40. t.Fatalf("inbounds = %d, want 1", len(inbounds))
  41. }
  42. if len(inbounds[0].ClientStats) != 1 {
  43. t.Fatalf("ClientStats = %d, want 1", len(inbounds[0].ClientStats))
  44. }
  45. got := inbounds[0].ClientStats[0].UUID
  46. if got != fresh {
  47. t.Fatalf("ClientStats.UUID = %q, want fresh %q (settings still had stale %q)", got, fresh, stale)
  48. }
  49. if got == stale {
  50. t.Fatalf("ClientStats.UUID still carries stale settings value %q", stale)
  51. }
  52. }