github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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 l4g "github.com/alecthomas/log4go" 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 TestGetOldClientConfig(t *testing.T) { 165 th := Setup().InitBasic().InitSystemAdmin() 166 defer th.TearDown() 167 Client := th.Client 168 169 config, resp := Client.GetOldClientConfig("") 170 CheckNoError(t, resp) 171 172 if len(config["Version"]) == 0 { 173 t.Fatal("config not returned correctly") 174 } 175 176 Client.Logout() 177 178 _, resp = Client.GetOldClientConfig("") 179 CheckNoError(t, resp) 180 181 if _, err := Client.DoApiGet("/config/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { 182 t.Fatal("should have errored with 501") 183 } 184 185 if _, err := Client.DoApiGet("/config/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { 186 t.Fatal("should have errored with 400") 187 } 188 } 189 190 func TestGetOldClientLicense(t *testing.T) { 191 th := Setup().InitBasic().InitSystemAdmin() 192 defer th.TearDown() 193 Client := th.Client 194 195 license, resp := Client.GetOldClientLicense("") 196 CheckNoError(t, resp) 197 198 if len(license["IsLicensed"]) == 0 { 199 t.Fatal("license not returned correctly") 200 } 201 202 Client.Logout() 203 204 _, resp = Client.GetOldClientLicense("") 205 CheckNoError(t, resp) 206 207 if _, err := Client.DoApiGet("/license/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented { 208 t.Fatal("should have errored with 501") 209 } 210 211 if _, err := Client.DoApiGet("/license/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest { 212 t.Fatal("should have errored with 400") 213 } 214 215 license, resp = th.SystemAdminClient.GetOldClientLicense("") 216 CheckNoError(t, resp) 217 218 if len(license["IsLicensed"]) == 0 { 219 t.Fatal("license not returned correctly") 220 } 221 } 222 223 func TestGetAudits(t *testing.T) { 224 th := Setup().InitBasic().InitSystemAdmin() 225 defer th.TearDown() 226 Client := th.Client 227 228 audits, resp := th.SystemAdminClient.GetAudits(0, 100, "") 229 CheckNoError(t, resp) 230 231 if len(audits) == 0 { 232 t.Fatal("should not be empty") 233 } 234 235 audits, resp = th.SystemAdminClient.GetAudits(0, 1, "") 236 CheckNoError(t, resp) 237 238 if len(audits) != 1 { 239 t.Fatal("should only be 1") 240 } 241 242 audits, resp = th.SystemAdminClient.GetAudits(1, 1, "") 243 CheckNoError(t, resp) 244 245 if len(audits) != 1 { 246 t.Fatal("should only be 1") 247 } 248 249 _, resp = th.SystemAdminClient.GetAudits(-1, -1, "") 250 CheckNoError(t, resp) 251 252 _, resp = Client.GetAudits(0, 100, "") 253 CheckForbiddenStatus(t, resp) 254 255 Client.Logout() 256 _, resp = Client.GetAudits(0, 100, "") 257 CheckUnauthorizedStatus(t, resp) 258 } 259 260 func TestEmailTest(t *testing.T) { 261 th := Setup().InitBasic().InitSystemAdmin() 262 defer th.TearDown() 263 Client := th.Client 264 265 config := model.Config{ 266 EmailSettings: model.EmailSettings{ 267 SMTPServer: "", 268 SMTPPort: "", 269 }, 270 } 271 272 _, resp := Client.TestEmail(&config) 273 CheckForbiddenStatus(t, resp) 274 275 _, resp = th.SystemAdminClient.TestEmail(&config) 276 CheckErrorMessage(t, resp, "api.admin.test_email.missing_server") 277 CheckBadRequestStatus(t, resp) 278 279 inbucket_host := os.Getenv("CI_HOST") 280 if inbucket_host == "" { 281 inbucket_host = "dockerhost" 282 } 283 284 inbucket_port := os.Getenv("CI_INBUCKET_PORT") 285 if inbucket_port == "" { 286 inbucket_port = "9000" 287 } 288 289 config.EmailSettings.SMTPServer = inbucket_host 290 config.EmailSettings.SMTPPort = inbucket_port 291 _, resp = th.SystemAdminClient.TestEmail(&config) 292 CheckOKStatus(t, resp) 293 } 294 295 func TestDatabaseRecycle(t *testing.T) { 296 th := Setup().InitBasic().InitSystemAdmin() 297 defer th.TearDown() 298 Client := th.Client 299 300 _, resp := Client.DatabaseRecycle() 301 CheckForbiddenStatus(t, resp) 302 303 _, resp = th.SystemAdminClient.DatabaseRecycle() 304 CheckNoError(t, resp) 305 } 306 307 func TestInvalidateCaches(t *testing.T) { 308 th := Setup().InitBasic().InitSystemAdmin() 309 defer th.TearDown() 310 Client := th.Client 311 312 flag, resp := Client.InvalidateCaches() 313 CheckForbiddenStatus(t, resp) 314 if flag { 315 t.Fatal("should not clean the cache due no permission.") 316 } 317 318 flag, resp = th.SystemAdminClient.InvalidateCaches() 319 CheckNoError(t, resp) 320 if !flag { 321 t.Fatal("should clean the cache") 322 } 323 } 324 325 func TestGetLogs(t *testing.T) { 326 th := Setup().InitBasic().InitSystemAdmin() 327 defer th.TearDown() 328 Client := th.Client 329 330 for i := 0; i < 20; i++ { 331 l4g.Info(i) 332 } 333 334 logs, resp := th.SystemAdminClient.GetLogs(0, 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, 10) 343 CheckNoError(t, resp) 344 345 if len(logs) != 10 { 346 t.Log(len(logs)) 347 t.Fatal("wrong length") 348 } 349 350 logs, resp = th.SystemAdminClient.GetLogs(-1, -1) 351 CheckNoError(t, resp) 352 353 if len(logs) == 0 { 354 t.Fatal("should not be empty") 355 } 356 357 _, resp = Client.GetLogs(0, 10) 358 CheckForbiddenStatus(t, resp) 359 360 Client.Logout() 361 _, resp = Client.GetLogs(0, 10) 362 CheckUnauthorizedStatus(t, resp) 363 } 364 365 func TestPostLog(t *testing.T) { 366 th := Setup().InitBasic().InitSystemAdmin() 367 defer th.TearDown() 368 Client := th.Client 369 370 enableDev := *th.App.Config().ServiceSettings.EnableDeveloper 371 defer func() { 372 *th.App.Config().ServiceSettings.EnableDeveloper = enableDev 373 }() 374 *th.App.Config().ServiceSettings.EnableDeveloper = true 375 376 message := make(map[string]string) 377 message["level"] = "ERROR" 378 message["message"] = "this is a test" 379 380 _, resp := Client.PostLog(message) 381 CheckNoError(t, resp) 382 383 Client.Logout() 384 385 _, resp = Client.PostLog(message) 386 CheckNoError(t, resp) 387 388 *th.App.Config().ServiceSettings.EnableDeveloper = false 389 390 _, resp = Client.PostLog(message) 391 CheckForbiddenStatus(t, resp) 392 393 logMessage, resp := th.SystemAdminClient.PostLog(message) 394 CheckNoError(t, resp) 395 if len(logMessage) == 0 { 396 t.Fatal("should return the log message") 397 } 398 } 399 400 func TestUploadLicenseFile(t *testing.T) { 401 th := Setup().InitBasic().InitSystemAdmin() 402 defer th.TearDown() 403 Client := th.Client 404 405 ok, resp := Client.UploadLicenseFile([]byte{}) 406 CheckForbiddenStatus(t, resp) 407 if ok { 408 t.Fatal("should fail") 409 } 410 411 ok, resp = th.SystemAdminClient.UploadLicenseFile([]byte{}) 412 CheckBadRequestStatus(t, resp) 413 if ok { 414 t.Fatal("should fail") 415 } 416 } 417 418 func TestRemoveLicenseFile(t *testing.T) { 419 th := Setup().InitBasic().InitSystemAdmin() 420 defer th.TearDown() 421 Client := th.Client 422 423 ok, resp := Client.RemoveLicenseFile() 424 CheckForbiddenStatus(t, resp) 425 if ok { 426 t.Fatal("should fail") 427 } 428 429 ok, resp = th.SystemAdminClient.RemoveLicenseFile() 430 CheckNoError(t, resp) 431 if !ok { 432 t.Fatal("should pass") 433 } 434 } 435 436 func TestGetAnalyticsOld(t *testing.T) { 437 th := Setup().InitBasic().InitSystemAdmin() 438 defer th.TearDown() 439 Client := th.Client 440 441 rows, resp := Client.GetAnalyticsOld("", "") 442 CheckForbiddenStatus(t, resp) 443 if rows != nil { 444 t.Fatal("should be nil") 445 } 446 447 rows, resp = th.SystemAdminClient.GetAnalyticsOld("", "") 448 CheckNoError(t, resp) 449 450 found := false 451 for _, row := range rows { 452 if row.Name == "unique_user_count" { 453 found = true 454 } 455 } 456 457 if !found { 458 t.Fatal("should return unique user count") 459 } 460 461 _, resp = th.SystemAdminClient.GetAnalyticsOld("post_counts_day", "") 462 CheckNoError(t, resp) 463 464 _, resp = th.SystemAdminClient.GetAnalyticsOld("user_counts_with_posts_day", "") 465 CheckNoError(t, resp) 466 467 _, resp = th.SystemAdminClient.GetAnalyticsOld("extra_counts", "") 468 CheckNoError(t, resp) 469 470 _, resp = th.SystemAdminClient.GetAnalyticsOld("", th.BasicTeam.Id) 471 CheckNoError(t, resp) 472 473 Client.Logout() 474 _, resp = Client.GetAnalyticsOld("", th.BasicTeam.Id) 475 CheckUnauthorizedStatus(t, resp) 476 } 477 478 func TestS3TestConnection(t *testing.T) { 479 th := Setup().InitBasic().InitSystemAdmin() 480 defer th.TearDown() 481 Client := th.Client 482 483 s3Host := os.Getenv("CI_HOST") 484 if s3Host == "" { 485 s3Host = "dockerhost" 486 } 487 488 s3Port := os.Getenv("CI_MINIO_PORT") 489 if s3Port == "" { 490 s3Port = "9001" 491 } 492 493 s3Endpoint := fmt.Sprintf("%s:%s", s3Host, s3Port) 494 config := model.Config{ 495 FileSettings: model.FileSettings{ 496 DriverName: model.NewString(model.IMAGE_DRIVER_S3), 497 AmazonS3AccessKeyId: model.MINIO_ACCESS_KEY, 498 AmazonS3SecretAccessKey: model.MINIO_SECRET_KEY, 499 AmazonS3Bucket: "", 500 AmazonS3Endpoint: s3Endpoint, 501 AmazonS3SSL: model.NewBool(false), 502 }, 503 } 504 505 _, resp := Client.TestS3Connection(&config) 506 CheckForbiddenStatus(t, resp) 507 508 _, resp = th.SystemAdminClient.TestS3Connection(&config) 509 CheckBadRequestStatus(t, resp) 510 if resp.Error.Message != "S3 Bucket is required" { 511 t.Fatal("should return error - missing s3 bucket") 512 } 513 514 config.FileSettings.AmazonS3Bucket = model.MINIO_BUCKET 515 config.FileSettings.AmazonS3Region = "us-east-1" 516 _, resp = th.SystemAdminClient.TestS3Connection(&config) 517 CheckOKStatus(t, resp) 518 519 config.FileSettings.AmazonS3Region = "" 520 _, resp = th.SystemAdminClient.TestS3Connection(&config) 521 CheckOKStatus(t, resp) 522 523 config.FileSettings.AmazonS3Bucket = "Wrong_bucket" 524 _, resp = th.SystemAdminClient.TestS3Connection(&config) 525 CheckInternalErrorStatus(t, resp) 526 if resp.Error.Message != "Error checking if bucket exists." { 527 t.Fatal("should return error ") 528 } 529 }