Files
mirror-3x-ui/internal/web/service/setting_external_traffic_test.go
T
Hamed 47565b71d3 fix(traffic): require a URI for external traffic notifications (#7263)
External Traffic Inform could be saved enabled with a blank URI. Every traffic tick then built the full payload and POSTed it nowhere, logging one "missing port in address" warning per tick (about 17,000 a day in the report).

Refuse the save when the switch is on and the URI is empty. A database that already holds that state skips the notification before the payload is built and warns once per episode, naming why the destination was refused without echoing a malformed URI. Local traffic counters and last-online times are still committed on the skip path.

Fixes #7261
2026-11-29 16:35:47 +03:30

48 lines
1.7 KiB
Go

package service
import (
"path/filepath"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
)
func TestSaveExternalTrafficInformRequiresURIWhenEnabled(t *testing.T) {
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
s := new(SettingService)
const endpoint = "https://example.test/traffic"
for _, tc := range []struct {
name, uri, wantURI, wantError string
enabled bool
}{
{name: "disabled with no URI"},
{name: "enabled with a valid URI", enabled: true, uri: " " + endpoint + " ", wantURI: endpoint},
{name: "enabled with an empty URI", enabled: true, wantURI: endpoint, wantError: "external traffic inform URI is required when enabled"},
{name: "enabled with a whitespace URI", enabled: true, uri: " \t ", wantURI: endpoint, wantError: "external traffic inform URI is required when enabled"},
{name: "disable and clear"},
} {
t.Run(tc.name, func(t *testing.T) {
settings, err := s.GetAllSetting()
if err != nil {
t.Fatal(err)
}
settings.ExternalTrafficInformEnable, settings.ExternalTrafficInformURI = tc.enabled, tc.uri
err = s.UpdateAllSetting(settings, SecretClears{})
if tc.wantError == "" {
if err != nil {
t.Fatal(err)
}
} else if err == nil || err.Error() != tc.wantError {
t.Errorf("save error = %v, want %q", err, tc.wantError)
}
stored, err := s.GetAllSetting()
if err != nil {
t.Fatal(err)
}
if stored.ExternalTrafficInformEnable != tc.enabled || stored.ExternalTrafficInformURI != tc.wantURI {
t.Errorf("stored settings = (%t, %q), want (%t, %q)", stored.ExternalTrafficInformEnable, stored.ExternalTrafficInformURI, tc.enabled, tc.wantURI)
}
})
}
}