github.com/kotovmak/go-admin@v1.1.1/modules/config/config_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/kotovmak/go-admin/modules/utils" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestConfig_GetIndexUrl(t *testing.T) { 14 Initialize(&Config{ 15 UrlPrefix: "admin", 16 IndexUrl: "/", 17 }) 18 19 assert.Equal(t, Get().GetIndexURL(), "/admin") 20 21 testSetCfg(&Config{ 22 UrlPrefix: "/admin", 23 IndexUrl: "/", 24 }) 25 26 assert.Equal(t, Get().GetIndexURL(), "/admin") 27 28 testSetCfg(&Config{ 29 UrlPrefix: "/admin", 30 IndexUrl: "/", 31 }) 32 33 assert.Equal(t, Get().GetIndexURL(), "/admin") 34 } 35 36 func TestConfig_Index(t *testing.T) { 37 testSetCfg(&Config{ 38 UrlPrefix: "admin", 39 IndexUrl: "/", 40 }) 41 42 assert.Equal(t, Get().Index(), "/") 43 } 44 45 func TestConfig_Prefix(t *testing.T) { 46 testSetCfg(&Config{ 47 UrlPrefix: "admin", 48 IndexUrl: "/", 49 }) 50 51 assert.Equal(t, Get().Prefix(), "/admin") 52 53 testSetCfg(&Config{ 54 UrlPrefix: "/admin", 55 IndexUrl: "/", 56 }) 57 58 assert.Equal(t, Get().Prefix(), "/admin") 59 } 60 61 func TestConfig_Url(t *testing.T) { 62 testSetCfg(&Config{ 63 UrlPrefix: "admin", 64 IndexUrl: "/", 65 }) 66 67 assert.Equal(t, Get().Url("/info/user"), "/admin/info/user") 68 69 testSetCfg(&Config{ 70 UrlPrefix: "/admin", 71 IndexUrl: "/", 72 }) 73 74 assert.Equal(t, Get().Url("/info/user"), "/admin/info/user") 75 assert.Equal(t, Get().Url("/info/user") != "/admin/info/user/", true) 76 } 77 78 func TestConfig_UrlRemovePrefix(t *testing.T) { 79 80 testSetCfg(&Config{ 81 UrlPrefix: "/admin", 82 IndexUrl: "/", 83 }) 84 85 assert.Equal(t, Get().URLRemovePrefix("/admin/info/user"), "/info/user") 86 } 87 88 func TestConfig_PrefixFixSlash(t *testing.T) { 89 90 testSetCfg(&Config{ 91 UrlPrefix: "/admin", 92 IndexUrl: "/", 93 }) 94 95 assert.Equal(t, Get().PrefixFixSlash(), "/admin") 96 97 testSetCfg(&Config{ 98 UrlPrefix: "admin", 99 IndexUrl: "/", 100 }) 101 102 assert.Equal(t, Get().PrefixFixSlash(), "/admin") 103 } 104 105 func TestSet(t *testing.T) { 106 testSetCfg(&Config{Theme: "abc"}) 107 testSetCfg(&Config{Theme: "bcd"}) 108 assert.Equal(t, Get().Theme, "bcd") 109 } 110 111 func TestStore_URL(t *testing.T) { 112 testSetCfg(&Config{ 113 Store: Store{ 114 Prefix: "/file", 115 Path: "./uploads", 116 }, 117 }) 118 119 assert.Equal(t, Get().Store.URL("/xxxxxx.png"), "/file/xxxxxx.png") 120 121 testSetCfg(&Config{ 122 Store: Store{ 123 Prefix: "http://xxxxx.com/xxxx/file", 124 Path: "./uploads", 125 }, 126 }) 127 128 assert.Equal(t, Get().Store.URL("/xxxxxx.png"), "http://xxxxx.com/xxxx/file/xxxxxx.png") 129 130 testSetCfg(&Config{ 131 Store: Store{ 132 Prefix: "/file", 133 Path: "./uploads", 134 }, 135 }) 136 137 assert.Equal(t, Get().Store.URL("http://xxxxx.com/xxxx/file/xxxx.png"), "http://xxxxx.com/xxxx/file/xxxx.png") 138 } 139 140 func TestDatabase_ParamStr(t *testing.T) { 141 cfg := Database{ 142 Driver: DriverMysql, 143 Params: map[string]string{ 144 "parseTime": "true", 145 }, 146 } 147 assert.Equal(t, cfg.ParamStr(), "?charset=utf8mb4&parseTime=true") 148 } 149 150 func TestReadFromYaml(t *testing.T) { 151 cfg := ReadFromYaml("./config.yaml") 152 assert.Equal(t, cfg.Databases.GetDefault().Driver, "mssql") 153 assert.Equal(t, cfg.Domain, "localhost") 154 assert.Equal(t, cfg.UrlPrefix, "admin") 155 assert.Equal(t, cfg.Store.Path, "./uploads") 156 assert.Equal(t, cfg.IndexUrl, "/") 157 assert.Equal(t, cfg.Debug, true) 158 assert.Equal(t, cfg.OpenAdminApi, true) 159 assert.Equal(t, cfg.ColorScheme, "skin-black") 160 } 161 162 func TestReadFromINI(t *testing.T) { 163 cfg := ReadFromINI("./config.ini") 164 assert.Equal(t, cfg.Databases.GetDefault().Driver, "postgresql") 165 assert.Equal(t, cfg.Domain, "localhost") 166 assert.Equal(t, cfg.UrlPrefix, "admin") 167 assert.Equal(t, cfg.Store.Path, "./uploads") 168 assert.Equal(t, cfg.IndexUrl, "/") 169 assert.Equal(t, cfg.Debug, true) 170 assert.Equal(t, cfg.OpenAdminApi, true) 171 assert.Equal(t, cfg.ColorScheme, "skin-black") 172 } 173 174 func testSetCfg(cfg *Config) { 175 count = 0 176 Initialize(cfg) 177 } 178 179 func TestUpdate(t *testing.T) { 180 m := map[string]string{ 181 "access_assets_log_off": `true`, 182 "access_log_off": `false`, 183 "access_log_path": "", 184 "allow_del_operation_log": `false`, 185 "animation": `{"type":"fadeInUp","duration":0,"delay":0}`, 186 "animation_delay": `0.00`, 187 "animation_duration": `0`, 188 "animation_type": `fadeInUp`, 189 "app_id": `70rv3KwjwjXE`, 190 "asset_root_path": `./public/`, 191 "asset_url": "", 192 "auth_user_table": `goadmin_users`, 193 "bootstrap_file_path": `./../datamodel/bootstrap.go`, 194 "color_scheme": `skin-black`, 195 "custom_403_html": "", 196 "custom_404_html": "", 197 "custom_500_html": "", 198 "custom_foot_html": "", 199 "custom_head_html": ` <link rel="icon" type="image/png" sizes="32x32" href="//quick.go-admin.cn/official/assets/imgs/icons.ico/favicon-32x32.png">`, 200 "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":""}}`, 201 "debug": `true`, 202 "domain": "", 203 "env": `test`, 204 "error_log_off": `false`, 205 "error_log_path": "", 206 "exclude_theme_components": `null`, 207 "extra": "", 208 "file_upload_engine": `{"name":"local","config":null}`, 209 "footer_info": "", 210 "go_mod_file_path": "", 211 "hide_app_info_entrance": `false`, 212 "hide_config_center_entrance": `false`, 213 "hide_plugin_entrance": `false`, 214 "hide_tool_entrance": `false`, 215 "hide_visitor_user_center_entrance": `false`, 216 "index_url": `/`, 217 "info_log_off": `false`, 218 "info_log_path": "", 219 "language": `zh`, 220 "logger_encoder_caller": `full`, 221 "logger_encoder_caller_key": `caller`, 222 "logger_encoder_duration": `string`, 223 "logger_encoder_encoding": `console`, 224 "logger_encoder_level": `capitalColor`, 225 "logger_encoder_level_key": `level`, 226 "logger_encoder_message_key": `msg`, 227 "logger_encoder_name_key": `logger`, 228 "logger_encoder_stacktrace_key": `stacktrace`, 229 "logger_encoder_time": `iso8601`, 230 "logger_encoder_time_key": `ts`, 231 "logger_level": `0`, 232 "logger_rotate_compress": `false`, 233 "logger_rotate_max_age": `30`, 234 "logger_rotate_max_backups": `5`, 235 "logger_rotate_max_size": `10`, 236 "login_logo": "", 237 "login_title": `GoAdmin`, 238 "login_url": `/login`, 239 "logo": `<b>Go</b>Admin`, 240 "mini_logo": `<b>G</b>A`, 241 "no_limit_login_ip": `false`, 242 "open_admin_api": `false`, 243 "operation_log_off": `false`, 244 "plugin_file_path": `/go/src/github.com/kotovmak/go-admin/examples/gin/plugins.go`, 245 "session_life_time": `7200`, 246 "site_off": `false`, 247 "sql_log": `true`, 248 "store": `{"path":"./uploads","prefix":"uploads"}`, 249 "theme": `sword`, 250 "title": `GoAdmin`, 251 "url_prefix": `admin`, 252 } 253 c := &Config{} 254 c2 := &Config{} 255 if c.Update(m) == nil { 256 if c.Language != c2.Language || 257 c.Domain != c2.Domain || 258 c.Theme != c2.Theme || 259 c.Title != c2.Title || 260 c.Logo != c2.Logo || 261 c.MiniLogo != c2.MiniLogo || 262 c.Debug != c2.Debug || 263 c.SiteOff != c2.SiteOff || 264 c.AccessLogOff != c2.AccessLogOff || 265 c.InfoLogOff != c2.InfoLogOff || 266 c.ErrorLogOff != c2.ErrorLogOff || 267 c.AccessAssetsLogOff != c2.AccessAssetsLogOff || 268 c.InfoLogPath != c2.InfoLogPath || 269 c.ErrorLogPath != c2.ErrorLogPath || 270 c.AccessLogPath != c2.AccessLogPath || 271 c.SqlLog != c2.SqlLog || 272 c.Logger.Rotate.MaxSize != c2.Logger.Rotate.MaxSize || 273 c.Logger.Rotate.MaxBackups != c2.Logger.Rotate.MaxBackups || 274 c.Logger.Rotate.MaxAge != c2.Logger.Rotate.MaxAge || 275 c.Logger.Rotate.Compress != c2.Logger.Rotate.Compress || 276 c.Logger.Encoder.Encoding != c2.Logger.Encoder.Encoding || 277 c.Logger.Level != c2.Logger.Level || 278 c.Logger.Encoder.TimeKey != c2.Logger.Encoder.TimeKey || 279 c.Logger.Encoder.LevelKey != c2.Logger.Encoder.LevelKey || 280 c.Logger.Encoder.NameKey != c2.Logger.Encoder.NameKey || 281 c.Logger.Encoder.CallerKey != c2.Logger.Encoder.CallerKey || 282 c.Logger.Encoder.MessageKey != c2.Logger.Encoder.MessageKey || 283 c.Logger.Encoder.StacktraceKey != c2.Logger.Encoder.StacktraceKey || 284 c.Logger.Encoder.Level != c2.Logger.Encoder.Level || 285 c.Logger.Encoder.Time != c2.Logger.Encoder.Time || 286 c.Logger.Encoder.Duration != c2.Logger.Encoder.Duration || 287 c.Logger.Encoder.Caller != c2.Logger.Encoder.Caller || 288 c.ColorScheme != c2.ColorScheme || 289 c.SessionLifeTime != c2.SessionLifeTime || 290 c.CustomHeadHtml != c2.CustomHeadHtml || 291 c.CustomFootHtml != c2.CustomFootHtml || 292 c.Custom404HTML != c2.Custom404HTML || 293 c.Custom403HTML != c2.Custom403HTML || 294 c.Custom500HTML != c2.Custom500HTML || 295 c.BootstrapFilePath != c2.BootstrapFilePath || 296 c.GoModFilePath != c2.GoModFilePath || 297 c.FooterInfo != c2.FooterInfo || 298 c.LoginTitle != c2.LoginTitle || 299 c.AssetUrl != c2.AssetUrl || 300 c.LoginLogo != c2.LoginLogo || 301 c.NoLimitLoginIP != c2.NoLimitLoginIP || 302 c.AllowDelOperationLog != c2.AllowDelOperationLog || 303 c.OperationLogOff != c2.OperationLogOff || 304 c.HideConfigCenterEntrance != c2.HideConfigCenterEntrance || 305 c.HideAppInfoEntrance != c2.HideAppInfoEntrance || 306 c.HideToolEntrance != c2.HideToolEntrance || 307 c.HidePluginEntrance != c2.HidePluginEntrance || 308 c.FileUploadEngine.Name != c2.FileUploadEngine.Name || 309 c.Animation.Type != c2.Animation.Type || 310 c.Animation.Duration != c2.Animation.Duration || 311 c.Animation.Delay != c2.Animation.Delay || 312 !reflect.DeepEqual(c.Extra, c2.Extra) { 313 panic("c.Extra") 314 } 315 } 316 } 317 318 func TestToMap(t *testing.T) { 319 c := &Config{ 320 UrlPrefix: "/admin", 321 IndexUrl: "/", 322 MiniLogo: "<asdfadsf>", 323 Animation: PageAnimation{ 324 Type: "12313213", 325 }, 326 SessionLifeTime: 40, 327 ExcludeThemeComponents: []string{"asdfas", "sadfasf"}, 328 } 329 m := c.ToMap() 330 fmt.Println(m) 331 fmt.Println(m["prefix"], m["animation_type"], m["mini_logo"]) 332 333 arr := []string{ 334 "language", "databases", "domain", "url_prefix", "theme", "store", "title", "logo", "mini_logo", "index_url", 335 "site_off", "login_url", "debug", "env", "open_admin_api", "hide_visitor_user_center_entrance", 336 "info_log_path", "error_log_path", "access_log_path", "sql_log", "access_log_off", "info_log_off", "error_log_off", 337 "access_assets_log_off", 338 "logger_rotate_max_size", "logger_rotate_max_backups", "logger_rotate_max_age", "logger_rotate_compress", 339 "logger_encoder_time_key", "logger_encoder_level_key", "logger_encoder_name_key", "logger_encoder_caller_key", 340 "logger_encoder_message_key", "logger_encoder_stacktrace_key", "logger_encoder_level", "logger_encoder_time", 341 "logger_encoder_duration", "logger_encoder_caller", "logger_encoder_encoding", "logger_level", 342 "color_scheme", "session_life_time", "asset_url", "file_upload_engine", "custom_head_html", "custom_foot_html", 343 "custom_404_html", "custom_403_html", "custom_500_html", "bootstrap_file_path", "go_mod_file_path", "footer_info", 344 "app_id", "login_title", "login_logo", "auth_user_table", "exclude_theme_components", 345 "extra", 346 "animation_type", "animation_duration", "animation_delay", 347 "no_limit_login_ip", "allow_del_operation_log", "operation_log_off", 348 "hide_config_center_entrance", "hide_app_info_entrance", "hide_tool_entrance", "hide_plugin_entrance", 349 "asset_root_path", 350 } 351 352 for key := range m { 353 if !utils.InArray(arr, key) { 354 panic(key) 355 } 356 } 357 358 fmt.Println(len(arr), len(m)) 359 }