Remove global cfg vars (#8099)
* remove global cfg vars * enterprise update
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
3d3a234fca
Коммит
7e5ce97668
@@ -9,18 +9,16 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
TranslationsPreInit()
|
||||
LoadGlobalConfig("config.json")
|
||||
InitTranslations(Cfg.LocalizationSettings)
|
||||
cfg, _, err := LoadConfig("config.json")
|
||||
require.Nil(t, err)
|
||||
InitTranslations(cfg.LocalizationSettings)
|
||||
}
|
||||
|
||||
func TestFindConfigFile(t *testing.T) {
|
||||
@@ -47,21 +45,22 @@ func TestConfigFromEnviroVars(t *testing.T) {
|
||||
os.Setenv("MM_SERVICESETTINGS_READTIMEOUT", "400")
|
||||
|
||||
TranslationsPreInit()
|
||||
LoadGlobalConfig("config.json")
|
||||
cfg, cfgPath, err := LoadConfig("config.json")
|
||||
require.Nil(t, err)
|
||||
|
||||
if Cfg.TeamSettings.SiteName != "From Enviroment" {
|
||||
if cfg.TeamSettings.SiteName != "From Enviroment" {
|
||||
t.Fatal("Couldn't read config from enviroment var")
|
||||
}
|
||||
|
||||
if *Cfg.TeamSettings.CustomBrandText != "Custom Brand" {
|
||||
if *cfg.TeamSettings.CustomBrandText != "Custom Brand" {
|
||||
t.Fatal("Couldn't read config from enviroment var")
|
||||
}
|
||||
|
||||
if *Cfg.ServiceSettings.EnableCommands {
|
||||
if *cfg.ServiceSettings.EnableCommands {
|
||||
t.Fatal("Couldn't read config from enviroment var")
|
||||
}
|
||||
|
||||
if *Cfg.ServiceSettings.ReadTimeout != 400 {
|
||||
if *cfg.ServiceSettings.ReadTimeout != 400 {
|
||||
t.Fatal("Couldn't read config from enviroment var")
|
||||
}
|
||||
|
||||
@@ -70,169 +69,67 @@ func TestConfigFromEnviroVars(t *testing.T) {
|
||||
os.Unsetenv("MM_SERVICESETTINGS_ENABLECOMMANDS")
|
||||
os.Unsetenv("MM_SERVICESETTINGS_READTIMEOUT")
|
||||
|
||||
Cfg.TeamSettings.SiteName = "Mattermost"
|
||||
*Cfg.ServiceSettings.SiteURL = ""
|
||||
*Cfg.ServiceSettings.EnableCommands = true
|
||||
*Cfg.ServiceSettings.ReadTimeout = 300
|
||||
SaveConfig(CfgFileName, Cfg)
|
||||
cfg.TeamSettings.SiteName = "Mattermost"
|
||||
*cfg.ServiceSettings.SiteURL = ""
|
||||
*cfg.ServiceSettings.EnableCommands = true
|
||||
*cfg.ServiceSettings.ReadTimeout = 300
|
||||
SaveConfig(cfgPath, cfg)
|
||||
|
||||
LoadGlobalConfig("config.json")
|
||||
cfg, _, err = LoadConfig("config.json")
|
||||
require.Nil(t, err)
|
||||
|
||||
if Cfg.TeamSettings.SiteName != "Mattermost" {
|
||||
if cfg.TeamSettings.SiteName != "Mattermost" {
|
||||
t.Fatal("should have been reset")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedirectStdLog(t *testing.T) {
|
||||
TranslationsPreInit()
|
||||
LoadGlobalConfig("config.json")
|
||||
InitTranslations(Cfg.LocalizationSettings)
|
||||
|
||||
log := NewRedirectStdLog("test", false)
|
||||
|
||||
log.Println("[DEBUG] this is a message")
|
||||
log.Println("[DEBG] this is a message")
|
||||
log.Println("[WARN] this is a message")
|
||||
log.Println("[ERROR] this is a message")
|
||||
log.Println("[EROR] this is a message")
|
||||
log.Println("[ERR] this is a message")
|
||||
log.Println("[INFO] this is a message")
|
||||
log.Println("this is a message")
|
||||
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
|
||||
func TestAddRemoveConfigListener(t *testing.T) {
|
||||
numIntitialCfgListeners := len(cfgListeners)
|
||||
|
||||
id1 := AddConfigListener(func(*model.Config, *model.Config) {
|
||||
})
|
||||
if len(cfgListeners) != numIntitialCfgListeners+1 {
|
||||
t.Fatal("should now have 1 listener")
|
||||
}
|
||||
|
||||
id2 := AddConfigListener(func(*model.Config, *model.Config) {
|
||||
})
|
||||
if len(cfgListeners) != numIntitialCfgListeners+2 {
|
||||
t.Fatal("should now have 2 listeners")
|
||||
}
|
||||
|
||||
RemoveConfigListener(id1)
|
||||
if len(cfgListeners) != numIntitialCfgListeners+1 {
|
||||
t.Fatal("should've removed first listener")
|
||||
}
|
||||
|
||||
RemoveConfigListener(id2)
|
||||
if len(cfgListeners) != numIntitialCfgListeners {
|
||||
t.Fatal("should've removed both listeners")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigListener(t *testing.T) {
|
||||
TranslationsPreInit()
|
||||
LoadGlobalConfig("config.json")
|
||||
|
||||
SiteName := Cfg.TeamSettings.SiteName
|
||||
defer func() {
|
||||
Cfg.TeamSettings.SiteName = SiteName
|
||||
SaveConfig(CfgFileName, Cfg)
|
||||
}()
|
||||
Cfg.TeamSettings.SiteName = "test123"
|
||||
|
||||
listenerCalled := false
|
||||
listener := func(oldConfig *model.Config, newConfig *model.Config) {
|
||||
if listenerCalled {
|
||||
t.Fatal("listener called twice")
|
||||
}
|
||||
|
||||
if oldConfig.TeamSettings.SiteName != "test123" {
|
||||
t.Fatal("old config contains incorrect site name")
|
||||
} else if newConfig.TeamSettings.SiteName != "Mattermost" {
|
||||
t.Fatal("new config contains incorrect site name")
|
||||
}
|
||||
|
||||
listenerCalled = true
|
||||
}
|
||||
listenerId := AddConfigListener(listener)
|
||||
defer RemoveConfigListener(listenerId)
|
||||
|
||||
listener2Called := false
|
||||
listener2 := func(oldConfig *model.Config, newConfig *model.Config) {
|
||||
if listener2Called {
|
||||
t.Fatal("listener2 called twice")
|
||||
}
|
||||
|
||||
listener2Called = true
|
||||
}
|
||||
listener2Id := AddConfigListener(listener2)
|
||||
defer RemoveConfigListener(listener2Id)
|
||||
|
||||
LoadGlobalConfig("config.json")
|
||||
|
||||
if !listenerCalled {
|
||||
t.Fatal("listener should've been called")
|
||||
} else if !listener2Called {
|
||||
t.Fatal("listener 2 should've been called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLocales(t *testing.T) {
|
||||
TranslationsPreInit()
|
||||
LoadGlobalConfig("config.json")
|
||||
cfg, _, err := LoadConfig("config.json")
|
||||
require.Nil(t, err)
|
||||
|
||||
defaultServerLocale := *Cfg.LocalizationSettings.DefaultServerLocale
|
||||
defaultClientLocale := *Cfg.LocalizationSettings.DefaultClientLocale
|
||||
availableLocales := *Cfg.LocalizationSettings.AvailableLocales
|
||||
*cfg.LocalizationSettings.DefaultServerLocale = "en"
|
||||
*cfg.LocalizationSettings.DefaultClientLocale = "en"
|
||||
*cfg.LocalizationSettings.AvailableLocales = ""
|
||||
|
||||
defer func() {
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = defaultClientLocale
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = defaultServerLocale
|
||||
*Cfg.LocalizationSettings.AvailableLocales = availableLocales
|
||||
}()
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "en"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "en"
|
||||
*Cfg.LocalizationSettings.AvailableLocales = ""
|
||||
|
||||
// t.Logf("*Cfg.LocalizationSettings.DefaultClientLocale: %+v", *Cfg.LocalizationSettings.DefaultClientLocale)
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
// t.Logf("*cfg.LocalizationSettings.DefaultClientLocale: %+v", *cfg.LocalizationSettings.DefaultClientLocale)
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
t.Fatal("Should have not returned an error")
|
||||
}
|
||||
|
||||
// validate DefaultServerLocale
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultServerLocale != "en" {
|
||||
t.Fatal("DefaultServerLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
|
||||
t.Fatal("Should have returned an error validating DefaultServerLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = ""
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultServerLocale != "en" {
|
||||
*cfg.LocalizationSettings.DefaultServerLocale = "junk"
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if *cfg.LocalizationSettings.DefaultServerLocale != "en" {
|
||||
t.Fatal("DefaultServerLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating DefaultServerLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "de"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultServerLocale) {
|
||||
*cfg.LocalizationSettings.DefaultServerLocale = ""
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if *cfg.LocalizationSettings.DefaultServerLocale != "en" {
|
||||
t.Fatal("DefaultServerLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating DefaultServerLocale")
|
||||
}
|
||||
|
||||
*cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
*cfg.LocalizationSettings.DefaultServerLocale = "de"
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale) {
|
||||
t.Fatal("DefaultServerLocale should not be added to AvailableLocales")
|
||||
}
|
||||
t.Fatal("Should have not returned an error validating DefaultServerLocale")
|
||||
}
|
||||
|
||||
// validate DefaultClientLocale
|
||||
*Cfg.LocalizationSettings.AvailableLocales = ""
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultClientLocale != "en" {
|
||||
*cfg.LocalizationSettings.AvailableLocales = ""
|
||||
*cfg.LocalizationSettings.DefaultClientLocale = "junk"
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if *cfg.LocalizationSettings.DefaultClientLocale != "en" {
|
||||
t.Fatal("DefaultClientLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
@@ -240,19 +137,19 @@ func TestValidateLocales(t *testing.T) {
|
||||
t.Fatal("Should have returned an error validating DefaultClientLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = ""
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultClientLocale != "en" {
|
||||
*cfg.LocalizationSettings.DefaultClientLocale = ""
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if *cfg.LocalizationSettings.DefaultClientLocale != "en" {
|
||||
t.Fatal("DefaultClientLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating DefaultClientLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "de"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if !strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultClientLocale) {
|
||||
*cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
*cfg.LocalizationSettings.DefaultClientLocale = "de"
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if !strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultClientLocale) {
|
||||
t.Fatal("DefaultClientLocale should have added to AvailableLocales")
|
||||
}
|
||||
} else {
|
||||
@@ -260,34 +157,34 @@ func TestValidateLocales(t *testing.T) {
|
||||
}
|
||||
|
||||
// validate AvailableLocales
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "en"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "en"
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.AvailableLocales != "" {
|
||||
*cfg.LocalizationSettings.DefaultServerLocale = "en"
|
||||
*cfg.LocalizationSettings.DefaultClientLocale = "en"
|
||||
*cfg.LocalizationSettings.AvailableLocales = "junk"
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if *cfg.LocalizationSettings.AvailableLocales != "" {
|
||||
t.Fatal("AvailableLocales should have assigned to empty string as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating AvailableLocales")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en,de,junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.AvailableLocales != "" {
|
||||
*cfg.LocalizationSettings.AvailableLocales = "en,de,junk"
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if *cfg.LocalizationSettings.AvailableLocales != "" {
|
||||
t.Fatal("AvailableLocales should have assigned to empty string as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating AvailableLocales")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "fr"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "de"
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultServerLocale) {
|
||||
*cfg.LocalizationSettings.DefaultServerLocale = "fr"
|
||||
*cfg.LocalizationSettings.DefaultClientLocale = "de"
|
||||
*cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
if err := ValidateLocales(cfg); err != nil {
|
||||
if strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale) {
|
||||
t.Fatal("DefaultServerLocale should not be added to AvailableLocales")
|
||||
}
|
||||
if !strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultClientLocale) {
|
||||
if !strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultClientLocale) {
|
||||
t.Fatal("DefaultClientLocale should have added to AvailableLocales")
|
||||
}
|
||||
} else {
|
||||
@@ -297,10 +194,11 @@ func TestValidateLocales(t *testing.T) {
|
||||
|
||||
func TestGetClientConfig(t *testing.T) {
|
||||
TranslationsPreInit()
|
||||
LoadGlobalConfig("config.json")
|
||||
cfg, _, err := LoadConfig("config.json")
|
||||
require.Nil(t, err)
|
||||
|
||||
configMap := GenerateClientConfig(Cfg, "")
|
||||
if configMap["EmailNotificationContentsType"] != *Cfg.EmailSettings.EmailNotificationContentsType {
|
||||
configMap := GenerateClientConfig(cfg, "")
|
||||
if configMap["EmailNotificationContentsType"] != *cfg.EmailSettings.EmailNotificationContentsType {
|
||||
t.Fatal("EmailSettings.EmailNotificationContentsType not exposed to client config")
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user