inbound_client_move_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // TestGetClientByEmail_AfterMoveBetweenInbounds is the #6059 regression: a
  9. // client moved to another inbound keeps a client_traffics row pointing at the
  10. // old inbound (which still exists), so email lookups used to fail with
  11. // "Client Not Found In Inbound For Email" and the Telegram bot could not build
  12. // links or QR codes for the client anymore.
  13. func TestGetClientByEmail_AfterMoveBetweenInbounds(t *testing.T) {
  14. setupBulkDB(t)
  15. svc := &InboundService{}
  16. db := database.GetDB()
  17. oldClients := []model.Client{
  18. {Email: "stay@x", ID: "11111111-1111-1111-1111-111111111111", Enable: true},
  19. }
  20. movedClients := []model.Client{
  21. {Email: "moved@x", ID: "22222222-2222-2222-2222-222222222222", Enable: true},
  22. }
  23. oldIb := mkInbound(t, 30101, model.VLESS, clientsSettings(t, oldClients))
  24. newIb := mkInbound(t, 30102, model.VLESS, clientsSettings(t, movedClients))
  25. if err := svc.clientService.SyncInbound(nil, oldIb.Id, oldClients); err != nil {
  26. t.Fatalf("SyncInbound old: %v", err)
  27. }
  28. if err := svc.clientService.SyncInbound(nil, newIb.Id, movedClients); err != nil {
  29. t.Fatalf("SyncInbound new: %v", err)
  30. }
  31. stale := xray.ClientTraffic{InboundId: oldIb.Id, Email: "moved@x", Enable: true, Up: 5, Down: 7}
  32. if err := db.Create(&stale).Error; err != nil {
  33. t.Fatalf("seed stale traffic row: %v", err)
  34. }
  35. traffic, inbound, err := svc.GetClientInboundByEmail("moved@x")
  36. if err != nil {
  37. t.Fatalf("GetClientInboundByEmail: %v", err)
  38. }
  39. if traffic == nil || traffic.Email != "moved@x" {
  40. t.Fatalf("traffic = %+v, want the moved@x row", traffic)
  41. }
  42. if inbound == nil || inbound.Id != newIb.Id {
  43. t.Fatalf("inbound = %+v, want the new inbound %d", inbound, newIb.Id)
  44. }
  45. _, client, err := svc.GetClientByEmail("moved@x")
  46. if err != nil {
  47. t.Fatalf("GetClientByEmail: %v", err)
  48. }
  49. if client == nil || client.Email != "moved@x" {
  50. t.Fatalf("client = %+v, want moved@x", client)
  51. }
  52. }