File size: 12,132 Bytes
530729e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package config

import (
	"fmt"
	"reflect"
	"testing"

	"github.com/GoAdminGroup/go-admin/modules/utils"

	"github.com/stretchr/testify/assert"
)

func TestConfig_GetIndexUrl(t *testing.T) {
	Initialize(&Config{
		UrlPrefix: "admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().GetIndexURL(), "/admin")

	testSetCfg(&Config{
		UrlPrefix: "/admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().GetIndexURL(), "/admin")

	testSetCfg(&Config{
		UrlPrefix: "/admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().GetIndexURL(), "/admin")
}

func TestConfig_Index(t *testing.T) {
	testSetCfg(&Config{
		UrlPrefix: "admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().Index(), "/")
}

func TestConfig_Prefix(t *testing.T) {
	testSetCfg(&Config{
		UrlPrefix: "admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().Prefix(), "/admin")

	testSetCfg(&Config{
		UrlPrefix: "/admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().Prefix(), "/admin")
}

func TestConfig_Url(t *testing.T) {
	testSetCfg(&Config{
		UrlPrefix: "admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().Url("/info/user"), "/admin/info/user")

	testSetCfg(&Config{
		UrlPrefix: "/admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().Url("/info/user"), "/admin/info/user")
	assert.Equal(t, Get().Url("/info/user") != "/admin/info/user/", true)
}

func TestConfig_UrlRemovePrefix(t *testing.T) {

	testSetCfg(&Config{
		UrlPrefix: "/admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().URLRemovePrefix("/admin/info/user"), "/info/user")
}

func TestConfig_PrefixFixSlash(t *testing.T) {

	testSetCfg(&Config{
		UrlPrefix: "/admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().PrefixFixSlash(), "/admin")

	testSetCfg(&Config{
		UrlPrefix: "admin",
		IndexUrl:  "/",
	})

	assert.Equal(t, Get().PrefixFixSlash(), "/admin")
}

func TestSet(t *testing.T) {
	testSetCfg(&Config{Theme: "abc"})
	testSetCfg(&Config{Theme: "bcd"})
	assert.Equal(t, Get().Theme, "bcd")
}

func TestStore_URL(t *testing.T) {
	testSetCfg(&Config{
		Store: Store{
			Prefix: "/file",
			Path:   "./uploads",
		},
	})

	assert.Equal(t, Get().Store.URL("/xxxxxx.png"), "/file/xxxxxx.png")

	testSetCfg(&Config{
		Store: Store{
			Prefix: "http://xxxxx.com/xxxx/file",
			Path:   "./uploads",
		},
	})

	assert.Equal(t, Get().Store.URL("/xxxxxx.png"), "http://xxxxx.com/xxxx/file/xxxxxx.png")

	testSetCfg(&Config{
		Store: Store{
			Prefix: "/file",
			Path:   "./uploads",
		},
	})

	assert.Equal(t, Get().Store.URL("http://xxxxx.com/xxxx/file/xxxx.png"), "http://xxxxx.com/xxxx/file/xxxx.png")
}

func TestDatabase_ParamStr(t *testing.T) {
	cfg := Database{
		Driver: DriverMysql,
		Params: map[string]string{
			"parseTime": "true",
		},
	}
	assert.Equal(t, cfg.ParamStr(), "?charset=utf8mb4&parseTime=true")
}

func TestReadFromYaml(t *testing.T) {
	cfg := ReadFromYaml("./config.yaml")
	assert.Equal(t, cfg.Databases.GetDefault().Driver, "mssql")
	assert.Equal(t, cfg.Domain, "localhost")
	assert.Equal(t, cfg.UrlPrefix, "admin")
	assert.Equal(t, cfg.Store.Path, "./uploads")
	assert.Equal(t, cfg.IndexUrl, "/")
	assert.Equal(t, cfg.Debug, true)
	assert.Equal(t, cfg.OpenAdminApi, true)
	assert.Equal(t, cfg.ColorScheme, "skin-black")
}

func TestReadFromINI(t *testing.T) {
	cfg := ReadFromINI("./config.ini")
	assert.Equal(t, cfg.Databases.GetDefault().Driver, "postgresql")
	assert.Equal(t, cfg.Domain, "localhost")
	assert.Equal(t, cfg.UrlPrefix, "admin")
	assert.Equal(t, cfg.Store.Path, "./uploads")
	assert.Equal(t, cfg.IndexUrl, "/")
	assert.Equal(t, cfg.Debug, true)
	assert.Equal(t, cfg.OpenAdminApi, true)
	assert.Equal(t, cfg.ColorScheme, "skin-black")
}

func testSetCfg(cfg *Config) {
	count = 0
	Initialize(cfg)
}

func TestUpdate(t *testing.T) {
	m := map[string]string{
		"access_assets_log_off":             `true`,
		"access_log_off":                    `false`,
		"access_log_path":                   "",
		"allow_del_operation_log":           `false`,
		"animation":                         `{"type":"fadeInUp","duration":0,"delay":0}`,
		"animation_delay":                   `0.00`,
		"animation_duration":                `0`,
		"animation_type":                    `fadeInUp`,
		"app_id":                            `70rv3KwjwjXE`,
		"asset_root_path":                   `./public/`,
		"asset_url":                         "",
		"auth_user_table":                   `goadmin_users`,
		"bootstrap_file_path":               `./../datamodel/bootstrap.go`,
		"color_scheme":                      `skin-black`,
		"custom_403_html":                   "",
		"custom_404_html":                   "",
		"custom_500_html":                   "",
		"custom_foot_html":                  "",
		"custom_head_html":                  ` <link rel="icon" type="image/png" sizes="32x32" href="//quick.go-admin.cn/official/assets/imgs/icons.ico/favicon-32x32.png">`,
		"databases":                         `{"default":{"host":"127.0.0.1","port":"3306","user":"root","pwd":"root","name":"godmin","max_idle_con":50,"max_open_con":150,"driver":"mysql","file":"","dsn":""}}`,
		"debug":                             `true`,
		"domain":                            "",
		"env":                               `test`,
		"error_log_off":                     `false`,
		"error_log_path":                    "",
		"exclude_theme_components":          `null`,
		"extra":                             "",
		"file_upload_engine":                `{"name":"local","config":null}`,
		"footer_info":                       "",
		"go_mod_file_path":                  "",
		"hide_app_info_entrance":            `false`,
		"hide_config_center_entrance":       `false`,
		"hide_plugin_entrance":              `false`,
		"hide_tool_entrance":                `false`,
		"hide_visitor_user_center_entrance": `false`,
		"index_url":                         `/`,
		"info_log_off":                      `false`,
		"info_log_path":                     "",
		"language":                          `zh`,
		"logger_encoder_caller":             `full`,
		"logger_encoder_caller_key":         `caller`,
		"logger_encoder_duration":           `string`,
		"logger_encoder_encoding":           `console`,
		"logger_encoder_level":              `capitalColor`,
		"logger_encoder_level_key":          `level`,
		"logger_encoder_message_key":        `msg`,
		"logger_encoder_name_key":           `logger`,
		"logger_encoder_stacktrace_key":     `stacktrace`,
		"logger_encoder_time":               `iso8601`,
		"logger_encoder_time_key":           `ts`,
		"logger_level":                      `0`,
		"logger_rotate_compress":            `false`,
		"logger_rotate_max_age":             `30`,
		"logger_rotate_max_backups":         `5`,
		"logger_rotate_max_size":            `10`,
		"login_logo":                        "",
		"login_title":                       `GoAdmin`,
		"login_url":                         `/login`,
		"logo":                              `<b>Go</b>Admin`,
		"mini_logo":                         `<b>G</b>A`,
		"no_limit_login_ip":                 `false`,
		"open_admin_api":                    `false`,
		"operation_log_off":                 `false`,
		"plugin_file_path":                  `/go/src/github.com/GoAdminGroup/go-admin/examples/gin/plugins.go`,
		"session_life_time":                 `7200`,
		"site_off":                          `false`,
		"sql_log":                           `true`,
		"store":                             `{"path":"./uploads","prefix":"uploads"}`,
		"theme":                             `sword`,
		"title":                             `GoAdmin`,
		"url_prefix":                        `admin`,
	}
	c := &Config{}
	c2 := &Config{}
	if c.Update(m) == nil {
		if c.Language != c2.Language ||
			c.Domain != c2.Domain ||
			c.Theme != c2.Theme ||
			c.Title != c2.Title ||
			c.Logo != c2.Logo ||
			c.MiniLogo != c2.MiniLogo ||
			c.Debug != c2.Debug ||
			c.SiteOff != c2.SiteOff ||
			c.AccessLogOff != c2.AccessLogOff ||
			c.InfoLogOff != c2.InfoLogOff ||
			c.ErrorLogOff != c2.ErrorLogOff ||
			c.AccessAssetsLogOff != c2.AccessAssetsLogOff ||
			c.InfoLogPath != c2.InfoLogPath ||
			c.ErrorLogPath != c2.ErrorLogPath ||
			c.AccessLogPath != c2.AccessLogPath ||
			c.SqlLog != c2.SqlLog ||
			c.Logger.Rotate.MaxSize != c2.Logger.Rotate.MaxSize ||
			c.Logger.Rotate.MaxBackups != c2.Logger.Rotate.MaxBackups ||
			c.Logger.Rotate.MaxAge != c2.Logger.Rotate.MaxAge ||
			c.Logger.Rotate.Compress != c2.Logger.Rotate.Compress ||
			c.Logger.Encoder.Encoding != c2.Logger.Encoder.Encoding ||
			c.Logger.Level != c2.Logger.Level ||
			c.Logger.Encoder.TimeKey != c2.Logger.Encoder.TimeKey ||
			c.Logger.Encoder.LevelKey != c2.Logger.Encoder.LevelKey ||
			c.Logger.Encoder.NameKey != c2.Logger.Encoder.NameKey ||
			c.Logger.Encoder.CallerKey != c2.Logger.Encoder.CallerKey ||
			c.Logger.Encoder.MessageKey != c2.Logger.Encoder.MessageKey ||
			c.Logger.Encoder.StacktraceKey != c2.Logger.Encoder.StacktraceKey ||
			c.Logger.Encoder.Level != c2.Logger.Encoder.Level ||
			c.Logger.Encoder.Time != c2.Logger.Encoder.Time ||
			c.Logger.Encoder.Duration != c2.Logger.Encoder.Duration ||
			c.Logger.Encoder.Caller != c2.Logger.Encoder.Caller ||
			c.ColorScheme != c2.ColorScheme ||
			c.SessionLifeTime != c2.SessionLifeTime ||
			c.CustomHeadHtml != c2.CustomHeadHtml ||
			c.CustomFootHtml != c2.CustomFootHtml ||
			c.Custom404HTML != c2.Custom404HTML ||
			c.Custom403HTML != c2.Custom403HTML ||
			c.Custom500HTML != c2.Custom500HTML ||
			c.BootstrapFilePath != c2.BootstrapFilePath ||
			c.GoModFilePath != c2.GoModFilePath ||
			c.FooterInfo != c2.FooterInfo ||
			c.LoginTitle != c2.LoginTitle ||
			c.AssetUrl != c2.AssetUrl ||
			c.LoginLogo != c2.LoginLogo ||
			c.NoLimitLoginIP != c2.NoLimitLoginIP ||
			c.AllowDelOperationLog != c2.AllowDelOperationLog ||
			c.OperationLogOff != c2.OperationLogOff ||
			c.HideConfigCenterEntrance != c2.HideConfigCenterEntrance ||
			c.HideAppInfoEntrance != c2.HideAppInfoEntrance ||
			c.HideToolEntrance != c2.HideToolEntrance ||
			c.HidePluginEntrance != c2.HidePluginEntrance ||
			c.FileUploadEngine.Name != c2.FileUploadEngine.Name ||
			c.Animation.Type != c2.Animation.Type ||
			c.Animation.Duration != c2.Animation.Duration ||
			c.Animation.Delay != c2.Animation.Delay ||
			!reflect.DeepEqual(c.Extra, c2.Extra) {
			panic("c.Extra")
		}
	}
}

func TestToMap(t *testing.T) {
	c := &Config{
		UrlPrefix: "/admin",
		IndexUrl:  "/",
		MiniLogo:  "<asdfadsf>",
		Animation: PageAnimation{
			Type: "12313213",
		},
		SessionLifeTime:        40,
		ExcludeThemeComponents: []string{"asdfas", "sadfasf"},
	}
	m := c.ToMap()
	fmt.Println(m)
	fmt.Println(m["prefix"], m["animation_type"], m["mini_logo"])

	arr := []string{
		"language", "databases", "domain", "url_prefix", "theme", "store", "title", "logo", "mini_logo", "index_url",
		"site_off", "login_url", "debug", "env", "open_admin_api", "hide_visitor_user_center_entrance",
		"info_log_path", "error_log_path", "access_log_path", "sql_log", "access_log_off", "info_log_off", "error_log_off",
		"access_assets_log_off",
		"logger_rotate_max_size", "logger_rotate_max_backups", "logger_rotate_max_age", "logger_rotate_compress",
		"logger_encoder_time_key", "logger_encoder_level_key", "logger_encoder_name_key", "logger_encoder_caller_key",
		"logger_encoder_message_key", "logger_encoder_stacktrace_key", "logger_encoder_level", "logger_encoder_time",
		"logger_encoder_duration", "logger_encoder_caller", "logger_encoder_encoding", "logger_level",
		"color_scheme", "session_life_time", "asset_url", "file_upload_engine", "custom_head_html", "custom_foot_html",
		"custom_404_html", "custom_403_html", "custom_500_html", "bootstrap_file_path", "go_mod_file_path", "footer_info",
		"app_id", "login_title", "login_logo", "auth_user_table", "exclude_theme_components",
		"extra",
		"animation_type", "animation_duration", "animation_delay",
		"no_limit_login_ip", "allow_del_operation_log", "operation_log_off",
		"hide_config_center_entrance", "hide_app_info_entrance", "hide_tool_entrance", "hide_plugin_entrance",
		"asset_root_path",
	}

	for key := range m {
		if !utils.InArray(arr, key) {
			panic(key)
		}
	}

	fmt.Println(len(arr), len(m))
}