ldap_sync_job_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 TestLdapCreateClients_AttachesToAllConfiguredInbounds(t *testing.T) {
  19. initLdapJobDB(t)
  20. db := database.GetDB()
  21. tags := []string{"in-1080-tcp", "in-1081-tcp", "in-1082-tcp"}
  22. protocols := []model.Protocol{model.VLESS, model.Trojan, model.VLESS}
  23. inboundIds := make([]int, 0, len(tags))
  24. for i, tag := range tags {
  25. ib := &model.Inbound{
  26. UserId: 1,
  27. Tag: tag,
  28. Enable: true,
  29. Port: 42080 + i,
  30. Protocol: protocols[i],
  31. Settings: `{"clients": []}`,
  32. StreamSettings: `{"network":"tcp","security":"none"}`,
  33. }
  34. if err := db.Create(ib).Error; err != nil {
  35. t.Fatalf("create inbound %s: %v", tag, err)
  36. }
  37. inboundIds = append(inboundIds, ib.Id)
  38. }
  39. j := NewLdapSyncJob()
  40. const email = "[email protected]"
  41. j.createClients([]model.Client{j.buildClient(email, 0, 0, 0)}, inboundIds, tags)
  42. rec := &model.ClientRecord{}
  43. if err := db.Where("email = ?", email).First(rec).Error; err != nil {
  44. t.Fatalf("client record for %s not created: %v", email, err)
  45. }
  46. if rec.SubID == "" {
  47. t.Error("created LDAP client must carry a subId")
  48. }
  49. clientSvc := &service.ClientService{}
  50. for i, id := range inboundIds {
  51. clients, err := clientSvc.ListForInbound(nil, id)
  52. if err != nil {
  53. t.Fatalf("ListForInbound(%s): %v", tags[i], err)
  54. }
  55. if len(clients) != 1 || clients[0].Email != email {
  56. t.Fatalf("inbound %s must carry exactly the LDAP client, got %d clients", tags[i], len(clients))
  57. }
  58. if clients[0].SubID != rec.SubID {
  59. t.Errorf("inbound %s client subId = %q, want the shared %q", tags[i], clients[0].SubID, rec.SubID)
  60. }
  61. }
  62. trojanClients, err := clientSvc.ListForInbound(nil, inboundIds[1])
  63. if err != nil {
  64. t.Fatalf("ListForInbound(trojan): %v", err)
  65. }
  66. if trojanClients[0].Password == "" {
  67. t.Error("trojan inbound client must get a generated password")
  68. }
  69. vlessClients, err := clientSvc.ListForInbound(nil, inboundIds[0])
  70. if err != nil {
  71. t.Fatalf("ListForInbound(vless): %v", err)
  72. }
  73. if vlessClients[0].ID == "" {
  74. t.Error("vless inbound client must get a generated uuid")
  75. }
  76. }