setting_sub_profile_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package service
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. func TestSubProfileModeReadsLegacyAndExplicitSettings(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. storedMode string
  13. storedURL string
  14. modeExists bool
  15. want string
  16. }{
  17. {name: "fresh installation", want: "none"},
  18. {name: "legacy URL", storedURL: " https://profile.example/account ", want: "custom"},
  19. {name: "legacy blank URL", storedURL: " \t ", want: "none"},
  20. {name: "legacy empty mode with URL", modeExists: true, storedURL: "https://profile.example/account", want: "custom"},
  21. {name: "legacy empty mode without URL", modeExists: true, want: "none"},
  22. {name: "explicit none preserves saved URL", modeExists: true, storedMode: "none", storedURL: "https://profile.example/account", want: "none"},
  23. {name: "explicit builtin", modeExists: true, storedMode: "builtin", storedURL: "https://profile.example/account", want: "builtin"},
  24. {name: "explicit custom", modeExists: true, storedMode: "custom", storedURL: "https://profile.example/account", want: "custom"},
  25. {name: "custom with empty URL", modeExists: true, storedMode: "custom", want: "custom"},
  26. {name: "invalid stored mode fails closed", modeExists: true, storedMode: "automatic", storedURL: "https://profile.example/account", want: "none"},
  27. }
  28. for _, tt := range tests {
  29. t.Run(tt.name, func(t *testing.T) {
  30. setupSettingTestDB(t)
  31. s := &SettingService{}
  32. if tt.modeExists {
  33. if err := s.saveSetting("subProfileMode", tt.storedMode); err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. if err := s.saveSetting("subProfileUrl", tt.storedURL); err != nil {
  38. t.Fatal(err)
  39. }
  40. assertSubProfileSettings(t, s, tt.want, tt.storedURL)
  41. })
  42. }
  43. }
  44. func TestSubProfileModeUpdatesPreserveURLAndLegacyPayloads(t *testing.T) {
  45. setupSettingTestDB(t)
  46. s := &SettingService{}
  47. if got := s.GetFactoryDefaults()["subProfileMode"]; got != "none" {
  48. t.Errorf("factory profile mode = %q, want none", got)
  49. }
  50. tests := []struct {
  51. name string
  52. mode string
  53. url string
  54. want string
  55. }{
  56. {name: "custom", mode: "custom", url: "https://profile.example/account", want: "custom"},
  57. {name: "none retains custom URL", mode: "none", url: "https://profile.example/account", want: "none"},
  58. {name: "builtin retains custom URL", mode: "builtin", url: "https://profile.example/account", want: "builtin"},
  59. {name: "custom restores saved URL", mode: "custom", url: "https://profile.example/account", want: "custom"},
  60. {name: "legacy URL submission", url: "https://legacy.example/account", want: "custom"},
  61. {name: "legacy empty URL submission", want: "none"},
  62. }
  63. for _, tt := range tests {
  64. t.Run(tt.name, func(t *testing.T) {
  65. settings, err := s.GetAllSetting()
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. payload, err := json.Marshal(map[string]string{"subProfileMode": tt.mode, "subProfileUrl": tt.url})
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if err := json.Unmarshal(payload, settings); err != nil {
  74. t.Fatal(err)
  75. }
  76. if err := s.UpdateAllSetting(settings, SecretClears{}); err != nil {
  77. t.Fatal(err)
  78. }
  79. assertSubProfileSettings(t, s, tt.want, tt.url)
  80. stored, err := s.getSetting("subProfileMode")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if stored.Value != tt.want {
  85. t.Fatalf("persisted mode = %q, want %q", stored.Value, tt.want)
  86. }
  87. })
  88. }
  89. }
  90. func TestSubProfileModeRejectsInvalidUpdateBeforeWrites(t *testing.T) {
  91. setupSettingTestDB(t)
  92. s := &SettingService{}
  93. if err := s.saveSetting("subTitle", "Original title"); err != nil {
  94. t.Fatal(err)
  95. }
  96. var before []model.Setting
  97. if err := database.GetDB().Order("id").Find(&before).Error; err != nil {
  98. t.Fatal(err)
  99. }
  100. settings, err := s.GetAllSetting()
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. if err := json.Unmarshal([]byte(`{"subProfileMode":"automatic","subTitle":"Changed title","subProfileUrl":"https://profile.example/account"}`), settings); err != nil {
  105. t.Fatal(err)
  106. }
  107. err = s.UpdateAllSetting(settings, SecretClears{})
  108. if err == nil || err.Error() != "subscription profile mode must be none, builtin, or custom" {
  109. t.Errorf("UpdateAllSetting error = %v, want invalid profile mode error", err)
  110. }
  111. var after []model.Setting
  112. if err := database.GetDB().Order("id").Find(&after).Error; err != nil {
  113. t.Fatal(err)
  114. }
  115. if !reflect.DeepEqual(before, after) {
  116. t.Fatal("invalid profile mode update modified stored settings")
  117. }
  118. }
  119. func assertSubProfileSettings(t *testing.T, s *SettingService, wantMode, wantURL string) {
  120. t.Helper()
  121. if mode, err := s.GetSubProfileMode(); err != nil || mode != wantMode {
  122. t.Fatalf("GetSubProfileMode = %q, %v; want %q, nil", mode, err, wantMode)
  123. }
  124. settings, err := s.GetAllSetting()
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. payload, err := json.Marshal(settings)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. var profile struct {
  133. Mode string `json:"subProfileMode"`
  134. URL string `json:"subProfileUrl"`
  135. }
  136. if err := json.Unmarshal(payload, &profile); err != nil {
  137. t.Fatal(err)
  138. }
  139. if profile.Mode != wantMode || profile.URL != wantURL {
  140. t.Fatalf("profile settings = (%q, %q), want (%q, %q)", profile.Mode, profile.URL, wantMode, wantURL)
  141. }
  142. }