github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/api4/apitestlib.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "bytes" 8 "fmt" 9 "io" 10 "io/ioutil" 11 "net" 12 "net/http" 13 "os" 14 "path/filepath" 15 "reflect" 16 "strconv" 17 "strings" 18 "sync" 19 "testing" 20 "time" 21 22 "github.com/mattermost/mattermost-server/app" 23 "github.com/mattermost/mattermost-server/mlog" 24 "github.com/mattermost/mattermost-server/model" 25 "github.com/mattermost/mattermost-server/store" 26 "github.com/mattermost/mattermost-server/store/sqlstore" 27 "github.com/mattermost/mattermost-server/store/storetest" 28 "github.com/mattermost/mattermost-server/utils" 29 "github.com/mattermost/mattermost-server/wsapi" 30 31 s3 "github.com/minio/minio-go" 32 "github.com/minio/minio-go/pkg/credentials" 33 ) 34 35 type TestHelper struct { 36 App *app.App 37 tempConfigPath string 38 39 Client *model.Client4 40 BasicUser *model.User 41 BasicUser2 *model.User 42 TeamAdminUser *model.User 43 BasicTeam *model.Team 44 BasicChannel *model.Channel 45 BasicPrivateChannel *model.Channel 46 BasicChannel2 *model.Channel 47 BasicPost *model.Post 48 49 SystemAdminClient *model.Client4 50 SystemAdminUser *model.User 51 } 52 53 type persistentTestStore struct { 54 store.Store 55 } 56 57 func (*persistentTestStore) Close() {} 58 59 var testStoreContainer *storetest.RunningContainer 60 var testStore *persistentTestStore 61 62 // UseTestStore sets the container and corresponding settings to use for tests. Once the tests are 63 // complete (e.g. at the end of your TestMain implementation), you should call StopTestStore. 64 func UseTestStore(container *storetest.RunningContainer, settings *model.SqlSettings) { 65 testStoreContainer = container 66 testStore = &persistentTestStore{store.NewLayeredStore(sqlstore.NewSqlSupplier(*settings, nil), nil, nil)} 67 } 68 69 func StopTestStore() { 70 if testStoreContainer != nil { 71 testStoreContainer.Stop() 72 testStoreContainer = nil 73 } 74 } 75 76 func setupTestHelper(enterprise bool) *TestHelper { 77 permConfig, err := os.Open(utils.FindConfigFile("config.json")) 78 if err != nil { 79 panic(err) 80 } 81 defer permConfig.Close() 82 tempConfig, err := ioutil.TempFile("", "") 83 if err != nil { 84 panic(err) 85 } 86 _, err = io.Copy(tempConfig, permConfig) 87 tempConfig.Close() 88 if err != nil { 89 panic(err) 90 } 91 92 options := []app.Option{app.ConfigFile(tempConfig.Name()), app.DisableConfigWatch} 93 if testStore != nil { 94 options = append(options, app.StoreOverride(testStore)) 95 } 96 97 a, err := app.New(options...) 98 if err != nil { 99 panic(err) 100 } 101 102 th := &TestHelper{ 103 App: a, 104 tempConfigPath: tempConfig.Name(), 105 } 106 107 th.App.UpdateConfig(func(cfg *model.Config) { 108 *cfg.TeamSettings.MaxUsersPerTeam = 50 109 *cfg.RateLimitSettings.Enable = false 110 cfg.EmailSettings.SendEmailNotifications = true 111 }) 112 prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress 113 if testStore != nil { 114 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" }) 115 } 116 serverErr := th.App.StartServer() 117 if serverErr != nil { 118 panic(serverErr) 119 } 120 121 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress }) 122 Init(th.App, th.App.Srv.Router, true) 123 wsapi.Init(th.App, th.App.Srv.WebSocketRouter) 124 th.App.Srv.Store.MarkSystemRanUnitTests() 125 th.App.DoAdvancedPermissionsMigration() 126 127 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true }) 128 129 if enterprise { 130 th.App.SetLicense(model.NewTestLicense()) 131 } else { 132 th.App.SetLicense(nil) 133 } 134 135 th.Client = th.CreateClient() 136 th.SystemAdminClient = th.CreateClient() 137 return th 138 } 139 140 func SetupEnterprise() *TestHelper { 141 return setupTestHelper(true) 142 } 143 144 func Setup() *TestHelper { 145 return setupTestHelper(false) 146 } 147 148 func (me *TestHelper) TearDown() { 149 utils.DisableDebugLogForTest() 150 151 var wg sync.WaitGroup 152 wg.Add(3) 153 154 go func() { 155 defer wg.Done() 156 options := map[string]bool{} 157 options[store.USER_SEARCH_OPTION_NAMES_ONLY_NO_FULL_NAME] = true 158 if result := <-me.App.Srv.Store.User().Search("", "fakeuser", options); result.Err != nil { 159 mlog.Error("Error tearing down test users") 160 } else { 161 users := result.Data.([]*model.User) 162 163 for _, u := range users { 164 if err := me.App.PermanentDeleteUser(u); err != nil { 165 mlog.Error(err.Error()) 166 } 167 } 168 } 169 }() 170 171 go func() { 172 defer wg.Done() 173 if result := <-me.App.Srv.Store.Team().SearchByName("faketeam"); result.Err != nil { 174 mlog.Error("Error tearing down test teams") 175 } else { 176 teams := result.Data.([]*model.Team) 177 178 for _, t := range teams { 179 if err := me.App.PermanentDeleteTeam(t); err != nil { 180 mlog.Error(err.Error()) 181 } 182 } 183 } 184 }() 185 186 go func() { 187 defer wg.Done() 188 if result := <-me.App.Srv.Store.OAuth().GetApps(0, 1000); result.Err != nil { 189 mlog.Error("Error tearing down test oauth apps") 190 } else { 191 apps := result.Data.([]*model.OAuthApp) 192 193 for _, a := range apps { 194 if strings.HasPrefix(a.Name, "fakeoauthapp") { 195 <-me.App.Srv.Store.OAuth().DeleteApp(a.Id) 196 } 197 } 198 } 199 }() 200 201 wg.Wait() 202 203 me.App.Shutdown() 204 os.Remove(me.tempConfigPath) 205 206 utils.EnableDebugLogForTest() 207 208 if err := recover(); err != nil { 209 StopTestStore() 210 panic(err) 211 } 212 } 213 214 func (me *TestHelper) InitBasic() *TestHelper { 215 me.waitForConnectivity() 216 217 me.TeamAdminUser = me.CreateUser() 218 me.App.UpdateUserRoles(me.TeamAdminUser.Id, model.SYSTEM_USER_ROLE_ID, false) 219 me.LoginTeamAdmin() 220 me.BasicTeam = me.CreateTeam() 221 me.BasicChannel = me.CreatePublicChannel() 222 me.BasicPrivateChannel = me.CreatePrivateChannel() 223 me.BasicChannel2 = me.CreatePublicChannel() 224 me.BasicPost = me.CreatePost() 225 me.BasicUser = me.CreateUser() 226 me.LinkUserToTeam(me.BasicUser, me.BasicTeam) 227 me.BasicUser2 = me.CreateUser() 228 me.LinkUserToTeam(me.BasicUser2, me.BasicTeam) 229 me.App.AddUserToChannel(me.BasicUser, me.BasicChannel) 230 me.App.AddUserToChannel(me.BasicUser2, me.BasicChannel) 231 me.App.AddUserToChannel(me.BasicUser, me.BasicChannel2) 232 me.App.AddUserToChannel(me.BasicUser2, me.BasicChannel2) 233 me.App.AddUserToChannel(me.BasicUser, me.BasicPrivateChannel) 234 me.App.AddUserToChannel(me.BasicUser2, me.BasicPrivateChannel) 235 me.App.UpdateUserRoles(me.BasicUser.Id, model.SYSTEM_USER_ROLE_ID, false) 236 me.LoginBasic() 237 238 return me 239 } 240 241 func (me *TestHelper) InitSystemAdmin() *TestHelper { 242 me.waitForConnectivity() 243 244 me.SystemAdminUser = me.CreateUser() 245 me.App.UpdateUserRoles(me.SystemAdminUser.Id, model.SYSTEM_USER_ROLE_ID+" "+model.SYSTEM_ADMIN_ROLE_ID, false) 246 me.LoginSystemAdmin() 247 248 return me 249 } 250 251 func (me *TestHelper) waitForConnectivity() { 252 for i := 0; i < 1000; i++ { 253 conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%v", me.App.Srv.ListenAddr.Port)) 254 if err == nil { 255 conn.Close() 256 return 257 } 258 time.Sleep(time.Millisecond * 20) 259 } 260 panic("unable to connect") 261 } 262 263 func (me *TestHelper) CreateClient() *model.Client4 { 264 return model.NewAPIv4Client(fmt.Sprintf("http://localhost:%v", me.App.Srv.ListenAddr.Port)) 265 } 266 267 func (me *TestHelper) CreateWebSocketClient() (*model.WebSocketClient, *model.AppError) { 268 return model.NewWebSocketClient4(fmt.Sprintf("ws://localhost:%v", me.App.Srv.ListenAddr.Port), me.Client.AuthToken) 269 } 270 271 func (me *TestHelper) CreateUser() *model.User { 272 return me.CreateUserWithClient(me.Client) 273 } 274 275 func (me *TestHelper) CreateTeam() *model.Team { 276 return me.CreateTeamWithClient(me.Client) 277 } 278 279 func (me *TestHelper) CreateTeamWithClient(client *model.Client4) *model.Team { 280 id := model.NewId() 281 team := &model.Team{ 282 DisplayName: "dn_" + id, 283 Name: GenerateTestTeamName(), 284 Email: me.GenerateTestEmail(), 285 Type: model.TEAM_OPEN, 286 } 287 288 utils.DisableDebugLogForTest() 289 rteam, _ := client.CreateTeam(team) 290 utils.EnableDebugLogForTest() 291 return rteam 292 } 293 294 func (me *TestHelper) CreateUserWithClient(client *model.Client4) *model.User { 295 id := model.NewId() 296 297 user := &model.User{ 298 Email: me.GenerateTestEmail(), 299 Username: GenerateTestUsername(), 300 Nickname: "nn_" + id, 301 FirstName: "f_" + id, 302 LastName: "l_" + id, 303 Password: "Password1", 304 } 305 306 utils.DisableDebugLogForTest() 307 ruser, response := client.CreateUser(user) 308 if response.Error != nil { 309 panic(response.Error) 310 } 311 312 ruser.Password = "Password1" 313 store.Must(me.App.Srv.Store.User().VerifyEmail(ruser.Id)) 314 utils.EnableDebugLogForTest() 315 return ruser 316 } 317 318 func (me *TestHelper) CreatePublicChannel() *model.Channel { 319 return me.CreateChannelWithClient(me.Client, model.CHANNEL_OPEN) 320 } 321 322 func (me *TestHelper) CreatePrivateChannel() *model.Channel { 323 return me.CreateChannelWithClient(me.Client, model.CHANNEL_PRIVATE) 324 } 325 326 func (me *TestHelper) CreateChannelWithClient(client *model.Client4, channelType string) *model.Channel { 327 return me.CreateChannelWithClientAndTeam(client, channelType, me.BasicTeam.Id) 328 } 329 330 func (me *TestHelper) CreateChannelWithClientAndTeam(client *model.Client4, channelType string, teamId string) *model.Channel { 331 id := model.NewId() 332 333 channel := &model.Channel{ 334 DisplayName: "dn_" + id, 335 Name: GenerateTestChannelName(), 336 Type: channelType, 337 TeamId: teamId, 338 } 339 340 utils.DisableDebugLogForTest() 341 rchannel, _ := client.CreateChannel(channel) 342 utils.EnableDebugLogForTest() 343 return rchannel 344 } 345 346 func (me *TestHelper) CreatePost() *model.Post { 347 return me.CreatePostWithClient(me.Client, me.BasicChannel) 348 } 349 350 func (me *TestHelper) CreatePinnedPost() *model.Post { 351 return me.CreatePinnedPostWithClient(me.Client, me.BasicChannel) 352 } 353 354 func (me *TestHelper) CreateMessagePost(message string) *model.Post { 355 return me.CreateMessagePostWithClient(me.Client, me.BasicChannel, message) 356 } 357 358 func (me *TestHelper) CreatePostWithClient(client *model.Client4, channel *model.Channel) *model.Post { 359 id := model.NewId() 360 361 post := &model.Post{ 362 ChannelId: channel.Id, 363 Message: "message_" + id, 364 } 365 366 utils.DisableDebugLogForTest() 367 rpost, resp := client.CreatePost(post) 368 if resp.Error != nil { 369 panic(resp.Error) 370 } 371 utils.EnableDebugLogForTest() 372 return rpost 373 } 374 375 func (me *TestHelper) CreatePinnedPostWithClient(client *model.Client4, channel *model.Channel) *model.Post { 376 id := model.NewId() 377 378 post := &model.Post{ 379 ChannelId: channel.Id, 380 Message: "message_" + id, 381 IsPinned: true, 382 } 383 384 utils.DisableDebugLogForTest() 385 rpost, resp := client.CreatePost(post) 386 if resp.Error != nil { 387 panic(resp.Error) 388 } 389 utils.EnableDebugLogForTest() 390 return rpost 391 } 392 393 func (me *TestHelper) CreateMessagePostWithClient(client *model.Client4, channel *model.Channel, message string) *model.Post { 394 post := &model.Post{ 395 ChannelId: channel.Id, 396 Message: message, 397 } 398 399 utils.DisableDebugLogForTest() 400 rpost, resp := client.CreatePost(post) 401 if resp.Error != nil { 402 panic(resp.Error) 403 } 404 utils.EnableDebugLogForTest() 405 return rpost 406 } 407 408 func (me *TestHelper) LoginBasic() { 409 me.LoginBasicWithClient(me.Client) 410 } 411 412 func (me *TestHelper) LoginBasic2() { 413 me.LoginBasic2WithClient(me.Client) 414 } 415 416 func (me *TestHelper) LoginTeamAdmin() { 417 me.LoginTeamAdminWithClient(me.Client) 418 } 419 420 func (me *TestHelper) LoginSystemAdmin() { 421 me.LoginSystemAdminWithClient(me.SystemAdminClient) 422 } 423 424 func (me *TestHelper) LoginBasicWithClient(client *model.Client4) { 425 utils.DisableDebugLogForTest() 426 client.Login(me.BasicUser.Email, me.BasicUser.Password) 427 utils.EnableDebugLogForTest() 428 } 429 430 func (me *TestHelper) LoginBasic2WithClient(client *model.Client4) { 431 utils.DisableDebugLogForTest() 432 client.Login(me.BasicUser2.Email, me.BasicUser2.Password) 433 utils.EnableDebugLogForTest() 434 } 435 436 func (me *TestHelper) LoginTeamAdminWithClient(client *model.Client4) { 437 utils.DisableDebugLogForTest() 438 client.Login(me.TeamAdminUser.Email, me.TeamAdminUser.Password) 439 utils.EnableDebugLogForTest() 440 } 441 442 func (me *TestHelper) LoginSystemAdminWithClient(client *model.Client4) { 443 utils.DisableDebugLogForTest() 444 client.Login(me.SystemAdminUser.Email, me.SystemAdminUser.Password) 445 utils.EnableDebugLogForTest() 446 } 447 448 func (me *TestHelper) UpdateActiveUser(user *model.User, active bool) { 449 utils.DisableDebugLogForTest() 450 451 _, err := me.App.UpdateActive(user, active) 452 if err != nil { 453 mlog.Error(err.Error()) 454 455 time.Sleep(time.Second) 456 panic(err) 457 } 458 459 utils.EnableDebugLogForTest() 460 } 461 462 func (me *TestHelper) LinkUserToTeam(user *model.User, team *model.Team) { 463 utils.DisableDebugLogForTest() 464 465 err := me.App.JoinUserToTeam(team, user, "") 466 if err != nil { 467 mlog.Error(err.Error()) 468 469 time.Sleep(time.Second) 470 panic(err) 471 } 472 473 utils.EnableDebugLogForTest() 474 } 475 476 func (me *TestHelper) AddUserToChannel(user *model.User, channel *model.Channel) *model.ChannelMember { 477 utils.DisableDebugLogForTest() 478 479 member, err := me.App.AddUserToChannel(user, channel) 480 if err != nil { 481 mlog.Error(err.Error()) 482 483 time.Sleep(time.Second) 484 panic(err) 485 } 486 487 utils.EnableDebugLogForTest() 488 489 return member 490 } 491 492 func (me *TestHelper) GenerateTestEmail() string { 493 if me.App.Config().EmailSettings.SMTPServer != "dockerhost" && os.Getenv("CI_INBUCKET_PORT") == "" { 494 return strings.ToLower("success+" + model.NewId() + "@simulator.amazonses.com") 495 } 496 return strings.ToLower(model.NewId() + "@dockerhost") 497 } 498 499 func GenerateTestUsername() string { 500 return "fakeuser" + model.NewRandomString(10) 501 } 502 503 func GenerateTestTeamName() string { 504 return "faketeam" + model.NewRandomString(6) 505 } 506 507 func GenerateTestChannelName() string { 508 return "fakechannel" + model.NewRandomString(10) 509 } 510 511 func GenerateTestAppName() string { 512 return "fakeoauthapp" + model.NewRandomString(10) 513 } 514 515 func GenerateTestId() string { 516 return model.NewId() 517 } 518 519 func CheckUserSanitization(t *testing.T, user *model.User) { 520 t.Helper() 521 522 if user.Password != "" { 523 t.Fatal("password wasn't blank") 524 } 525 526 if user.AuthData != nil && *user.AuthData != "" { 527 t.Fatal("auth data wasn't blank") 528 } 529 530 if user.MfaSecret != "" { 531 t.Fatal("mfa secret wasn't blank") 532 } 533 } 534 535 func CheckEtag(t *testing.T, data interface{}, resp *model.Response) { 536 t.Helper() 537 538 if !reflect.ValueOf(data).IsNil() { 539 t.Fatal("etag data was not nil") 540 } 541 542 if resp.StatusCode != http.StatusNotModified { 543 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 544 t.Log("expected: " + strconv.Itoa(http.StatusNotModified)) 545 t.Fatal("wrong status code for etag") 546 } 547 } 548 549 func CheckNoError(t *testing.T, resp *model.Response) { 550 t.Helper() 551 552 if resp.Error != nil { 553 t.Fatal("Expected no error, got " + resp.Error.Error()) 554 } 555 } 556 557 func CheckCreatedStatus(t *testing.T, resp *model.Response) { 558 t.Helper() 559 560 if resp.StatusCode != http.StatusCreated { 561 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 562 t.Log("expected: " + strconv.Itoa(http.StatusCreated)) 563 t.Fatal("wrong status code") 564 } 565 } 566 567 func CheckForbiddenStatus(t *testing.T, resp *model.Response) { 568 t.Helper() 569 570 if resp.Error == nil { 571 t.Fatal("should have errored with status:" + strconv.Itoa(http.StatusForbidden)) 572 return 573 } 574 575 if resp.StatusCode != http.StatusForbidden { 576 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 577 t.Log("expected: " + strconv.Itoa(http.StatusForbidden)) 578 t.Fatal("wrong status code") 579 } 580 } 581 582 func CheckUnauthorizedStatus(t *testing.T, resp *model.Response) { 583 t.Helper() 584 585 if resp.Error == nil { 586 t.Fatal("should have errored with status:" + strconv.Itoa(http.StatusUnauthorized)) 587 return 588 } 589 590 if resp.StatusCode != http.StatusUnauthorized { 591 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 592 t.Log("expected: " + strconv.Itoa(http.StatusUnauthorized)) 593 t.Fatal("wrong status code") 594 } 595 } 596 597 func CheckNotFoundStatus(t *testing.T, resp *model.Response) { 598 t.Helper() 599 600 if resp.Error == nil { 601 t.Fatal("should have errored with status:" + strconv.Itoa(http.StatusNotFound)) 602 return 603 } 604 605 if resp.StatusCode != http.StatusNotFound { 606 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 607 t.Log("expected: " + strconv.Itoa(http.StatusNotFound)) 608 t.Fatal("wrong status code") 609 } 610 } 611 612 func CheckBadRequestStatus(t *testing.T, resp *model.Response) { 613 t.Helper() 614 615 if resp.Error == nil { 616 t.Fatal("should have errored with status:" + strconv.Itoa(http.StatusBadRequest)) 617 return 618 } 619 620 if resp.StatusCode != http.StatusBadRequest { 621 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 622 t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) 623 t.Fatal("wrong status code") 624 } 625 } 626 627 func CheckNotImplementedStatus(t *testing.T, resp *model.Response) { 628 t.Helper() 629 630 if resp.Error == nil { 631 t.Fatal("should have errored with status:" + strconv.Itoa(http.StatusNotImplemented)) 632 return 633 } 634 635 if resp.StatusCode != http.StatusNotImplemented { 636 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 637 t.Log("expected: " + strconv.Itoa(http.StatusNotImplemented)) 638 t.Fatal("wrong status code") 639 } 640 } 641 642 func CheckOKStatus(t *testing.T, resp *model.Response) { 643 t.Helper() 644 645 CheckNoError(t, resp) 646 647 if resp.StatusCode != http.StatusOK { 648 t.Fatalf("wrong status code. expected %d got %d", http.StatusOK, resp.StatusCode) 649 } 650 } 651 652 func CheckErrorMessage(t *testing.T, resp *model.Response, errorId string) { 653 t.Helper() 654 655 if resp.Error == nil { 656 t.Fatal("should have errored with message:" + errorId) 657 return 658 } 659 660 if resp.Error.Id != errorId { 661 t.Log("actual: " + resp.Error.Id) 662 t.Log("expected: " + errorId) 663 t.Fatal("incorrect error message") 664 } 665 } 666 667 func CheckInternalErrorStatus(t *testing.T, resp *model.Response) { 668 t.Helper() 669 670 if resp.Error == nil { 671 t.Fatal("should have errored with status:" + strconv.Itoa(http.StatusInternalServerError)) 672 return 673 } 674 675 if resp.StatusCode != http.StatusInternalServerError { 676 t.Log("actual: " + strconv.Itoa(resp.StatusCode)) 677 t.Log("expected: " + strconv.Itoa(http.StatusInternalServerError)) 678 t.Fatal("wrong status code") 679 } 680 } 681 682 func readTestFile(name string) ([]byte, error) { 683 path, _ := utils.FindDir("tests") 684 file, err := os.Open(filepath.Join(path, name)) 685 if err != nil { 686 return nil, err 687 } 688 defer file.Close() 689 690 data := &bytes.Buffer{} 691 if _, err := io.Copy(data, file); err != nil { 692 return nil, err 693 } else { 694 return data.Bytes(), nil 695 } 696 } 697 698 // Similar to s3.New() but allows initialization of signature v2 or signature v4 client. 699 // If signV2 input is false, function always returns signature v4. 700 // 701 // Additionally this function also takes a user defined region, if set 702 // disables automatic region lookup. 703 func s3New(endpoint, accessKey, secretKey string, secure bool, signV2 bool, region string) (*s3.Client, error) { 704 var creds *credentials.Credentials 705 if signV2 { 706 creds = credentials.NewStatic(accessKey, secretKey, "", credentials.SignatureV2) 707 } else { 708 creds = credentials.NewStatic(accessKey, secretKey, "", credentials.SignatureV4) 709 } 710 return s3.NewWithCredentials(endpoint, creds, secure, region) 711 } 712 713 func (me *TestHelper) cleanupTestFile(info *model.FileInfo) error { 714 cfg := me.App.Config() 715 if *cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 { 716 endpoint := cfg.FileSettings.AmazonS3Endpoint 717 accessKey := cfg.FileSettings.AmazonS3AccessKeyId 718 secretKey := cfg.FileSettings.AmazonS3SecretAccessKey 719 secure := *cfg.FileSettings.AmazonS3SSL 720 signV2 := *cfg.FileSettings.AmazonS3SignV2 721 region := cfg.FileSettings.AmazonS3Region 722 s3Clnt, err := s3New(endpoint, accessKey, secretKey, secure, signV2, region) 723 if err != nil { 724 return err 725 } 726 bucket := cfg.FileSettings.AmazonS3Bucket 727 if err := s3Clnt.RemoveObject(bucket, info.Path); err != nil { 728 return err 729 } 730 731 if info.ThumbnailPath != "" { 732 if err := s3Clnt.RemoveObject(bucket, info.ThumbnailPath); err != nil { 733 return err 734 } 735 } 736 737 if info.PreviewPath != "" { 738 if err := s3Clnt.RemoveObject(bucket, info.PreviewPath); err != nil { 739 return err 740 } 741 } 742 } else if *cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL { 743 if err := os.Remove(cfg.FileSettings.Directory + info.Path); err != nil { 744 return err 745 } 746 747 if info.ThumbnailPath != "" { 748 if err := os.Remove(cfg.FileSettings.Directory + info.ThumbnailPath); err != nil { 749 return err 750 } 751 } 752 753 if info.PreviewPath != "" { 754 if err := os.Remove(cfg.FileSettings.Directory + info.PreviewPath); err != nil { 755 return err 756 } 757 } 758 } 759 760 return nil 761 } 762 763 func (me *TestHelper) MakeUserChannelAdmin(user *model.User, channel *model.Channel) { 764 utils.DisableDebugLogForTest() 765 766 if cmr := <-me.App.Srv.Store.Channel().GetMember(channel.Id, user.Id); cmr.Err == nil { 767 cm := cmr.Data.(*model.ChannelMember) 768 cm.Roles = "channel_admin channel_user" 769 if sr := <-me.App.Srv.Store.Channel().UpdateMember(cm); sr.Err != nil { 770 utils.EnableDebugLogForTest() 771 panic(sr.Err) 772 } 773 } else { 774 utils.EnableDebugLogForTest() 775 panic(cmr.Err) 776 } 777 778 utils.EnableDebugLogForTest() 779 } 780 781 func (me *TestHelper) UpdateUserToTeamAdmin(user *model.User, team *model.Team) { 782 utils.DisableDebugLogForTest() 783 784 tm := &model.TeamMember{TeamId: team.Id, UserId: user.Id, Roles: model.TEAM_USER_ROLE_ID + " " + model.TEAM_ADMIN_ROLE_ID} 785 if tmr := <-me.App.Srv.Store.Team().UpdateMember(tm); tmr.Err != nil { 786 utils.EnableDebugLogForTest() 787 mlog.Error(tmr.Err.Error()) 788 789 time.Sleep(time.Second) 790 panic(tmr.Err) 791 } 792 utils.EnableDebugLogForTest() 793 } 794 795 func (me *TestHelper) UpdateUserToNonTeamAdmin(user *model.User, team *model.Team) { 796 utils.DisableDebugLogForTest() 797 798 tm := &model.TeamMember{TeamId: team.Id, UserId: user.Id, Roles: model.TEAM_USER_ROLE_ID} 799 if tmr := <-me.App.Srv.Store.Team().UpdateMember(tm); tmr.Err != nil { 800 utils.EnableDebugLogForTest() 801 mlog.Error(tmr.Err.Error()) 802 803 time.Sleep(time.Second) 804 panic(tmr.Err) 805 } 806 utils.EnableDebugLogForTest() 807 } 808 809 func (me *TestHelper) SaveDefaultRolePermissions() map[string][]string { 810 utils.DisableDebugLogForTest() 811 812 results := make(map[string][]string) 813 814 for _, roleName := range []string{ 815 "system_user", 816 "system_admin", 817 "team_user", 818 "team_admin", 819 "channel_user", 820 "channel_admin", 821 } { 822 role, err1 := me.App.GetRoleByName(roleName) 823 if err1 != nil { 824 utils.EnableDebugLogForTest() 825 panic(err1) 826 } 827 828 results[roleName] = role.Permissions 829 } 830 831 utils.EnableDebugLogForTest() 832 return results 833 } 834 835 func (me *TestHelper) RestoreDefaultRolePermissions(data map[string][]string) { 836 utils.DisableDebugLogForTest() 837 838 for roleName, permissions := range data { 839 role, err1 := me.App.GetRoleByName(roleName) 840 if err1 != nil { 841 utils.EnableDebugLogForTest() 842 panic(err1) 843 } 844 845 if strings.Join(role.Permissions, " ") == strings.Join(permissions, " ") { 846 continue 847 } 848 849 role.Permissions = permissions 850 851 _, err2 := me.App.UpdateRole(role) 852 if err2 != nil { 853 utils.EnableDebugLogForTest() 854 panic(err2) 855 } 856 } 857 858 utils.EnableDebugLogForTest() 859 } 860 861 func (me *TestHelper) RemovePermissionFromRole(permission string, roleName string) { 862 utils.DisableDebugLogForTest() 863 864 role, err1 := me.App.GetRoleByName(roleName) 865 if err1 != nil { 866 utils.EnableDebugLogForTest() 867 panic(err1) 868 } 869 870 var newPermissions []string 871 for _, p := range role.Permissions { 872 if p != permission { 873 newPermissions = append(newPermissions, p) 874 } 875 } 876 877 if strings.Join(role.Permissions, " ") == strings.Join(newPermissions, " ") { 878 utils.EnableDebugLogForTest() 879 return 880 } 881 882 role.Permissions = newPermissions 883 884 _, err2 := me.App.UpdateRole(role) 885 if err2 != nil { 886 utils.EnableDebugLogForTest() 887 panic(err2) 888 } 889 890 utils.EnableDebugLogForTest() 891 } 892 893 func (me *TestHelper) AddPermissionToRole(permission string, roleName string) { 894 utils.DisableDebugLogForTest() 895 896 role, err1 := me.App.GetRoleByName(roleName) 897 if err1 != nil { 898 utils.EnableDebugLogForTest() 899 panic(err1) 900 } 901 902 for _, existingPermission := range role.Permissions { 903 if existingPermission == permission { 904 utils.EnableDebugLogForTest() 905 return 906 } 907 } 908 909 role.Permissions = append(role.Permissions, permission) 910 911 _, err2 := me.App.UpdateRole(role) 912 if err2 != nil { 913 utils.EnableDebugLogForTest() 914 panic(err2) 915 } 916 917 utils.EnableDebugLogForTest() 918 }