ldap_sync_job_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package job
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  9. )
  10. func initLdapJobDB(t *testing.T) {
  11. t.Helper()
  12. dbDir := t.TempDir()
  13. t.Setenv("XUI_DB_FOLDER", dbDir)
  14. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  15. }
  16. func TestBuildClient_ConvertsDefaultTotalGBToBytes(t *testing.T) {
  17. j := NewLdapSyncJob()
  18. c := j.buildClient("[email protected]", 10, 0, 0)
  19. if want := int64(10) * 1024 * 1024 * 1024; c.TotalGB != want {
  20. t.Errorf("TotalGB = %d, want %d", c.TotalGB, want)
  21. }
  22. }
  23. func TestLdapCreateClients_AttachesToAllConfiguredInbounds(t *testing.T) {
  24. initLdapJobDB(t)
  25. db := database.GetDB()
  26. tags := []string{"in-1080-tcp", "in-1081-tcp", "in-1082-tcp"}
  27. protocols := []model.Protocol{model.VLESS, model.Trojan, model.VLESS}
  28. inboundIds := make([]int, 0, len(tags))
  29. for i, tag := range tags {
  30. ib := &model.Inbound{
  31. UserId: 1,
  32. Tag: tag,
  33. Enable: true,
  34. Port: 42080 + i,
  35. Protocol: protocols[i],
  36. Settings: `{"clients": []}`,
  37. StreamSettings: `{"network":"tcp","security":"none"}`,
  38. }
  39. if err := db.Create(ib).Error; err != nil {
  40. t.Fatalf("create inbound %s: %v", tag, err)
  41. }
  42. inboundIds = append(inboundIds, ib.Id)
  43. }
  44. j := NewLdapSyncJob()
  45. const email = "[email protected]"
  46. j.createClients([]model.Client{j.buildClient(email, 0, 0, 0)}, inboundIds, tags)
  47. rec := &model.ClientRecord{}
  48. if err := db.Where("email = ?", email).First(rec).Error; err != nil {
  49. t.Fatalf("client record for %s not created: %v", email, err)
  50. }
  51. if rec.SubID == "" {
  52. t.Error("created LDAP client must carry a subId")
  53. }
  54. clientSvc := &service.ClientService{}
  55. for i, id := range inboundIds {
  56. clients, err := clientSvc.ListForInbound(nil, id)
  57. if err != nil {
  58. t.Fatalf("ListForInbound(%s): %v", tags[i], err)
  59. }
  60. if len(clients) != 1 || clients[0].Email != email {
  61. t.Fatalf("inbound %s must carry exactly the LDAP client, got %d clients", tags[i], len(clients))
  62. }
  63. if clients[0].SubID != rec.SubID {
  64. t.Errorf("inbound %s client subId = %q, want the shared %q", tags[i], clients[0].SubID, rec.SubID)
  65. }
  66. }
  67. trojanClients, err := clientSvc.ListForInbound(nil, inboundIds[1])
  68. if err != nil {
  69. t.Fatalf("ListForInbound(trojan): %v", err)
  70. }
  71. if trojanClients[0].Password == "" {
  72. t.Error("trojan inbound client must get a generated password")
  73. }
  74. vlessClients, err := clientSvc.ListForInbound(nil, inboundIds[0])
  75. if err != nil {
  76. t.Fatalf("ListForInbound(vless): %v", err)
  77. }
  78. if vlessClients[0].ID == "" {
  79. t.Error("vless inbound client must get a generated uuid")
  80. }
  81. }
  82. // TestLdapCreateClients_FlagsRestartWhenEveryClientPartlyApplies pins that the
  83. // restart survives created == 0, the case a partly-applied batch always hits.
  84. func TestLdapCreateClients_FlagsRestartWhenEveryClientPartlyApplies(t *testing.T) {
  85. initLdapJobDB(t)
  86. db := database.GetDB()
  87. healthy := &model.Inbound{
  88. UserId: 1, Tag: "in-42180-tcp", Enable: true, Port: 42180,
  89. Protocol: model.VLESS, Settings: `{"clients": []}`,
  90. StreamSettings: `{"network":"tcp","security":"none"}`,
  91. }
  92. broken := &model.Inbound{
  93. UserId: 1, Tag: "in-42181-tcp", Enable: true, Port: 42181,
  94. Protocol: model.VLESS, Settings: `{"clients":`,
  95. StreamSettings: `{"network":"tcp","security":"none"}`,
  96. }
  97. for _, ib := range []*model.Inbound{healthy, broken} {
  98. if err := db.Create(ib).Error; err != nil {
  99. t.Fatalf("create inbound %s: %v", ib.Tag, err)
  100. }
  101. }
  102. j := NewLdapSyncJob()
  103. j.xrayService.IsNeedRestartAndSetFalse()
  104. j.createClients([]model.Client{j.buildClient("[email protected]", 0, 0, 0)},
  105. []int{healthy.Id, broken.Id}, []string{healthy.Tag, broken.Tag})
  106. clients, err := (&service.ClientService{}).ListForInbound(nil, healthy.Id)
  107. if err != nil {
  108. t.Fatalf("ListForInbound(%s): %v", healthy.Tag, err)
  109. }
  110. if len(clients) != 1 {
  111. t.Fatalf("healthy inbound holds %d clients, want the partly-applied 1", len(clients))
  112. }
  113. if !j.xrayService.IsNeedRestartAndSetFalse() {
  114. t.Fatal("a partly-applied LDAP batch left Xray unflagged for restart")
  115. }
  116. }