1
0

client_traffic_cycle_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. // The cycle has to survive the clients table, not just the settings JSON: an
  10. // ordinary edit rebuilds the client from the record and writes it back (#5497).
  11. func TestClientEditKeepsTheTrafficResetCycle(t *testing.T) {
  12. setupBulkDB(t)
  13. svc := &InboundService{}
  14. db := database.GetDB()
  15. clients := []model.Client{
  16. {
  17. Email: "cyc@x", ID: "66666666-6666-6666-6666-666666666666", Enable: true,
  18. TrafficReset: "monthly", TrafficResetDay: 15,
  19. ExpiryTime: time.Now().Add(24 * time.Hour).UnixMilli(),
  20. },
  21. }
  22. ib := mkInbound(t, 30301, model.VLESS, clientsSettings(t, clients))
  23. if err := svc.clientService.SyncInbound(nil, ib.Id, clients); err != nil {
  24. t.Fatalf("SyncInbound: %v", err)
  25. }
  26. rec, err := svc.clientService.GetRecordByEmail(nil, "cyc@x")
  27. if err != nil {
  28. t.Fatalf("GetRecordByEmail: %v", err)
  29. }
  30. if rec.TrafficReset != "monthly" || rec.TrafficResetDay != 15 {
  31. t.Fatalf("clients row holds %q/%d, want monthly/15", rec.TrafficReset, rec.TrafficResetDay)
  32. }
  33. // What the edit dialog does: hydrate the record, change something else, save.
  34. edited := rec.ToClient()
  35. edited.Comment = "renamed"
  36. if _, err := svc.clientService.Update(svc, rec.Id, *edited, rec.LimitHwid); err != nil {
  37. t.Fatalf("Update: %v", err)
  38. }
  39. var stored model.Inbound
  40. if err := db.Where("id = ?", ib.Id).First(&stored).Error; err != nil {
  41. t.Fatal(err)
  42. }
  43. var settings struct {
  44. Clients []model.Client `json:"clients"`
  45. }
  46. if err := json.Unmarshal([]byte(stored.Settings), &settings); err != nil {
  47. t.Fatalf("parse inbound settings: %v", err)
  48. }
  49. if len(settings.Clients) != 1 {
  50. t.Fatalf("inbound holds %d clients, want 1", len(settings.Clients))
  51. }
  52. if got := settings.Clients[0]; got.TrafficReset != "monthly" || got.TrafficResetDay != 15 {
  53. t.Fatalf("inbound settings hold %q/%d after an unrelated edit, want monthly/15: the cycle was silently switched off",
  54. got.TrafficReset, got.TrafficResetDay)
  55. }
  56. rec, err = svc.clientService.GetRecordByEmail(nil, "cyc@x")
  57. if err != nil {
  58. t.Fatalf("GetRecordByEmail after edit: %v", err)
  59. }
  60. if rec.TrafficReset != "monthly" || rec.TrafficResetDay != 15 {
  61. t.Fatalf("clients row holds %q/%d after an unrelated edit, want monthly/15", rec.TrafficReset, rec.TrafficResetDay)
  62. }
  63. }
  64. // The setting is useless if it can only be chosen once. This is the assertion
  65. // the earlier "survives an unrelated edit" test could not make: that one passed
  66. // precisely because nothing on the attached-inbound path ever wrote the column.
  67. func TestClientEditChangesTheTrafficResetCycle(t *testing.T) {
  68. setupBulkDB(t)
  69. svc := &InboundService{}
  70. clients := []model.Client{
  71. {
  72. Email: "chg@x", ID: "77777777-7777-7777-7777-777777777777", Enable: true,
  73. TrafficReset: "weekly", TrafficResetDay: 1,
  74. ExpiryTime: time.Now().Add(24 * time.Hour).UnixMilli(),
  75. },
  76. }
  77. ib := mkInbound(t, 30302, model.VLESS, clientsSettings(t, clients))
  78. if err := svc.clientService.SyncInbound(nil, ib.Id, clients); err != nil {
  79. t.Fatalf("SyncInbound: %v", err)
  80. }
  81. rec, err := svc.clientService.GetRecordByEmail(nil, "chg@x")
  82. if err != nil {
  83. t.Fatalf("GetRecordByEmail: %v", err)
  84. }
  85. edited := rec.ToClient()
  86. edited.TrafficReset = "monthly"
  87. edited.TrafficResetDay = 9
  88. if _, err := svc.clientService.Update(svc, rec.Id, *edited, rec.LimitHwid); err != nil {
  89. t.Fatalf("Update: %v", err)
  90. }
  91. rec, err = svc.clientService.GetRecordByEmail(nil, "chg@x")
  92. if err != nil {
  93. t.Fatalf("GetRecordByEmail after edit: %v", err)
  94. }
  95. if rec.TrafficReset != "monthly" || rec.TrafficResetDay != 9 {
  96. t.Fatalf("clients row holds %q/%d after the operator changed it to monthly/9: the job keeps applying the old cycle",
  97. rec.TrafficReset, rec.TrafficResetDay)
  98. }
  99. // Turning it off has to work too, and "never" is not an empty value.
  100. edited = rec.ToClient()
  101. edited.TrafficReset = "never"
  102. if _, err := svc.clientService.Update(svc, rec.Id, *edited, rec.LimitHwid); err != nil {
  103. t.Fatalf("Update to never: %v", err)
  104. }
  105. rec, err = svc.clientService.GetRecordByEmail(nil, "chg@x")
  106. if err != nil {
  107. t.Fatalf("GetRecordByEmail after disabling: %v", err)
  108. }
  109. if rec.TrafficReset != "never" {
  110. t.Fatalf("clients row holds %q after the operator switched the cycle off", rec.TrafficReset)
  111. }
  112. }
  113. // An unknown cycle would leave a field that reads as configured while no job
  114. // ever selects the client, so it is rejected instead of coerced.
  115. func TestClientTrafficResetValidation(t *testing.T) {
  116. for _, tc := range []struct {
  117. name string
  118. period string
  119. day int
  120. ok bool
  121. }{
  122. {"unset", "", 0, true},
  123. {"never", "never", 1, true},
  124. {"monthly last day", "monthly", 31, true},
  125. {"unknown period", "fortnightly", 1, false},
  126. {"day past the month", "monthly", 32, false},
  127. {"negative day", "monthly", -1, false},
  128. } {
  129. t.Run(tc.name, func(t *testing.T) {
  130. err := validateClientTrafficReset(tc.period, tc.day)
  131. if tc.ok && err != nil {
  132. t.Errorf("validateClientTrafficReset(%q, %d) = %v, want accepted", tc.period, tc.day, err)
  133. }
  134. if !tc.ok && err == nil {
  135. t.Errorf("validateClientTrafficReset(%q, %d) accepted, want rejected", tc.period, tc.day)
  136. }
  137. })
  138. }
  139. }