github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/utils/config_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestConfig(t *testing.T) {
    18  	TranslationsPreInit()
    19  	cfg, _, err := LoadConfig("config.json")
    20  	require.Nil(t, err)
    21  	InitTranslations(cfg.LocalizationSettings)
    22  }
    23  
    24  func TestFindConfigFile(t *testing.T) {
    25  	dir, err := ioutil.TempDir("", "")
    26  	require.NoError(t, err)
    27  	defer os.RemoveAll(dir)
    28  
    29  	path := filepath.Join(dir, "config.json")
    30  	require.NoError(t, ioutil.WriteFile(path, []byte("{}"), 0600))
    31  
    32  	assert.Equal(t, path, FindConfigFile(path))
    33  
    34  	prevDir, err := os.Getwd()
    35  	require.NoError(t, err)
    36  	defer os.Chdir(prevDir)
    37  	os.Chdir(dir)
    38  	assert.Equal(t, path, FindConfigFile(path))
    39  }
    40  
    41  func TestConfigFromEnviroVars(t *testing.T) {
    42  	os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Enviroment")
    43  	os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand")
    44  	os.Setenv("MM_SERVICESETTINGS_ENABLECOMMANDS", "false")
    45  	os.Setenv("MM_SERVICESETTINGS_READTIMEOUT", "400")
    46  
    47  	TranslationsPreInit()
    48  	cfg, cfgPath, err := LoadConfig("config.json")
    49  	require.Nil(t, err)
    50  
    51  	if cfg.TeamSettings.SiteName != "From Enviroment" {
    52  		t.Fatal("Couldn't read config from enviroment var")
    53  	}
    54  
    55  	if *cfg.TeamSettings.CustomBrandText != "Custom Brand" {
    56  		t.Fatal("Couldn't read config from enviroment var")
    57  	}
    58  
    59  	if *cfg.ServiceSettings.EnableCommands {
    60  		t.Fatal("Couldn't read config from enviroment var")
    61  	}
    62  
    63  	if *cfg.ServiceSettings.ReadTimeout != 400 {
    64  		t.Fatal("Couldn't read config from enviroment var")
    65  	}
    66  
    67  	os.Unsetenv("MM_TEAMSETTINGS_SITENAME")
    68  	os.Unsetenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT")
    69  	os.Unsetenv("MM_SERVICESETTINGS_ENABLECOMMANDS")
    70  	os.Unsetenv("MM_SERVICESETTINGS_READTIMEOUT")
    71  
    72  	cfg.TeamSettings.SiteName = "Mattermost"
    73  	*cfg.ServiceSettings.SiteURL = ""
    74  	*cfg.ServiceSettings.EnableCommands = true
    75  	*cfg.ServiceSettings.ReadTimeout = 300
    76  	SaveConfig(cfgPath, cfg)
    77  
    78  	cfg, _, err = LoadConfig("config.json")
    79  	require.Nil(t, err)
    80  
    81  	if cfg.TeamSettings.SiteName != "Mattermost" {
    82  		t.Fatal("should have been reset")
    83  	}
    84  }
    85  
    86  func TestValidateLocales(t *testing.T) {
    87  	TranslationsPreInit()
    88  	cfg, _, err := LoadConfig("config.json")
    89  	require.Nil(t, err)
    90  
    91  	*cfg.LocalizationSettings.DefaultServerLocale = "en"
    92  	*cfg.LocalizationSettings.DefaultClientLocale = "en"
    93  	*cfg.LocalizationSettings.AvailableLocales = ""
    94  
    95  	// t.Logf("*cfg.LocalizationSettings.DefaultClientLocale: %+v", *cfg.LocalizationSettings.DefaultClientLocale)
    96  	if err := ValidateLocales(cfg); err != nil {
    97  		t.Fatal("Should have not returned an error")
    98  	}
    99  
   100  	// validate DefaultServerLocale
   101  	*cfg.LocalizationSettings.DefaultServerLocale = "junk"
   102  	if err := ValidateLocales(cfg); err != nil {
   103  		if *cfg.LocalizationSettings.DefaultServerLocale != "en" {
   104  			t.Fatal("DefaultServerLocale should have assigned to en as a default value")
   105  		}
   106  	} else {
   107  		t.Fatal("Should have returned an error validating DefaultServerLocale")
   108  	}
   109  
   110  	*cfg.LocalizationSettings.DefaultServerLocale = ""
   111  	if err := ValidateLocales(cfg); err != nil {
   112  		if *cfg.LocalizationSettings.DefaultServerLocale != "en" {
   113  			t.Fatal("DefaultServerLocale should have assigned to en as a default value")
   114  		}
   115  	} else {
   116  		t.Fatal("Should have returned an error validating DefaultServerLocale")
   117  	}
   118  
   119  	*cfg.LocalizationSettings.AvailableLocales = "en"
   120  	*cfg.LocalizationSettings.DefaultServerLocale = "de"
   121  	if err := ValidateLocales(cfg); err != nil {
   122  		if strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale) {
   123  			t.Fatal("DefaultServerLocale should not be added to AvailableLocales")
   124  		}
   125  		t.Fatal("Should have not returned an error validating DefaultServerLocale")
   126  	}
   127  
   128  	// validate DefaultClientLocale
   129  	*cfg.LocalizationSettings.AvailableLocales = ""
   130  	*cfg.LocalizationSettings.DefaultClientLocale = "junk"
   131  	if err := ValidateLocales(cfg); err != nil {
   132  		if *cfg.LocalizationSettings.DefaultClientLocale != "en" {
   133  			t.Fatal("DefaultClientLocale should have assigned to en as a default value")
   134  		}
   135  	} else {
   136  
   137  		t.Fatal("Should have returned an error validating DefaultClientLocale")
   138  	}
   139  
   140  	*cfg.LocalizationSettings.DefaultClientLocale = ""
   141  	if err := ValidateLocales(cfg); err != nil {
   142  		if *cfg.LocalizationSettings.DefaultClientLocale != "en" {
   143  			t.Fatal("DefaultClientLocale should have assigned to en as a default value")
   144  		}
   145  	} else {
   146  		t.Fatal("Should have returned an error validating DefaultClientLocale")
   147  	}
   148  
   149  	*cfg.LocalizationSettings.AvailableLocales = "en"
   150  	*cfg.LocalizationSettings.DefaultClientLocale = "de"
   151  	if err := ValidateLocales(cfg); err != nil {
   152  		if !strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultClientLocale) {
   153  			t.Fatal("DefaultClientLocale should have added to AvailableLocales")
   154  		}
   155  	} else {
   156  		t.Fatal("Should have returned an error validating DefaultClientLocale")
   157  	}
   158  
   159  	// validate AvailableLocales
   160  	*cfg.LocalizationSettings.DefaultServerLocale = "en"
   161  	*cfg.LocalizationSettings.DefaultClientLocale = "en"
   162  	*cfg.LocalizationSettings.AvailableLocales = "junk"
   163  	if err := ValidateLocales(cfg); err != nil {
   164  		if *cfg.LocalizationSettings.AvailableLocales != "" {
   165  			t.Fatal("AvailableLocales should have assigned to empty string as a default value")
   166  		}
   167  	} else {
   168  		t.Fatal("Should have returned an error validating AvailableLocales")
   169  	}
   170  
   171  	*cfg.LocalizationSettings.AvailableLocales = "en,de,junk"
   172  	if err := ValidateLocales(cfg); err != nil {
   173  		if *cfg.LocalizationSettings.AvailableLocales != "" {
   174  			t.Fatal("AvailableLocales should have assigned to empty string as a default value")
   175  		}
   176  	} else {
   177  		t.Fatal("Should have returned an error validating AvailableLocales")
   178  	}
   179  
   180  	*cfg.LocalizationSettings.DefaultServerLocale = "fr"
   181  	*cfg.LocalizationSettings.DefaultClientLocale = "de"
   182  	*cfg.LocalizationSettings.AvailableLocales = "en"
   183  	if err := ValidateLocales(cfg); err != nil {
   184  		if strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale) {
   185  			t.Fatal("DefaultServerLocale should not be added to AvailableLocales")
   186  		}
   187  		if !strings.Contains(*cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultClientLocale) {
   188  			t.Fatal("DefaultClientLocale should have added to AvailableLocales")
   189  		}
   190  	} else {
   191  		t.Fatal("Should have returned an error validating AvailableLocales")
   192  	}
   193  }
   194  
   195  func TestGetClientConfig(t *testing.T) {
   196  	TranslationsPreInit()
   197  	cfg, _, err := LoadConfig("config.json")
   198  	require.Nil(t, err)
   199  
   200  	configMap := GenerateClientConfig(cfg, "")
   201  	if configMap["EmailNotificationContentsType"] != *cfg.EmailSettings.EmailNotificationContentsType {
   202  		t.Fatal("EmailSettings.EmailNotificationContentsType not exposed to client config")
   203  	}
   204  }
   205  
   206  func TestReadConfig(t *testing.T) {
   207  	config, err := ReadConfig(strings.NewReader(`{
   208  		"ServiceSettings": {
   209  			"SiteURL": "http://foo.bar"
   210  		}
   211  	}`), false)
   212  	require.NoError(t, err)
   213  
   214  	assert.Equal(t, "http://foo.bar", *config.ServiceSettings.SiteURL)
   215  }