inbound_update_stale_form_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  7. "gorm.io/gorm"
  8. )
  9. func readClientTraffic(t *testing.T, email string) xray.ClientTraffic {
  10. t.Helper()
  11. var row xray.ClientTraffic
  12. if err := database.GetDB().Where("email = ?", email).First(&row).Error; err != nil {
  13. t.Fatalf("read client_traffics %s: %v", email, err)
  14. }
  15. return row
  16. }
  17. // The inbound modal posts the clients it loaded on open; a renewal committed
  18. // while it was open must survive the save in settings, record and stats.
  19. func TestInboundFormSaveKeepsClientRenewedWhileOpen(t *testing.T) {
  20. setupBulkDB(t)
  21. ib := seedRenewableNeighbour(t, 23201, nil)
  22. form := *ib
  23. if err := database.GetDB().Transaction(autoRenewTick); err != nil {
  24. t.Fatalf("autoRenew: %v", err)
  25. }
  26. form.Remark = "edited"
  27. if _, _, err := (&InboundService{}).UpdateInbound(&form); err != nil {
  28. t.Fatalf("UpdateInbound: %v", err)
  29. }
  30. requireNeighbourRenewed(t, ib.Id)
  31. now := time.Now().UnixMilli()
  32. if row := readClientTraffic(t, "y@stale"); !row.Enable || row.ExpiryTime <= now {
  33. t.Fatalf("client_traffics rolled back: enable=%v expiryTime=%d", row.Enable, row.ExpiryTime)
  34. }
  35. if rec := lookupClientRecord(t, "y@stale"); !rec.Enable || rec.ExpiryTime <= now {
  36. t.Fatalf("client record rolled back: enable=%v expiryTime=%d", rec.Enable, rec.ExpiryTime)
  37. }
  38. }
  39. // On a node the master's push is authoritative, lifecycle fields included.
  40. func TestInboundUpdateFromMasterAppliesClientLifecycle(t *testing.T) {
  41. setupBulkDB(t)
  42. ib := seedRenewableNeighbour(t, 23202, nil)
  43. clients, err := (&InboundService{}).GetClients(ib)
  44. if err != nil {
  45. t.Fatalf("GetClients: %v", err)
  46. }
  47. for i := range clients {
  48. if clients[i].Email == "x@stale" {
  49. clients[i].Enable = false
  50. }
  51. }
  52. push := *ib
  53. push.Settings = clientsSettings(t, clients)
  54. if _, _, err := (&InboundService{FromNodeSync: true}).UpdateInbound(&push); err != nil {
  55. t.Fatalf("UpdateInbound: %v", err)
  56. }
  57. if x, _ := settingsClient(t, ib.Id, "x@stale"); x.Enable {
  58. t.Fatal("master push disabling x@stale was ignored in settings")
  59. }
  60. if readClientTraffic(t, "x@stale").Enable {
  61. t.Fatal("master push disabling x@stale was ignored in client_traffics")
  62. }
  63. }
  64. // Traffic the poll adds after UpdateInbound read the row must not be written
  65. // back over by the edit.
  66. func TestInboundUpdateKeepsTrafficAddedMidEdit(t *testing.T) {
  67. setupBulkDB(t)
  68. ib := seedRenewableNeighbour(t, 23203, nil)
  69. form := *ib
  70. form.Remark = "edited"
  71. commitTickBetweenReadAndWrite(t, func(tx *gorm.DB) error {
  72. return (&InboundService{}).addInboundTraffic(tx, []*xray.Traffic{{IsInbound: true, Tag: ib.Tag, Up: 100, Down: 50}})
  73. }, func() {
  74. if _, _, err := (&InboundService{}).UpdateInbound(&form); err != nil {
  75. t.Errorf("UpdateInbound: %v", err)
  76. }
  77. })
  78. saved, err := (&InboundService{}).GetInbound(ib.Id)
  79. if err != nil {
  80. t.Fatalf("GetInbound: %v", err)
  81. }
  82. if saved.Up != 100 || saved.Down != 50 || saved.Remark != "edited" {
  83. t.Fatalf("inbound after edit: up=%d down=%d remark=%q, want 100/50/edited", saved.Up, saved.Down, saved.Remark)
  84. }
  85. }