ldap_sync_job_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  8. )
  9. func initLdapJobDB(t *testing.T) {
  10. t.Helper()
  11. dbDir := t.TempDir()
  12. t.Setenv("XUI_DB_FOLDER", dbDir)
  13. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  14. t.Fatalf("InitDB: %v", err)
  15. }
  16. t.Cleanup(func() { _ = database.CloseDB() })
  17. }
  18. func TestBuildClient_ConvertsDefaultTotalGBToBytes(t *testing.T) {
  19. j := NewLdapSyncJob()
  20. c := j.buildClient("[email protected]", 10, 0, 0)
  21. if want := int64(10) * 1024 * 1024 * 1024; c.TotalGB != want {
  22. t.Errorf("TotalGB = %d, want %d", c.TotalGB, want)
  23. }
  24. }
  25. func TestLdapCreateClients_AttachesToAllConfiguredInbounds(t *testing.T) {
  26. initLdapJobDB(t)
  27. db := database.GetDB()
  28. tags := []string{"in-1080-tcp", "in-1081-tcp", "in-1082-tcp"}
  29. protocols := []model.Protocol{model.VLESS, model.Trojan, model.VLESS}
  30. inboundIds := make([]int, 0, len(tags))
  31. for i, tag := range tags {
  32. ib := &model.Inbound{
  33. UserId: 1,
  34. Tag: tag,
  35. Enable: true,
  36. Port: 42080 + i,
  37. Protocol: protocols[i],
  38. Settings: `{"clients": []}`,
  39. StreamSettings: `{"network":"tcp","security":"none"}`,
  40. }
  41. if err := db.Create(ib).Error; err != nil {
  42. t.Fatalf("create inbound %s: %v", tag, err)
  43. }
  44. inboundIds = append(inboundIds, ib.Id)
  45. }
  46. j := NewLdapSyncJob()
  47. const email = "[email protected]"
  48. j.createClients([]model.Client{j.buildClient(email, 0, 0, 0)}, inboundIds, tags)
  49. rec := &model.ClientRecord{}
  50. if err := db.Where("email = ?", email).First(rec).Error; err != nil {
  51. t.Fatalf("client record for %s not created: %v", email, err)
  52. }
  53. if rec.SubID == "" {
  54. t.Error("created LDAP client must carry a subId")
  55. }
  56. clientSvc := &service.ClientService{}
  57. for i, id := range inboundIds {
  58. clients, err := clientSvc.ListForInbound(nil, id)
  59. if err != nil {
  60. t.Fatalf("ListForInbound(%s): %v", tags[i], err)
  61. }
  62. if len(clients) != 1 || clients[0].Email != email {
  63. t.Fatalf("inbound %s must carry exactly the LDAP client, got %d clients", tags[i], len(clients))
  64. }
  65. if clients[0].SubID != rec.SubID {
  66. t.Errorf("inbound %s client subId = %q, want the shared %q", tags[i], clients[0].SubID, rec.SubID)
  67. }
  68. }
  69. trojanClients, err := clientSvc.ListForInbound(nil, inboundIds[1])
  70. if err != nil {
  71. t.Fatalf("ListForInbound(trojan): %v", err)
  72. }
  73. if trojanClients[0].Password == "" {
  74. t.Error("trojan inbound client must get a generated password")
  75. }
  76. vlessClients, err := clientSvc.ListForInbound(nil, inboundIds[0])
  77. if err != nil {
  78. t.Fatalf("ListForInbound(vless): %v", err)
  79. }
  80. if vlessClients[0].ID == "" {
  81. t.Error("vless inbound client must get a generated uuid")
  82. }
  83. }