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