client_create_fanout_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  5. )
  6. func TestCreateAcrossManyInboundsUsesOneEmailSnapshot(t *testing.T) {
  7. setupBulkDB(t)
  8. svc := &ClientService{}
  9. inboundSvc := &InboundService{}
  10. const uuid = "bbbbbbbb-1111-2222-3333-555555555555"
  11. ids := make([]int, 0, 6)
  12. for i := range 6 {
  13. ib := mkInbound(t, 23001+i, model.VLESS, `{"clients":[]}`)
  14. ids = append(ids, ib.Id)
  15. }
  16. if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
  17. Client: model.Client{Email: "fan@x", ID: uuid, SubID: "sub-fan", Enable: true},
  18. InboundIds: ids,
  19. }); err != nil {
  20. t.Fatalf("Create across %d inbounds: %v", len(ids), err)
  21. }
  22. if n := countClientRecords(t); n != 1 {
  23. t.Fatalf("client records = %d, want 1", n)
  24. }
  25. rec := lookupClientRecord(t, "fan@x")
  26. if rec.UUID != uuid || rec.SubID != "sub-fan" {
  27. t.Fatalf("record = {uuid:%q sub:%q}, want {%q sub-fan}", rec.UUID, rec.SubID, uuid)
  28. }
  29. for _, id := range ids {
  30. if !settingsHoldUUID(t, inboundSvc, id, uuid) {
  31. t.Fatalf("inbound %d settings missing the client", id)
  32. }
  33. }
  34. linked, err := svc.GetInboundIdsForRecord(rec.Id)
  35. if err != nil {
  36. t.Fatalf("GetInboundIdsForRecord: %v", err)
  37. }
  38. if len(linked) != len(ids) {
  39. t.Fatalf("linked inbounds = %d, want %d", len(linked), len(ids))
  40. }
  41. }
  42. func TestAttachAcrossManyInboundsUsesOneEmailSnapshot(t *testing.T) {
  43. setupBulkDB(t)
  44. svc := &ClientService{}
  45. inboundSvc := &InboundService{}
  46. first := mkInbound(t, 23101, model.VLESS, `{"clients":[]}`)
  47. if _, err := svc.Create(inboundSvc, &ClientCreatePayload{
  48. Client: model.Client{Email: "att@x", ID: "cccccccc-1111-2222-3333-666666666666", SubID: "sub-att", Enable: true},
  49. InboundIds: []int{first.Id},
  50. }); err != nil {
  51. t.Fatalf("seed Create: %v", err)
  52. }
  53. rec := lookupClientRecord(t, "att@x")
  54. ids := []int{first.Id}
  55. for i := range 4 {
  56. ib := mkInbound(t, 23102+i, model.VLESS, `{"clients":[]}`)
  57. ids = append(ids, ib.Id)
  58. }
  59. if _, err := svc.Attach(inboundSvc, rec.Id, ids); err != nil {
  60. t.Fatalf("Attach across %d inbounds: %v", len(ids), err)
  61. }
  62. if n := countClientRecords(t); n != 1 {
  63. t.Fatalf("client records after attach = %d, want 1", n)
  64. }
  65. linked, err := svc.GetInboundIdsForRecord(rec.Id)
  66. if err != nil {
  67. t.Fatalf("GetInboundIdsForRecord: %v", err)
  68. }
  69. if len(linked) != len(ids) {
  70. t.Fatalf("linked inbounds = %d, want %d", len(linked), len(ids))
  71. }
  72. }