Files
3x-ui-web-src/internal/web/service/setting_factory_defaults_test.go
Sanaei f727d04f65
CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
v3.7.0
2026-08-24 15:07:15 +02:00

85 lines
1.9 KiB
Go

package service
import (
"reflect"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/util/reflect_util"
"github.com/mhsanaei/3x-ui/v3/internal/web/entity"
)
func allSettingJSONTags(t *testing.T) map[string]bool {
t.Helper()
tags := make(map[string]bool)
for _, field := range reflect_util.GetFields(reflect.TypeFor[entity.AllSetting]()) {
if tag := field.Tag.Get("json"); tag != "" {
tags[tag] = true
}
}
return tags
}
func TestGetFactoryDefaultsExposesBrowserSafeKeys(t *testing.T) {
defaults := (&SettingService{}).GetFactoryDefaults()
tests := []struct {
key string
want string
}{
{key: "webPort", want: "2053"},
{key: "subPort", want: "2096"},
}
for _, tc := range tests {
t.Run(tc.key, func(t *testing.T) {
got, ok := defaults[tc.key]
if !ok {
t.Fatalf("expected key %q in factory defaults", tc.key)
}
if got != tc.want {
t.Errorf("factory default for %q = %q, want %q", tc.key, got, tc.want)
}
})
}
}
func TestGetFactoryDefaultsOmitsSensitiveMaterial(t *testing.T) {
defaults := (&SettingService{}).GetFactoryDefaults()
for _, key := range []string{
"secret",
"panelGuid",
"nodeMtlsCaCertPem",
"nodeMtlsCaKeyPem",
"nodeMtlsClientCertPem",
"nodeMtlsClientKeyPem",
"nodeMtlsClientCertSha256",
"xrayTemplateConfig",
"tgBotToken",
"twoFactorToken",
"ldapPassword",
"smtpPassword",
} {
t.Run(key, func(t *testing.T) {
if _, ok := defaults[key]; ok {
t.Errorf("factory defaults must not expose %q", key)
}
})
}
}
func TestGetFactoryDefaultsInvariant(t *testing.T) {
defaults := (&SettingService{}).GetFactoryDefaults()
tags := allSettingJSONTags(t)
for key := range defaults {
t.Run(key, func(t *testing.T) {
if !tags[key] {
t.Errorf("key %q is not an entity.AllSetting json tag", key)
}
if factoryDefaultSecretKeys[key] {
t.Errorf("key %q is in the credential deny-list and must not be returned", key)
}
})
}
}