client_update_rename_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 TestClientUpdateDuplicateSubIDDoesNotRenameEmail(t *testing.T) {
  42. setupBulkDB(t)
  43. svc := &ClientService{}
  44. inboundSvc := &InboundService{}
  45. source := []model.Client{
  46. {Email: "keep@x", ID: "aaaaaaaa-0000-0000-0000-000000000003", SubID: "sub-keep", Enable: true},
  47. {Email: "other@x", ID: "aaaaaaaa-0000-0000-0000-000000000004", SubID: "sub-other", Enable: true},
  48. }
  49. ib := mkInbound(t, 22003, model.VLESS, clientsSettings(t, source))
  50. if err := svc.SyncInbound(nil, ib.Id, source); err != nil {
  51. t.Fatalf("seed linkage: %v", err)
  52. }
  53. origId := lookupClientRecord(t, "keep@x").Id
  54. origSettings := mustInboundSettings(t, inboundSvc, ib.Id)
  55. updated := source[0]
  56. updated.Email = "kept@x"
  57. updated.SubID = "sub-other"
  58. if _, err := svc.Update(inboundSvc, origId, updated); err == nil {
  59. t.Fatalf("Update with colliding subId succeeded, want error")
  60. }
  61. rec := lookupClientRecord(t, "keep@x")
  62. if rec.Id != origId {
  63. t.Fatalf("record id changed after rejected update")
  64. }
  65. if got := mustInboundSettings(t, inboundSvc, ib.Id); got != origSettings {
  66. t.Fatalf("inbound settings changed after rejected update")
  67. }
  68. }
  69. func mustInboundSettings(t *testing.T, inboundSvc *InboundService, id int) string {
  70. t.Helper()
  71. ib, err := inboundSvc.GetInbound(id)
  72. if err != nil {
  73. t.Fatalf("GetInbound %d: %v", id, err)
  74. }
  75. return ib.Settings
  76. }