client_update_rename_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // The IP-limit job keys its tracking rows on the casing Xray reports, so an
  68. // inbound whose settings JSON drifted in case leaves a row under each spelling.
  69. func TestUpdateInboundClientCaseOnlyRenameSurvivesExistingClientIpsRow(t *testing.T) {
  70. setupBulkDB(t)
  71. svc := &ClientService{}
  72. inboundSvc := &InboundService{}
  73. source := []model.Client{{Email: "Sanaei", ID: "aaaaaaaa-0000-0000-0000-000000000009", SubID: "sub-ips", Enable: true}}
  74. ib := mkInbound(t, 22011, model.VLESS, clientsSettings(t, source))
  75. if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
  76. t.Fatalf("seed linkage: %v", err)
  77. }
  78. for _, email := range []string{"Sanaei", "sanaei"} {
  79. row := &model.InboundClientIps{ClientEmail: email, Ips: `[{"ip":"1.2.3.4","timestamp":1700000000}]`}
  80. if err := database.GetDB().Create(row).Error; err != nil {
  81. t.Fatalf("seed client ips for %q: %v", email, err)
  82. }
  83. }
  84. lowered := source
  85. lowered[0].Email = "sanaei"
  86. if _, err := svc.UpdateInboundClient(inboundSvc, &model.Inbound{
  87. Id: ib.Id,
  88. Settings: clientsSettings(t, lowered),
  89. }, "sanaei"); err != nil {
  90. t.Fatalf("UpdateInboundClient with a colliding client ips row: %v", err)
  91. }
  92. var rows []model.InboundClientIps
  93. if err := database.GetDB().Find(&rows).Error; err != nil {
  94. t.Fatalf("read client ips: %v", err)
  95. }
  96. if len(rows) != 1 || rows[0].ClientEmail != "sanaei" {
  97. t.Fatalf("client ips rows after rename = %+v, want a single row for %q", rows, "sanaei")
  98. }
  99. }
  100. func TestClientUpdateDuplicateSubIDDoesNotRenameEmail(t *testing.T) {
  101. setupBulkDB(t)
  102. svc := &ClientService{}
  103. inboundSvc := &InboundService{}
  104. source := []model.Client{
  105. {Email: "keep@x", ID: "aaaaaaaa-0000-0000-0000-000000000003", SubID: "sub-keep", Enable: true},
  106. {Email: "other@x", ID: "aaaaaaaa-0000-0000-0000-000000000004", SubID: "sub-other", Enable: true},
  107. }
  108. ib := mkInbound(t, 22003, model.VLESS, clientsSettings(t, source))
  109. if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
  110. t.Fatalf("seed linkage: %v", err)
  111. }
  112. origId := lookupClientRecord(t, "keep@x").Id
  113. origSettings := mustInboundSettings(t, inboundSvc, ib.Id)
  114. updated := source[0]
  115. updated.Email = "kept@x"
  116. updated.SubID = "sub-other"
  117. if _, err := svc.Update(inboundSvc, origId, updated); err == nil {
  118. t.Fatalf("Update with colliding subId succeeded, want error")
  119. }
  120. rec := lookupClientRecord(t, "keep@x")
  121. if rec.Id != origId {
  122. t.Fatalf("record id changed after rejected update")
  123. }
  124. if got := mustInboundSettings(t, inboundSvc, ib.Id); got != origSettings {
  125. t.Fatalf("inbound settings changed after rejected update")
  126. }
  127. }
  128. func TestClientUpdateKeepsSharedSubIDEditable(t *testing.T) {
  129. setupBulkDB(t)
  130. svc := &ClientService{}
  131. inboundSvc := &InboundService{}
  132. source := []model.Client{
  133. {Email: "a@node", ID: "aaaaaaaa-0000-0000-0000-000000000005", SubID: "sub-shared", Enable: true},
  134. {Email: "b@node", ID: "aaaaaaaa-0000-0000-0000-000000000006", SubID: "sub-shared", Enable: true},
  135. }
  136. ib := mkInbound(t, 22004, model.VLESS, clientsSettings(t, source))
  137. if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
  138. t.Fatalf("seed linkage: %v", err)
  139. }
  140. first := lookupClientRecord(t, "a@node")
  141. if first.SubID != "sub-shared" || lookupClientRecord(t, "b@node").SubID != "sub-shared" {
  142. t.Fatalf("seed did not produce a shared subId")
  143. }
  144. updated := source[0]
  145. updated.TotalGB = 42
  146. if _, err := svc.Update(inboundSvc, first.Id, updated); err != nil {
  147. t.Fatalf("Update of a client whose subId is already shared: %v", err)
  148. }
  149. if got := lookupClientRecord(t, "a@node").TotalGB; got != 42 {
  150. t.Fatalf("totalGB after update = %d, want 42", got)
  151. }
  152. omitted := source[0]
  153. omitted.SubID = ""
  154. omitted.TotalGB = 43
  155. if _, err := svc.Update(inboundSvc, first.Id, omitted); err != nil {
  156. t.Fatalf("Update with subId omitted entirely: %v", err)
  157. }
  158. other := lookupClientRecord(t, "b@node")
  159. if other.SubID != "sub-shared" {
  160. t.Fatalf("other client subId = %q, want %q", other.SubID, "sub-shared")
  161. }
  162. }
  163. func mustInboundSettings(t *testing.T, inboundSvc *InboundService, id int) string {
  164. t.Helper()
  165. ib, err := inboundSvc.GetInbound(id)
  166. if err != nil {
  167. t.Fatalf("GetInbound %d: %v", id, err)
  168. }
  169. return ib.Settings
  170. }