github.com/dschalla/mattermost-server@v4.8.1-rc1+incompatible/api4/system_test.go (about) 1 package api4 2 3 import ( 4 "net/http" 5 "strings" 6 "testing" 7 8 l4g "github.com/alecthomas/log4go" 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestGetPing(t *testing.T) { 14 th := Setup().InitBasic().InitSystemAdmin() 15 defer th.TearDown() 16 Client := th.Client 17 18 goRoutineHealthThreshold := *th.App.Config().ServiceSettings.GoroutineHealthThreshold 19 defer func() { 20 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = goRoutineHealthThreshold }) 21 }() 22 23 status, resp := Client.GetPing() 24 CheckNoError(t, resp) 25 if status != "OK" { 26 t.Fatal("should return OK") 27 } 28 29 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = 10 }) 30 status, resp = th.SystemAdminClient.GetPing() 31 CheckInternalErrorStatus(t, resp) 32 if status != "unhealthy" { 33 t.Fatal("should return unhealthy") 34 } 35 } 36 37 func TestGetConfig(t *testing.T) { 38 th := Setup().InitBasic().InitSystemAdmin() 39 defer th.TearDown() 40 Client := th.Client 41 42 _, resp := Client.GetConfig() 43 CheckForbiddenStatus(t, resp) 44 45 cfg, resp := th.SystemAdminClient.GetConfig() 46 CheckNoError(t, resp) 47 48 if len(cfg.TeamSettings.SiteName) == 0 { 49 t.Fatal() 50 } 51 52 if *cfg.LdapSettings.BindPassword != model.FAKE_SETTING && len(*cfg.LdapSettings.BindPassword) != 0 { 53 t.Fatal("did not sanitize properly") 54 } 55 if *cfg.FileSettings.PublicLinkSalt != model.FAKE_SETTING { 56 t.Fatal("did not sanitize properly") 57 } 58 if cfg.FileSettings.AmazonS3SecretAccessKey != model.FAKE_SETTING && len(cfg.FileSettings.AmazonS3SecretAccessKey) != 0 { 59 t.Fatal("did not sanitize properly") 60 } 61 if cfg.EmailSettings.InviteSalt != model.FAKE_SETTING { 62 t.Fatal("did not sanitize properly") 63 } 64 if cfg.EmailSettings.SMTPPassword != model.FAKE_SETTING && len(cfg.EmailSettings.SMTPPassword) != 0 { 65 t.Fatal("did not sanitize properly") 66 } 67 if cfg.GitLabSettings.Secret != model.FAKE_SETTING && len(cfg.GitLabSettings.Secret) != 0 { 68 t.Fatal("did not sanitize properly") 69 } 70 if *cfg.SqlSettings.DataSource != model.FAKE_SETTING { 71 t.Fatal("did not sanitize properly") 72 } 73 if cfg.SqlSettings.AtRestEncryptKey != model.FAKE_SETTING { 74 t.Fatal("did not sanitize properly") 75 } 76 if !strings.Contains(strings.Join(cfg.SqlSettings.DataSourceReplicas, " "), model.FAKE_SETTING) && len(cfg.SqlSettings.DataSourceReplicas) != 0 { 77 t.Fatal("did not sanitize properly") 78 } 79 if !strings.Contains(strings.Join(cfg.SqlSettings.DataSourceSearchReplicas, " "), model.FAKE_SETTING) && len(cfg.SqlSettings.DataSourceSearchReplicas) != 0 { 80 t.Fatal("did not sanitize properly") 81 } 82 } 83 84 func TestReloadConfig(t *testing.T) { 85 th := Setup().InitBasic().InitSystemAdmin() 86 defer th.TearDown() 87 Client := th.Client 88 89 flag, resp := Client.ReloadConfig() 90 CheckForbiddenStatus(t, resp) 91 if flag { 92 t.Fatal("should not Reload the config due no permission.") 93 } 94 95 flag, resp = th.SystemAdminClient.ReloadConfig() 96 CheckNoError(t, resp) 97 if !flag { 98 t.Fatal("should Reload the config") 99 } 100 101 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.MaxUsersPerTeam = 50 }) 102 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true }) 103 } 104 105 func TestUpdateConfig(t *testing.T) { 106 th := Setup().InitBasic().InitSystemAdmin() 107 defer th.TearDown() 108 Client := th.Client 109 110 cfg, resp := th.SystemAdminClient.GetConfig() 111 CheckNoError(t, resp) 112 113 _, resp = Client.UpdateConfig(cfg) 114 CheckForbiddenStatus(t, resp) 115 116 SiteName := th.App.Config().TeamSettings.SiteName 117 118 cfg.TeamSettings.SiteName = "MyFancyName" 119 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 120 CheckNoError(t, resp) 121 122 if len(cfg.TeamSettings.SiteName) == 0 { 123 t.Fatal() 124 } else { 125 if cfg.TeamSettings.SiteName != "MyFancyName" { 126 t.Log("It should update the SiteName") 127 t.Fatal() 128 } 129 } 130 131 //Revert the change 132 cfg.TeamSettings.SiteName = SiteName 133 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 134 CheckNoError(t, resp) 135 136 if len(cfg.TeamSettings.SiteName) == 0 { 137 t.Fatal() 138 } else { 139 if cfg.TeamSettings.SiteName != SiteName { 140 t.Log("It should update the SiteName") 141 t.Fatal() 142 } 143 } 144 145 t.Run("Should not be able to modify PluginSettings.EnableUploads", func(t *testing.T) { 146 oldEnableUploads := *th.App.GetConfig().PluginSettings.EnableUploads 147 *cfg.PluginSettings.EnableUploads = !oldEnableUploads 148 149 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 150 CheckNoError(t, resp) 151 assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads) 152 assert.Equal(t, oldEnableUploads, *th.App.GetConfig().PluginSettings.EnableUploads) 153 154 cfg.PluginSettings.EnableUploads = nil 155 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 156 CheckNoError(t, resp) 157 assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads) 158 assert.Equal(t, oldEnableUploads, *th.App.GetConfig().PluginSettings.EnableUploads) 159 }) 160 } 161 162 func TestGetOldClientConfig(t *testing.T) { 163 th := Setup().InitBasic().InitSystemAdmin() 164 defer th.TearDown() 165 Client := th.Client 166 167 config, resp := Client.GetOldClientConfig("") 168 CheckNoError(t, resp) 169 170 if len(config["Version"]) == 0 { 171 t.Fatal("config not returned correctly") 172 } 173 174 Client.Logout() 175 176 _, resp = Client.GetOldClientConfig("") 177 CheckNoError(t, resp) 178 179 if _, err := Client.DoApiGet("/config/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { 180 t.Fatal("should have errored with 501") 181 } 182 183 if _, err := Client.DoApiGet("/config/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { 184 t.Fatal("should have errored with 400") 185 } 186 } 187 188 func TestGetOldClientLicense(t *testing.T) { 189 th := Setup().InitBasic().InitSystemAdmin() 190 defer th.TearDown() 191 Client := th.Client 192 193 license, resp := Client.GetOldClientLicense("") 194 CheckNoError(t, resp) 195 196 if len(license["IsLicensed"]) == 0 { 197 t.Fatal("license not returned correctly") 198 } 199 200 Client.Logout() 201 202 _, resp = Client.GetOldClientLicense("") 203 CheckNoError(t, resp) 204 205 if _, err := Client.DoApiGet("/license/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { 206 t.Fatal("should have errored with 501") 207 } 208 209 if _, err := Client.DoApiGet("/license/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { 210 t.Fatal("should have errored with 400") 211 } 212 213 license, resp = th.SystemAdminClient.GetOldClientLicense("") 214 CheckNoError(t, resp) 215 216 if len(license["IsLicensed"]) == 0 { 217 t.Fatal("license not returned correctly") 218 } 219 } 220 221 func TestGetAudits(t *testing.T) { 222 th := Setup().InitBasic().InitSystemAdmin() 223 defer th.TearDown() 224 Client := th.Client 225 226 audits, resp := th.SystemAdminClient.GetAudits(0, 100, "") 227 CheckNoError(t, resp) 228 229 if len(audits) == 0 { 230 t.Fatal("should not be empty") 231 } 232 233 audits, resp = th.SystemAdminClient.GetAudits(0, 1, "") 234 CheckNoError(t, resp) 235 236 if len(audits) != 1 { 237 t.Fatal("should only be 1") 238 } 239 240 audits, resp = th.SystemAdminClient.GetAudits(1, 1, "") 241 CheckNoError(t, resp) 242 243 if len(audits) != 1 { 244 t.Fatal("should only be 1") 245 } 246 247 _, resp = th.SystemAdminClient.GetAudits(-1, -1, "") 248 CheckNoError(t, resp) 249 250 _, resp = Client.GetAudits(0, 100, "") 251 CheckForbiddenStatus(t, resp) 252 253 Client.Logout() 254 _, resp = Client.GetAudits(0, 100, "") 255 CheckUnauthorizedStatus(t, resp) 256 } 257 258 func TestEmailTest(t *testing.T) { 259 th := Setup().InitBasic().InitSystemAdmin() 260 defer th.TearDown() 261 Client := th.Client 262 263 SendEmailNotifications := th.App.Config().EmailSettings.SendEmailNotifications 264 SMTPServer := th.App.Config().EmailSettings.SMTPServer 265 SMTPPort := th.App.Config().EmailSettings.SMTPPort 266 FeedbackEmail := th.App.Config().EmailSettings.FeedbackEmail 267 defer func() { 268 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SendEmailNotifications = SendEmailNotifications }) 269 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPServer = SMTPServer }) 270 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPPort = SMTPPort }) 271 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.FeedbackEmail = FeedbackEmail }) 272 }() 273 274 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SendEmailNotifications = false }) 275 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPServer = "" }) 276 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPPort = "" }) 277 th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.FeedbackEmail = "" }) 278 279 _, resp := Client.TestEmail() 280 CheckForbiddenStatus(t, resp) 281 282 _, resp = th.SystemAdminClient.TestEmail() 283 CheckErrorMessage(t, resp, "api.admin.test_email.missing_server") 284 CheckBadRequestStatus(t, resp) 285 } 286 287 func TestDatabaseRecycle(t *testing.T) { 288 th := Setup().InitBasic().InitSystemAdmin() 289 defer th.TearDown() 290 Client := th.Client 291 292 _, resp := Client.DatabaseRecycle() 293 CheckForbiddenStatus(t, resp) 294 295 _, resp = th.SystemAdminClient.DatabaseRecycle() 296 CheckNoError(t, resp) 297 } 298 299 func TestInvalidateCaches(t *testing.T) { 300 th := Setup().InitBasic().InitSystemAdmin() 301 defer th.TearDown() 302 Client := th.Client 303 304 flag, resp := Client.InvalidateCaches() 305 CheckForbiddenStatus(t, resp) 306 if flag { 307 t.Fatal("should not clean the cache due no permission.") 308 } 309 310 flag, resp = th.SystemAdminClient.InvalidateCaches() 311 CheckNoError(t, resp) 312 if !flag { 313 t.Fatal("should clean the cache") 314 } 315 } 316 317 func TestGetLogs(t *testing.T) { 318 th := Setup().InitBasic().InitSystemAdmin() 319 defer th.TearDown() 320 Client := th.Client 321 322 for i := 0; i < 20; i++ { 323 l4g.Info(i) 324 } 325 326 logs, resp := th.SystemAdminClient.GetLogs(0, 10) 327 CheckNoError(t, resp) 328 329 if len(logs) != 10 { 330 t.Log(len(logs)) 331 t.Fatal("wrong length") 332 } 333 334 logs, resp = th.SystemAdminClient.GetLogs(1, 10) 335 CheckNoError(t, resp) 336 337 if len(logs) != 10 { 338 t.Log(len(logs)) 339 t.Fatal("wrong length") 340 } 341 342 logs, resp = th.SystemAdminClient.GetLogs(-1, -1) 343 CheckNoError(t, resp) 344 345 if len(logs) == 0 { 346 t.Fatal("should not be empty") 347 } 348 349 _, resp = Client.GetLogs(0, 10) 350 CheckForbiddenStatus(t, resp) 351 352 Client.Logout() 353 _, resp = Client.GetLogs(0, 10) 354 CheckUnauthorizedStatus(t, resp) 355 } 356 357 func TestPostLog(t *testing.T) { 358 th := Setup().InitBasic().InitSystemAdmin() 359 defer th.TearDown() 360 Client := th.Client 361 362 enableDev := *th.App.Config().ServiceSettings.EnableDeveloper 363 defer func() { 364 *th.App.Config().ServiceSettings.EnableDeveloper = enableDev 365 }() 366 *th.App.Config().ServiceSettings.EnableDeveloper = true 367 368 message := make(map[string]string) 369 message["level"] = "ERROR" 370 message["message"] = "this is a test" 371 372 _, resp := Client.PostLog(message) 373 CheckNoError(t, resp) 374 375 Client.Logout() 376 377 _, resp = Client.PostLog(message) 378 CheckNoError(t, resp) 379 380 *th.App.Config().ServiceSettings.EnableDeveloper = false 381 382 _, resp = Client.PostLog(message) 383 CheckForbiddenStatus(t, resp) 384 385 logMessage, resp := th.SystemAdminClient.PostLog(message) 386 CheckNoError(t, resp) 387 if len(logMessage) == 0 { 388 t.Fatal("should return the log message") 389 } 390 } 391 392 func TestUploadLicenseFile(t *testing.T) { 393 th := Setup().InitBasic().InitSystemAdmin() 394 defer th.TearDown() 395 Client := th.Client 396 397 ok, resp := Client.UploadLicenseFile([]byte{}) 398 CheckForbiddenStatus(t, resp) 399 if ok { 400 t.Fatal("should fail") 401 } 402 403 ok, resp = th.SystemAdminClient.UploadLicenseFile([]byte{}) 404 CheckBadRequestStatus(t, resp) 405 if ok { 406 t.Fatal("should fail") 407 } 408 } 409 410 func TestRemoveLicenseFile(t *testing.T) { 411 th := Setup().InitBasic().InitSystemAdmin() 412 defer th.TearDown() 413 Client := th.Client 414 415 ok, resp := Client.RemoveLicenseFile() 416 CheckForbiddenStatus(t, resp) 417 if ok { 418 t.Fatal("should fail") 419 } 420 421 ok, resp = th.SystemAdminClient.RemoveLicenseFile() 422 CheckNoError(t, resp) 423 if !ok { 424 t.Fatal("should pass") 425 } 426 } 427 428 func TestGetAnalyticsOld(t *testing.T) { 429 th := Setup().InitBasic().InitSystemAdmin() 430 defer th.TearDown() 431 Client := th.Client 432 433 rows, resp := Client.GetAnalyticsOld("", "") 434 CheckForbiddenStatus(t, resp) 435 if rows != nil { 436 t.Fatal("should be nil") 437 } 438 439 rows, resp = th.SystemAdminClient.GetAnalyticsOld("", "") 440 CheckNoError(t, resp) 441 442 found := false 443 for _, row := range rows { 444 if row.Name == "unique_user_count" { 445 found = true 446 } 447 } 448 449 if !found { 450 t.Fatal("should return unique user count") 451 } 452 453 _, resp = th.SystemAdminClient.GetAnalyticsOld("post_counts_day", "") 454 CheckNoError(t, resp) 455 456 _, resp = th.SystemAdminClient.GetAnalyticsOld("user_counts_with_posts_day", "") 457 CheckNoError(t, resp) 458 459 _, resp = th.SystemAdminClient.GetAnalyticsOld("extra_counts", "") 460 CheckNoError(t, resp) 461 462 _, resp = th.SystemAdminClient.GetAnalyticsOld("", th.BasicTeam.Id) 463 CheckNoError(t, resp) 464 465 Client.Logout() 466 _, resp = Client.GetAnalyticsOld("", th.BasicTeam.Id) 467 CheckUnauthorizedStatus(t, resp) 468 }