client_update_rename_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. )
  7. func countClientRecords(t *testing.T) int64 {
  8. t.Helper()
  9. var n int64
  10. if err := database.GetDB().Model(&model.ClientRecord{}).Count(&n).Error; err != nil {
  11. t.Fatalf("count client records: %v", err)
  12. }
  13. return n
  14. }
  15. func TestUpdateInboundClientRenameDoesNotDuplicateRecord(t *testing.T) {
  16. setupBulkDB(t)
  17. svc := &ClientService{}
  18. inboundSvc := &InboundService{}
  19. source := []model.Client{{Email: "old@x", ID: "aaaaaaaa-0000-0000-0000-000000000001", SubID: "sub-old", Enable: true}}
  20. ib := mkInbound(t, 22001, model.VLESS, clientsSettings(t, source))
  21. if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
  22. t.Fatalf("seed linkage: %v", err)
  23. }
  24. origId := lookupClientRecord(t, "old@x").Id
  25. renamed := source
  26. renamed[0].Email = "new@x"
  27. if _, err := svc.UpdateInboundClient(inboundSvc, &model.Inbound{
  28. Id: ib.Id,
  29. Settings: clientsSettings(t, renamed),
  30. }, "old@x"); err != nil {
  31. t.Fatalf("UpdateInboundClient: %v", err)
  32. }
  33. if n := countClientRecords(t); n != 1 {
  34. t.Fatalf("client records after rename = %d, want 1", n)
  35. }
  36. rec := lookupClientRecord(t, "new@x")
  37. if rec.Id != origId {
  38. t.Fatalf("record id after rename = %d, want %d", rec.Id, origId)
  39. }
  40. }
  41. func TestUpdateInboundClientCaseOnlyRenameDoesNotDuplicateRecord(t *testing.T) {
  42. setupBulkDB(t)
  43. svc := &ClientService{}
  44. inboundSvc := &InboundService{}
  45. source := []model.Client{{Email: "test", ID: "aaaaaaaa-0000-0000-0000-000000000002", SubID: "sub-case", Enable: true}}
  46. ib := mkInbound(t, 22002, model.VLESS, clientsSettings(t, source))
  47. if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
  48. t.Fatalf("seed linkage: %v", err)
  49. }
  50. origId := lookupClientRecord(t, "test").Id
  51. updated := source[0]
  52. updated.Email = "Test"
  53. if _, err := svc.Update(inboundSvc, origId, updated); err != nil {
  54. t.Fatalf("Update case-only email: %v", err)
  55. }
  56. if n := countClientRecords(t); n != 1 {
  57. t.Fatalf("client records after case-only rename = %d, want 1", n)
  58. }
  59. rec := lookupClientRecord(t, "Test")
  60. if rec.Id != origId {
  61. t.Fatalf("record id after case-only rename = %d, want %d", rec.Id, origId)
  62. }
  63. if rec.Email != "Test" {
  64. t.Fatalf("email after case-only rename = %q, want %q", rec.Email, "Test")
  65. }
  66. }
  67. func TestClientUpdateDuplicateSubIDDoesNotRenameEmail(t *testing.T) {
  68. setupBulkDB(t)
  69. svc := &ClientService{}
  70. inboundSvc := &InboundService{}
  71. source := []model.Client{
  72. {Email: "keep@x", ID: "aaaaaaaa-0000-0000-0000-000000000003", SubID: "sub-keep", Enable: true},
  73. {Email: "other@x", ID: "aaaaaaaa-0000-0000-0000-000000000004", SubID: "sub-other", Enable: true},
  74. }
  75. ib := mkInbound(t, 22003, model.VLESS, clientsSettings(t, source))
  76. if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
  77. t.Fatalf("seed linkage: %v", err)
  78. }
  79. origId := lookupClientRecord(t, "keep@x").Id
  80. origSettings := mustInboundSettings(t, inboundSvc, ib.Id)
  81. updated := source[0]
  82. updated.Email = "kept@x"
  83. updated.SubID = "sub-other"
  84. if _, err := svc.Update(inboundSvc, origId, updated); err == nil {
  85. t.Fatalf("Update with colliding subId succeeded, want error")
  86. }
  87. rec := lookupClientRecord(t, "keep@x")
  88. if rec.Id != origId {
  89. t.Fatalf("record id changed after rejected update")
  90. }
  91. if got := mustInboundSettings(t, inboundSvc, ib.Id); got != origSettings {
  92. t.Fatalf("inbound settings changed after rejected update")
  93. }
  94. }
  95. func mustInboundSettings(t *testing.T, inboundSvc *InboundService, id int) string {
  96. t.Helper()
  97. ib, err := inboundSvc.GetInbound(id)
  98. if err != nil {
  99. t.Fatalf("GetInbound %d: %v", id, err)
  100. }
  101. return ib.Settings
  102. }