summaryrefslogtreecommitdiffstats
path: root/utils/config_test.go
blob: f3e230fa6da57b9a1bcefc66cd7cb94402b550a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"os"
	"strings"
	"testing"
	"time"

	"github.com/mattermost/platform/model"
)

func TestConfig(t *testing.T) {
	TranslationsPreInit()
	LoadConfig("config.json")
	InitTranslations(Cfg.LocalizationSettings)
}

func TestConfigFromEnviroVars(t *testing.T) {

	os.Setenv("MM_TEAMSETTINGS_SITENAME", "From Enviroment")
	os.Setenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT", "Custom Brand")
	os.Setenv("MM_SERVICESETTINGS_ENABLECOMMANDS", "false")
	os.Setenv("MM_SERVICESETTINGS_READTIMEOUT", "400")

	TranslationsPreInit()
	EnableConfigFromEnviromentVars()
	LoadConfig("config.json")

	if Cfg.TeamSettings.SiteName != "From Enviroment" {
		t.Fatal("Couldn't read config from enviroment var")
	}

	if *Cfg.TeamSettings.CustomBrandText != "Custom Brand" {
		t.Fatal("Couldn't read config from enviroment var")
	}

	if *Cfg.ServiceSettings.EnableCommands != false {
		t.Fatal("Couldn't read config from enviroment var")
	}

	if *Cfg.ServiceSettings.ReadTimeout != 400 {
		t.Fatal("Couldn't read config from enviroment var")
	}

	os.Unsetenv("MM_TEAMSETTINGS_SITENAME")
	os.Unsetenv("MM_TEAMSETTINGS_CUSTOMBRANDTEXT")
	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)

	LoadConfig("config.json")

	if Cfg.TeamSettings.SiteName != "Mattermost" {
		t.Fatal("should have been reset")
	}
}

func TestRedirectStdLog(t *testing.T) {
	TranslationsPreInit()
	LoadConfig("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()
	EnableConfigFromEnviromentVars()
	LoadConfig("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)

	LoadConfig("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()
	LoadConfig("config.json")

	defaultServerLocale := *Cfg.LocalizationSettings.DefaultServerLocale
	defaultClientLocale := *Cfg.LocalizationSettings.DefaultClientLocale
	availableLocales := *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.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" {
			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" {
			t.Fatal("DefaultClientLocale should have assigned to en as a default value")
		}
	} else {

		t.Fatal("Should have returned an error validating DefaultClientLocale")
	}

	*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) {
			t.Fatal("DefaultClientLocale should have added to AvailableLocales")
		}
	} else {
		t.Fatal("Should have returned an error validating DefaultClientLocale")
	}

	// validate 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 != "" {
			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) {
			t.Fatal("DefaultServerLocale should not be added to AvailableLocales")
		}
		if !strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultClientLocale) {
			t.Fatal("DefaultClientLocale should have added to AvailableLocales")
		}
	} else {
		t.Fatal("Should have returned an error validating AvailableLocales")
	}
}

func TestGetClientConfig(t *testing.T) {
	TranslationsPreInit()
	LoadConfig("config.json")

	configMap := getClientConfig(Cfg)
	if configMap["EmailNotificationContentsType"] != *Cfg.EmailSettings.EmailNotificationContentsType {
		t.Fatal("EmailSettings.EmailNotificationContentsType not exposed to client config")
	}
}