github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/api4/system_test.go (about) 1 package api4 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "strings" 8 "testing" 9 10 "github.com/mattermost/mattermost-server/mlog" 11 "github.com/mattermost/mattermost-server/model" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestGetPing(t *testing.T) { 16 th := Setup().InitBasic().InitSystemAdmin() 17 defer th.TearDown() 18 Client := th.Client 19 20 goRoutineHealthThreshold := *th.App.Config().ServiceSettings.GoroutineHealthThreshold 21 defer func() { 22 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = goRoutineHealthThreshold }) 23 }() 24 25 status, resp := Client.GetPing() 26 CheckNoError(t, resp) 27 if status != "OK" { 28 t.Fatal("should return OK") 29 } 30 31 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = 10 }) 32 status, resp = th.SystemAdminClient.GetPing() 33 CheckInternalErrorStatus(t, resp) 34 if status != "unhealthy" { 35 t.Fatal("should return unhealthy") 36 } 37 } 38 39 func TestGetConfig(t *testing.T) { 40 th := Setup().InitBasic().InitSystemAdmin() 41 defer th.TearDown() 42 Client := th.Client 43 44 _, resp := Client.GetConfig() 45 CheckForbiddenStatus(t, resp) 46 47 cfg, resp := th.SystemAdminClient.GetConfig() 48 CheckNoError(t, resp) 49 50 if len(cfg.TeamSettings.SiteName) == 0 { 51 t.Fatal() 52 } 53 54 if *cfg.LdapSettings.BindPassword != model.FAKE_SETTING && len(*cfg.LdapSettings.BindPassword) != 0 { 55 t.Fatal("did not sanitize properly") 56 } 57 if *cfg.FileSettings.PublicLinkSalt != model.FAKE_SETTING { 58 t.Fatal("did not sanitize properly") 59 } 60 if cfg.FileSettings.AmazonS3SecretAccessKey != model.FAKE_SETTING && len(cfg.FileSettings.AmazonS3SecretAccessKey) != 0 { 61 t.Fatal("did not sanitize properly") 62 } 63 if cfg.EmailSettings.InviteSalt != model.FAKE_SETTING { 64 t.Fatal("did not sanitize properly") 65 } 66 if cfg.EmailSettings.SMTPPassword != model.FAKE_SETTING && len(cfg.EmailSettings.SMTPPassword) != 0 { 67 t.Fatal("did not sanitize properly") 68 } 69 if cfg.GitLabSettings.Secret != model.FAKE_SETTING && len(cfg.GitLabSettings.Secret) != 0 { 70 t.Fatal("did not sanitize properly") 71 } 72 if *cfg.SqlSettings.DataSource != model.FAKE_SETTING { 73 t.Fatal("did not sanitize properly") 74 } 75 if cfg.SqlSettings.AtRestEncryptKey != model.FAKE_SETTING { 76 t.Fatal("did not sanitize properly") 77 } 78 if !strings.Contains(strings.Join(cfg.SqlSettings.DataSourceReplicas, " "), model.FAKE_SETTING) && len(cfg.SqlSettings.DataSourceReplicas) != 0 { 79 t.Fatal("did not sanitize properly") 80 } 81 if !strings.Contains(strings.Join(cfg.SqlSettings.DataSourceSearchReplicas, " "), model.FAKE_SETTING) && len(cfg.SqlSettings.DataSourceSearchReplicas) != 0 { 82 t.Fatal("did not sanitize properly") 83 } 84 } 85 86 func TestReloadConfig(t *testing.T) { 87 th := Setup().InitBasic().InitSystemAdmin() 88 defer th.TearDown() 89 Client := th.Client 90 91 flag, resp := Client.ReloadConfig() 92 CheckForbiddenStatus(t, resp) 93 if flag { 94 t.Fatal("should not Reload the config due no permission.") 95 } 96 97 flag, resp = th.SystemAdminClient.ReloadConfig() 98 CheckNoError(t, resp) 99 if !flag { 100 t.Fatal("should Reload the config") 101 } 102 103 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.MaxUsersPerTeam = 50 }) 104 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true }) 105 } 106 107 func TestUpdateConfig(t *testing.T) { 108 th := Setup().InitBasic().InitSystemAdmin() 109 defer th.TearDown() 110 Client := th.Client 111 112 cfg, resp := th.SystemAdminClient.GetConfig() 113 CheckNoError(t, resp) 114 115 _, resp = Client.UpdateConfig(cfg) 116 CheckForbiddenStatus(t, resp) 117 118 SiteName := th.App.Config().TeamSettings.SiteName 119 120 cfg.TeamSettings.SiteName = "MyFancyName" 121 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 122 CheckNoError(t, resp) 123 124 if len(cfg.TeamSettings.SiteName) == 0 { 125 t.Fatal() 126 } else { 127 if cfg.TeamSettings.SiteName != "MyFancyName" { 128 t.Log("It should update the SiteName") 129 t.Fatal() 130 } 131 } 132 133 //Revert the change 134 cfg.TeamSettings.SiteName = SiteName 135 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 136 CheckNoError(t, resp) 137 138 if len(cfg.TeamSettings.SiteName) == 0 { 139 t.Fatal() 140 } else { 141 if cfg.TeamSettings.SiteName != SiteName { 142 t.Log("It should update the SiteName") 143 t.Fatal() 144 } 145 } 146 147 t.Run("Should not be able to modify PluginSettings.EnableUploads", func(t *testing.T) { 148 oldEnableUploads := *th.App.GetConfig().PluginSettings.EnableUploads 149 *cfg.PluginSettings.EnableUploads = !oldEnableUploads 150 151 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 152 CheckNoError(t, resp) 153 assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads) 154 assert.Equal(t, oldEnableUploads, *th.App.GetConfig().PluginSettings.EnableUploads) 155 156 cfg.PluginSettings.EnableUploads = nil 157 cfg, resp = th.SystemAdminClient.UpdateConfig(cfg) 158 CheckNoError(t, resp) 159 assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads) 160 assert.Equal(t, oldEnableUploads, *th.App.GetConfig().PluginSettings.EnableUploads) 161 }) 162 } 163 164 func TestGetEnvironmentConfig(t *testing.T) { 165 os.Setenv("MM_SERVICESETTINGS_SITEURL", "http://example.mattermost.com") 166 os.Setenv("MM_SERVICESETTINGS_ENABLECUSTOMEMOJI", "true") 167 defer os.Unsetenv("MM_SERVICESETTINGS_SITEURL") 168 169 th := Setup().InitBasic().InitSystemAdmin() 170 defer th.TearDown() 171 172 t.Run("as system admin", func(t *testing.T) { 173 SystemAdminClient := th.SystemAdminClient 174 175 envConfig, resp := SystemAdminClient.GetEnvironmentConfig() 176 CheckNoError(t, resp) 177 178 if serviceSettings, ok := envConfig["ServiceSettings"]; !ok { 179 t.Fatal("should've returned ServiceSettings") 180 } else if serviceSettingsAsMap, ok := serviceSettings.(map[string]interface{}); !ok { 181 t.Fatal("should've returned ServiceSettings as a map") 182 } else { 183 if siteURL, ok := serviceSettingsAsMap["SiteURL"]; !ok { 184 t.Fatal("should've returned ServiceSettings.SiteURL") 185 } else if siteURLAsBool, ok := siteURL.(bool); !ok { 186 t.Fatal("should've returned ServiceSettings.SiteURL as a boolean") 187 } else if !siteURLAsBool { 188 t.Fatal("should've returned ServiceSettings.SiteURL as true") 189 } 190 191 if enableCustomEmoji, ok := serviceSettingsAsMap["EnableCustomEmoji"]; !ok { 192 t.Fatal("should've returned ServiceSettings.EnableCustomEmoji") 193 } else if enableCustomEmojiAsBool, ok := enableCustomEmoji.(bool); !ok { 194 t.Fatal("should've returned ServiceSettings.EnableCustomEmoji as a boolean") 195 } else if !enableCustomEmojiAsBool { 196 t.Fatal("should've returned ServiceSettings.EnableCustomEmoji as true") 197 } 198 } 199 200 if _, ok := envConfig["TeamSettings"]; ok { 201 t.Fatal("should not have returned TeamSettings") 202 } 203 }) 204 205 t.Run("as team admin", func(t *testing.T) { 206 TeamAdminClient := th.CreateClient() 207 th.LoginTeamAdminWithClient(TeamAdminClient) 208 209 _, resp := TeamAdminClient.GetEnvironmentConfig() 210 CheckForbiddenStatus(t, resp) 211 }) 212 213 t.Run("as regular user", func(t *testing.T) { 214 Client := th.Client 215 216 _, resp := Client.GetEnvironmentConfig() 217 CheckForbiddenStatus(t, resp) 218 }) 219 220 t.Run("as not-regular user", func(t *testing.T) { 221 Client := th.CreateClient() 222 223 _, resp := Client.GetEnvironmentConfig() 224 CheckUnauthorizedStatus(t, resp) 225 }) 226 } 227 228 func TestGetOldClientConfig(t *testing.T) { 229 th := Setup().InitBasic().InitSystemAdmin() 230 defer th.TearDown() 231 Client := th.Client 232 233 config, resp := Client.GetOldClientConfig("") 234 CheckNoError(t, resp) 235 236 if len(config["Version"]) == 0 { 237 t.Fatal("config not returned correctly") 238 } 239 240 Client.Logout() 241 242 _, resp = Client.GetOldClientConfig("") 243 CheckNoError(t, resp) 244 245 if _, err := Client.DoApiGet("/config/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { 246 t.Fatal("should have errored with 501") 247 } 248 249 if _, err := Client.DoApiGet("/config/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { 250 t.Fatal("should have errored with 400") 251 } 252 } 253 254 func TestGetOldClientLicense(t *testing.T) { 255 th := Setup().InitBasic().InitSystemAdmin() 256 defer th.TearDown() 257 Client := th.Client 258 259 license, resp := Client.GetOldClientLicense("") 260 CheckNoError(t, resp) 261 262 if len(license["IsLicensed"]) == 0 { 263 t.Fatal("license not returned correctly") 264 } 265 266 Client.Logout() 267 268 _, resp = Client.GetOldClientLicense("") 269 CheckNoError(t, resp) 270 271 if _, err := Client.DoApiGet("/license/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { 272 t.Fatal("should have errored with 501") 273 } 274 275 if _, err := Client.DoApiGet("/license/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { 276 t.Fatal("should have errored with 400") 277 } 278 279 license, resp = th.SystemAdminClient.GetOldClientLicense("") 280 CheckNoError(t, resp) 281 282 if len(license["IsLicensed"]) == 0 { 283 t.Fatal("license not returned correctly") 284 } 285 } 286 287 func TestGetAudits(t *testing.T) { 288 th := Setup().InitBasic().InitSystemAdmin() 289 defer th.TearDown() 290 Client := th.Client 291 292 audits, resp := th.SystemAdminClient.GetAudits(0, 100, "") 293 CheckNoError(t, resp) 294 295 if len(audits) == 0 { 296 t.Fatal("should not be empty") 297 } 298 299 audits, resp = th.SystemAdminClient.GetAudits(0, 1, "") 300 CheckNoError(t, resp) 301 302 if len(audits) != 1 { 303 t.Fatal("should only be 1") 304 } 305 306 audits, resp = th.SystemAdminClient.GetAudits(1, 1, "") 307 CheckNoError(t, resp) 308 309 if len(audits) != 1 { 310 t.Fatal("should only be 1") 311 } 312 313 _, resp = th.SystemAdminClient.GetAudits(-1, -1, "") 314 CheckNoError(t, resp) 315 316 _, resp = Client.GetAudits(0, 100, "") 317 CheckForbiddenStatus(t, resp) 318 319 Client.Logout() 320 _, resp = Client.GetAudits(0, 100, "") 321 CheckUnauthorizedStatus(t, resp) 322 } 323 324 func TestEmailTest(t *testing.T) { 325 th := Setup().InitBasic().InitSystemAdmin() 326 defer th.TearDown() 327 Client := th.Client 328 329 config := model.Config{ 330 EmailSettings: model.EmailSettings{ 331 SMTPServer: "", 332 SMTPPort: "", 333 }, 334 } 335 336 _, resp := Client.TestEmail(&config) 337 CheckForbiddenStatus(t, resp) 338 339 _, resp = th.SystemAdminClient.TestEmail(&config) 340 CheckErrorMessage(t, resp, "api.admin.test_email.missing_server") 341 CheckBadRequestStatus(t, resp) 342 343 inbucket_host := os.Getenv("CI_HOST") 344 if inbucket_host == "" { 345 inbucket_host = "dockerhost" 346 } 347 348 inbucket_port := os.Getenv("CI_INBUCKET_PORT") 349 if inbucket_port == "" { 350 inbucket_port = "9000" 351 } 352 353 config.EmailSettings.SMTPServer = inbucket_host 354 config.EmailSettings.SMTPPort = inbucket_port 355 _, resp = th.SystemAdminClient.TestEmail(&config) 356 CheckOKStatus(t, resp) 357 } 358 359 func TestDatabaseRecycle(t *testing.T) { 360 th := Setup().InitBasic().InitSystemAdmin() 361 defer th.TearDown() 362 Client := th.Client 363 364 _, resp := Client.DatabaseRecycle() 365 CheckForbiddenStatus(t, resp) 366 367 _, resp = th.SystemAdminClient.DatabaseRecycle() 368 CheckNoError(t, resp) 369 } 370 371 func TestInvalidateCaches(t *testing.T) { 372 th := Setup().InitBasic().InitSystemAdmin() 373 defer th.TearDown() 374 Client := th.Client 375 376 flag, resp := Client.InvalidateCaches() 377 CheckForbiddenStatus(t, resp) 378 if flag { 379 t.Fatal("should not clean the cache due no permission.") 380 } 381 382 flag, resp = th.SystemAdminClient.InvalidateCaches() 383 CheckNoError(t, resp) 384 if !flag { 385 t.Fatal("should clean the cache") 386 } 387 } 388 389 func TestGetLogs(t *testing.T) { 390 th := Setup().InitBasic().InitSystemAdmin() 391 defer th.TearDown() 392 Client := th.Client 393 394 for i := 0; i < 20; i++ { 395 mlog.Info(fmt.Sprint(i)) 396 } 397 398 logs, resp := th.SystemAdminClient.GetLogs(0, 10) 399 CheckNoError(t, resp) 400 401 if len(logs) != 10 { 402 t.Log(len(logs)) 403 t.Fatal("wrong length") 404 } 405 406 logs, resp = th.SystemAdminClient.GetLogs(1, 10) 407 CheckNoError(t, resp) 408 409 if len(logs) != 10 { 410 t.Log(len(logs)) 411 t.Fatal("wrong length") 412 } 413 414 logs, resp = th.SystemAdminClient.GetLogs(-1, -1) 415 CheckNoError(t, resp) 416 417 if len(logs) == 0 { 418 t.Fatal("should not be empty") 419 } 420 421 _, resp = Client.GetLogs(0, 10) 422 CheckForbiddenStatus(t, resp) 423 424 Client.Logout() 425 _, resp = Client.GetLogs(0, 10) 426 CheckUnauthorizedStatus(t, resp) 427 } 428 429 func TestPostLog(t *testing.T) { 430 th := Setup().InitBasic().InitSystemAdmin() 431 defer th.TearDown() 432 Client := th.Client 433 434 enableDev := *th.App.Config().ServiceSettings.EnableDeveloper 435 defer func() { 436 *th.App.Config().ServiceSettings.EnableDeveloper = enableDev 437 }() 438 *th.App.Config().ServiceSettings.EnableDeveloper = true 439 440 message := make(map[string]string) 441 message["level"] = "ERROR" 442 message["message"] = "this is a test" 443 444 _, resp := Client.PostLog(message) 445 CheckNoError(t, resp) 446 447 Client.Logout() 448 449 _, resp = Client.PostLog(message) 450 CheckNoError(t, resp) 451 452 *th.App.Config().ServiceSettings.EnableDeveloper = false 453 454 _, resp = Client.PostLog(message) 455 CheckForbiddenStatus(t, resp) 456 457 logMessage, resp := th.SystemAdminClient.PostLog(message) 458 CheckNoError(t, resp) 459 if len(logMessage) == 0 { 460 t.Fatal("should return the log message") 461 } 462 } 463 464 func TestUploadLicenseFile(t *testing.T) { 465 th := Setup().InitBasic().InitSystemAdmin() 466 defer th.TearDown() 467 Client := th.Client 468 469 ok, resp := Client.UploadLicenseFile([]byte{}) 470 CheckForbiddenStatus(t, resp) 471 if ok { 472 t.Fatal("should fail") 473 } 474 475 ok, resp = th.SystemAdminClient.UploadLicenseFile([]byte{}) 476 CheckBadRequestStatus(t, resp) 477 if ok { 478 t.Fatal("should fail") 479 } 480 } 481 482 func TestRemoveLicenseFile(t *testing.T) { 483 th := Setup().InitBasic().InitSystemAdmin() 484 defer th.TearDown() 485 Client := th.Client 486 487 ok, resp := Client.RemoveLicenseFile() 488 CheckForbiddenStatus(t, resp) 489 if ok { 490 t.Fatal("should fail") 491 } 492 493 ok, resp = th.SystemAdminClient.RemoveLicenseFile() 494 CheckNoError(t, resp) 495 if !ok { 496 t.Fatal("should pass") 497 } 498 } 499 500 func TestGetAnalyticsOld(t *testing.T) { 501 th := Setup().InitBasic().InitSystemAdmin() 502 defer th.TearDown() 503 Client := th.Client 504 505 rows, resp := Client.GetAnalyticsOld("", "") 506 CheckForbiddenStatus(t, resp) 507 if rows != nil { 508 t.Fatal("should be nil") 509 } 510 511 rows, resp = th.SystemAdminClient.GetAnalyticsOld("", "") 512 CheckNoError(t, resp) 513 514 found := false 515 found2 := false 516 for _, row := range rows { 517 if row.Name == "unique_user_count" { 518 found = true 519 } else if row.Name == "inactive_user_count" { 520 found2 = true 521 assert.True(t, row.Value >= 0) 522 } 523 } 524 525 assert.True(t, found, "should return unique user count") 526 assert.True(t, found2, "should return inactive user count") 527 528 _, resp = th.SystemAdminClient.GetAnalyticsOld("post_counts_day", "") 529 CheckNoError(t, resp) 530 531 _, resp = th.SystemAdminClient.GetAnalyticsOld("user_counts_with_posts_day", "") 532 CheckNoError(t, resp) 533 534 _, resp = th.SystemAdminClient.GetAnalyticsOld("extra_counts", "") 535 CheckNoError(t, resp) 536 537 rows, resp = th.SystemAdminClient.GetAnalyticsOld("", th.BasicTeam.Id) 538 CheckNoError(t, resp) 539 540 for _, row := range rows { 541 if row.Name == "inactive_user_count" { 542 assert.Equal(t, float64(-1), row.Value, "inactive user count should be -1 when team specified") 543 } 544 } 545 546 rows2, resp2 := th.SystemAdminClient.GetAnalyticsOld("standard", "") 547 CheckNoError(t, resp2) 548 assert.Equal(t, "total_websocket_connections", rows2[5].Name) 549 assert.Equal(t, float64(0), rows2[5].Value) 550 551 WebSocketClient, err := th.CreateWebSocketClient() 552 if err != nil { 553 t.Fatal(err) 554 } 555 556 rows2, resp2 = th.SystemAdminClient.GetAnalyticsOld("standard", "") 557 CheckNoError(t, resp2) 558 assert.Equal(t, "total_websocket_connections", rows2[5].Name) 559 assert.Equal(t, float64(1), rows2[5].Value) 560 561 WebSocketClient.Close() 562 563 rows2, resp2 = th.SystemAdminClient.GetAnalyticsOld("standard", "") 564 CheckNoError(t, resp2) 565 assert.Equal(t, "total_websocket_connections", rows2[5].Name) 566 assert.Equal(t, float64(0), rows2[5].Value) 567 568 Client.Logout() 569 _, resp = Client.GetAnalyticsOld("", th.BasicTeam.Id) 570 CheckUnauthorizedStatus(t, resp) 571 } 572 573 func TestS3TestConnection(t *testing.T) { 574 th := Setup().InitBasic().InitSystemAdmin() 575 defer th.TearDown() 576 Client := th.Client 577 578 s3Host := os.Getenv("CI_HOST") 579 if s3Host == "" { 580 s3Host = "dockerhost" 581 } 582 583 s3Port := os.Getenv("CI_MINIO_PORT") 584 if s3Port == "" { 585 s3Port = "9001" 586 } 587 588 s3Endpoint := fmt.Sprintf("%s:%s", s3Host, s3Port) 589 config := model.Config{ 590 FileSettings: model.FileSettings{ 591 DriverName: model.NewString(model.IMAGE_DRIVER_S3), 592 AmazonS3AccessKeyId: model.MINIO_ACCESS_KEY, 593 AmazonS3SecretAccessKey: model.MINIO_SECRET_KEY, 594 AmazonS3Bucket: "", 595 AmazonS3Endpoint: s3Endpoint, 596 AmazonS3SSL: model.NewBool(false), 597 }, 598 } 599 600 _, resp := Client.TestS3Connection(&config) 601 CheckForbiddenStatus(t, resp) 602 603 _, resp = th.SystemAdminClient.TestS3Connection(&config) 604 CheckBadRequestStatus(t, resp) 605 if resp.Error.Message != "S3 Bucket is required" { 606 t.Fatal("should return error - missing s3 bucket") 607 } 608 609 config.FileSettings.AmazonS3Bucket = model.MINIO_BUCKET 610 config.FileSettings.AmazonS3Region = "us-east-1" 611 _, resp = th.SystemAdminClient.TestS3Connection(&config) 612 CheckOKStatus(t, resp) 613 614 config.FileSettings.AmazonS3Region = "" 615 _, resp = th.SystemAdminClient.TestS3Connection(&config) 616 CheckOKStatus(t, resp) 617 618 config.FileSettings.AmazonS3Bucket = "Wrong_bucket" 619 _, resp = th.SystemAdminClient.TestS3Connection(&config) 620 CheckInternalErrorStatus(t, resp) 621 if resp.Error.Message != "Unable to create bucket" { 622 t.Fatal("should return error ") 623 } 624 625 config.FileSettings.AmazonS3Bucket = "shouldcreatenewbucket" 626 _, resp = th.SystemAdminClient.TestS3Connection(&config) 627 CheckOKStatus(t, resp) 628 629 } 630 631 func TestSupportedTimezones(t *testing.T) { 632 th := Setup().InitBasic() 633 defer th.TearDown() 634 Client := th.Client 635 636 supportedTimezonesFromConfig := th.App.Timezones() 637 supportedTimezones, resp := Client.GetSupportedTimezone() 638 639 CheckNoError(t, resp) 640 assert.Equal(t, supportedTimezonesFromConfig, supportedTimezones) 641 }