github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/api4/channel_test.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 "fmt" 8 "net/http" 9 "reflect" 10 "sort" 11 "strconv" 12 "strings" 13 "testing" 14 15 "github.com/mattermost/mattermost-server/model" 16 ) 17 18 func TestCreateChannel(t *testing.T) { 19 th := Setup().InitBasic().InitSystemAdmin() 20 defer th.TearDown() 21 Client := th.Client 22 team := th.BasicTeam 23 24 channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} 25 private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id} 26 27 rchannel, resp := Client.CreateChannel(channel) 28 CheckNoError(t, resp) 29 CheckCreatedStatus(t, resp) 30 31 if rchannel.Name != channel.Name { 32 t.Fatal("names did not match") 33 } 34 35 if rchannel.DisplayName != channel.DisplayName { 36 t.Fatal("display names did not match") 37 } 38 39 if rchannel.TeamId != channel.TeamId { 40 t.Fatal("team ids did not match") 41 } 42 43 rprivate, resp := Client.CreateChannel(private) 44 CheckNoError(t, resp) 45 46 if rprivate.Name != private.Name { 47 t.Fatal("names did not match") 48 } 49 50 if rprivate.Type != model.CHANNEL_PRIVATE { 51 t.Fatal("wrong channel type") 52 } 53 54 if rprivate.CreatorId != th.BasicUser.Id { 55 t.Fatal("wrong creator id") 56 } 57 58 _, resp = Client.CreateChannel(channel) 59 CheckErrorMessage(t, resp, "store.sql_channel.save_channel.exists.app_error") 60 CheckBadRequestStatus(t, resp) 61 62 direct := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_DIRECT, TeamId: team.Id} 63 _, resp = Client.CreateChannel(direct) 64 CheckErrorMessage(t, resp, "api.channel.create_channel.direct_channel.app_error") 65 CheckBadRequestStatus(t, resp) 66 67 Client.Logout() 68 _, resp = Client.CreateChannel(channel) 69 CheckUnauthorizedStatus(t, resp) 70 71 userNotOnTeam := th.CreateUser() 72 Client.Login(userNotOnTeam.Email, userNotOnTeam.Password) 73 74 _, resp = Client.CreateChannel(channel) 75 CheckForbiddenStatus(t, resp) 76 77 _, resp = Client.CreateChannel(private) 78 CheckForbiddenStatus(t, resp) 79 80 th.LoginBasic() 81 82 // Check permissions with policy config changes 83 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_ALL }) 84 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_ALL }) 85 th.App.SetLicense(model.NewTestLicense()) 86 87 channel.Name = GenerateTestChannelName() 88 _, resp = Client.CreateChannel(channel) 89 CheckNoError(t, resp) 90 91 private.Name = GenerateTestChannelName() 92 _, resp = Client.CreateChannel(private) 93 CheckNoError(t, resp) 94 95 th.App.UpdateConfig(func(cfg *model.Config) { 96 *cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_TEAM_ADMIN 97 *cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_TEAM_ADMIN 98 }) 99 100 _, resp = Client.CreateChannel(channel) 101 CheckForbiddenStatus(t, resp) 102 103 _, resp = Client.CreateChannel(private) 104 CheckForbiddenStatus(t, resp) 105 106 th.LoginTeamAdmin() 107 108 channel.Name = GenerateTestChannelName() 109 _, resp = Client.CreateChannel(channel) 110 CheckNoError(t, resp) 111 112 private.Name = GenerateTestChannelName() 113 _, resp = Client.CreateChannel(private) 114 CheckNoError(t, resp) 115 116 channel.Name = GenerateTestChannelName() 117 _, resp = th.SystemAdminClient.CreateChannel(channel) 118 CheckNoError(t, resp) 119 120 private.Name = GenerateTestChannelName() 121 _, resp = th.SystemAdminClient.CreateChannel(private) 122 CheckNoError(t, resp) 123 124 th.App.UpdateConfig(func(cfg *model.Config) { 125 *cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_SYSTEM_ADMIN 126 *cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_SYSTEM_ADMIN 127 }) 128 129 th.LoginBasic() 130 131 _, resp = Client.CreateChannel(channel) 132 CheckForbiddenStatus(t, resp) 133 134 _, resp = Client.CreateChannel(private) 135 CheckForbiddenStatus(t, resp) 136 137 th.LoginTeamAdmin() 138 139 _, resp = Client.CreateChannel(channel) 140 CheckForbiddenStatus(t, resp) 141 142 _, resp = Client.CreateChannel(private) 143 CheckForbiddenStatus(t, resp) 144 145 channel.Name = GenerateTestChannelName() 146 _, resp = th.SystemAdminClient.CreateChannel(channel) 147 CheckNoError(t, resp) 148 149 private.Name = GenerateTestChannelName() 150 _, resp = th.SystemAdminClient.CreateChannel(private) 151 CheckNoError(t, resp) 152 153 // Check that if unlicensed the policy restriction is not enforced. 154 th.App.SetLicense(nil) 155 156 channel.Name = GenerateTestChannelName() 157 _, resp = Client.CreateChannel(channel) 158 CheckNoError(t, resp) 159 160 private.Name = GenerateTestChannelName() 161 _, resp = Client.CreateChannel(private) 162 CheckNoError(t, resp) 163 164 if r, err := Client.DoApiPost("/channels", "garbage"); err == nil { 165 t.Fatal("should have errored") 166 } else { 167 if r.StatusCode != http.StatusBadRequest { 168 t.Log("actual: " + strconv.Itoa(r.StatusCode)) 169 t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) 170 t.Fatal("wrong status code") 171 } 172 } 173 } 174 175 func TestUpdateChannel(t *testing.T) { 176 th := Setup().InitBasic().InitSystemAdmin() 177 defer th.TearDown() 178 Client := th.Client 179 team := th.BasicTeam 180 181 channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} 182 private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id} 183 184 channel, resp := Client.CreateChannel(channel) 185 private, resp = Client.CreateChannel(private) 186 187 //Update a open channel 188 channel.DisplayName = "My new display name" 189 channel.Header = "My fancy header" 190 channel.Purpose = "Mattermost ftw!" 191 192 newChannel, resp := Client.UpdateChannel(channel) 193 CheckNoError(t, resp) 194 195 if newChannel.DisplayName != channel.DisplayName { 196 t.Fatal("Update failed for DisplayName") 197 } 198 199 if newChannel.Header != channel.Header { 200 t.Fatal("Update failed for Header") 201 } 202 203 if newChannel.Purpose != channel.Purpose { 204 t.Fatal("Update failed for Purpose") 205 } 206 207 //Update a private channel 208 private.DisplayName = "My new display name for private channel" 209 private.Header = "My fancy private header" 210 private.Purpose = "Mattermost ftw! in private mode" 211 212 newPrivateChannel, resp := Client.UpdateChannel(private) 213 CheckNoError(t, resp) 214 215 if newPrivateChannel.DisplayName != private.DisplayName { 216 t.Fatal("Update failed for DisplayName in private channel") 217 } 218 219 if newPrivateChannel.Header != private.Header { 220 t.Fatal("Update failed for Header in private channel") 221 } 222 223 if newPrivateChannel.Purpose != private.Purpose { 224 t.Fatal("Update failed for Purpose in private channel") 225 } 226 227 //Non existing channel 228 channel1 := &model.Channel{DisplayName: "Test API Name for apiv4", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} 229 _, resp = Client.UpdateChannel(channel1) 230 CheckNotFoundStatus(t, resp) 231 232 //Try to update with not logged user 233 Client.Logout() 234 _, resp = Client.UpdateChannel(channel) 235 CheckUnauthorizedStatus(t, resp) 236 237 //Try to update using another user 238 user := th.CreateUser() 239 Client.Login(user.Email, user.Password) 240 241 channel.DisplayName = "Should not update" 242 _, resp = Client.UpdateChannel(channel) 243 CheckNotFoundStatus(t, resp) 244 245 } 246 247 func TestPatchChannel(t *testing.T) { 248 th := Setup().InitBasic().InitSystemAdmin() 249 defer th.TearDown() 250 Client := th.Client 251 252 patch := &model.ChannelPatch{ 253 Name: new(string), 254 DisplayName: new(string), 255 Header: new(string), 256 Purpose: new(string), 257 } 258 *patch.Name = model.NewId() 259 *patch.DisplayName = model.NewId() 260 *patch.Header = model.NewId() 261 *patch.Purpose = model.NewId() 262 263 channel, resp := Client.PatchChannel(th.BasicChannel.Id, patch) 264 CheckNoError(t, resp) 265 266 if *patch.Name != channel.Name { 267 t.Fatal("do not match") 268 } else if *patch.DisplayName != channel.DisplayName { 269 t.Fatal("do not match") 270 } else if *patch.Header != channel.Header { 271 t.Fatal("do not match") 272 } else if *patch.Purpose != channel.Purpose { 273 t.Fatal("do not match") 274 } 275 276 patch.Name = nil 277 oldName := channel.Name 278 channel, resp = Client.PatchChannel(th.BasicChannel.Id, patch) 279 CheckNoError(t, resp) 280 281 if channel.Name != oldName { 282 t.Fatal("should not have updated") 283 } 284 285 _, resp = Client.PatchChannel("junk", patch) 286 CheckBadRequestStatus(t, resp) 287 288 _, resp = Client.PatchChannel(model.NewId(), patch) 289 CheckNotFoundStatus(t, resp) 290 291 user := th.CreateUser() 292 Client.Login(user.Email, user.Password) 293 _, resp = Client.PatchChannel(th.BasicChannel.Id, patch) 294 CheckForbiddenStatus(t, resp) 295 296 _, resp = th.SystemAdminClient.PatchChannel(th.BasicChannel.Id, patch) 297 CheckNoError(t, resp) 298 299 _, resp = th.SystemAdminClient.PatchChannel(th.BasicPrivateChannel.Id, patch) 300 CheckNoError(t, resp) 301 } 302 303 func TestCreateDirectChannel(t *testing.T) { 304 th := Setup().InitBasic().InitSystemAdmin() 305 defer th.TearDown() 306 Client := th.Client 307 user1 := th.BasicUser 308 user2 := th.BasicUser2 309 user3 := th.CreateUser() 310 311 dm, resp := Client.CreateDirectChannel(user1.Id, user2.Id) 312 CheckNoError(t, resp) 313 314 channelName := "" 315 if user2.Id > user1.Id { 316 channelName = user1.Id + "__" + user2.Id 317 } else { 318 channelName = user2.Id + "__" + user1.Id 319 } 320 321 if dm.Name != channelName { 322 t.Fatal("dm name didn't match") 323 } 324 325 _, resp = Client.CreateDirectChannel("junk", user2.Id) 326 CheckBadRequestStatus(t, resp) 327 328 _, resp = Client.CreateDirectChannel(user1.Id, model.NewId()) 329 CheckBadRequestStatus(t, resp) 330 331 _, resp = Client.CreateDirectChannel(model.NewId(), user1.Id) 332 CheckBadRequestStatus(t, resp) 333 334 _, resp = Client.CreateDirectChannel(model.NewId(), user2.Id) 335 CheckForbiddenStatus(t, resp) 336 337 if r, err := Client.DoApiPost("/channels/direct", "garbage"); err == nil { 338 t.Fatal("should have errored") 339 } else { 340 if r.StatusCode != http.StatusBadRequest { 341 t.Log("actual: " + strconv.Itoa(r.StatusCode)) 342 t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) 343 t.Fatal("wrong status code") 344 } 345 } 346 347 Client.Logout() 348 _, resp = Client.CreateDirectChannel(model.NewId(), user2.Id) 349 CheckUnauthorizedStatus(t, resp) 350 351 _, resp = th.SystemAdminClient.CreateDirectChannel(user3.Id, user2.Id) 352 CheckNoError(t, resp) 353 } 354 355 func TestCreateGroupChannel(t *testing.T) { 356 th := Setup().InitBasic().InitSystemAdmin() 357 defer th.TearDown() 358 Client := th.Client 359 user := th.BasicUser 360 user2 := th.BasicUser2 361 user3 := th.CreateUser() 362 363 userIds := []string{user.Id, user2.Id, user3.Id} 364 365 rgc, resp := Client.CreateGroupChannel(userIds) 366 CheckNoError(t, resp) 367 CheckCreatedStatus(t, resp) 368 369 if rgc == nil { 370 t.Fatal("should have created a group channel") 371 } 372 373 if rgc.Type != model.CHANNEL_GROUP { 374 t.Fatal("should have created a channel of group type") 375 } 376 377 m, _ := th.App.GetChannelMembersPage(rgc.Id, 0, 10) 378 if len(*m) != 3 { 379 t.Fatal("should have 3 channel members") 380 } 381 382 // saving duplicate group channel 383 rgc2, resp := Client.CreateGroupChannel([]string{user3.Id, user2.Id}) 384 CheckNoError(t, resp) 385 386 if rgc.Id != rgc2.Id { 387 t.Fatal("should have returned existing channel") 388 } 389 390 m2, _ := th.App.GetChannelMembersPage(rgc2.Id, 0, 10) 391 if !reflect.DeepEqual(*m, *m2) { 392 t.Fatal("should be equal") 393 } 394 395 rgc, resp = Client.CreateGroupChannel([]string{user2.Id}) 396 CheckBadRequestStatus(t, resp) 397 398 user4 := th.CreateUser() 399 user5 := th.CreateUser() 400 user6 := th.CreateUser() 401 user7 := th.CreateUser() 402 user8 := th.CreateUser() 403 user9 := th.CreateUser() 404 405 rgc, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, user4.Id, user5.Id, user6.Id, user7.Id, user8.Id, user9.Id}) 406 CheckBadRequestStatus(t, resp) 407 408 if rgc != nil { 409 t.Fatal("should return nil") 410 } 411 412 _, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, GenerateTestId()}) 413 CheckBadRequestStatus(t, resp) 414 415 _, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, "junk"}) 416 CheckBadRequestStatus(t, resp) 417 418 Client.Logout() 419 420 _, resp = Client.CreateGroupChannel(userIds) 421 CheckUnauthorizedStatus(t, resp) 422 423 _, resp = th.SystemAdminClient.CreateGroupChannel(userIds) 424 CheckNoError(t, resp) 425 } 426 427 func TestGetChannel(t *testing.T) { 428 th := Setup().InitBasic().InitSystemAdmin() 429 defer th.TearDown() 430 Client := th.Client 431 432 channel, resp := Client.GetChannel(th.BasicChannel.Id, "") 433 CheckNoError(t, resp) 434 435 if channel.Id != th.BasicChannel.Id { 436 t.Fatal("ids did not match") 437 } 438 439 Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 440 _, resp = Client.GetChannel(th.BasicChannel.Id, "") 441 CheckNoError(t, resp) 442 443 channel, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "") 444 CheckNoError(t, resp) 445 446 if channel.Id != th.BasicPrivateChannel.Id { 447 t.Fatal("ids did not match") 448 } 449 450 Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id) 451 _, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "") 452 CheckForbiddenStatus(t, resp) 453 454 _, resp = Client.GetChannel(model.NewId(), "") 455 CheckNotFoundStatus(t, resp) 456 457 Client.Logout() 458 _, resp = Client.GetChannel(th.BasicChannel.Id, "") 459 CheckUnauthorizedStatus(t, resp) 460 461 user := th.CreateUser() 462 Client.Login(user.Email, user.Password) 463 _, resp = Client.GetChannel(th.BasicChannel.Id, "") 464 CheckForbiddenStatus(t, resp) 465 466 _, resp = th.SystemAdminClient.GetChannel(th.BasicChannel.Id, "") 467 CheckNoError(t, resp) 468 469 _, resp = th.SystemAdminClient.GetChannel(th.BasicPrivateChannel.Id, "") 470 CheckNoError(t, resp) 471 472 _, resp = th.SystemAdminClient.GetChannel(th.BasicUser.Id, "") 473 CheckNotFoundStatus(t, resp) 474 } 475 476 func TestGetDeletedChannelsForTeam(t *testing.T) { 477 th := Setup().InitBasic().InitSystemAdmin() 478 defer th.TearDown() 479 Client := th.Client 480 team := th.BasicTeam 481 482 channels, resp := Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 483 CheckForbiddenStatus(t, resp) 484 485 th.LoginTeamAdmin() 486 487 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 488 CheckNoError(t, resp) 489 numInitialChannelsForTeam := len(channels) 490 491 // create and delete public channel 492 publicChannel1 := th.CreatePublicChannel() 493 Client.DeleteChannel(publicChannel1.Id) 494 495 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 496 CheckNoError(t, resp) 497 if len(channels) != numInitialChannelsForTeam+1 { 498 t.Fatal("should be 1 deleted channel") 499 } 500 501 publicChannel2 := th.CreatePublicChannel() 502 Client.DeleteChannel(publicChannel2.Id) 503 504 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 505 CheckNoError(t, resp) 506 if len(channels) != numInitialChannelsForTeam+2 { 507 t.Fatal("should be 2 deleted channels") 508 } 509 510 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 1, "") 511 CheckNoError(t, resp) 512 if len(channels) != 1 { 513 t.Fatal("should be one channel per page") 514 } 515 516 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 1, 1, "") 517 CheckNoError(t, resp) 518 if len(channels) != 1 { 519 t.Fatal("should be one channel per page") 520 } 521 } 522 523 func TestGetPublicChannelsForTeam(t *testing.T) { 524 th := Setup().InitBasic().InitSystemAdmin() 525 defer th.TearDown() 526 Client := th.Client 527 team := th.BasicTeam 528 publicChannel1 := th.BasicChannel 529 publicChannel2 := th.BasicChannel2 530 531 channels, resp := Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 532 CheckNoError(t, resp) 533 if len(channels) != 4 { 534 t.Fatal("wrong length") 535 } 536 537 for i, c := range channels { 538 if c.Type != model.CHANNEL_OPEN { 539 t.Fatal("should include open channel only") 540 } 541 542 // only check the created 2 public channels 543 if i < 2 && !(c.DisplayName == publicChannel1.DisplayName || c.DisplayName == publicChannel2.DisplayName) { 544 t.Logf("channel %v: %v", i, c.DisplayName) 545 t.Fatal("should match public channel display name only") 546 } 547 } 548 549 privateChannel := th.CreatePrivateChannel() 550 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 551 CheckNoError(t, resp) 552 if len(channels) != 4 { 553 t.Fatal("wrong length") 554 } 555 556 for _, c := range channels { 557 if c.Type != model.CHANNEL_OPEN { 558 t.Fatal("should not include private channel") 559 } 560 561 if c.DisplayName == privateChannel.DisplayName { 562 t.Fatal("should not match private channel display name") 563 } 564 } 565 566 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 1, "") 567 CheckNoError(t, resp) 568 if len(channels) != 1 { 569 t.Fatal("should be one channel per page") 570 } 571 572 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 1, 1, "") 573 CheckNoError(t, resp) 574 if len(channels) != 1 { 575 t.Fatal("should be one channel per page") 576 } 577 578 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 10000, 100, "") 579 CheckNoError(t, resp) 580 if len(channels) != 0 { 581 t.Fatal("should be no channel") 582 } 583 584 _, resp = Client.GetPublicChannelsForTeam("junk", 0, 100, "") 585 CheckBadRequestStatus(t, resp) 586 587 _, resp = Client.GetPublicChannelsForTeam(model.NewId(), 0, 100, "") 588 CheckForbiddenStatus(t, resp) 589 590 Client.Logout() 591 _, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 592 CheckUnauthorizedStatus(t, resp) 593 594 user := th.CreateUser() 595 Client.Login(user.Email, user.Password) 596 _, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 597 CheckForbiddenStatus(t, resp) 598 599 _, resp = th.SystemAdminClient.GetPublicChannelsForTeam(team.Id, 0, 100, "") 600 CheckNoError(t, resp) 601 } 602 603 func TestGetPublicChannelsByIdsForTeam(t *testing.T) { 604 th := Setup().InitBasic().InitSystemAdmin() 605 defer th.TearDown() 606 Client := th.Client 607 teamId := th.BasicTeam.Id 608 input := []string{th.BasicChannel.Id} 609 output := []string{th.BasicChannel.DisplayName} 610 611 channels, resp := Client.GetPublicChannelsByIdsForTeam(teamId, input) 612 CheckNoError(t, resp) 613 614 if len(channels) != 1 { 615 t.Fatal("should return 1 channel") 616 } 617 618 if (channels)[0].DisplayName != output[0] { 619 t.Fatal("missing channel") 620 } 621 622 input = append(input, GenerateTestId()) 623 input = append(input, th.BasicChannel2.Id) 624 input = append(input, th.BasicPrivateChannel.Id) 625 output = append(output, th.BasicChannel2.DisplayName) 626 sort.Strings(output) 627 628 channels, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input) 629 CheckNoError(t, resp) 630 631 if len(channels) != 2 { 632 t.Fatal("should return 2 channels") 633 } 634 635 for i, c := range channels { 636 if c.DisplayName != output[i] { 637 t.Fatal("missing channel") 638 } 639 } 640 641 _, resp = Client.GetPublicChannelsByIdsForTeam(GenerateTestId(), input) 642 CheckForbiddenStatus(t, resp) 643 644 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{}) 645 CheckBadRequestStatus(t, resp) 646 647 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{"junk"}) 648 CheckBadRequestStatus(t, resp) 649 650 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{GenerateTestId()}) 651 CheckNotFoundStatus(t, resp) 652 653 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{th.BasicPrivateChannel.Id}) 654 CheckNotFoundStatus(t, resp) 655 656 Client.Logout() 657 658 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input) 659 CheckUnauthorizedStatus(t, resp) 660 661 _, resp = th.SystemAdminClient.GetPublicChannelsByIdsForTeam(teamId, input) 662 CheckNoError(t, resp) 663 } 664 665 func TestGetChannelsForTeamForUser(t *testing.T) { 666 th := Setup().InitBasic().InitSystemAdmin() 667 defer th.TearDown() 668 Client := th.Client 669 670 channels, resp := Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "") 671 CheckNoError(t, resp) 672 673 found := make([]bool, 3) 674 for _, c := range channels { 675 if c.Id == th.BasicChannel.Id { 676 found[0] = true 677 } else if c.Id == th.BasicChannel2.Id { 678 found[1] = true 679 } else if c.Id == th.BasicPrivateChannel.Id { 680 found[2] = true 681 } 682 683 if c.TeamId != th.BasicTeam.Id && c.TeamId != "" { 684 t.Fatal("wrong team") 685 } 686 } 687 688 for _, f := range found { 689 if !f { 690 t.Fatal("missing a channel") 691 } 692 } 693 694 channels, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, resp.Etag) 695 CheckEtag(t, channels, resp) 696 697 _, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, "junk", "") 698 CheckBadRequestStatus(t, resp) 699 700 _, resp = Client.GetChannelsForTeamForUser("junk", th.BasicUser.Id, "") 701 CheckBadRequestStatus(t, resp) 702 703 _, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser2.Id, "") 704 CheckForbiddenStatus(t, resp) 705 706 _, resp = Client.GetChannelsForTeamForUser(model.NewId(), th.BasicUser.Id, "") 707 CheckForbiddenStatus(t, resp) 708 709 _, resp = th.SystemAdminClient.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "") 710 CheckNoError(t, resp) 711 } 712 713 func TestSearchChannels(t *testing.T) { 714 th := Setup().InitBasic().InitSystemAdmin() 715 defer th.TearDown() 716 Client := th.Client 717 718 search := &model.ChannelSearch{Term: th.BasicChannel.Name} 719 720 channels, resp := Client.SearchChannels(th.BasicTeam.Id, search) 721 CheckNoError(t, resp) 722 723 found := false 724 for _, c := range channels { 725 if c.Type != model.CHANNEL_OPEN { 726 t.Fatal("should only return public channels") 727 } 728 729 if c.Id == th.BasicChannel.Id { 730 found = true 731 } 732 } 733 734 if !found { 735 t.Fatal("didn't find channel") 736 } 737 738 search.Term = th.BasicPrivateChannel.Name 739 channels, resp = Client.SearchChannels(th.BasicTeam.Id, search) 740 CheckNoError(t, resp) 741 742 found = false 743 for _, c := range channels { 744 if c.Id == th.BasicPrivateChannel.Id { 745 found = true 746 } 747 } 748 749 if found { 750 t.Fatal("shouldn't find private channel") 751 } 752 753 search.Term = "" 754 _, resp = Client.SearchChannels(th.BasicTeam.Id, search) 755 CheckNoError(t, resp) 756 757 search.Term = th.BasicChannel.Name 758 _, resp = Client.SearchChannels(model.NewId(), search) 759 CheckForbiddenStatus(t, resp) 760 761 _, resp = Client.SearchChannels("junk", search) 762 CheckBadRequestStatus(t, resp) 763 764 _, resp = th.SystemAdminClient.SearchChannels(th.BasicTeam.Id, search) 765 CheckNoError(t, resp) 766 } 767 768 func TestDeleteChannel(t *testing.T) { 769 th := Setup().InitBasic().InitSystemAdmin() 770 defer th.TearDown() 771 Client := th.Client 772 team := th.BasicTeam 773 user := th.BasicUser 774 user2 := th.BasicUser2 775 776 // successful delete of public channel 777 publicChannel1 := th.CreatePublicChannel() 778 pass, resp := Client.DeleteChannel(publicChannel1.Id) 779 CheckNoError(t, resp) 780 781 if !pass { 782 t.Fatal("should have passed") 783 } 784 785 if ch, err := th.App.GetChannel(publicChannel1.Id); err == nil && ch.DeleteAt == 0 { 786 t.Fatal("should have failed to get deleted channel") 787 } else if err := th.App.JoinChannel(ch, user2.Id); err == nil { 788 t.Fatal("should have failed to join deleted channel") 789 } 790 791 post1 := &model.Post{ChannelId: publicChannel1.Id, Message: "a" + GenerateTestId() + "a"} 792 if _, err := Client.CreatePost(post1); err == nil { 793 t.Fatal("should have failed to post to deleted channel") 794 } 795 796 // successful delete of private channel 797 privateChannel2 := th.CreatePrivateChannel() 798 _, resp = Client.DeleteChannel(privateChannel2.Id) 799 CheckNoError(t, resp) 800 801 // successful delete of channel with multiple members 802 publicChannel3 := th.CreatePublicChannel() 803 th.App.AddUserToChannel(user2, publicChannel3) 804 _, resp = Client.DeleteChannel(publicChannel3.Id) 805 CheckNoError(t, resp) 806 807 // successful delete by TeamAdmin of channel created by user 808 publicChannel4 := th.CreatePublicChannel() 809 th.LoginTeamAdmin() 810 _, resp = Client.DeleteChannel(publicChannel4.Id) 811 CheckNoError(t, resp) 812 813 // default channel cannot be deleted. 814 defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, team.Id) 815 pass, resp = Client.DeleteChannel(defaultChannel.Id) 816 CheckBadRequestStatus(t, resp) 817 818 if pass { 819 t.Fatal("should have failed") 820 } 821 822 // check system admin can delete a channel without any appropriate team or channel membership. 823 sdTeam := th.CreateTeamWithClient(Client) 824 sdPublicChannel := &model.Channel{ 825 DisplayName: "dn_" + model.NewId(), 826 Name: GenerateTestChannelName(), 827 Type: model.CHANNEL_OPEN, 828 TeamId: sdTeam.Id, 829 } 830 sdPublicChannel, resp = Client.CreateChannel(sdPublicChannel) 831 CheckNoError(t, resp) 832 _, resp = th.SystemAdminClient.DeleteChannel(sdPublicChannel.Id) 833 CheckNoError(t, resp) 834 835 sdPrivateChannel := &model.Channel{ 836 DisplayName: "dn_" + model.NewId(), 837 Name: GenerateTestChannelName(), 838 Type: model.CHANNEL_PRIVATE, 839 TeamId: sdTeam.Id, 840 } 841 sdPrivateChannel, resp = Client.CreateChannel(sdPrivateChannel) 842 CheckNoError(t, resp) 843 _, resp = th.SystemAdminClient.DeleteChannel(sdPrivateChannel.Id) 844 CheckNoError(t, resp) 845 846 th.LoginBasic() 847 publicChannel5 := th.CreatePublicChannel() 848 Client.Logout() 849 850 Client.Login(user2.Id, user2.Password) 851 _, resp = Client.DeleteChannel(publicChannel5.Id) 852 CheckUnauthorizedStatus(t, resp) 853 854 _, resp = Client.DeleteChannel("junk") 855 CheckUnauthorizedStatus(t, resp) 856 857 Client.Logout() 858 _, resp = Client.DeleteChannel(GenerateTestId()) 859 CheckUnauthorizedStatus(t, resp) 860 861 _, resp = th.SystemAdminClient.DeleteChannel(publicChannel5.Id) 862 CheckNoError(t, resp) 863 864 th.InitBasic().InitSystemAdmin() 865 866 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_ALL }) 867 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_ALL }) 868 th.App.SetLicense(model.NewTestLicense()) 869 870 Client = th.Client 871 team = th.BasicTeam 872 user = th.BasicUser 873 user2 = th.BasicUser2 874 875 // channels created by SystemAdmin 876 publicChannel6 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) 877 privateChannel7 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 878 th.App.AddUserToChannel(user, publicChannel6) 879 th.App.AddUserToChannel(user, privateChannel7) 880 th.App.AddUserToChannel(user2, privateChannel7) 881 882 // successful delete by user 883 _, resp = Client.DeleteChannel(publicChannel6.Id) 884 CheckNoError(t, resp) 885 886 _, resp = Client.DeleteChannel(privateChannel7.Id) 887 CheckNoError(t, resp) 888 889 th.App.UpdateConfig(func(cfg *model.Config) { 890 *cfg.TeamSettings.RestrictPublicChannelDeletion = model.PERMISSIONS_CHANNEL_ADMIN 891 *cfg.TeamSettings.RestrictPrivateChannelDeletion = model.PERMISSIONS_CHANNEL_ADMIN 892 }) 893 894 // channels created by SystemAdmin 895 publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) 896 privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 897 th.App.AddUserToChannel(user, publicChannel6) 898 th.App.AddUserToChannel(user, privateChannel7) 899 th.App.AddUserToChannel(user2, privateChannel7) 900 901 // cannot delete by user 902 _, resp = Client.DeleteChannel(publicChannel6.Id) 903 CheckForbiddenStatus(t, resp) 904 905 _, resp = Client.DeleteChannel(privateChannel7.Id) 906 CheckForbiddenStatus(t, resp) 907 908 // successful delete by channel admin 909 th.MakeUserChannelAdmin(user, publicChannel6) 910 th.MakeUserChannelAdmin(user, privateChannel7) 911 th.App.Srv.Store.Channel().ClearCaches() 912 913 _, resp = Client.DeleteChannel(publicChannel6.Id) 914 CheckNoError(t, resp) 915 916 _, resp = Client.DeleteChannel(privateChannel7.Id) 917 CheckNoError(t, resp) 918 919 // // channels created by SystemAdmin 920 publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) 921 privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 922 th.App.AddUserToChannel(user, publicChannel6) 923 th.App.AddUserToChannel(user, privateChannel7) 924 th.App.AddUserToChannel(user2, privateChannel7) 925 926 // successful delete by team admin 927 th.UpdateUserToTeamAdmin(user, team) 928 th.App.InvalidateAllCaches() 929 th.App.SetLicense(model.NewTestLicense()) 930 931 _, resp = Client.DeleteChannel(publicChannel6.Id) 932 CheckNoError(t, resp) 933 934 _, resp = Client.DeleteChannel(privateChannel7.Id) 935 CheckNoError(t, resp) 936 937 th.App.UpdateConfig(func(cfg *model.Config) { 938 *cfg.TeamSettings.RestrictPublicChannelDeletion = model.PERMISSIONS_TEAM_ADMIN 939 *cfg.TeamSettings.RestrictPrivateChannelDeletion = model.PERMISSIONS_TEAM_ADMIN 940 }) 941 th.UpdateUserToNonTeamAdmin(user, team) 942 th.App.InvalidateAllCaches() 943 th.App.SetLicense(model.NewTestLicense()) 944 945 // channels created by SystemAdmin 946 publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) 947 privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 948 th.App.AddUserToChannel(user, publicChannel6) 949 th.App.AddUserToChannel(user, privateChannel7) 950 th.App.AddUserToChannel(user2, privateChannel7) 951 952 // cannot delete by user 953 _, resp = Client.DeleteChannel(publicChannel6.Id) 954 CheckForbiddenStatus(t, resp) 955 956 _, resp = Client.DeleteChannel(privateChannel7.Id) 957 CheckForbiddenStatus(t, resp) 958 959 // // cannot delete by channel admin 960 th.MakeUserChannelAdmin(user, publicChannel6) 961 th.MakeUserChannelAdmin(user, privateChannel7) 962 th.App.Srv.Store.Channel().ClearCaches() 963 964 _, resp = Client.DeleteChannel(publicChannel6.Id) 965 CheckForbiddenStatus(t, resp) 966 967 _, resp = Client.DeleteChannel(privateChannel7.Id) 968 CheckForbiddenStatus(t, resp) 969 970 // successful delete by team admin 971 th.UpdateUserToTeamAdmin(th.BasicUser, team) 972 th.App.InvalidateAllCaches() 973 th.App.SetLicense(model.NewTestLicense()) 974 975 _, resp = Client.DeleteChannel(publicChannel6.Id) 976 CheckNoError(t, resp) 977 978 _, resp = Client.DeleteChannel(privateChannel7.Id) 979 CheckNoError(t, resp) 980 981 th.App.UpdateConfig(func(cfg *model.Config) { 982 *cfg.TeamSettings.RestrictPublicChannelDeletion = model.PERMISSIONS_SYSTEM_ADMIN 983 *cfg.TeamSettings.RestrictPrivateChannelDeletion = model.PERMISSIONS_SYSTEM_ADMIN 984 }) 985 986 // channels created by SystemAdmin 987 publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) 988 privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 989 th.App.AddUserToChannel(user, publicChannel6) 990 th.App.AddUserToChannel(user, privateChannel7) 991 th.App.AddUserToChannel(user2, privateChannel7) 992 993 // cannot delete by user 994 _, resp = Client.DeleteChannel(publicChannel6.Id) 995 CheckForbiddenStatus(t, resp) 996 997 _, resp = Client.DeleteChannel(privateChannel7.Id) 998 CheckForbiddenStatus(t, resp) 999 1000 // cannot delete by channel admin 1001 th.MakeUserChannelAdmin(user, publicChannel6) 1002 th.MakeUserChannelAdmin(user, privateChannel7) 1003 th.App.Srv.Store.Channel().ClearCaches() 1004 1005 _, resp = Client.DeleteChannel(publicChannel6.Id) 1006 CheckForbiddenStatus(t, resp) 1007 1008 _, resp = Client.DeleteChannel(privateChannel7.Id) 1009 CheckForbiddenStatus(t, resp) 1010 1011 // cannot delete by team admin 1012 th.UpdateUserToTeamAdmin(th.BasicUser, team) 1013 th.App.InvalidateAllCaches() 1014 th.App.SetLicense(model.NewTestLicense()) 1015 1016 _, resp = Client.DeleteChannel(publicChannel6.Id) 1017 CheckForbiddenStatus(t, resp) 1018 1019 _, resp = Client.DeleteChannel(privateChannel7.Id) 1020 CheckForbiddenStatus(t, resp) 1021 1022 // successful delete by SystemAdmin 1023 _, resp = th.SystemAdminClient.DeleteChannel(publicChannel6.Id) 1024 CheckNoError(t, resp) 1025 1026 _, resp = th.SystemAdminClient.DeleteChannel(privateChannel7.Id) 1027 CheckNoError(t, resp) 1028 1029 // last member of a public channel should have required permission to delete 1030 publicChannel6 = th.CreateChannelWithClient(th.Client, model.CHANNEL_OPEN) 1031 _, resp = Client.DeleteChannel(publicChannel6.Id) 1032 CheckForbiddenStatus(t, resp) 1033 1034 // last member of a private channel should not be able to delete it if they don't have required permissions 1035 privateChannel7 = th.CreateChannelWithClient(th.Client, model.CHANNEL_PRIVATE) 1036 _, resp = Client.DeleteChannel(privateChannel7.Id) 1037 CheckForbiddenStatus(t, resp) 1038 } 1039 1040 func TestRestoreChannel(t *testing.T) { 1041 th := Setup().InitBasic().InitSystemAdmin() 1042 defer th.TearDown() 1043 Client := th.Client 1044 1045 publicChannel1 := th.CreatePublicChannel() 1046 Client.DeleteChannel(publicChannel1.Id) 1047 1048 privateChannel1 := th.CreatePrivateChannel() 1049 Client.DeleteChannel(privateChannel1.Id) 1050 1051 _, resp := Client.RestoreChannel(publicChannel1.Id) 1052 CheckForbiddenStatus(t, resp) 1053 1054 _, resp = Client.RestoreChannel(privateChannel1.Id) 1055 CheckForbiddenStatus(t, resp) 1056 1057 th.LoginTeamAdmin() 1058 1059 _, resp = Client.RestoreChannel(publicChannel1.Id) 1060 CheckOKStatus(t, resp) 1061 1062 _, resp = Client.RestoreChannel(privateChannel1.Id) 1063 CheckOKStatus(t, resp) 1064 } 1065 1066 func TestGetChannelByName(t *testing.T) { 1067 th := Setup().InitBasic().InitSystemAdmin() 1068 defer th.TearDown() 1069 Client := th.Client 1070 1071 channel, resp := Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1072 CheckNoError(t, resp) 1073 1074 if channel.Name != th.BasicChannel.Name { 1075 t.Fatal("names did not match") 1076 } 1077 1078 channel, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "") 1079 CheckNoError(t, resp) 1080 1081 if channel.Name != th.BasicPrivateChannel.Name { 1082 t.Fatal("names did not match") 1083 } 1084 1085 _, resp = Client.GetChannelByName(strings.ToUpper(th.BasicPrivateChannel.Name), th.BasicTeam.Id, "") 1086 CheckNoError(t, resp) 1087 1088 Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 1089 _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1090 CheckNoError(t, resp) 1091 1092 Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id) 1093 _, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "") 1094 CheckForbiddenStatus(t, resp) 1095 1096 _, resp = Client.GetChannelByName(GenerateTestChannelName(), th.BasicTeam.Id, "") 1097 CheckNotFoundStatus(t, resp) 1098 1099 _, resp = Client.GetChannelByName(GenerateTestChannelName(), "junk", "") 1100 CheckBadRequestStatus(t, resp) 1101 1102 Client.Logout() 1103 _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1104 CheckUnauthorizedStatus(t, resp) 1105 1106 user := th.CreateUser() 1107 Client.Login(user.Email, user.Password) 1108 _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1109 CheckForbiddenStatus(t, resp) 1110 1111 _, resp = th.SystemAdminClient.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1112 CheckNoError(t, resp) 1113 } 1114 1115 func TestGetChannelByNameForTeamName(t *testing.T) { 1116 th := Setup().InitBasic().InitSystemAdmin() 1117 defer th.TearDown() 1118 Client := th.Client 1119 1120 channel, resp := th.SystemAdminClient.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1121 CheckNoError(t, resp) 1122 1123 if channel.Name != th.BasicChannel.Name { 1124 t.Fatal("names did not match") 1125 } 1126 1127 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1128 CheckNoError(t, resp) 1129 1130 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, model.NewRandomString(15), "") 1131 CheckNotFoundStatus(t, resp) 1132 1133 _, resp = Client.GetChannelByNameForTeamName(GenerateTestChannelName(), th.BasicTeam.Name, "") 1134 CheckNotFoundStatus(t, resp) 1135 1136 Client.Logout() 1137 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1138 CheckUnauthorizedStatus(t, resp) 1139 1140 user := th.CreateUser() 1141 Client.Login(user.Email, user.Password) 1142 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1143 CheckForbiddenStatus(t, resp) 1144 } 1145 1146 func TestGetChannelMembers(t *testing.T) { 1147 th := Setup().InitBasic().InitSystemAdmin() 1148 defer th.TearDown() 1149 Client := th.Client 1150 1151 members, resp := Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1152 CheckNoError(t, resp) 1153 1154 if len(*members) != 3 { 1155 t.Fatal("should only be 3 users in channel") 1156 } 1157 1158 members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 2, "") 1159 CheckNoError(t, resp) 1160 1161 if len(*members) != 2 { 1162 t.Fatal("should only be 2 users") 1163 } 1164 1165 members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 1, 1, "") 1166 CheckNoError(t, resp) 1167 1168 if len(*members) != 1 { 1169 t.Fatal("should only be 1 user") 1170 } 1171 1172 members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 1000, 100000, "") 1173 CheckNoError(t, resp) 1174 1175 if len(*members) != 0 { 1176 t.Fatal("should be 0 users") 1177 } 1178 1179 _, resp = Client.GetChannelMembers("", 0, 60, "") 1180 CheckBadRequestStatus(t, resp) 1181 1182 _, resp = Client.GetChannelMembers("junk", 0, 60, "") 1183 CheckBadRequestStatus(t, resp) 1184 1185 _, resp = Client.GetChannelMembers(model.NewId(), 0, 60, "") 1186 CheckForbiddenStatus(t, resp) 1187 1188 Client.Logout() 1189 _, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1190 CheckUnauthorizedStatus(t, resp) 1191 1192 user := th.CreateUser() 1193 Client.Login(user.Email, user.Password) 1194 _, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1195 CheckForbiddenStatus(t, resp) 1196 1197 _, resp = th.SystemAdminClient.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1198 CheckNoError(t, resp) 1199 } 1200 1201 func TestGetChannelMembersByIds(t *testing.T) { 1202 th := Setup().InitBasic().InitSystemAdmin() 1203 defer th.TearDown() 1204 Client := th.Client 1205 1206 cm, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id}) 1207 CheckNoError(t, resp) 1208 1209 if (*cm)[0].UserId != th.BasicUser.Id { 1210 t.Fatal("returned wrong user") 1211 } 1212 1213 _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{}) 1214 CheckBadRequestStatus(t, resp) 1215 1216 cm1, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk"}) 1217 CheckNoError(t, resp) 1218 if len(*cm1) > 0 { 1219 t.Fatal("no users should be returned") 1220 } 1221 1222 cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk", th.BasicUser.Id}) 1223 CheckNoError(t, resp) 1224 if len(*cm1) != 1 { 1225 t.Fatal("1 member should be returned") 1226 } 1227 1228 cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id}) 1229 CheckNoError(t, resp) 1230 if len(*cm1) != 2 { 1231 t.Fatal("2 members should be returned") 1232 } 1233 1234 _, resp = Client.GetChannelMembersByIds("junk", []string{th.BasicUser.Id}) 1235 CheckBadRequestStatus(t, resp) 1236 1237 _, resp = Client.GetChannelMembersByIds(model.NewId(), []string{th.BasicUser.Id}) 1238 CheckForbiddenStatus(t, resp) 1239 1240 Client.Logout() 1241 _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id}) 1242 CheckUnauthorizedStatus(t, resp) 1243 1244 _, resp = th.SystemAdminClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id}) 1245 CheckNoError(t, resp) 1246 } 1247 1248 func TestGetChannelMember(t *testing.T) { 1249 th := Setup().InitBasic().InitSystemAdmin() 1250 defer th.TearDown() 1251 Client := th.Client 1252 1253 member, resp := Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1254 CheckNoError(t, resp) 1255 1256 if member.ChannelId != th.BasicChannel.Id { 1257 t.Fatal("wrong channel id") 1258 } 1259 1260 if member.UserId != th.BasicUser.Id { 1261 t.Fatal("wrong user id") 1262 } 1263 1264 _, resp = Client.GetChannelMember("", th.BasicUser.Id, "") 1265 CheckNotFoundStatus(t, resp) 1266 1267 _, resp = Client.GetChannelMember("junk", th.BasicUser.Id, "") 1268 CheckBadRequestStatus(t, resp) 1269 1270 _, resp = Client.GetChannelMember(model.NewId(), th.BasicUser.Id, "") 1271 CheckForbiddenStatus(t, resp) 1272 1273 _, resp = Client.GetChannelMember(th.BasicChannel.Id, "", "") 1274 CheckNotFoundStatus(t, resp) 1275 1276 _, resp = Client.GetChannelMember(th.BasicChannel.Id, "junk", "") 1277 CheckBadRequestStatus(t, resp) 1278 1279 _, resp = Client.GetChannelMember(th.BasicChannel.Id, model.NewId(), "") 1280 CheckNotFoundStatus(t, resp) 1281 1282 Client.Logout() 1283 _, resp = Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1284 CheckUnauthorizedStatus(t, resp) 1285 1286 user := th.CreateUser() 1287 Client.Login(user.Email, user.Password) 1288 _, resp = Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1289 CheckForbiddenStatus(t, resp) 1290 1291 _, resp = th.SystemAdminClient.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1292 CheckNoError(t, resp) 1293 } 1294 1295 func TestGetChannelMembersForUser(t *testing.T) { 1296 th := Setup().InitBasic().InitSystemAdmin() 1297 defer th.TearDown() 1298 Client := th.Client 1299 1300 members, resp := Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1301 CheckNoError(t, resp) 1302 1303 if len(*members) != 5 { 1304 t.Fatal("should have 5 members on team") 1305 } 1306 1307 _, resp = Client.GetChannelMembersForUser("", th.BasicTeam.Id, "") 1308 CheckNotFoundStatus(t, resp) 1309 1310 _, resp = Client.GetChannelMembersForUser("junk", th.BasicTeam.Id, "") 1311 CheckBadRequestStatus(t, resp) 1312 1313 _, resp = Client.GetChannelMembersForUser(model.NewId(), th.BasicTeam.Id, "") 1314 CheckForbiddenStatus(t, resp) 1315 1316 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, "", "") 1317 CheckNotFoundStatus(t, resp) 1318 1319 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, "junk", "") 1320 CheckBadRequestStatus(t, resp) 1321 1322 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, model.NewId(), "") 1323 CheckForbiddenStatus(t, resp) 1324 1325 Client.Logout() 1326 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1327 CheckUnauthorizedStatus(t, resp) 1328 1329 user := th.CreateUser() 1330 Client.Login(user.Email, user.Password) 1331 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1332 CheckForbiddenStatus(t, resp) 1333 1334 _, resp = th.SystemAdminClient.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1335 CheckNoError(t, resp) 1336 } 1337 1338 func TestViewChannel(t *testing.T) { 1339 th := Setup().InitBasic().InitSystemAdmin() 1340 defer th.TearDown() 1341 Client := th.Client 1342 1343 view := &model.ChannelView{ 1344 ChannelId: th.BasicChannel.Id, 1345 } 1346 1347 viewResp, resp := Client.ViewChannel(th.BasicUser.Id, view) 1348 CheckNoError(t, resp) 1349 1350 if viewResp.Status != "OK" { 1351 t.Fatal("should have passed") 1352 } 1353 1354 channel, _ := th.App.GetChannel(th.BasicChannel.Id) 1355 1356 if lastViewedAt := viewResp.LastViewedAtTimes[channel.Id]; lastViewedAt != channel.LastPostAt { 1357 t.Fatal("LastPostAt does not match returned LastViewedAt time") 1358 } 1359 1360 view.PrevChannelId = th.BasicChannel.Id 1361 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1362 CheckNoError(t, resp) 1363 1364 view.PrevChannelId = "" 1365 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1366 CheckNoError(t, resp) 1367 1368 view.PrevChannelId = "junk" 1369 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1370 CheckNoError(t, resp) 1371 1372 member, resp := Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1373 CheckNoError(t, resp) 1374 channel, resp = Client.GetChannel(th.BasicChannel.Id, "") 1375 CheckNoError(t, resp) 1376 1377 if member.MsgCount != channel.TotalMsgCount { 1378 t.Fatal("should match message counts") 1379 } 1380 1381 if member.MentionCount != 0 { 1382 t.Fatal("should have no mentions") 1383 } 1384 1385 _, resp = Client.ViewChannel("junk", view) 1386 CheckBadRequestStatus(t, resp) 1387 1388 _, resp = Client.ViewChannel(th.BasicUser2.Id, view) 1389 CheckForbiddenStatus(t, resp) 1390 1391 if r, err := Client.DoApiPost(fmt.Sprintf("/channels/members/%v/view", th.BasicUser.Id), "garbage"); err == nil { 1392 t.Fatal("should have errored") 1393 } else { 1394 if r.StatusCode != http.StatusBadRequest { 1395 t.Log("actual: " + strconv.Itoa(r.StatusCode)) 1396 t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) 1397 t.Fatal("wrong status code") 1398 } 1399 } 1400 1401 Client.Logout() 1402 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1403 CheckUnauthorizedStatus(t, resp) 1404 1405 _, resp = th.SystemAdminClient.ViewChannel(th.BasicUser.Id, view) 1406 CheckNoError(t, resp) 1407 } 1408 1409 func TestGetChannelUnread(t *testing.T) { 1410 th := Setup().InitBasic().InitSystemAdmin() 1411 defer th.TearDown() 1412 Client := th.Client 1413 user := th.BasicUser 1414 channel := th.BasicChannel 1415 1416 channelUnread, resp := Client.GetChannelUnread(channel.Id, user.Id) 1417 CheckNoError(t, resp) 1418 if channelUnread.TeamId != th.BasicTeam.Id { 1419 t.Fatal("wrong team id returned for a regular user call") 1420 } else if channelUnread.ChannelId != channel.Id { 1421 t.Fatal("wrong team id returned for a regular user call") 1422 } 1423 1424 _, resp = Client.GetChannelUnread("junk", user.Id) 1425 CheckBadRequestStatus(t, resp) 1426 1427 _, resp = Client.GetChannelUnread(channel.Id, "junk") 1428 CheckBadRequestStatus(t, resp) 1429 1430 _, resp = Client.GetChannelUnread(channel.Id, model.NewId()) 1431 CheckForbiddenStatus(t, resp) 1432 1433 _, resp = Client.GetChannelUnread(model.NewId(), user.Id) 1434 CheckForbiddenStatus(t, resp) 1435 1436 newUser := th.CreateUser() 1437 Client.Login(newUser.Email, newUser.Password) 1438 _, resp = Client.GetChannelUnread(th.BasicChannel.Id, user.Id) 1439 CheckForbiddenStatus(t, resp) 1440 1441 Client.Logout() 1442 1443 _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, user.Id) 1444 CheckNoError(t, resp) 1445 1446 _, resp = th.SystemAdminClient.GetChannelUnread(model.NewId(), user.Id) 1447 CheckForbiddenStatus(t, resp) 1448 1449 _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, model.NewId()) 1450 CheckNotFoundStatus(t, resp) 1451 } 1452 1453 func TestGetChannelStats(t *testing.T) { 1454 th := Setup().InitBasic().InitSystemAdmin() 1455 defer th.TearDown() 1456 Client := th.Client 1457 channel := th.CreatePrivateChannel() 1458 1459 stats, resp := Client.GetChannelStats(channel.Id, "") 1460 CheckNoError(t, resp) 1461 1462 if stats.ChannelId != channel.Id { 1463 t.Fatal("couldnt't get extra info") 1464 } else if stats.MemberCount != 1 { 1465 t.Fatal("got incorrect member count") 1466 } 1467 1468 _, resp = Client.GetChannelStats("junk", "") 1469 CheckBadRequestStatus(t, resp) 1470 1471 _, resp = Client.GetChannelStats(model.NewId(), "") 1472 CheckForbiddenStatus(t, resp) 1473 1474 Client.Logout() 1475 _, resp = Client.GetChannelStats(channel.Id, "") 1476 CheckUnauthorizedStatus(t, resp) 1477 1478 th.LoginBasic2() 1479 1480 _, resp = Client.GetChannelStats(channel.Id, "") 1481 CheckForbiddenStatus(t, resp) 1482 1483 _, resp = th.SystemAdminClient.GetChannelStats(channel.Id, "") 1484 CheckNoError(t, resp) 1485 } 1486 1487 func TestGetPinnedPosts(t *testing.T) { 1488 th := Setup().InitBasic().InitSystemAdmin() 1489 defer th.TearDown() 1490 Client := th.Client 1491 channel := th.BasicChannel 1492 1493 posts, resp := Client.GetPinnedPosts(channel.Id, "") 1494 CheckNoError(t, resp) 1495 if len(posts.Posts) != 0 { 1496 t.Fatal("should not have gotten a pinned post") 1497 } 1498 1499 pinnedPost := th.CreatePinnedPost() 1500 posts, resp = Client.GetPinnedPosts(channel.Id, "") 1501 CheckNoError(t, resp) 1502 if len(posts.Posts) != 1 { 1503 t.Fatal("should have returned 1 pinned post") 1504 } 1505 if _, ok := posts.Posts[pinnedPost.Id]; !ok { 1506 t.Fatal("missing pinned post") 1507 } 1508 1509 posts, resp = Client.GetPinnedPosts(channel.Id, resp.Etag) 1510 CheckEtag(t, posts, resp) 1511 1512 _, resp = Client.GetPinnedPosts(GenerateTestId(), "") 1513 CheckForbiddenStatus(t, resp) 1514 1515 _, resp = Client.GetPinnedPosts("junk", "") 1516 CheckBadRequestStatus(t, resp) 1517 1518 Client.Logout() 1519 _, resp = Client.GetPinnedPosts(channel.Id, "") 1520 CheckUnauthorizedStatus(t, resp) 1521 1522 _, resp = th.SystemAdminClient.GetPinnedPosts(channel.Id, "") 1523 CheckNoError(t, resp) 1524 } 1525 1526 func TestUpdateChannelRoles(t *testing.T) { 1527 th := Setup().InitBasic().InitSystemAdmin() 1528 defer th.TearDown() 1529 Client := th.Client 1530 1531 const CHANNEL_ADMIN = "channel_admin channel_user" 1532 const CHANNEL_MEMBER = "channel_user" 1533 1534 // User 1 creates a channel, making them channel admin by default. 1535 channel := th.CreatePublicChannel() 1536 1537 // Adds User 2 to the channel, making them a channel member by default. 1538 th.App.AddUserToChannel(th.BasicUser2, channel) 1539 1540 // User 1 promotes User 2 1541 pass, resp := Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_ADMIN) 1542 CheckNoError(t, resp) 1543 1544 if !pass { 1545 t.Fatal("should have passed") 1546 } 1547 1548 member, resp := Client.GetChannelMember(channel.Id, th.BasicUser2.Id, "") 1549 CheckNoError(t, resp) 1550 1551 if member.Roles != CHANNEL_ADMIN { 1552 t.Fatal("roles don't match") 1553 } 1554 1555 // User 1 demotes User 2 1556 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_MEMBER) 1557 CheckNoError(t, resp) 1558 1559 th.LoginBasic2() 1560 1561 // User 2 cannot demote User 1 1562 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER) 1563 CheckForbiddenStatus(t, resp) 1564 1565 // User 2 cannot promote self 1566 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_ADMIN) 1567 CheckForbiddenStatus(t, resp) 1568 1569 th.LoginBasic() 1570 1571 // User 1 demotes self 1572 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER) 1573 CheckNoError(t, resp) 1574 1575 // System Admin promotes User 1 1576 _, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_ADMIN) 1577 CheckNoError(t, resp) 1578 1579 // System Admin demotes User 1 1580 _, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER) 1581 CheckNoError(t, resp) 1582 1583 // System Admin promotes User 1 1584 pass, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_ADMIN) 1585 CheckNoError(t, resp) 1586 1587 th.LoginBasic() 1588 1589 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, "junk") 1590 CheckBadRequestStatus(t, resp) 1591 1592 _, resp = Client.UpdateChannelRoles(channel.Id, "junk", CHANNEL_MEMBER) 1593 CheckBadRequestStatus(t, resp) 1594 1595 _, resp = Client.UpdateChannelRoles("junk", th.BasicUser.Id, CHANNEL_MEMBER) 1596 CheckBadRequestStatus(t, resp) 1597 1598 _, resp = Client.UpdateChannelRoles(channel.Id, model.NewId(), CHANNEL_MEMBER) 1599 CheckNotFoundStatus(t, resp) 1600 1601 _, resp = Client.UpdateChannelRoles(model.NewId(), th.BasicUser.Id, CHANNEL_MEMBER) 1602 CheckForbiddenStatus(t, resp) 1603 } 1604 1605 func TestUpdateChannelNotifyProps(t *testing.T) { 1606 th := Setup().InitBasic().InitSystemAdmin() 1607 defer th.TearDown() 1608 Client := th.Client 1609 1610 props := map[string]string{} 1611 props[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION 1612 props[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION 1613 1614 pass, resp := Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props) 1615 CheckNoError(t, resp) 1616 1617 if !pass { 1618 t.Fatal("should have passed") 1619 } 1620 1621 member, err := th.App.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id) 1622 if err != nil { 1623 t.Fatal(err) 1624 } 1625 1626 if member.NotifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION { 1627 t.Fatal("bad update") 1628 } else if member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_MENTION { 1629 t.Fatal("bad update") 1630 } 1631 1632 _, resp = Client.UpdateChannelNotifyProps("junk", th.BasicUser.Id, props) 1633 CheckBadRequestStatus(t, resp) 1634 1635 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, "junk", props) 1636 CheckBadRequestStatus(t, resp) 1637 1638 _, resp = Client.UpdateChannelNotifyProps(model.NewId(), th.BasicUser.Id, props) 1639 CheckNotFoundStatus(t, resp) 1640 1641 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, model.NewId(), props) 1642 CheckForbiddenStatus(t, resp) 1643 1644 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, map[string]string{}) 1645 CheckNoError(t, resp) 1646 1647 Client.Logout() 1648 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props) 1649 CheckUnauthorizedStatus(t, resp) 1650 1651 _, resp = th.SystemAdminClient.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props) 1652 CheckNoError(t, resp) 1653 } 1654 1655 func TestAddChannelMember(t *testing.T) { 1656 th := Setup().InitBasic().InitSystemAdmin() 1657 defer th.TearDown() 1658 Client := th.Client 1659 user := th.BasicUser 1660 user2 := th.BasicUser2 1661 team := th.BasicTeam 1662 publicChannel := th.CreatePublicChannel() 1663 privateChannel := th.CreatePrivateChannel() 1664 1665 user3 := th.CreateUserWithClient(th.SystemAdminClient) 1666 _, resp := th.SystemAdminClient.AddTeamMember(team.Id, user3.Id) 1667 CheckNoError(t, resp) 1668 1669 cm, resp := Client.AddChannelMember(publicChannel.Id, user2.Id) 1670 CheckNoError(t, resp) 1671 CheckCreatedStatus(t, resp) 1672 1673 if cm.ChannelId != publicChannel.Id { 1674 t.Fatal("should have returned exact channel") 1675 } 1676 1677 if cm.UserId != user2.Id { 1678 t.Fatal("should have returned exact user added to public channel") 1679 } 1680 1681 cm, resp = Client.AddChannelMember(privateChannel.Id, user2.Id) 1682 CheckNoError(t, resp) 1683 1684 if cm.ChannelId != privateChannel.Id { 1685 t.Fatal("should have returned exact channel") 1686 } 1687 1688 if cm.UserId != user2.Id { 1689 t.Fatal("should have returned exact user added to private channel") 1690 } 1691 1692 post := &model.Post{ChannelId: publicChannel.Id, Message: "a" + GenerateTestId() + "a"} 1693 rpost, err := Client.CreatePost(post) 1694 if err == nil { 1695 t.Fatal("should have created a post") 1696 } 1697 1698 Client.RemoveUserFromChannel(publicChannel.Id, user.Id) 1699 _, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, rpost.Id) 1700 CheckNoError(t, resp) 1701 CheckCreatedStatus(t, resp) 1702 1703 Client.RemoveUserFromChannel(publicChannel.Id, user.Id) 1704 _, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, "junk") 1705 CheckBadRequestStatus(t, resp) 1706 1707 _, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, GenerateTestId()) 1708 CheckNotFoundStatus(t, resp) 1709 1710 Client.RemoveUserFromChannel(publicChannel.Id, user.Id) 1711 _, resp = Client.AddChannelMember(publicChannel.Id, user.Id) 1712 CheckNoError(t, resp) 1713 1714 cm, resp = Client.AddChannelMember(publicChannel.Id, "junk") 1715 CheckBadRequestStatus(t, resp) 1716 1717 if cm != nil { 1718 t.Fatal("should return nothing") 1719 } 1720 1721 _, resp = Client.AddChannelMember(publicChannel.Id, GenerateTestId()) 1722 CheckNotFoundStatus(t, resp) 1723 1724 _, resp = Client.AddChannelMember("junk", user2.Id) 1725 CheckBadRequestStatus(t, resp) 1726 1727 _, resp = Client.AddChannelMember(GenerateTestId(), user2.Id) 1728 CheckNotFoundStatus(t, resp) 1729 1730 otherUser := th.CreateUser() 1731 otherChannel := th.CreatePublicChannel() 1732 Client.Logout() 1733 Client.Login(user2.Id, user2.Password) 1734 1735 _, resp = Client.AddChannelMember(publicChannel.Id, otherUser.Id) 1736 CheckUnauthorizedStatus(t, resp) 1737 1738 _, resp = Client.AddChannelMember(privateChannel.Id, otherUser.Id) 1739 CheckUnauthorizedStatus(t, resp) 1740 1741 _, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id) 1742 CheckUnauthorizedStatus(t, resp) 1743 1744 Client.Logout() 1745 Client.Login(user.Id, user.Password) 1746 1747 // should fail adding user who is not a member of the team 1748 _, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id) 1749 CheckUnauthorizedStatus(t, resp) 1750 1751 Client.DeleteChannel(otherChannel.Id) 1752 1753 // should fail adding user to a deleted channel 1754 _, resp = Client.AddChannelMember(otherChannel.Id, user2.Id) 1755 CheckUnauthorizedStatus(t, resp) 1756 1757 Client.Logout() 1758 _, resp = Client.AddChannelMember(publicChannel.Id, user2.Id) 1759 CheckUnauthorizedStatus(t, resp) 1760 1761 _, resp = Client.AddChannelMember(privateChannel.Id, user2.Id) 1762 CheckUnauthorizedStatus(t, resp) 1763 1764 _, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user2.Id) 1765 CheckNoError(t, resp) 1766 1767 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 1768 CheckNoError(t, resp) 1769 1770 // Test policy does not apply to TE. 1771 th.App.UpdateConfig(func(cfg *model.Config) { 1772 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_CHANNEL_ADMIN 1773 }) 1774 1775 Client.Login(user2.Username, user2.Password) 1776 privateChannel = th.CreatePrivateChannel() 1777 _, resp = Client.AddChannelMember(privateChannel.Id, user.Id) 1778 CheckNoError(t, resp) 1779 Client.Logout() 1780 1781 Client.Login(user.Username, user.Password) 1782 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1783 CheckNoError(t, resp) 1784 Client.Logout() 1785 1786 // Add a license 1787 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_ALL }) 1788 th.App.SetLicense(model.NewTestLicense()) 1789 1790 // Check that a regular channel user can add other users. 1791 Client.Login(user2.Username, user2.Password) 1792 privateChannel = th.CreatePrivateChannel() 1793 _, resp = Client.AddChannelMember(privateChannel.Id, user.Id) 1794 CheckNoError(t, resp) 1795 Client.Logout() 1796 1797 Client.Login(user.Username, user.Password) 1798 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1799 CheckNoError(t, resp) 1800 Client.Logout() 1801 1802 // Test with CHANNEL_ADMIN level permission. 1803 th.App.UpdateConfig(func(cfg *model.Config) { 1804 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_CHANNEL_ADMIN 1805 }) 1806 1807 Client.Login(user2.Username, user2.Password) 1808 privateChannel = th.CreatePrivateChannel() 1809 _, resp = Client.AddChannelMember(privateChannel.Id, user.Id) 1810 CheckNoError(t, resp) 1811 Client.Logout() 1812 1813 Client.Login(user.Username, user.Password) 1814 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1815 CheckForbiddenStatus(t, resp) 1816 Client.Logout() 1817 1818 th.MakeUserChannelAdmin(user, privateChannel) 1819 th.App.InvalidateAllCaches() 1820 th.App.SetLicense(model.NewTestLicense()) 1821 1822 Client.Login(user.Username, user.Password) 1823 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1824 CheckNoError(t, resp) 1825 Client.Logout() 1826 1827 // Test with TEAM_ADMIN level permission. 1828 th.App.UpdateConfig(func(cfg *model.Config) { 1829 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_TEAM_ADMIN 1830 }) 1831 1832 Client.Login(user2.Username, user2.Password) 1833 privateChannel = th.CreatePrivateChannel() 1834 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user.Id) 1835 CheckNoError(t, resp) 1836 Client.Logout() 1837 1838 Client.Login(user.Username, user.Password) 1839 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1840 CheckForbiddenStatus(t, resp) 1841 Client.Logout() 1842 1843 th.UpdateUserToTeamAdmin(user, team) 1844 th.App.InvalidateAllCaches() 1845 th.App.SetLicense(model.NewTestLicense()) 1846 1847 Client.Login(user.Username, user.Password) 1848 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1849 CheckNoError(t, resp) 1850 Client.Logout() 1851 1852 // Test with SYSTEM_ADMIN level permission. 1853 th.App.UpdateConfig(func(cfg *model.Config) { 1854 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_SYSTEM_ADMIN 1855 }) 1856 1857 Client.Login(user2.Username, user2.Password) 1858 privateChannel = th.CreatePrivateChannel() 1859 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user.Id) 1860 CheckNoError(t, resp) 1861 Client.Logout() 1862 1863 Client.Login(user.Username, user.Password) 1864 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1865 CheckForbiddenStatus(t, resp) 1866 Client.Logout() 1867 1868 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user3.Id) 1869 CheckNoError(t, resp) 1870 } 1871 1872 func TestRemoveChannelMember(t *testing.T) { 1873 th := Setup().InitBasic().InitSystemAdmin() 1874 user1 := th.BasicUser 1875 user2 := th.BasicUser2 1876 team := th.BasicTeam 1877 defer th.TearDown() 1878 Client := th.Client 1879 1880 pass, resp := Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id) 1881 CheckNoError(t, resp) 1882 1883 if !pass { 1884 t.Fatal("should have passed") 1885 } 1886 1887 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id) 1888 CheckNoError(t, resp) 1889 1890 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, "junk") 1891 CheckBadRequestStatus(t, resp) 1892 1893 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, model.NewId()) 1894 CheckNotFoundStatus(t, resp) 1895 1896 _, resp = Client.RemoveUserFromChannel(model.NewId(), th.BasicUser2.Id) 1897 CheckNotFoundStatus(t, resp) 1898 1899 th.LoginBasic2() 1900 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 1901 CheckForbiddenStatus(t, resp) 1902 1903 th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel) 1904 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id) 1905 CheckNoError(t, resp) 1906 1907 _, resp = Client.RemoveUserFromChannel(th.BasicChannel2.Id, th.BasicUser.Id) 1908 CheckNoError(t, resp) 1909 1910 _, resp = th.SystemAdminClient.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 1911 CheckNoError(t, resp) 1912 1913 th.LoginBasic() 1914 private := th.CreatePrivateChannel() 1915 th.App.AddUserToChannel(th.BasicUser2, private) 1916 1917 _, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser2.Id) 1918 CheckNoError(t, resp) 1919 1920 th.LoginBasic2() 1921 _, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser.Id) 1922 CheckForbiddenStatus(t, resp) 1923 1924 _, resp = th.SystemAdminClient.RemoveUserFromChannel(private.Id, th.BasicUser.Id) 1925 CheckNoError(t, resp) 1926 1927 th.LoginBasic() 1928 th.UpdateUserToNonTeamAdmin(user1, team) 1929 th.App.InvalidateAllCaches() 1930 1931 // Test policy does not apply to TE. 1932 th.App.UpdateConfig(func(cfg *model.Config) { 1933 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_CHANNEL_ADMIN 1934 }) 1935 1936 privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 1937 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id) 1938 CheckNoError(t, resp) 1939 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 1940 CheckNoError(t, resp) 1941 1942 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 1943 CheckNoError(t, resp) 1944 1945 // Add a license 1946 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_ALL }) 1947 th.App.SetLicense(model.NewTestLicense()) 1948 1949 // Check that a regular channel user can remove other users. 1950 privateChannel = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 1951 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id) 1952 CheckNoError(t, resp) 1953 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 1954 CheckNoError(t, resp) 1955 1956 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 1957 CheckNoError(t, resp) 1958 1959 // Test with CHANNEL_ADMIN level permission. 1960 th.App.UpdateConfig(func(cfg *model.Config) { 1961 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_CHANNEL_ADMIN 1962 }) 1963 1964 privateChannel = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 1965 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id) 1966 CheckNoError(t, resp) 1967 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 1968 CheckNoError(t, resp) 1969 1970 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 1971 CheckForbiddenStatus(t, resp) 1972 1973 th.MakeUserChannelAdmin(user1, privateChannel) 1974 th.App.InvalidateAllCaches() 1975 th.App.SetLicense(model.NewTestLicense()) 1976 1977 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 1978 CheckNoError(t, resp) 1979 1980 // Test with TEAM_ADMIN level permission. 1981 th.App.UpdateConfig(func(cfg *model.Config) { 1982 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_TEAM_ADMIN 1983 }) 1984 1985 privateChannel = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 1986 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id) 1987 CheckNoError(t, resp) 1988 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 1989 CheckNoError(t, resp) 1990 1991 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 1992 CheckForbiddenStatus(t, resp) 1993 1994 th.UpdateUserToTeamAdmin(user1, team) 1995 th.App.InvalidateAllCaches() 1996 th.App.SetLicense(model.NewTestLicense()) 1997 1998 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 1999 CheckNoError(t, resp) 2000 2001 // Test with SYSTEM_ADMIN level permission. 2002 th.App.UpdateConfig(func(cfg *model.Config) { 2003 *cfg.TeamSettings.RestrictPrivateChannelManageMembers = model.PERMISSIONS_SYSTEM_ADMIN 2004 }) 2005 2006 privateChannel = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 2007 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id) 2008 CheckNoError(t, resp) 2009 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 2010 CheckNoError(t, resp) 2011 2012 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 2013 CheckForbiddenStatus(t, resp) 2014 2015 _, resp = th.SystemAdminClient.RemoveUserFromChannel(privateChannel.Id, user2.Id) 2016 CheckNoError(t, resp) 2017 }