github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/api4/system_test.go (about) 1 package api4 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "testing" 9 10 "github.com/xzl8028/xenia-server/mlog" 11 "github.com/xzl8028/xenia-server/model" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestGetPing(t *testing.T) { 16 th := Setup().InitBasic() 17 defer th.TearDown() 18 19 t.Run("basic ping", func(t *testing.T) { 20 21 t.Run("healthy", func(t *testing.T) { 22 status, resp := th.Client.GetPing() 23 CheckNoError(t, resp) 24 assert.Equal(t, model.STATUS_OK, status) 25 }) 26 27 t.Run("unhealthy", func(t *testing.T) { 28 goRoutineHealthThreshold := *th.App.Config().ServiceSettings.GoroutineHealthThreshold 29 defer func() { 30 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = goRoutineHealthThreshold }) 31 }() 32 33 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = 10 }) 34 status, resp := th.Client.GetPing() 35 CheckInternalErrorStatus(t, resp) 36 assert.Equal(t, model.STATUS_UNHEALTHY, status) 37 }) 38 39 }) 40 41 t.Run("with server status", func(t *testing.T) { 42 43 t.Run("healthy", func(t *testing.T) { 44 status, resp := th.Client.GetPingWithServerStatus() 45 CheckNoError(t, resp) 46 assert.Equal(t, model.STATUS_OK, status) 47 }) 48 49 t.Run("unhealthy", func(t *testing.T) { 50 badDriver := "badDriverName" 51 th.App.Config().FileSettings.DriverName = &badDriver 52 53 status, resp := th.Client.GetPingWithServerStatus() 54 CheckInternalErrorStatus(t, resp) 55 assert.Equal(t, model.STATUS_UNHEALTHY, status) 56 }) 57 58 }) 59 } 60 61 func TestGetAudits(t *testing.T) { 62 th := Setup().InitBasic() 63 defer th.TearDown() 64 Client := th.Client 65 66 audits, resp := th.SystemAdminClient.GetAudits(0, 100, "") 67 CheckNoError(t, resp) 68 69 if len(audits) == 0 { 70 t.Fatal("should not be empty") 71 } 72 73 audits, resp = th.SystemAdminClient.GetAudits(0, 1, "") 74 CheckNoError(t, resp) 75 76 if len(audits) != 1 { 77 t.Fatal("should only be 1") 78 } 79 80 audits, resp = th.SystemAdminClient.GetAudits(1, 1, "") 81 CheckNoError(t, resp) 82 83 if len(audits) != 1 { 84 t.Fatal("should only be 1") 85 } 86 87 _, resp = th.SystemAdminClient.GetAudits(-1, -1, "") 88 CheckNoError(t, resp) 89 90 _, resp = Client.GetAudits(0, 100, "") 91 CheckForbiddenStatus(t, resp) 92 93 Client.Logout() 94 _, resp = Client.GetAudits(0, 100, "") 95 CheckUnauthorizedStatus(t, resp) 96 } 97 98 func TestEmailTest(t *testing.T) { 99 th := Setup().InitBasic() 100 defer th.TearDown() 101 Client := th.Client 102 103 config := model.Config{ 104 ServiceSettings: model.ServiceSettings{ 105 SiteURL: model.NewString(""), 106 }, 107 EmailSettings: model.EmailSettings{ 108 SMTPServer: model.NewString(""), 109 SMTPPort: model.NewString(""), 110 SMTPPassword: model.NewString(""), 111 FeedbackName: model.NewString(""), 112 FeedbackEmail: model.NewString(""), 113 ReplyToAddress: model.NewString(""), 114 SendEmailNotifications: model.NewBool(false), 115 }, 116 } 117 118 t.Run("as system user", func(t *testing.T) { 119 _, resp := Client.TestEmail(&config) 120 CheckForbiddenStatus(t, resp) 121 }) 122 123 t.Run("as system admin", func(t *testing.T) { 124 _, resp := th.SystemAdminClient.TestEmail(&config) 125 CheckErrorMessage(t, resp, "api.admin.test_email.missing_server") 126 CheckBadRequestStatus(t, resp) 127 128 inbucket_host := os.Getenv("CI_INBUCKET_HOST") 129 if inbucket_host == "" { 130 inbucket_host = "dockerhost" 131 } 132 133 inbucket_port := os.Getenv("CI_INBUCKET_PORT") 134 if inbucket_port == "" { 135 inbucket_port = "9000" 136 } 137 138 *config.EmailSettings.SMTPServer = inbucket_host 139 *config.EmailSettings.SMTPPort = inbucket_port 140 _, resp = th.SystemAdminClient.TestEmail(&config) 141 CheckOKStatus(t, resp) 142 }) 143 144 t.Run("as restricted system admin", func(t *testing.T) { 145 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 146 147 _, resp := th.SystemAdminClient.TestEmail(&config) 148 CheckForbiddenStatus(t, resp) 149 }) 150 } 151 152 func TestDatabaseRecycle(t *testing.T) { 153 th := Setup().InitBasic() 154 defer th.TearDown() 155 Client := th.Client 156 157 t.Run("as system user", func(t *testing.T) { 158 _, resp := Client.DatabaseRecycle() 159 CheckForbiddenStatus(t, resp) 160 }) 161 162 t.Run("as system admin", func(t *testing.T) { 163 _, resp := th.SystemAdminClient.DatabaseRecycle() 164 CheckNoError(t, resp) 165 }) 166 167 t.Run("as restricted system admin", func(t *testing.T) { 168 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 169 170 _, resp := th.SystemAdminClient.DatabaseRecycle() 171 CheckForbiddenStatus(t, resp) 172 }) 173 } 174 175 func TestInvalidateCaches(t *testing.T) { 176 th := Setup().InitBasic() 177 defer th.TearDown() 178 Client := th.Client 179 180 t.Run("as system user", func(t *testing.T) { 181 ok, resp := Client.InvalidateCaches() 182 CheckForbiddenStatus(t, resp) 183 if ok { 184 t.Fatal("should not clean the cache due no permission.") 185 } 186 }) 187 188 t.Run("as system admin", func(t *testing.T) { 189 ok, resp := th.SystemAdminClient.InvalidateCaches() 190 CheckNoError(t, resp) 191 if !ok { 192 t.Fatal("should clean the cache") 193 } 194 }) 195 196 t.Run("as restricted system admin", func(t *testing.T) { 197 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 198 199 ok, resp := th.SystemAdminClient.InvalidateCaches() 200 CheckForbiddenStatus(t, resp) 201 if ok { 202 t.Fatal("should not clean the cache due no permission.") 203 } 204 }) 205 } 206 207 func TestGetLogs(t *testing.T) { 208 th := Setup().InitBasic() 209 defer th.TearDown() 210 Client := th.Client 211 212 for i := 0; i < 20; i++ { 213 mlog.Info(fmt.Sprint(i)) 214 } 215 216 logs, resp := th.SystemAdminClient.GetLogs(0, 10) 217 CheckNoError(t, resp) 218 219 if len(logs) != 10 { 220 t.Log(len(logs)) 221 t.Fatal("wrong length") 222 } 223 for i := 10; i < 20; i++ { 224 assert.Containsf(t, logs[i-10], fmt.Sprintf(`"msg":"%d"`, i), "Log line doesn't contain correct message") 225 } 226 227 logs, resp = th.SystemAdminClient.GetLogs(1, 10) 228 CheckNoError(t, resp) 229 230 if len(logs) != 10 { 231 t.Log(len(logs)) 232 t.Fatal("wrong length") 233 } 234 235 logs, resp = th.SystemAdminClient.GetLogs(-1, -1) 236 CheckNoError(t, resp) 237 238 if len(logs) == 0 { 239 t.Fatal("should not be empty") 240 } 241 242 _, resp = Client.GetLogs(0, 10) 243 CheckForbiddenStatus(t, resp) 244 245 Client.Logout() 246 _, resp = Client.GetLogs(0, 10) 247 CheckUnauthorizedStatus(t, resp) 248 } 249 250 func TestPostLog(t *testing.T) { 251 th := Setup().InitBasic() 252 defer th.TearDown() 253 Client := th.Client 254 255 enableDev := *th.App.Config().ServiceSettings.EnableDeveloper 256 defer func() { 257 *th.App.Config().ServiceSettings.EnableDeveloper = enableDev 258 }() 259 *th.App.Config().ServiceSettings.EnableDeveloper = true 260 261 message := make(map[string]string) 262 message["level"] = "ERROR" 263 message["message"] = "this is a test" 264 265 _, resp := Client.PostLog(message) 266 CheckNoError(t, resp) 267 268 Client.Logout() 269 270 _, resp = Client.PostLog(message) 271 CheckNoError(t, resp) 272 273 *th.App.Config().ServiceSettings.EnableDeveloper = false 274 275 _, resp = Client.PostLog(message) 276 CheckForbiddenStatus(t, resp) 277 278 logMessage, resp := th.SystemAdminClient.PostLog(message) 279 CheckNoError(t, resp) 280 if len(logMessage) == 0 { 281 t.Fatal("should return the log message") 282 } 283 } 284 285 func TestGetAnalyticsOld(t *testing.T) { 286 th := Setup().InitBasic() 287 defer th.TearDown() 288 Client := th.Client 289 290 rows, resp := Client.GetAnalyticsOld("", "") 291 CheckForbiddenStatus(t, resp) 292 if rows != nil { 293 t.Fatal("should be nil") 294 } 295 296 rows, resp = th.SystemAdminClient.GetAnalyticsOld("", "") 297 CheckNoError(t, resp) 298 299 found := false 300 found2 := false 301 for _, row := range rows { 302 if row.Name == "unique_user_count" { 303 found = true 304 } else if row.Name == "inactive_user_count" { 305 found2 = true 306 assert.True(t, row.Value >= 0) 307 } 308 } 309 310 assert.True(t, found, "should return unique user count") 311 assert.True(t, found2, "should return inactive user count") 312 313 _, resp = th.SystemAdminClient.GetAnalyticsOld("post_counts_day", "") 314 CheckNoError(t, resp) 315 316 _, resp = th.SystemAdminClient.GetAnalyticsOld("user_counts_with_posts_day", "") 317 CheckNoError(t, resp) 318 319 _, resp = th.SystemAdminClient.GetAnalyticsOld("extra_counts", "") 320 CheckNoError(t, resp) 321 322 rows, resp = th.SystemAdminClient.GetAnalyticsOld("", th.BasicTeam.Id) 323 CheckNoError(t, resp) 324 325 for _, row := range rows { 326 if row.Name == "inactive_user_count" { 327 assert.Equal(t, float64(-1), row.Value, "inactive user count should be -1 when team specified") 328 } 329 } 330 331 rows2, resp2 := th.SystemAdminClient.GetAnalyticsOld("standard", "") 332 CheckNoError(t, resp2) 333 assert.Equal(t, "total_websocket_connections", rows2[5].Name) 334 assert.Equal(t, float64(0), rows2[5].Value) 335 336 WebSocketClient, err := th.CreateWebSocketClient() 337 if err != nil { 338 t.Fatal(err) 339 } 340 341 rows2, resp2 = th.SystemAdminClient.GetAnalyticsOld("standard", "") 342 CheckNoError(t, resp2) 343 assert.Equal(t, "total_websocket_connections", rows2[5].Name) 344 assert.Equal(t, float64(1), rows2[5].Value) 345 346 WebSocketClient.Close() 347 348 rows2, resp2 = th.SystemAdminClient.GetAnalyticsOld("standard", "") 349 CheckNoError(t, resp2) 350 assert.Equal(t, "total_websocket_connections", rows2[5].Name) 351 assert.Equal(t, float64(0), rows2[5].Value) 352 353 Client.Logout() 354 _, resp = Client.GetAnalyticsOld("", th.BasicTeam.Id) 355 CheckUnauthorizedStatus(t, resp) 356 } 357 358 func TestS3TestConnection(t *testing.T) { 359 th := Setup().InitBasic() 360 defer th.TearDown() 361 Client := th.Client 362 363 s3Host := os.Getenv("CI_MINIO_HOST") 364 if s3Host == "" { 365 s3Host = "dockerhost" 366 } 367 368 s3Port := os.Getenv("CI_MINIO_PORT") 369 if s3Port == "" { 370 s3Port = "9001" 371 } 372 373 s3Endpoint := fmt.Sprintf("%s:%s", s3Host, s3Port) 374 config := model.Config{ 375 FileSettings: model.FileSettings{ 376 DriverName: model.NewString(model.IMAGE_DRIVER_S3), 377 AmazonS3AccessKeyId: model.NewString(model.MINIO_ACCESS_KEY), 378 AmazonS3SecretAccessKey: model.NewString(model.MINIO_SECRET_KEY), 379 AmazonS3Bucket: model.NewString(""), 380 AmazonS3Endpoint: model.NewString(s3Endpoint), 381 AmazonS3Region: model.NewString(""), 382 AmazonS3SSL: model.NewBool(false), 383 }, 384 } 385 386 t.Run("as system user", func(t *testing.T) { 387 _, resp := Client.TestS3Connection(&config) 388 CheckForbiddenStatus(t, resp) 389 }) 390 391 t.Run("as system admin", func(t *testing.T) { 392 _, resp := th.SystemAdminClient.TestS3Connection(&config) 393 CheckBadRequestStatus(t, resp) 394 if resp.Error.Message != "S3 Bucket is required" { 395 t.Fatal("should return error - missing s3 bucket") 396 } 397 398 // If this fails, check the test configuration to ensure minio is setup with the 399 // `xenia-test` bucket defined by model.MINIO_BUCKET. 400 *config.FileSettings.AmazonS3Bucket = model.MINIO_BUCKET 401 *config.FileSettings.AmazonS3Region = "us-east-1" 402 _, resp = th.SystemAdminClient.TestS3Connection(&config) 403 CheckOKStatus(t, resp) 404 405 config.FileSettings.AmazonS3Region = model.NewString("") 406 _, resp = th.SystemAdminClient.TestS3Connection(&config) 407 CheckOKStatus(t, resp) 408 409 config.FileSettings.AmazonS3Bucket = model.NewString("Wrong_bucket") 410 _, resp = th.SystemAdminClient.TestS3Connection(&config) 411 CheckInternalErrorStatus(t, resp) 412 assert.Equal(t, "Unable to create bucket.", resp.Error.Message) 413 414 *config.FileSettings.AmazonS3Bucket = "shouldcreatenewbucket" 415 _, resp = th.SystemAdminClient.TestS3Connection(&config) 416 CheckOKStatus(t, resp) 417 }) 418 419 t.Run("as restricted system admin", func(t *testing.T) { 420 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true }) 421 422 _, resp := th.SystemAdminClient.TestS3Connection(&config) 423 CheckForbiddenStatus(t, resp) 424 }) 425 426 } 427 428 func TestSupportedTimezones(t *testing.T) { 429 th := Setup().InitBasic() 430 defer th.TearDown() 431 Client := th.Client 432 433 supportedTimezonesFromConfig := th.App.Timezones.GetSupported() 434 supportedTimezones, resp := Client.GetSupportedTimezone() 435 436 CheckNoError(t, resp) 437 assert.Equal(t, supportedTimezonesFromConfig, supportedTimezones) 438 } 439 440 func TestRedirectLocation(t *testing.T) { 441 expected := "https://xenia.com/wp-content/themes/xeniav2/img/logo-light.svg" 442 443 testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 444 res.Header().Set("Location", expected) 445 res.WriteHeader(http.StatusFound) 446 res.Write([]byte("body")) 447 })) 448 defer func() { testServer.Close() }() 449 450 mockBitlyLink := testServer.URL 451 452 th := Setup().InitBasic() 453 defer th.TearDown() 454 Client := th.Client 455 enableLinkPreviews := *th.App.Config().ServiceSettings.EnableLinkPreviews 456 defer func() { 457 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews }) 458 }() 459 460 *th.App.Config().ServiceSettings.EnableLinkPreviews = true 461 *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections = "127.0.0.1" 462 463 _, resp := th.SystemAdminClient.GetRedirectLocation("https://xenia.com/", "") 464 CheckNoError(t, resp) 465 466 _, resp = th.SystemAdminClient.GetRedirectLocation("", "") 467 CheckBadRequestStatus(t, resp) 468 469 actual, resp := th.SystemAdminClient.GetRedirectLocation(mockBitlyLink, "") 470 CheckNoError(t, resp) 471 assert.Equal(t, expected, actual) 472 473 // Check cached value 474 actual, resp = th.SystemAdminClient.GetRedirectLocation(mockBitlyLink, "") 475 CheckNoError(t, resp) 476 assert.Equal(t, expected, actual) 477 478 *th.App.Config().ServiceSettings.EnableLinkPreviews = false 479 actual, resp = th.SystemAdminClient.GetRedirectLocation("https://xenia.com/", "") 480 CheckNoError(t, resp) 481 assert.Equal(t, actual, "") 482 483 actual, resp = th.SystemAdminClient.GetRedirectLocation("", "") 484 CheckNoError(t, resp) 485 assert.Equal(t, actual, "") 486 487 actual, resp = th.SystemAdminClient.GetRedirectLocation(mockBitlyLink, "") 488 CheckNoError(t, resp) 489 assert.Equal(t, actual, "") 490 491 Client.Logout() 492 _, resp = Client.GetRedirectLocation("", "") 493 CheckUnauthorizedStatus(t, resp) 494 }