github.com/kongr45gpen/mattermost-server@v5.11.1+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 "time" 15 16 "github.com/mattermost/mattermost-server/model" 17 "github.com/mattermost/mattermost-server/utils" 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestCreateChannel(t *testing.T) { 23 th := Setup().InitBasic() 24 defer th.TearDown() 25 Client := th.Client 26 team := th.BasicTeam 27 28 channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} 29 private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id} 30 31 rchannel, resp := Client.CreateChannel(channel) 32 CheckNoError(t, resp) 33 CheckCreatedStatus(t, resp) 34 35 if rchannel.Name != channel.Name { 36 t.Fatal("names did not match") 37 } 38 39 if rchannel.DisplayName != channel.DisplayName { 40 t.Fatal("display names did not match") 41 } 42 43 if rchannel.TeamId != channel.TeamId { 44 t.Fatal("team ids did not match") 45 } 46 47 rprivate, resp := Client.CreateChannel(private) 48 CheckNoError(t, resp) 49 50 if rprivate.Name != private.Name { 51 t.Fatal("names did not match") 52 } 53 54 if rprivate.Type != model.CHANNEL_PRIVATE { 55 t.Fatal("wrong channel type") 56 } 57 58 if rprivate.CreatorId != th.BasicUser.Id { 59 t.Fatal("wrong creator id") 60 } 61 62 _, resp = Client.CreateChannel(channel) 63 CheckErrorMessage(t, resp, "store.sql_channel.save_channel.exists.app_error") 64 CheckBadRequestStatus(t, resp) 65 66 direct := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_DIRECT, TeamId: team.Id} 67 _, resp = Client.CreateChannel(direct) 68 CheckErrorMessage(t, resp, "api.channel.create_channel.direct_channel.app_error") 69 CheckBadRequestStatus(t, resp) 70 71 Client.Logout() 72 _, resp = Client.CreateChannel(channel) 73 CheckUnauthorizedStatus(t, resp) 74 75 userNotOnTeam := th.CreateUser() 76 Client.Login(userNotOnTeam.Email, userNotOnTeam.Password) 77 78 _, resp = Client.CreateChannel(channel) 79 CheckForbiddenStatus(t, resp) 80 81 _, resp = Client.CreateChannel(private) 82 CheckForbiddenStatus(t, resp) 83 84 // Check the appropriate permissions are enforced. 85 defaultRolePermissions := th.SaveDefaultRolePermissions() 86 defer func() { 87 th.RestoreDefaultRolePermissions(defaultRolePermissions) 88 }() 89 90 th.AddPermissionToRole(model.PERMISSION_CREATE_PUBLIC_CHANNEL.Id, model.TEAM_USER_ROLE_ID) 91 th.AddPermissionToRole(model.PERMISSION_CREATE_PRIVATE_CHANNEL.Id, model.TEAM_USER_ROLE_ID) 92 93 th.LoginBasic() 94 95 channel.Name = GenerateTestChannelName() 96 _, resp = Client.CreateChannel(channel) 97 CheckNoError(t, resp) 98 99 private.Name = GenerateTestChannelName() 100 _, resp = Client.CreateChannel(private) 101 CheckNoError(t, resp) 102 103 th.AddPermissionToRole(model.PERMISSION_CREATE_PUBLIC_CHANNEL.Id, model.TEAM_ADMIN_ROLE_ID) 104 th.AddPermissionToRole(model.PERMISSION_CREATE_PRIVATE_CHANNEL.Id, model.TEAM_ADMIN_ROLE_ID) 105 th.RemovePermissionFromRole(model.PERMISSION_CREATE_PUBLIC_CHANNEL.Id, model.TEAM_USER_ROLE_ID) 106 th.RemovePermissionFromRole(model.PERMISSION_CREATE_PRIVATE_CHANNEL.Id, model.TEAM_USER_ROLE_ID) 107 108 _, resp = Client.CreateChannel(channel) 109 CheckForbiddenStatus(t, resp) 110 111 _, resp = Client.CreateChannel(private) 112 CheckForbiddenStatus(t, resp) 113 114 th.LoginTeamAdmin() 115 116 channel.Name = GenerateTestChannelName() 117 _, resp = Client.CreateChannel(channel) 118 CheckNoError(t, resp) 119 120 private.Name = GenerateTestChannelName() 121 _, resp = Client.CreateChannel(private) 122 CheckNoError(t, resp) 123 124 channel.Name = GenerateTestChannelName() 125 _, resp = th.SystemAdminClient.CreateChannel(channel) 126 CheckNoError(t, resp) 127 128 private.Name = GenerateTestChannelName() 129 _, resp = th.SystemAdminClient.CreateChannel(private) 130 CheckNoError(t, resp) 131 132 // Test posting Garbage 133 if r, err := Client.DoApiPost("/channels", "garbage"); err == nil { 134 t.Fatal("should have errored") 135 } else { 136 if r.StatusCode != http.StatusBadRequest { 137 t.Log("actual: " + strconv.Itoa(r.StatusCode)) 138 t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) 139 t.Fatal("wrong status code") 140 } 141 } 142 } 143 144 func TestUpdateChannel(t *testing.T) { 145 th := Setup().InitBasic() 146 defer th.TearDown() 147 Client := th.Client 148 team := th.BasicTeam 149 150 channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} 151 private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id} 152 153 channel, _ = Client.CreateChannel(channel) 154 private, _ = Client.CreateChannel(private) 155 156 //Update a open channel 157 channel.DisplayName = "My new display name" 158 channel.Header = "My fancy header" 159 channel.Purpose = "Mattermost ftw!" 160 161 newChannel, resp := Client.UpdateChannel(channel) 162 CheckNoError(t, resp) 163 164 if newChannel.DisplayName != channel.DisplayName { 165 t.Fatal("Update failed for DisplayName") 166 } 167 168 if newChannel.Header != channel.Header { 169 t.Fatal("Update failed for Header") 170 } 171 172 if newChannel.Purpose != channel.Purpose { 173 t.Fatal("Update failed for Purpose") 174 } 175 176 //Update a private channel 177 private.DisplayName = "My new display name for private channel" 178 private.Header = "My fancy private header" 179 private.Purpose = "Mattermost ftw! in private mode" 180 181 newPrivateChannel, resp := Client.UpdateChannel(private) 182 CheckNoError(t, resp) 183 184 if newPrivateChannel.DisplayName != private.DisplayName { 185 t.Fatal("Update failed for DisplayName in private channel") 186 } 187 188 if newPrivateChannel.Header != private.Header { 189 t.Fatal("Update failed for Header in private channel") 190 } 191 192 if newPrivateChannel.Purpose != private.Purpose { 193 t.Fatal("Update failed for Purpose in private channel") 194 } 195 196 // Updating a private channel requires permission *and* membership, so this should fail. 197 _, resp = th.SystemAdminClient.UpdateChannel(private) 198 CheckForbiddenStatus(t, resp) 199 200 //Non existing channel 201 channel1 := &model.Channel{DisplayName: "Test API Name for apiv4", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} 202 _, resp = Client.UpdateChannel(channel1) 203 CheckNotFoundStatus(t, resp) 204 205 //Try to update with not logged user 206 Client.Logout() 207 _, resp = Client.UpdateChannel(channel) 208 CheckUnauthorizedStatus(t, resp) 209 210 //Try to update using another user 211 user := th.CreateUser() 212 Client.Login(user.Email, user.Password) 213 214 channel.DisplayName = "Should not update" 215 _, resp = Client.UpdateChannel(channel) 216 CheckForbiddenStatus(t, resp) 217 218 // Test updating the header of someone else's GM channel. 219 user1 := th.CreateUser() 220 user2 := th.CreateUser() 221 user3 := th.CreateUser() 222 223 groupChannel, resp := Client.CreateGroupChannel([]string{user1.Id, user2.Id}) 224 CheckNoError(t, resp) 225 226 groupChannel.Header = "lolololol" 227 Client.Logout() 228 Client.Login(user3.Email, user3.Password) 229 _, resp = Client.UpdateChannel(groupChannel) 230 CheckForbiddenStatus(t, resp) 231 232 // Test updating the header of someone else's GM channel. 233 Client.Logout() 234 Client.Login(user.Email, user.Password) 235 236 directChannel, resp := Client.CreateDirectChannel(user.Id, user1.Id) 237 CheckNoError(t, resp) 238 239 directChannel.Header = "lolololol" 240 Client.Logout() 241 Client.Login(user3.Email, user3.Password) 242 _, resp = Client.UpdateChannel(directChannel) 243 CheckForbiddenStatus(t, resp) 244 } 245 246 func TestPatchChannel(t *testing.T) { 247 th := Setup().InitBasic() 248 defer th.TearDown() 249 Client := th.Client 250 251 patch := &model.ChannelPatch{ 252 Name: new(string), 253 DisplayName: new(string), 254 Header: new(string), 255 Purpose: new(string), 256 } 257 *patch.Name = model.NewId() 258 *patch.DisplayName = model.NewId() 259 *patch.Header = model.NewId() 260 *patch.Purpose = model.NewId() 261 262 channel, resp := Client.PatchChannel(th.BasicChannel.Id, patch) 263 CheckNoError(t, resp) 264 265 if *patch.Name != channel.Name { 266 t.Fatal("do not match") 267 } else if *patch.DisplayName != channel.DisplayName { 268 t.Fatal("do not match") 269 } else if *patch.Header != channel.Header { 270 t.Fatal("do not match") 271 } else if *patch.Purpose != channel.Purpose { 272 t.Fatal("do not match") 273 } 274 275 patch.Name = nil 276 oldName := channel.Name 277 channel, resp = Client.PatchChannel(th.BasicChannel.Id, patch) 278 CheckNoError(t, resp) 279 280 if channel.Name != oldName { 281 t.Fatal("should not have updated") 282 } 283 284 _, resp = Client.PatchChannel("junk", patch) 285 CheckBadRequestStatus(t, resp) 286 287 _, resp = Client.PatchChannel(model.NewId(), patch) 288 CheckNotFoundStatus(t, resp) 289 290 user := th.CreateUser() 291 Client.Login(user.Email, user.Password) 292 _, resp = Client.PatchChannel(th.BasicChannel.Id, patch) 293 CheckForbiddenStatus(t, resp) 294 295 _, resp = th.SystemAdminClient.PatchChannel(th.BasicChannel.Id, patch) 296 CheckNoError(t, resp) 297 298 Client.Logout() 299 Client.Login(th.BasicUser.Username, th.BasicUser.Password) 300 301 _, resp = th.Client.PatchChannel(th.BasicPrivateChannel.Id, patch) 302 CheckNoError(t, resp) 303 304 // Patching a private channel requires permission *and* membership, so this should fail. 305 _, resp = th.SystemAdminClient.PatchChannel(th.BasicPrivateChannel.Id, patch) 306 CheckForbiddenStatus(t, resp) 307 308 // Test updating the header of someone else's GM channel. 309 user1 := th.CreateUser() 310 user2 := th.CreateUser() 311 user3 := th.CreateUser() 312 313 groupChannel, resp := Client.CreateGroupChannel([]string{user1.Id, user2.Id}) 314 CheckNoError(t, resp) 315 316 Client.Logout() 317 Client.Login(user3.Email, user3.Password) 318 319 channelPatch := &model.ChannelPatch{} 320 channelPatch.Header = new(string) 321 *channelPatch.Header = "lolololol" 322 323 _, resp = Client.PatchChannel(groupChannel.Id, channelPatch) 324 CheckForbiddenStatus(t, resp) 325 326 // Test updating the header of someone else's GM channel. 327 Client.Logout() 328 Client.Login(user.Email, user.Password) 329 330 directChannel, resp := Client.CreateDirectChannel(user.Id, user1.Id) 331 CheckNoError(t, resp) 332 333 Client.Logout() 334 Client.Login(user3.Email, user3.Password) 335 _, resp = Client.PatchChannel(directChannel.Id, channelPatch) 336 CheckForbiddenStatus(t, resp) 337 } 338 339 func TestCreateDirectChannel(t *testing.T) { 340 th := Setup().InitBasic() 341 defer th.TearDown() 342 Client := th.Client 343 user1 := th.BasicUser 344 user2 := th.BasicUser2 345 user3 := th.CreateUser() 346 347 dm, resp := Client.CreateDirectChannel(user1.Id, user2.Id) 348 CheckNoError(t, resp) 349 350 channelName := "" 351 if user2.Id > user1.Id { 352 channelName = user1.Id + "__" + user2.Id 353 } else { 354 channelName = user2.Id + "__" + user1.Id 355 } 356 357 if dm.Name != channelName { 358 t.Fatal("dm name didn't match") 359 } 360 361 _, resp = Client.CreateDirectChannel("junk", user2.Id) 362 CheckBadRequestStatus(t, resp) 363 364 _, resp = Client.CreateDirectChannel(user1.Id, model.NewId()) 365 CheckBadRequestStatus(t, resp) 366 367 _, resp = Client.CreateDirectChannel(model.NewId(), user1.Id) 368 CheckBadRequestStatus(t, resp) 369 370 _, resp = Client.CreateDirectChannel(model.NewId(), user2.Id) 371 CheckForbiddenStatus(t, resp) 372 373 if r, err := Client.DoApiPost("/channels/direct", "garbage"); err == nil { 374 t.Fatal("should have errored") 375 } else { 376 if r.StatusCode != http.StatusBadRequest { 377 t.Log("actual: " + strconv.Itoa(r.StatusCode)) 378 t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) 379 t.Fatal("wrong status code") 380 } 381 } 382 383 Client.Logout() 384 _, resp = Client.CreateDirectChannel(model.NewId(), user2.Id) 385 CheckUnauthorizedStatus(t, resp) 386 387 _, resp = th.SystemAdminClient.CreateDirectChannel(user3.Id, user2.Id) 388 CheckNoError(t, resp) 389 } 390 391 func TestDeleteDirectChannel(t *testing.T) { 392 th := Setup().InitBasic() 393 defer th.TearDown() 394 Client := th.Client 395 user := th.BasicUser 396 user2 := th.BasicUser2 397 398 rgc, resp := Client.CreateDirectChannel(user.Id, user2.Id) 399 CheckNoError(t, resp) 400 CheckCreatedStatus(t, resp) 401 require.NotNil(t, rgc, "should have created a direct channel") 402 403 deleted, resp := Client.DeleteChannel(rgc.Id) 404 CheckErrorMessage(t, resp, "api.channel.delete_channel.type.invalid") 405 require.False(t, deleted, "should not have been able to delete direct channel.") 406 } 407 408 func TestCreateGroupChannel(t *testing.T) { 409 th := Setup().InitBasic() 410 defer th.TearDown() 411 Client := th.Client 412 user := th.BasicUser 413 user2 := th.BasicUser2 414 user3 := th.CreateUser() 415 416 userIds := []string{user.Id, user2.Id, user3.Id} 417 418 rgc, resp := Client.CreateGroupChannel(userIds) 419 CheckNoError(t, resp) 420 CheckCreatedStatus(t, resp) 421 422 if rgc == nil { 423 t.Fatal("should have created a group channel") 424 } 425 426 if rgc.Type != model.CHANNEL_GROUP { 427 t.Fatal("should have created a channel of group type") 428 } 429 430 m, _ := th.App.GetChannelMembersPage(rgc.Id, 0, 10) 431 if len(*m) != 3 { 432 t.Fatal("should have 3 channel members") 433 } 434 435 // saving duplicate group channel 436 rgc2, resp := Client.CreateGroupChannel([]string{user3.Id, user2.Id}) 437 CheckNoError(t, resp) 438 439 if rgc.Id != rgc2.Id { 440 t.Fatal("should have returned existing channel") 441 } 442 443 m2, _ := th.App.GetChannelMembersPage(rgc2.Id, 0, 10) 444 if !reflect.DeepEqual(*m, *m2) { 445 t.Fatal("should be equal") 446 } 447 448 _, resp = Client.CreateGroupChannel([]string{user2.Id}) 449 CheckBadRequestStatus(t, resp) 450 451 user4 := th.CreateUser() 452 user5 := th.CreateUser() 453 user6 := th.CreateUser() 454 user7 := th.CreateUser() 455 user8 := th.CreateUser() 456 user9 := th.CreateUser() 457 458 rgc, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, user4.Id, user5.Id, user6.Id, user7.Id, user8.Id, user9.Id}) 459 CheckBadRequestStatus(t, resp) 460 461 if rgc != nil { 462 t.Fatal("should return nil") 463 } 464 465 _, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, GenerateTestId()}) 466 CheckBadRequestStatus(t, resp) 467 468 _, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, "junk"}) 469 CheckBadRequestStatus(t, resp) 470 471 Client.Logout() 472 473 _, resp = Client.CreateGroupChannel(userIds) 474 CheckUnauthorizedStatus(t, resp) 475 476 _, resp = th.SystemAdminClient.CreateGroupChannel(userIds) 477 CheckNoError(t, resp) 478 } 479 480 func TestDeleteGroupChannel(t *testing.T) { 481 th := Setup().InitBasic() 482 defer th.TearDown() 483 Client := th.Client 484 user := th.BasicUser 485 user2 := th.BasicUser2 486 user3 := th.CreateUser() 487 488 userIds := []string{user.Id, user2.Id, user3.Id} 489 490 rgc, resp := Client.CreateGroupChannel(userIds) 491 CheckNoError(t, resp) 492 CheckCreatedStatus(t, resp) 493 require.NotNil(t, rgc, "should have created a group channel") 494 495 deleted, resp := Client.DeleteChannel(rgc.Id) 496 CheckErrorMessage(t, resp, "api.channel.delete_channel.type.invalid") 497 require.False(t, deleted, "should not have been able to delete group channel.") 498 } 499 500 func TestGetChannel(t *testing.T) { 501 th := Setup().InitBasic() 502 defer th.TearDown() 503 Client := th.Client 504 505 channel, resp := Client.GetChannel(th.BasicChannel.Id, "") 506 CheckNoError(t, resp) 507 508 if channel.Id != th.BasicChannel.Id { 509 t.Fatal("ids did not match") 510 } 511 512 Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 513 _, resp = Client.GetChannel(th.BasicChannel.Id, "") 514 CheckNoError(t, resp) 515 516 channel, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "") 517 CheckNoError(t, resp) 518 519 if channel.Id != th.BasicPrivateChannel.Id { 520 t.Fatal("ids did not match") 521 } 522 523 Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id) 524 _, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "") 525 CheckForbiddenStatus(t, resp) 526 527 _, resp = Client.GetChannel(model.NewId(), "") 528 CheckNotFoundStatus(t, resp) 529 530 Client.Logout() 531 _, resp = Client.GetChannel(th.BasicChannel.Id, "") 532 CheckUnauthorizedStatus(t, resp) 533 534 user := th.CreateUser() 535 Client.Login(user.Email, user.Password) 536 _, resp = Client.GetChannel(th.BasicChannel.Id, "") 537 CheckForbiddenStatus(t, resp) 538 539 _, resp = th.SystemAdminClient.GetChannel(th.BasicChannel.Id, "") 540 CheckNoError(t, resp) 541 542 _, resp = th.SystemAdminClient.GetChannel(th.BasicPrivateChannel.Id, "") 543 CheckNoError(t, resp) 544 545 _, resp = th.SystemAdminClient.GetChannel(th.BasicUser.Id, "") 546 CheckNotFoundStatus(t, resp) 547 } 548 549 func TestGetDeletedChannelsForTeam(t *testing.T) { 550 th := Setup().InitBasic() 551 defer th.TearDown() 552 Client := th.Client 553 team := th.BasicTeam 554 555 _, resp := Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 556 CheckForbiddenStatus(t, resp) 557 558 th.LoginTeamAdmin() 559 560 channels, resp := Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 561 CheckNoError(t, resp) 562 numInitialChannelsForTeam := len(channels) 563 564 // create and delete public channel 565 publicChannel1 := th.CreatePublicChannel() 566 Client.DeleteChannel(publicChannel1.Id) 567 568 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 569 CheckNoError(t, resp) 570 if len(channels) != numInitialChannelsForTeam+1 { 571 t.Fatal("should be 1 deleted channel") 572 } 573 574 publicChannel2 := th.CreatePublicChannel() 575 Client.DeleteChannel(publicChannel2.Id) 576 577 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") 578 CheckNoError(t, resp) 579 if len(channels) != numInitialChannelsForTeam+2 { 580 t.Fatal("should be 2 deleted channels") 581 } 582 583 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 1, "") 584 CheckNoError(t, resp) 585 if len(channels) != 1 { 586 t.Fatal("should be one channel per page") 587 } 588 589 channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 1, 1, "") 590 CheckNoError(t, resp) 591 if len(channels) != 1 { 592 t.Fatal("should be one channel per page") 593 } 594 } 595 596 func TestGetPublicChannelsForTeam(t *testing.T) { 597 th := Setup().InitBasic() 598 defer th.TearDown() 599 Client := th.Client 600 team := th.BasicTeam 601 publicChannel1 := th.BasicChannel 602 publicChannel2 := th.BasicChannel2 603 604 channels, resp := Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 605 CheckNoError(t, resp) 606 if len(channels) != 4 { 607 t.Fatal("wrong length") 608 } 609 610 for i, c := range channels { 611 if c.Type != model.CHANNEL_OPEN { 612 t.Fatal("should include open channel only") 613 } 614 615 // only check the created 2 public channels 616 if i < 2 && !(c.DisplayName == publicChannel1.DisplayName || c.DisplayName == publicChannel2.DisplayName) { 617 t.Logf("channel %v: %v", i, c.DisplayName) 618 t.Fatal("should match public channel display name only") 619 } 620 } 621 622 privateChannel := th.CreatePrivateChannel() 623 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 624 CheckNoError(t, resp) 625 if len(channels) != 4 { 626 t.Fatal("wrong length") 627 } 628 629 for _, c := range channels { 630 if c.Type != model.CHANNEL_OPEN { 631 t.Fatal("should not include private channel") 632 } 633 634 if c.DisplayName == privateChannel.DisplayName { 635 t.Fatal("should not match private channel display name") 636 } 637 } 638 639 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 1, "") 640 CheckNoError(t, resp) 641 if len(channels) != 1 { 642 t.Fatal("should be one channel per page") 643 } 644 645 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 1, 1, "") 646 CheckNoError(t, resp) 647 if len(channels) != 1 { 648 t.Fatal("should be one channel per page") 649 } 650 651 channels, resp = Client.GetPublicChannelsForTeam(team.Id, 10000, 100, "") 652 CheckNoError(t, resp) 653 if len(channels) != 0 { 654 t.Fatal("should be no channel") 655 } 656 657 _, resp = Client.GetPublicChannelsForTeam("junk", 0, 100, "") 658 CheckBadRequestStatus(t, resp) 659 660 _, resp = Client.GetPublicChannelsForTeam(model.NewId(), 0, 100, "") 661 CheckForbiddenStatus(t, resp) 662 663 Client.Logout() 664 _, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 665 CheckUnauthorizedStatus(t, resp) 666 667 user := th.CreateUser() 668 Client.Login(user.Email, user.Password) 669 _, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") 670 CheckForbiddenStatus(t, resp) 671 672 _, resp = th.SystemAdminClient.GetPublicChannelsForTeam(team.Id, 0, 100, "") 673 CheckNoError(t, resp) 674 } 675 676 func TestGetPublicChannelsByIdsForTeam(t *testing.T) { 677 th := Setup().InitBasic() 678 defer th.TearDown() 679 Client := th.Client 680 teamId := th.BasicTeam.Id 681 input := []string{th.BasicChannel.Id} 682 output := []string{th.BasicChannel.DisplayName} 683 684 channels, resp := Client.GetPublicChannelsByIdsForTeam(teamId, input) 685 CheckNoError(t, resp) 686 687 if len(channels) != 1 { 688 t.Fatal("should return 1 channel") 689 } 690 691 if (channels)[0].DisplayName != output[0] { 692 t.Fatal("missing channel") 693 } 694 695 input = append(input, GenerateTestId()) 696 input = append(input, th.BasicChannel2.Id) 697 input = append(input, th.BasicPrivateChannel.Id) 698 output = append(output, th.BasicChannel2.DisplayName) 699 sort.Strings(output) 700 701 channels, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input) 702 CheckNoError(t, resp) 703 704 if len(channels) != 2 { 705 t.Fatal("should return 2 channels") 706 } 707 708 for i, c := range channels { 709 if c.DisplayName != output[i] { 710 t.Fatal("missing channel") 711 } 712 } 713 714 _, resp = Client.GetPublicChannelsByIdsForTeam(GenerateTestId(), input) 715 CheckForbiddenStatus(t, resp) 716 717 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{}) 718 CheckBadRequestStatus(t, resp) 719 720 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{"junk"}) 721 CheckBadRequestStatus(t, resp) 722 723 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{GenerateTestId()}) 724 CheckNotFoundStatus(t, resp) 725 726 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{th.BasicPrivateChannel.Id}) 727 CheckNotFoundStatus(t, resp) 728 729 Client.Logout() 730 731 _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input) 732 CheckUnauthorizedStatus(t, resp) 733 734 _, resp = th.SystemAdminClient.GetPublicChannelsByIdsForTeam(teamId, input) 735 CheckNoError(t, resp) 736 } 737 738 func TestGetChannelsForTeamForUser(t *testing.T) { 739 th := Setup().InitBasic() 740 defer th.TearDown() 741 Client := th.Client 742 743 channels, resp := Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "") 744 CheckNoError(t, resp) 745 746 found := make([]bool, 3) 747 for _, c := range channels { 748 if c.Id == th.BasicChannel.Id { 749 found[0] = true 750 } else if c.Id == th.BasicChannel2.Id { 751 found[1] = true 752 } else if c.Id == th.BasicPrivateChannel.Id { 753 found[2] = true 754 } 755 756 if c.TeamId != th.BasicTeam.Id && c.TeamId != "" { 757 t.Fatal("wrong team") 758 } 759 } 760 761 for _, f := range found { 762 if !f { 763 t.Fatal("missing a channel") 764 } 765 } 766 767 channels, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, resp.Etag) 768 CheckEtag(t, channels, resp) 769 770 _, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, "junk", "") 771 CheckBadRequestStatus(t, resp) 772 773 _, resp = Client.GetChannelsForTeamForUser("junk", th.BasicUser.Id, "") 774 CheckBadRequestStatus(t, resp) 775 776 _, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser2.Id, "") 777 CheckForbiddenStatus(t, resp) 778 779 _, resp = Client.GetChannelsForTeamForUser(model.NewId(), th.BasicUser.Id, "") 780 CheckForbiddenStatus(t, resp) 781 782 _, resp = th.SystemAdminClient.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "") 783 CheckNoError(t, resp) 784 } 785 786 func TestGetAllChannels(t *testing.T) { 787 th := Setup().InitBasic() 788 defer th.TearDown() 789 Client := th.Client 790 791 channels, resp := th.SystemAdminClient.GetAllChannels(0, 20, "") 792 CheckNoError(t, resp) 793 794 // At least, all the not-deleted channels created during the InitBasic 795 require.True(t, len(*channels) >= 3) 796 for _, c := range *channels { 797 require.NotEqual(t, c.TeamId, "") 798 } 799 800 channels, resp = th.SystemAdminClient.GetAllChannels(0, 10, "") 801 CheckNoError(t, resp) 802 require.True(t, len(*channels) >= 3) 803 804 channels, resp = th.SystemAdminClient.GetAllChannels(1, 1, "") 805 CheckNoError(t, resp) 806 require.Len(t, *channels, 1) 807 808 channels, resp = th.SystemAdminClient.GetAllChannels(10000, 10000, "") 809 CheckNoError(t, resp) 810 require.Len(t, *channels, 0) 811 812 _, resp = Client.GetAllChannels(0, 20, "") 813 CheckForbiddenStatus(t, resp) 814 } 815 816 func TestSearchChannels(t *testing.T) { 817 th := Setup().InitBasic() 818 defer th.TearDown() 819 Client := th.Client 820 821 search := &model.ChannelSearch{Term: th.BasicChannel.Name} 822 823 channels, resp := Client.SearchChannels(th.BasicTeam.Id, search) 824 CheckNoError(t, resp) 825 826 found := false 827 for _, c := range channels { 828 if c.Type != model.CHANNEL_OPEN { 829 t.Fatal("should only return public channels") 830 } 831 832 if c.Id == th.BasicChannel.Id { 833 found = true 834 } 835 } 836 837 if !found { 838 t.Fatal("didn't find channel") 839 } 840 841 search.Term = th.BasicPrivateChannel.Name 842 channels, resp = Client.SearchChannels(th.BasicTeam.Id, search) 843 CheckNoError(t, resp) 844 845 found = false 846 for _, c := range channels { 847 if c.Id == th.BasicPrivateChannel.Id { 848 found = true 849 } 850 } 851 852 if found { 853 t.Fatal("shouldn't find private channel") 854 } 855 856 search.Term = "" 857 _, resp = Client.SearchChannels(th.BasicTeam.Id, search) 858 CheckNoError(t, resp) 859 860 search.Term = th.BasicChannel.Name 861 _, resp = Client.SearchChannels(model.NewId(), search) 862 CheckForbiddenStatus(t, resp) 863 864 _, resp = Client.SearchChannels("junk", search) 865 CheckBadRequestStatus(t, resp) 866 867 _, resp = th.SystemAdminClient.SearchChannels(th.BasicTeam.Id, search) 868 CheckNoError(t, resp) 869 } 870 871 func TestSearchAllChannels(t *testing.T) { 872 th := Setup().InitBasic() 873 defer th.TearDown() 874 Client := th.Client 875 876 search := &model.ChannelSearch{Term: th.BasicChannel.Name} 877 878 channels, resp := th.SystemAdminClient.SearchAllChannels(search) 879 CheckNoError(t, resp) 880 881 assert.Len(t, *channels, 1) 882 assert.Equal(t, (*channels)[0].Id, th.BasicChannel.Id) 883 884 search.Term = th.BasicPrivateChannel.Name 885 channels, resp = th.SystemAdminClient.SearchAllChannels(search) 886 CheckNoError(t, resp) 887 888 assert.Len(t, *channels, 1) 889 assert.Equal(t, (*channels)[0].Id, th.BasicPrivateChannel.Id) 890 891 search.Term = "" 892 channels, resp = th.SystemAdminClient.SearchAllChannels(search) 893 CheckNoError(t, resp) 894 // At least, all the not-deleted channels created during the InitBasic 895 assert.True(t, len(*channels) >= 3) 896 897 search.Term = th.BasicChannel.Name 898 _, resp = Client.SearchAllChannels(search) 899 CheckForbiddenStatus(t, resp) 900 } 901 902 func TestDeleteChannel(t *testing.T) { 903 th := Setup().InitBasic() 904 defer th.TearDown() 905 Client := th.Client 906 team := th.BasicTeam 907 user := th.BasicUser 908 user2 := th.BasicUser2 909 910 // successful delete of public channel 911 publicChannel1 := th.CreatePublicChannel() 912 pass, resp := Client.DeleteChannel(publicChannel1.Id) 913 CheckNoError(t, resp) 914 915 if !pass { 916 t.Fatal("should have passed") 917 } 918 919 if ch, err := th.App.GetChannel(publicChannel1.Id); err == nil && ch.DeleteAt == 0 { 920 t.Fatal("should have failed to get deleted channel") 921 } 922 923 post1 := &model.Post{ChannelId: publicChannel1.Id, Message: "a" + GenerateTestId() + "a"} 924 if _, err := Client.CreatePost(post1); err == nil { 925 t.Fatal("should have failed to post to deleted channel") 926 } 927 928 // successful delete of private channel 929 privateChannel2 := th.CreatePrivateChannel() 930 _, resp = Client.DeleteChannel(privateChannel2.Id) 931 CheckNoError(t, resp) 932 933 // successful delete of channel with multiple members 934 publicChannel3 := th.CreatePublicChannel() 935 th.App.AddUserToChannel(user, publicChannel3) 936 th.App.AddUserToChannel(user2, publicChannel3) 937 _, resp = Client.DeleteChannel(publicChannel3.Id) 938 CheckNoError(t, resp) 939 940 // default channel cannot be deleted. 941 defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, team.Id, false) 942 pass, resp = Client.DeleteChannel(defaultChannel.Id) 943 CheckBadRequestStatus(t, resp) 944 945 if pass { 946 t.Fatal("should have failed") 947 } 948 949 // check system admin can delete a channel without any appropriate team or channel membership. 950 sdTeam := th.CreateTeamWithClient(Client) 951 sdPublicChannel := &model.Channel{ 952 DisplayName: "dn_" + model.NewId(), 953 Name: GenerateTestChannelName(), 954 Type: model.CHANNEL_OPEN, 955 TeamId: sdTeam.Id, 956 } 957 sdPublicChannel, resp = Client.CreateChannel(sdPublicChannel) 958 CheckNoError(t, resp) 959 _, resp = th.SystemAdminClient.DeleteChannel(sdPublicChannel.Id) 960 CheckNoError(t, resp) 961 962 sdPrivateChannel := &model.Channel{ 963 DisplayName: "dn_" + model.NewId(), 964 Name: GenerateTestChannelName(), 965 Type: model.CHANNEL_PRIVATE, 966 TeamId: sdTeam.Id, 967 } 968 sdPrivateChannel, resp = Client.CreateChannel(sdPrivateChannel) 969 CheckNoError(t, resp) 970 _, resp = th.SystemAdminClient.DeleteChannel(sdPrivateChannel.Id) 971 CheckNoError(t, resp) 972 973 th.LoginBasic() 974 publicChannel5 := th.CreatePublicChannel() 975 Client.Logout() 976 977 Client.Login(user.Id, user.Password) 978 _, resp = Client.DeleteChannel(publicChannel5.Id) 979 CheckUnauthorizedStatus(t, resp) 980 981 _, resp = Client.DeleteChannel("junk") 982 CheckUnauthorizedStatus(t, resp) 983 984 Client.Logout() 985 _, resp = Client.DeleteChannel(GenerateTestId()) 986 CheckUnauthorizedStatus(t, resp) 987 988 _, resp = th.SystemAdminClient.DeleteChannel(publicChannel5.Id) 989 CheckNoError(t, resp) 990 } 991 992 func TestDeleteChannel2(t *testing.T) { 993 th := Setup().InitBasic() 994 defer th.TearDown() 995 Client := th.Client 996 user := th.BasicUser 997 998 // Check the appropriate permissions are enforced. 999 defaultRolePermissions := th.SaveDefaultRolePermissions() 1000 defer func() { 1001 th.RestoreDefaultRolePermissions(defaultRolePermissions) 1002 }() 1003 1004 th.AddPermissionToRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.CHANNEL_USER_ROLE_ID) 1005 th.AddPermissionToRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.CHANNEL_USER_ROLE_ID) 1006 1007 // channels created by SystemAdmin 1008 publicChannel6 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) 1009 privateChannel7 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 1010 th.App.AddUserToChannel(user, publicChannel6) 1011 th.App.AddUserToChannel(user, privateChannel7) 1012 th.App.AddUserToChannel(user, privateChannel7) 1013 1014 // successful delete by user 1015 _, resp := Client.DeleteChannel(publicChannel6.Id) 1016 CheckNoError(t, resp) 1017 1018 _, resp = Client.DeleteChannel(privateChannel7.Id) 1019 CheckNoError(t, resp) 1020 1021 // Restrict permissions to Channel Admins 1022 th.RemovePermissionFromRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.CHANNEL_USER_ROLE_ID) 1023 th.RemovePermissionFromRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.CHANNEL_USER_ROLE_ID) 1024 th.AddPermissionToRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID) 1025 th.AddPermissionToRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID) 1026 1027 // channels created by SystemAdmin 1028 publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) 1029 privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 1030 th.App.AddUserToChannel(user, publicChannel6) 1031 th.App.AddUserToChannel(user, privateChannel7) 1032 th.App.AddUserToChannel(user, privateChannel7) 1033 1034 // cannot delete by user 1035 _, resp = Client.DeleteChannel(publicChannel6.Id) 1036 CheckForbiddenStatus(t, resp) 1037 1038 _, resp = Client.DeleteChannel(privateChannel7.Id) 1039 CheckForbiddenStatus(t, resp) 1040 1041 // successful delete by channel admin 1042 th.MakeUserChannelAdmin(user, publicChannel6) 1043 th.MakeUserChannelAdmin(user, privateChannel7) 1044 th.App.Srv.Store.Channel().ClearCaches() 1045 1046 _, resp = Client.DeleteChannel(publicChannel6.Id) 1047 CheckNoError(t, resp) 1048 1049 _, resp = Client.DeleteChannel(privateChannel7.Id) 1050 CheckNoError(t, resp) 1051 1052 // Make sure team admins don't have permission to delete channels. 1053 th.RemovePermissionFromRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID) 1054 th.RemovePermissionFromRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID) 1055 1056 // last member of a public channel should have required permission to delete 1057 publicChannel6 = th.CreateChannelWithClient(th.Client, model.CHANNEL_OPEN) 1058 _, resp = Client.DeleteChannel(publicChannel6.Id) 1059 CheckForbiddenStatus(t, resp) 1060 1061 // last member of a private channel should not be able to delete it if they don't have required permissions 1062 privateChannel7 = th.CreateChannelWithClient(th.Client, model.CHANNEL_PRIVATE) 1063 _, resp = Client.DeleteChannel(privateChannel7.Id) 1064 CheckForbiddenStatus(t, resp) 1065 } 1066 1067 func TestConvertChannelToPrivate(t *testing.T) { 1068 th := Setup().InitBasic() 1069 defer th.TearDown() 1070 Client := th.Client 1071 1072 defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id, false) 1073 _, resp := Client.ConvertChannelToPrivate(defaultChannel.Id) 1074 CheckForbiddenStatus(t, resp) 1075 1076 privateChannel := th.CreatePrivateChannel() 1077 _, resp = Client.ConvertChannelToPrivate(privateChannel.Id) 1078 CheckForbiddenStatus(t, resp) 1079 1080 publicChannel := th.CreatePublicChannel() 1081 _, resp = Client.ConvertChannelToPrivate(publicChannel.Id) 1082 CheckForbiddenStatus(t, resp) 1083 1084 th.LoginTeamAdmin() 1085 rchannel, resp := Client.ConvertChannelToPrivate(publicChannel.Id) 1086 CheckOKStatus(t, resp) 1087 if rchannel.Type != model.CHANNEL_PRIVATE { 1088 t.Fatal("channel should be converted from public to private") 1089 } 1090 1091 rchannel, resp = th.SystemAdminClient.ConvertChannelToPrivate(privateChannel.Id) 1092 CheckBadRequestStatus(t, resp) 1093 if rchannel != nil { 1094 t.Fatal("should not return a channel") 1095 } 1096 1097 rchannel, resp = th.SystemAdminClient.ConvertChannelToPrivate(defaultChannel.Id) 1098 CheckBadRequestStatus(t, resp) 1099 if rchannel != nil { 1100 t.Fatal("should not return a channel") 1101 } 1102 1103 WebSocketClient, err := th.CreateWebSocketClient() 1104 if err != nil { 1105 t.Fatal(err) 1106 } 1107 WebSocketClient.Listen() 1108 1109 publicChannel2 := th.CreatePublicChannel() 1110 rchannel, resp = th.SystemAdminClient.ConvertChannelToPrivate(publicChannel2.Id) 1111 CheckOKStatus(t, resp) 1112 if rchannel.Type != model.CHANNEL_PRIVATE { 1113 t.Fatal("channel should be converted from public to private") 1114 } 1115 1116 stop := make(chan bool) 1117 eventHit := false 1118 1119 go func() { 1120 for { 1121 select { 1122 case resp := <-WebSocketClient.EventChannel: 1123 if resp.Event == model.WEBSOCKET_EVENT_CHANNEL_CONVERTED && resp.Data["channel_id"].(string) == publicChannel2.Id { 1124 eventHit = true 1125 } 1126 case <-stop: 1127 return 1128 } 1129 } 1130 }() 1131 1132 time.Sleep(400 * time.Millisecond) 1133 1134 stop <- true 1135 1136 if !eventHit { 1137 t.Fatal("did not receive channel_converted event") 1138 } 1139 } 1140 1141 func TestRestoreChannel(t *testing.T) { 1142 th := Setup().InitBasic() 1143 defer th.TearDown() 1144 Client := th.Client 1145 1146 publicChannel1 := th.CreatePublicChannel() 1147 Client.DeleteChannel(publicChannel1.Id) 1148 1149 privateChannel1 := th.CreatePrivateChannel() 1150 Client.DeleteChannel(privateChannel1.Id) 1151 1152 _, resp := Client.RestoreChannel(publicChannel1.Id) 1153 CheckForbiddenStatus(t, resp) 1154 1155 _, resp = Client.RestoreChannel(privateChannel1.Id) 1156 CheckForbiddenStatus(t, resp) 1157 1158 th.LoginTeamAdmin() 1159 1160 _, resp = Client.RestoreChannel(publicChannel1.Id) 1161 CheckOKStatus(t, resp) 1162 1163 _, resp = Client.RestoreChannel(privateChannel1.Id) 1164 CheckOKStatus(t, resp) 1165 } 1166 1167 func TestGetChannelByName(t *testing.T) { 1168 th := Setup().InitBasic() 1169 defer th.TearDown() 1170 Client := th.Client 1171 1172 channel, resp := Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1173 CheckNoError(t, resp) 1174 1175 if channel.Name != th.BasicChannel.Name { 1176 t.Fatal("names did not match") 1177 } 1178 1179 channel, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "") 1180 CheckNoError(t, resp) 1181 1182 if channel.Name != th.BasicPrivateChannel.Name { 1183 t.Fatal("names did not match") 1184 } 1185 1186 _, resp = Client.GetChannelByName(strings.ToUpper(th.BasicPrivateChannel.Name), th.BasicTeam.Id, "") 1187 CheckNoError(t, resp) 1188 1189 _, resp = Client.GetChannelByName(th.BasicDeletedChannel.Name, th.BasicTeam.Id, "") 1190 CheckNotFoundStatus(t, resp) 1191 1192 channel, resp = Client.GetChannelByNameIncludeDeleted(th.BasicDeletedChannel.Name, th.BasicTeam.Id, "") 1193 CheckNoError(t, resp) 1194 1195 if channel.Name != th.BasicDeletedChannel.Name { 1196 t.Fatal("names did not match") 1197 } 1198 1199 Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 1200 _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1201 CheckNoError(t, resp) 1202 1203 Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id) 1204 _, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "") 1205 CheckForbiddenStatus(t, resp) 1206 1207 _, resp = Client.GetChannelByName(GenerateTestChannelName(), th.BasicTeam.Id, "") 1208 CheckNotFoundStatus(t, resp) 1209 1210 _, resp = Client.GetChannelByName(GenerateTestChannelName(), "junk", "") 1211 CheckBadRequestStatus(t, resp) 1212 1213 Client.Logout() 1214 _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1215 CheckUnauthorizedStatus(t, resp) 1216 1217 user := th.CreateUser() 1218 Client.Login(user.Email, user.Password) 1219 _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1220 CheckForbiddenStatus(t, resp) 1221 1222 _, resp = th.SystemAdminClient.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") 1223 CheckNoError(t, resp) 1224 } 1225 1226 func TestGetChannelByNameForTeamName(t *testing.T) { 1227 th := Setup().InitBasic() 1228 defer th.TearDown() 1229 Client := th.Client 1230 1231 channel, resp := th.SystemAdminClient.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1232 CheckNoError(t, resp) 1233 1234 if channel.Name != th.BasicChannel.Name { 1235 t.Fatal("names did not match") 1236 } 1237 1238 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1239 CheckNoError(t, resp) 1240 1241 _, resp = Client.GetChannelByNameForTeamName(th.BasicDeletedChannel.Name, th.BasicTeam.Name, "") 1242 CheckNotFoundStatus(t, resp) 1243 1244 channel, resp = Client.GetChannelByNameForTeamNameIncludeDeleted(th.BasicDeletedChannel.Name, th.BasicTeam.Name, "") 1245 CheckNoError(t, resp) 1246 1247 if channel.Name != th.BasicDeletedChannel.Name { 1248 t.Fatal("names did not match") 1249 } 1250 1251 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, model.NewRandomString(15), "") 1252 CheckNotFoundStatus(t, resp) 1253 1254 _, resp = Client.GetChannelByNameForTeamName(GenerateTestChannelName(), th.BasicTeam.Name, "") 1255 CheckNotFoundStatus(t, resp) 1256 1257 Client.Logout() 1258 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1259 CheckUnauthorizedStatus(t, resp) 1260 1261 user := th.CreateUser() 1262 Client.Login(user.Email, user.Password) 1263 _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") 1264 CheckForbiddenStatus(t, resp) 1265 } 1266 1267 func TestGetChannelMembers(t *testing.T) { 1268 th := Setup().InitBasic() 1269 defer th.TearDown() 1270 Client := th.Client 1271 1272 members, resp := Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1273 CheckNoError(t, resp) 1274 1275 if len(*members) != 3 { 1276 t.Fatal("should only be 3 users in channel") 1277 } 1278 1279 members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 2, "") 1280 CheckNoError(t, resp) 1281 1282 if len(*members) != 2 { 1283 t.Fatal("should only be 2 users") 1284 } 1285 1286 members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 1, 1, "") 1287 CheckNoError(t, resp) 1288 1289 if len(*members) != 1 { 1290 t.Fatal("should only be 1 user") 1291 } 1292 1293 members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 1000, 100000, "") 1294 CheckNoError(t, resp) 1295 1296 if len(*members) != 0 { 1297 t.Fatal("should be 0 users") 1298 } 1299 1300 _, resp = Client.GetChannelMembers("", 0, 60, "") 1301 CheckBadRequestStatus(t, resp) 1302 1303 _, resp = Client.GetChannelMembers("junk", 0, 60, "") 1304 CheckBadRequestStatus(t, resp) 1305 1306 _, resp = Client.GetChannelMembers(model.NewId(), 0, 60, "") 1307 CheckForbiddenStatus(t, resp) 1308 1309 Client.Logout() 1310 _, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1311 CheckUnauthorizedStatus(t, resp) 1312 1313 user := th.CreateUser() 1314 Client.Login(user.Email, user.Password) 1315 _, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1316 CheckForbiddenStatus(t, resp) 1317 1318 _, resp = th.SystemAdminClient.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") 1319 CheckNoError(t, resp) 1320 } 1321 1322 func TestGetChannelMembersByIds(t *testing.T) { 1323 th := Setup().InitBasic() 1324 defer th.TearDown() 1325 Client := th.Client 1326 1327 cm, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id}) 1328 CheckNoError(t, resp) 1329 1330 if (*cm)[0].UserId != th.BasicUser.Id { 1331 t.Fatal("returned wrong user") 1332 } 1333 1334 _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{}) 1335 CheckBadRequestStatus(t, resp) 1336 1337 cm1, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk"}) 1338 CheckNoError(t, resp) 1339 if len(*cm1) > 0 { 1340 t.Fatal("no users should be returned") 1341 } 1342 1343 cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk", th.BasicUser.Id}) 1344 CheckNoError(t, resp) 1345 if len(*cm1) != 1 { 1346 t.Fatal("1 member should be returned") 1347 } 1348 1349 cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id}) 1350 CheckNoError(t, resp) 1351 if len(*cm1) != 2 { 1352 t.Fatal("2 members should be returned") 1353 } 1354 1355 _, resp = Client.GetChannelMembersByIds("junk", []string{th.BasicUser.Id}) 1356 CheckBadRequestStatus(t, resp) 1357 1358 _, resp = Client.GetChannelMembersByIds(model.NewId(), []string{th.BasicUser.Id}) 1359 CheckForbiddenStatus(t, resp) 1360 1361 Client.Logout() 1362 _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id}) 1363 CheckUnauthorizedStatus(t, resp) 1364 1365 _, resp = th.SystemAdminClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id}) 1366 CheckNoError(t, resp) 1367 } 1368 1369 func TestGetChannelMember(t *testing.T) { 1370 th := Setup().InitBasic() 1371 defer th.TearDown() 1372 Client := th.Client 1373 1374 member, resp := Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1375 CheckNoError(t, resp) 1376 1377 if member.ChannelId != th.BasicChannel.Id { 1378 t.Fatal("wrong channel id") 1379 } 1380 1381 if member.UserId != th.BasicUser.Id { 1382 t.Fatal("wrong user id") 1383 } 1384 1385 _, resp = Client.GetChannelMember("", th.BasicUser.Id, "") 1386 CheckNotFoundStatus(t, resp) 1387 1388 _, resp = Client.GetChannelMember("junk", th.BasicUser.Id, "") 1389 CheckBadRequestStatus(t, resp) 1390 1391 _, resp = Client.GetChannelMember(model.NewId(), th.BasicUser.Id, "") 1392 CheckForbiddenStatus(t, resp) 1393 1394 _, resp = Client.GetChannelMember(th.BasicChannel.Id, "", "") 1395 CheckNotFoundStatus(t, resp) 1396 1397 _, resp = Client.GetChannelMember(th.BasicChannel.Id, "junk", "") 1398 CheckBadRequestStatus(t, resp) 1399 1400 _, resp = Client.GetChannelMember(th.BasicChannel.Id, model.NewId(), "") 1401 CheckNotFoundStatus(t, resp) 1402 1403 Client.Logout() 1404 _, resp = Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1405 CheckUnauthorizedStatus(t, resp) 1406 1407 user := th.CreateUser() 1408 Client.Login(user.Email, user.Password) 1409 _, resp = Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1410 CheckForbiddenStatus(t, resp) 1411 1412 _, resp = th.SystemAdminClient.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1413 CheckNoError(t, resp) 1414 } 1415 1416 func TestGetChannelMembersForUser(t *testing.T) { 1417 th := Setup().InitBasic() 1418 defer th.TearDown() 1419 Client := th.Client 1420 1421 members, resp := Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1422 CheckNoError(t, resp) 1423 1424 if len(*members) != 6 { 1425 t.Fatal("should have 6 members on team") 1426 } 1427 1428 _, resp = Client.GetChannelMembersForUser("", th.BasicTeam.Id, "") 1429 CheckNotFoundStatus(t, resp) 1430 1431 _, resp = Client.GetChannelMembersForUser("junk", th.BasicTeam.Id, "") 1432 CheckBadRequestStatus(t, resp) 1433 1434 _, resp = Client.GetChannelMembersForUser(model.NewId(), th.BasicTeam.Id, "") 1435 CheckForbiddenStatus(t, resp) 1436 1437 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, "", "") 1438 CheckNotFoundStatus(t, resp) 1439 1440 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, "junk", "") 1441 CheckBadRequestStatus(t, resp) 1442 1443 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, model.NewId(), "") 1444 CheckForbiddenStatus(t, resp) 1445 1446 Client.Logout() 1447 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1448 CheckUnauthorizedStatus(t, resp) 1449 1450 user := th.CreateUser() 1451 Client.Login(user.Email, user.Password) 1452 _, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1453 CheckForbiddenStatus(t, resp) 1454 1455 _, resp = th.SystemAdminClient.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") 1456 CheckNoError(t, resp) 1457 } 1458 1459 func TestViewChannel(t *testing.T) { 1460 th := Setup().InitBasic() 1461 defer th.TearDown() 1462 Client := th.Client 1463 1464 view := &model.ChannelView{ 1465 ChannelId: th.BasicChannel.Id, 1466 } 1467 1468 viewResp, resp := Client.ViewChannel(th.BasicUser.Id, view) 1469 CheckNoError(t, resp) 1470 1471 if viewResp.Status != "OK" { 1472 t.Fatal("should have passed") 1473 } 1474 1475 channel, _ := th.App.GetChannel(th.BasicChannel.Id) 1476 1477 if lastViewedAt := viewResp.LastViewedAtTimes[channel.Id]; lastViewedAt != channel.LastPostAt { 1478 t.Fatal("LastPostAt does not match returned LastViewedAt time") 1479 } 1480 1481 view.PrevChannelId = th.BasicChannel.Id 1482 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1483 CheckNoError(t, resp) 1484 1485 view.PrevChannelId = "" 1486 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1487 CheckNoError(t, resp) 1488 1489 view.PrevChannelId = "junk" 1490 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1491 CheckBadRequestStatus(t, resp) 1492 1493 // All blank is OK we use it for clicking off of the browser. 1494 view.PrevChannelId = "" 1495 view.ChannelId = "" 1496 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1497 CheckNoError(t, resp) 1498 1499 view.PrevChannelId = "" 1500 view.ChannelId = "junk" 1501 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1502 CheckBadRequestStatus(t, resp) 1503 1504 view.ChannelId = "correctlysizedjunkdddfdfdf" 1505 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1506 CheckBadRequestStatus(t, resp) 1507 view.ChannelId = th.BasicChannel.Id 1508 1509 member, resp := Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1510 CheckNoError(t, resp) 1511 channel, resp = Client.GetChannel(th.BasicChannel.Id, "") 1512 CheckNoError(t, resp) 1513 1514 if member.MsgCount != channel.TotalMsgCount { 1515 t.Fatal("should match message counts") 1516 } 1517 1518 if member.MentionCount != 0 { 1519 t.Fatal("should have no mentions") 1520 } 1521 1522 _, resp = Client.ViewChannel("junk", view) 1523 CheckBadRequestStatus(t, resp) 1524 1525 _, resp = Client.ViewChannel(th.BasicUser2.Id, view) 1526 CheckForbiddenStatus(t, resp) 1527 1528 if r, err := Client.DoApiPost(fmt.Sprintf("/channels/members/%v/view", th.BasicUser.Id), "garbage"); err == nil { 1529 t.Fatal("should have errored") 1530 } else { 1531 if r.StatusCode != http.StatusBadRequest { 1532 t.Log("actual: " + strconv.Itoa(r.StatusCode)) 1533 t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) 1534 t.Fatal("wrong status code") 1535 } 1536 } 1537 1538 Client.Logout() 1539 _, resp = Client.ViewChannel(th.BasicUser.Id, view) 1540 CheckUnauthorizedStatus(t, resp) 1541 1542 _, resp = th.SystemAdminClient.ViewChannel(th.BasicUser.Id, view) 1543 CheckNoError(t, resp) 1544 } 1545 1546 func TestGetChannelUnread(t *testing.T) { 1547 th := Setup().InitBasic() 1548 defer th.TearDown() 1549 Client := th.Client 1550 user := th.BasicUser 1551 channel := th.BasicChannel 1552 1553 channelUnread, resp := Client.GetChannelUnread(channel.Id, user.Id) 1554 CheckNoError(t, resp) 1555 if channelUnread.TeamId != th.BasicTeam.Id { 1556 t.Fatal("wrong team id returned for a regular user call") 1557 } else if channelUnread.ChannelId != channel.Id { 1558 t.Fatal("wrong team id returned for a regular user call") 1559 } 1560 1561 _, resp = Client.GetChannelUnread("junk", user.Id) 1562 CheckBadRequestStatus(t, resp) 1563 1564 _, resp = Client.GetChannelUnread(channel.Id, "junk") 1565 CheckBadRequestStatus(t, resp) 1566 1567 _, resp = Client.GetChannelUnread(channel.Id, model.NewId()) 1568 CheckForbiddenStatus(t, resp) 1569 1570 _, resp = Client.GetChannelUnread(model.NewId(), user.Id) 1571 CheckForbiddenStatus(t, resp) 1572 1573 newUser := th.CreateUser() 1574 Client.Login(newUser.Email, newUser.Password) 1575 _, resp = Client.GetChannelUnread(th.BasicChannel.Id, user.Id) 1576 CheckForbiddenStatus(t, resp) 1577 1578 Client.Logout() 1579 1580 _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, user.Id) 1581 CheckNoError(t, resp) 1582 1583 _, resp = th.SystemAdminClient.GetChannelUnread(model.NewId(), user.Id) 1584 CheckForbiddenStatus(t, resp) 1585 1586 _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, model.NewId()) 1587 CheckNotFoundStatus(t, resp) 1588 } 1589 1590 func TestGetChannelStats(t *testing.T) { 1591 th := Setup().InitBasic() 1592 defer th.TearDown() 1593 Client := th.Client 1594 channel := th.CreatePrivateChannel() 1595 1596 stats, resp := Client.GetChannelStats(channel.Id, "") 1597 CheckNoError(t, resp) 1598 1599 if stats.ChannelId != channel.Id { 1600 t.Fatal("couldnt't get extra info") 1601 } else if stats.MemberCount != 1 { 1602 t.Fatal("got incorrect member count") 1603 } 1604 1605 _, resp = Client.GetChannelStats("junk", "") 1606 CheckBadRequestStatus(t, resp) 1607 1608 _, resp = Client.GetChannelStats(model.NewId(), "") 1609 CheckForbiddenStatus(t, resp) 1610 1611 Client.Logout() 1612 _, resp = Client.GetChannelStats(channel.Id, "") 1613 CheckUnauthorizedStatus(t, resp) 1614 1615 th.LoginBasic2() 1616 1617 _, resp = Client.GetChannelStats(channel.Id, "") 1618 CheckForbiddenStatus(t, resp) 1619 1620 _, resp = th.SystemAdminClient.GetChannelStats(channel.Id, "") 1621 CheckNoError(t, resp) 1622 } 1623 1624 func TestGetPinnedPosts(t *testing.T) { 1625 th := Setup().InitBasic() 1626 defer th.TearDown() 1627 Client := th.Client 1628 channel := th.BasicChannel 1629 1630 posts, resp := Client.GetPinnedPosts(channel.Id, "") 1631 CheckNoError(t, resp) 1632 if len(posts.Posts) != 0 { 1633 t.Fatal("should not have gotten a pinned post") 1634 } 1635 1636 pinnedPost := th.CreatePinnedPost() 1637 posts, resp = Client.GetPinnedPosts(channel.Id, "") 1638 CheckNoError(t, resp) 1639 if len(posts.Posts) != 1 { 1640 t.Fatal("should have returned 1 pinned post") 1641 } 1642 if _, ok := posts.Posts[pinnedPost.Id]; !ok { 1643 t.Fatal("missing pinned post") 1644 } 1645 1646 posts, resp = Client.GetPinnedPosts(channel.Id, resp.Etag) 1647 CheckEtag(t, posts, resp) 1648 1649 _, resp = Client.GetPinnedPosts(GenerateTestId(), "") 1650 CheckForbiddenStatus(t, resp) 1651 1652 _, resp = Client.GetPinnedPosts("junk", "") 1653 CheckBadRequestStatus(t, resp) 1654 1655 Client.Logout() 1656 _, resp = Client.GetPinnedPosts(channel.Id, "") 1657 CheckUnauthorizedStatus(t, resp) 1658 1659 _, resp = th.SystemAdminClient.GetPinnedPosts(channel.Id, "") 1660 CheckNoError(t, resp) 1661 } 1662 1663 func TestUpdateChannelRoles(t *testing.T) { 1664 th := Setup().InitBasic() 1665 defer th.TearDown() 1666 Client := th.Client 1667 1668 const CHANNEL_ADMIN = "channel_user channel_admin" 1669 const CHANNEL_MEMBER = "channel_user" 1670 1671 // User 1 creates a channel, making them channel admin by default. 1672 channel := th.CreatePublicChannel() 1673 1674 // Adds User 2 to the channel, making them a channel member by default. 1675 th.App.AddUserToChannel(th.BasicUser2, channel) 1676 1677 // User 1 promotes User 2 1678 pass, resp := Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_ADMIN) 1679 CheckNoError(t, resp) 1680 1681 if !pass { 1682 t.Fatal("should have passed") 1683 } 1684 1685 member, resp := Client.GetChannelMember(channel.Id, th.BasicUser2.Id, "") 1686 CheckNoError(t, resp) 1687 1688 if member.Roles != CHANNEL_ADMIN { 1689 t.Fatal("roles don't match") 1690 } 1691 1692 // User 1 demotes User 2 1693 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_MEMBER) 1694 CheckNoError(t, resp) 1695 1696 th.LoginBasic2() 1697 1698 // User 2 cannot demote User 1 1699 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER) 1700 CheckForbiddenStatus(t, resp) 1701 1702 // User 2 cannot promote self 1703 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_ADMIN) 1704 CheckForbiddenStatus(t, resp) 1705 1706 th.LoginBasic() 1707 1708 // User 1 demotes self 1709 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER) 1710 CheckNoError(t, resp) 1711 1712 // System Admin promotes User 1 1713 _, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_ADMIN) 1714 CheckNoError(t, resp) 1715 1716 // System Admin demotes User 1 1717 _, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER) 1718 CheckNoError(t, resp) 1719 1720 // System Admin promotes User 1 1721 _, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_ADMIN) 1722 CheckNoError(t, resp) 1723 1724 th.LoginBasic() 1725 1726 _, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, "junk") 1727 CheckBadRequestStatus(t, resp) 1728 1729 _, resp = Client.UpdateChannelRoles(channel.Id, "junk", CHANNEL_MEMBER) 1730 CheckBadRequestStatus(t, resp) 1731 1732 _, resp = Client.UpdateChannelRoles("junk", th.BasicUser.Id, CHANNEL_MEMBER) 1733 CheckBadRequestStatus(t, resp) 1734 1735 _, resp = Client.UpdateChannelRoles(channel.Id, model.NewId(), CHANNEL_MEMBER) 1736 CheckNotFoundStatus(t, resp) 1737 1738 _, resp = Client.UpdateChannelRoles(model.NewId(), th.BasicUser.Id, CHANNEL_MEMBER) 1739 CheckForbiddenStatus(t, resp) 1740 } 1741 1742 func TestUpdateChannelMemberSchemeRoles(t *testing.T) { 1743 th := Setup().InitBasic() 1744 defer th.TearDown() 1745 SystemAdminClient := th.SystemAdminClient 1746 th.LoginBasic() 1747 1748 s1 := &model.SchemeRoles{ 1749 SchemeAdmin: false, 1750 SchemeUser: false, 1751 } 1752 _, r1 := SystemAdminClient.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, th.BasicUser.Id, s1) 1753 CheckNoError(t, r1) 1754 1755 tm1, rtm1 := SystemAdminClient.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1756 CheckNoError(t, rtm1) 1757 assert.Equal(t, false, tm1.SchemeUser) 1758 assert.Equal(t, false, tm1.SchemeAdmin) 1759 1760 s2 := &model.SchemeRoles{ 1761 SchemeAdmin: false, 1762 SchemeUser: true, 1763 } 1764 _, r2 := SystemAdminClient.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, th.BasicUser.Id, s2) 1765 CheckNoError(t, r2) 1766 1767 tm2, rtm2 := SystemAdminClient.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1768 CheckNoError(t, rtm2) 1769 assert.Equal(t, true, tm2.SchemeUser) 1770 assert.Equal(t, false, tm2.SchemeAdmin) 1771 1772 s3 := &model.SchemeRoles{ 1773 SchemeAdmin: true, 1774 SchemeUser: false, 1775 } 1776 _, r3 := SystemAdminClient.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, th.BasicUser.Id, s3) 1777 CheckNoError(t, r3) 1778 1779 tm3, rtm3 := SystemAdminClient.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1780 CheckNoError(t, rtm3) 1781 assert.Equal(t, false, tm3.SchemeUser) 1782 assert.Equal(t, true, tm3.SchemeAdmin) 1783 1784 s4 := &model.SchemeRoles{ 1785 SchemeAdmin: true, 1786 SchemeUser: true, 1787 } 1788 _, r4 := SystemAdminClient.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, th.BasicUser.Id, s4) 1789 CheckNoError(t, r4) 1790 1791 tm4, rtm4 := SystemAdminClient.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "") 1792 CheckNoError(t, rtm4) 1793 assert.Equal(t, true, tm4.SchemeUser) 1794 assert.Equal(t, true, tm4.SchemeAdmin) 1795 1796 _, resp := SystemAdminClient.UpdateChannelMemberSchemeRoles(model.NewId(), th.BasicUser.Id, s4) 1797 CheckForbiddenStatus(t, resp) 1798 1799 _, resp = SystemAdminClient.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, model.NewId(), s4) 1800 CheckNotFoundStatus(t, resp) 1801 1802 _, resp = SystemAdminClient.UpdateChannelMemberSchemeRoles("ASDF", th.BasicUser.Id, s4) 1803 CheckBadRequestStatus(t, resp) 1804 1805 _, resp = SystemAdminClient.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, "ASDF", s4) 1806 CheckBadRequestStatus(t, resp) 1807 1808 th.LoginBasic2() 1809 _, resp = th.Client.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, th.BasicUser.Id, s4) 1810 CheckForbiddenStatus(t, resp) 1811 1812 SystemAdminClient.Logout() 1813 _, resp = SystemAdminClient.UpdateChannelMemberSchemeRoles(th.BasicChannel.Id, th.SystemAdminUser.Id, s4) 1814 CheckUnauthorizedStatus(t, resp) 1815 } 1816 1817 func TestUpdateChannelNotifyProps(t *testing.T) { 1818 th := Setup().InitBasic() 1819 defer th.TearDown() 1820 Client := th.Client 1821 1822 props := map[string]string{} 1823 props[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION 1824 props[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION 1825 1826 pass, resp := Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props) 1827 CheckNoError(t, resp) 1828 1829 if !pass { 1830 t.Fatal("should have passed") 1831 } 1832 1833 member, err := th.App.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id) 1834 if err != nil { 1835 t.Fatal(err) 1836 } 1837 1838 if member.NotifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION { 1839 t.Fatal("bad update") 1840 } else if member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_MENTION { 1841 t.Fatal("bad update") 1842 } 1843 1844 _, resp = Client.UpdateChannelNotifyProps("junk", th.BasicUser.Id, props) 1845 CheckBadRequestStatus(t, resp) 1846 1847 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, "junk", props) 1848 CheckBadRequestStatus(t, resp) 1849 1850 _, resp = Client.UpdateChannelNotifyProps(model.NewId(), th.BasicUser.Id, props) 1851 CheckNotFoundStatus(t, resp) 1852 1853 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, model.NewId(), props) 1854 CheckForbiddenStatus(t, resp) 1855 1856 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, map[string]string{}) 1857 CheckNoError(t, resp) 1858 1859 Client.Logout() 1860 _, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props) 1861 CheckUnauthorizedStatus(t, resp) 1862 1863 _, resp = th.SystemAdminClient.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props) 1864 CheckNoError(t, resp) 1865 } 1866 1867 func TestAddChannelMember(t *testing.T) { 1868 th := Setup().InitBasic() 1869 defer th.TearDown() 1870 Client := th.Client 1871 user := th.BasicUser 1872 user2 := th.BasicUser2 1873 team := th.BasicTeam 1874 publicChannel := th.CreatePublicChannel() 1875 privateChannel := th.CreatePrivateChannel() 1876 1877 user3 := th.CreateUserWithClient(th.SystemAdminClient) 1878 _, resp := th.SystemAdminClient.AddTeamMember(team.Id, user3.Id) 1879 CheckNoError(t, resp) 1880 1881 cm, resp := Client.AddChannelMember(publicChannel.Id, user2.Id) 1882 CheckNoError(t, resp) 1883 CheckCreatedStatus(t, resp) 1884 1885 if cm.ChannelId != publicChannel.Id { 1886 t.Fatal("should have returned exact channel") 1887 } 1888 1889 if cm.UserId != user2.Id { 1890 t.Fatal("should have returned exact user added to public channel") 1891 } 1892 1893 cm, resp = Client.AddChannelMember(privateChannel.Id, user2.Id) 1894 CheckNoError(t, resp) 1895 1896 if cm.ChannelId != privateChannel.Id { 1897 t.Fatal("should have returned exact channel") 1898 } 1899 1900 if cm.UserId != user2.Id { 1901 t.Fatal("should have returned exact user added to private channel") 1902 } 1903 1904 post := &model.Post{ChannelId: publicChannel.Id, Message: "a" + GenerateTestId() + "a"} 1905 rpost, err := Client.CreatePost(post) 1906 if err == nil { 1907 t.Fatal("should have created a post") 1908 } 1909 1910 Client.RemoveUserFromChannel(publicChannel.Id, user.Id) 1911 _, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, rpost.Id) 1912 CheckNoError(t, resp) 1913 CheckCreatedStatus(t, resp) 1914 1915 Client.RemoveUserFromChannel(publicChannel.Id, user.Id) 1916 _, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, "junk") 1917 CheckBadRequestStatus(t, resp) 1918 1919 _, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, GenerateTestId()) 1920 CheckNotFoundStatus(t, resp) 1921 1922 Client.RemoveUserFromChannel(publicChannel.Id, user.Id) 1923 _, resp = Client.AddChannelMember(publicChannel.Id, user.Id) 1924 CheckNoError(t, resp) 1925 1926 cm, resp = Client.AddChannelMember(publicChannel.Id, "junk") 1927 CheckBadRequestStatus(t, resp) 1928 1929 if cm != nil { 1930 t.Fatal("should return nothing") 1931 } 1932 1933 _, resp = Client.AddChannelMember(publicChannel.Id, GenerateTestId()) 1934 CheckNotFoundStatus(t, resp) 1935 1936 _, resp = Client.AddChannelMember("junk", user2.Id) 1937 CheckBadRequestStatus(t, resp) 1938 1939 _, resp = Client.AddChannelMember(GenerateTestId(), user2.Id) 1940 CheckNotFoundStatus(t, resp) 1941 1942 otherUser := th.CreateUser() 1943 otherChannel := th.CreatePublicChannel() 1944 Client.Logout() 1945 Client.Login(user2.Id, user2.Password) 1946 1947 _, resp = Client.AddChannelMember(publicChannel.Id, otherUser.Id) 1948 CheckUnauthorizedStatus(t, resp) 1949 1950 _, resp = Client.AddChannelMember(privateChannel.Id, otherUser.Id) 1951 CheckUnauthorizedStatus(t, resp) 1952 1953 _, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id) 1954 CheckUnauthorizedStatus(t, resp) 1955 1956 Client.Logout() 1957 Client.Login(user.Id, user.Password) 1958 1959 // should fail adding user who is not a member of the team 1960 _, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id) 1961 CheckUnauthorizedStatus(t, resp) 1962 1963 Client.DeleteChannel(otherChannel.Id) 1964 1965 // should fail adding user to a deleted channel 1966 _, resp = Client.AddChannelMember(otherChannel.Id, user2.Id) 1967 CheckUnauthorizedStatus(t, resp) 1968 1969 Client.Logout() 1970 _, resp = Client.AddChannelMember(publicChannel.Id, user2.Id) 1971 CheckUnauthorizedStatus(t, resp) 1972 1973 _, resp = Client.AddChannelMember(privateChannel.Id, user2.Id) 1974 CheckUnauthorizedStatus(t, resp) 1975 1976 _, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user2.Id) 1977 CheckNoError(t, resp) 1978 1979 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 1980 CheckNoError(t, resp) 1981 1982 // Check the appropriate permissions are enforced. 1983 defaultRolePermissions := th.SaveDefaultRolePermissions() 1984 defer func() { 1985 th.RestoreDefaultRolePermissions(defaultRolePermissions) 1986 }() 1987 1988 th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID) 1989 1990 // Check that a regular channel user can add other users. 1991 Client.Login(user2.Username, user2.Password) 1992 privateChannel = th.CreatePrivateChannel() 1993 _, resp = Client.AddChannelMember(privateChannel.Id, user.Id) 1994 CheckNoError(t, resp) 1995 Client.Logout() 1996 1997 Client.Login(user.Username, user.Password) 1998 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 1999 CheckNoError(t, resp) 2000 Client.Logout() 2001 2002 // Restrict the permission for adding users to Channel Admins 2003 th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_ADMIN_ROLE_ID) 2004 th.RemovePermissionFromRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID) 2005 2006 Client.Login(user2.Username, user2.Password) 2007 privateChannel = th.CreatePrivateChannel() 2008 _, resp = Client.AddChannelMember(privateChannel.Id, user.Id) 2009 CheckNoError(t, resp) 2010 Client.Logout() 2011 2012 Client.Login(user.Username, user.Password) 2013 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 2014 CheckForbiddenStatus(t, resp) 2015 Client.Logout() 2016 2017 th.MakeUserChannelAdmin(user, privateChannel) 2018 th.App.InvalidateAllCaches() 2019 2020 Client.Login(user.Username, user.Password) 2021 _, resp = Client.AddChannelMember(privateChannel.Id, user3.Id) 2022 CheckNoError(t, resp) 2023 Client.Logout() 2024 } 2025 2026 func TestRemoveChannelMember(t *testing.T) { 2027 th := Setup().InitBasic() 2028 user1 := th.BasicUser 2029 user2 := th.BasicUser2 2030 team := th.BasicTeam 2031 defer th.TearDown() 2032 Client := th.Client 2033 2034 pass, resp := Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id) 2035 CheckNoError(t, resp) 2036 2037 if !pass { 2038 t.Fatal("should have passed") 2039 } 2040 2041 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, "junk") 2042 CheckBadRequestStatus(t, resp) 2043 2044 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, model.NewId()) 2045 CheckNotFoundStatus(t, resp) 2046 2047 _, resp = Client.RemoveUserFromChannel(model.NewId(), th.BasicUser2.Id) 2048 CheckNotFoundStatus(t, resp) 2049 2050 th.LoginBasic2() 2051 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 2052 CheckForbiddenStatus(t, resp) 2053 2054 th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel) 2055 _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id) 2056 CheckNoError(t, resp) 2057 2058 _, resp = Client.RemoveUserFromChannel(th.BasicChannel2.Id, th.BasicUser.Id) 2059 CheckNoError(t, resp) 2060 2061 _, resp = th.SystemAdminClient.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) 2062 CheckNoError(t, resp) 2063 2064 // Leave deleted channel 2065 th.LoginBasic() 2066 deletedChannel := th.CreatePublicChannel() 2067 th.App.AddUserToChannel(th.BasicUser, deletedChannel) 2068 th.App.AddUserToChannel(th.BasicUser2, deletedChannel) 2069 2070 deletedChannel.DeleteAt = 1 2071 th.App.UpdateChannel(deletedChannel) 2072 2073 _, resp = Client.RemoveUserFromChannel(deletedChannel.Id, th.BasicUser.Id) 2074 CheckNoError(t, resp) 2075 2076 th.LoginBasic() 2077 private := th.CreatePrivateChannel() 2078 th.App.AddUserToChannel(th.BasicUser2, private) 2079 2080 _, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser2.Id) 2081 CheckNoError(t, resp) 2082 2083 th.LoginBasic2() 2084 _, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser.Id) 2085 CheckForbiddenStatus(t, resp) 2086 2087 _, resp = th.SystemAdminClient.RemoveUserFromChannel(private.Id, th.BasicUser.Id) 2088 CheckNoError(t, resp) 2089 2090 th.LoginBasic() 2091 th.UpdateUserToNonTeamAdmin(user1, team) 2092 th.App.InvalidateAllCaches() 2093 2094 // Check the appropriate permissions are enforced. 2095 defaultRolePermissions := th.SaveDefaultRolePermissions() 2096 defer func() { 2097 th.RestoreDefaultRolePermissions(defaultRolePermissions) 2098 }() 2099 2100 th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID) 2101 2102 // Check that a regular channel user can remove other users. 2103 privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 2104 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id) 2105 CheckNoError(t, resp) 2106 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 2107 CheckNoError(t, resp) 2108 2109 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 2110 CheckNoError(t, resp) 2111 2112 // Restrict the permission for adding users to Channel Admins 2113 th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_ADMIN_ROLE_ID) 2114 th.RemovePermissionFromRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID) 2115 2116 privateChannel = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) 2117 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id) 2118 CheckNoError(t, resp) 2119 _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) 2120 CheckNoError(t, resp) 2121 2122 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 2123 CheckForbiddenStatus(t, resp) 2124 2125 th.MakeUserChannelAdmin(user1, privateChannel) 2126 th.App.InvalidateAllCaches() 2127 2128 _, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id) 2129 CheckNoError(t, resp) 2130 2131 // Test on preventing removal of user from a direct channel 2132 directChannel, resp := Client.CreateDirectChannel(user1.Id, user2.Id) 2133 CheckNoError(t, resp) 2134 2135 _, resp = Client.RemoveUserFromChannel(directChannel.Id, user1.Id) 2136 CheckBadRequestStatus(t, resp) 2137 2138 _, resp = Client.RemoveUserFromChannel(directChannel.Id, user2.Id) 2139 CheckBadRequestStatus(t, resp) 2140 2141 _, resp = th.SystemAdminClient.RemoveUserFromChannel(directChannel.Id, user1.Id) 2142 CheckBadRequestStatus(t, resp) 2143 2144 // Test on preventing removal of user from a group channel 2145 user3 := th.CreateUser() 2146 groupChannel, resp := Client.CreateGroupChannel([]string{user1.Id, user2.Id, user3.Id}) 2147 CheckNoError(t, resp) 2148 2149 _, resp = Client.RemoveUserFromChannel(groupChannel.Id, user1.Id) 2150 CheckBadRequestStatus(t, resp) 2151 2152 _, resp = th.SystemAdminClient.RemoveUserFromChannel(groupChannel.Id, user1.Id) 2153 CheckBadRequestStatus(t, resp) 2154 } 2155 2156 func TestAutocompleteChannels(t *testing.T) { 2157 th := Setup().InitBasic() 2158 defer th.TearDown() 2159 2160 // A private channel to make sure private channels are not used 2161 utils.DisableDebugLogForTest() 2162 ptown, _ := th.Client.CreateChannel(&model.Channel{ 2163 DisplayName: "Town", 2164 Name: "town", 2165 Type: model.CHANNEL_PRIVATE, 2166 TeamId: th.BasicTeam.Id, 2167 }) 2168 utils.EnableDebugLogForTest() 2169 defer func() { 2170 th.Client.DeleteChannel(ptown.Id) 2171 }() 2172 2173 for _, tc := range []struct { 2174 description string 2175 teamId string 2176 fragment string 2177 expectedIncludes []string 2178 expectedExcludes []string 2179 }{ 2180 { 2181 "Basic town-square", 2182 th.BasicTeam.Id, 2183 "town", 2184 []string{"town-square"}, 2185 []string{"off-topic", "town"}, 2186 }, 2187 { 2188 "Basic off-topic", 2189 th.BasicTeam.Id, 2190 "off-to", 2191 []string{"off-topic"}, 2192 []string{"town-square", "town"}, 2193 }, 2194 { 2195 "Basic town square and off topic", 2196 th.BasicTeam.Id, 2197 "to", 2198 []string{"off-topic", "town-square"}, 2199 []string{"town"}, 2200 }, 2201 } { 2202 t.Run(tc.description, func(t *testing.T) { 2203 channels, resp := th.Client.AutocompleteChannelsForTeam(tc.teamId, tc.fragment) 2204 if resp.Error != nil { 2205 t.Fatal("Err: " + resp.Error.Error()) 2206 } 2207 for _, expectedInclude := range tc.expectedIncludes { 2208 found := false 2209 for _, channel := range *channels { 2210 if channel.Name == expectedInclude { 2211 found = true 2212 break 2213 } 2214 } 2215 if !found { 2216 t.Fatal("Expected but didn't find channel: " + expectedInclude) 2217 } 2218 } 2219 for _, expectedExclude := range tc.expectedExcludes { 2220 for _, channel := range *channels { 2221 if channel.Name == expectedExclude { 2222 t.Fatal("Found channel we didn't want: " + expectedExclude) 2223 } 2224 } 2225 } 2226 }) 2227 } 2228 } 2229 2230 func TestAutocompleteChannelsForSearch(t *testing.T) { 2231 th := Setup().InitBasic() 2232 defer th.TearDown() 2233 2234 th.LoginSystemAdminWithClient(th.SystemAdminClient) 2235 th.LoginBasicWithClient(th.Client) 2236 2237 u1 := th.CreateUserWithClient(th.SystemAdminClient) 2238 u2 := th.CreateUserWithClient(th.SystemAdminClient) 2239 u3 := th.CreateUserWithClient(th.SystemAdminClient) 2240 u4 := th.CreateUserWithClient(th.SystemAdminClient) 2241 2242 // A private channel to make sure private channels are not used 2243 utils.DisableDebugLogForTest() 2244 ptown, _ := th.SystemAdminClient.CreateChannel(&model.Channel{ 2245 DisplayName: "Town", 2246 Name: "town", 2247 Type: model.CHANNEL_PRIVATE, 2248 TeamId: th.BasicTeam.Id, 2249 }) 2250 defer func() { 2251 th.Client.DeleteChannel(ptown.Id) 2252 }() 2253 mypriv, _ := th.Client.CreateChannel(&model.Channel{ 2254 DisplayName: "My private town", 2255 Name: "townpriv", 2256 Type: model.CHANNEL_PRIVATE, 2257 TeamId: th.BasicTeam.Id, 2258 }) 2259 defer func() { 2260 th.Client.DeleteChannel(mypriv.Id) 2261 }() 2262 utils.EnableDebugLogForTest() 2263 2264 dc1, resp := th.Client.CreateDirectChannel(th.BasicUser.Id, u1.Id) 2265 CheckNoError(t, resp) 2266 defer func() { 2267 th.Client.DeleteChannel(dc1.Id) 2268 }() 2269 2270 dc2, resp := th.SystemAdminClient.CreateDirectChannel(u2.Id, u3.Id) 2271 CheckNoError(t, resp) 2272 defer func() { 2273 th.SystemAdminClient.DeleteChannel(dc2.Id) 2274 }() 2275 2276 gc1, resp := th.Client.CreateGroupChannel([]string{th.BasicUser.Id, u2.Id, u3.Id}) 2277 CheckNoError(t, resp) 2278 defer func() { 2279 th.Client.DeleteChannel(gc1.Id) 2280 }() 2281 2282 gc2, resp := th.SystemAdminClient.CreateGroupChannel([]string{u2.Id, u3.Id, u4.Id}) 2283 CheckNoError(t, resp) 2284 defer func() { 2285 th.SystemAdminClient.DeleteChannel(gc2.Id) 2286 }() 2287 2288 for _, tc := range []struct { 2289 description string 2290 teamId string 2291 fragment string 2292 expectedIncludes []string 2293 expectedExcludes []string 2294 }{ 2295 { 2296 "Basic town-square", 2297 th.BasicTeam.Id, 2298 "town", 2299 []string{"town-square", "townpriv"}, 2300 []string{"off-topic", "town"}, 2301 }, 2302 { 2303 "Basic off-topic", 2304 th.BasicTeam.Id, 2305 "off-to", 2306 []string{"off-topic"}, 2307 []string{"town-square", "town", "townpriv"}, 2308 }, 2309 { 2310 "Basic town square and off topic", 2311 th.BasicTeam.Id, 2312 "to", 2313 []string{"off-topic", "town-square", "townpriv"}, 2314 []string{"town"}, 2315 }, 2316 { 2317 "Direct and group messages", 2318 th.BasicTeam.Id, 2319 "fakeuser", 2320 []string{dc1.Name, gc1.Name}, 2321 []string{dc2.Name, gc2.Name}, 2322 }, 2323 } { 2324 t.Run(tc.description, func(t *testing.T) { 2325 channels, resp := th.Client.AutocompleteChannelsForTeamForSearch(tc.teamId, tc.fragment) 2326 if resp.Error != nil { 2327 t.Fatal("Err: " + resp.Error.Error()) 2328 } 2329 for _, expectedInclude := range tc.expectedIncludes { 2330 found := false 2331 for _, channel := range *channels { 2332 if channel.Name == expectedInclude { 2333 found = true 2334 break 2335 } 2336 } 2337 if !found { 2338 t.Fatal("Expected but didn't find channel: " + expectedInclude + " Channels: " + fmt.Sprintf("%v", channels)) 2339 } 2340 } 2341 2342 for _, expectedExclude := range tc.expectedExcludes { 2343 for _, channel := range *channels { 2344 if channel.Name == expectedExclude { 2345 t.Fatal("Found channel we didn't want: " + expectedExclude) 2346 } 2347 } 2348 } 2349 }) 2350 } 2351 } 2352 2353 func TestUpdateChannelScheme(t *testing.T) { 2354 th := Setup().InitBasic() 2355 defer th.TearDown() 2356 2357 th.App.SetLicense(model.NewTestLicense("")) 2358 2359 th.App.SetPhase2PermissionsMigrationStatus(true) 2360 2361 team, resp := th.SystemAdminClient.CreateTeam(&model.Team{ 2362 DisplayName: "Name", 2363 Description: "Some description", 2364 CompanyName: "Some company name", 2365 AllowOpenInvite: false, 2366 InviteId: "inviteid0", 2367 Name: "z-z-" + model.NewId() + "a", 2368 Email: "success+" + model.NewId() + "@simulator.amazonses.com", 2369 Type: model.TEAM_OPEN, 2370 }) 2371 CheckNoError(t, resp) 2372 2373 channel, resp := th.SystemAdminClient.CreateChannel(&model.Channel{ 2374 DisplayName: "Name", 2375 Name: "z-z-" + model.NewId() + "a", 2376 Type: model.CHANNEL_OPEN, 2377 TeamId: team.Id, 2378 }) 2379 CheckNoError(t, resp) 2380 2381 channelScheme, resp := th.SystemAdminClient.CreateScheme(&model.Scheme{ 2382 DisplayName: "DisplayName", 2383 Name: model.NewId(), 2384 Description: "Some description", 2385 Scope: model.SCHEME_SCOPE_CHANNEL, 2386 }) 2387 CheckNoError(t, resp) 2388 2389 teamScheme, resp := th.SystemAdminClient.CreateScheme(&model.Scheme{ 2390 DisplayName: "DisplayName", 2391 Name: model.NewId(), 2392 Description: "Some description", 2393 Scope: model.SCHEME_SCOPE_TEAM, 2394 }) 2395 CheckNoError(t, resp) 2396 2397 // Test the setup/base case. 2398 _, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, channelScheme.Id) 2399 CheckNoError(t, resp) 2400 2401 // Test various invalid channel and scheme id combinations. 2402 _, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, "x") 2403 CheckBadRequestStatus(t, resp) 2404 _, resp = th.SystemAdminClient.UpdateChannelScheme("x", channelScheme.Id) 2405 CheckBadRequestStatus(t, resp) 2406 _, resp = th.SystemAdminClient.UpdateChannelScheme("x", "x") 2407 CheckBadRequestStatus(t, resp) 2408 2409 // Test that permissions are required. 2410 _, resp = th.Client.UpdateChannelScheme(channel.Id, channelScheme.Id) 2411 CheckForbiddenStatus(t, resp) 2412 2413 // Test that a license is requried. 2414 th.App.SetLicense(nil) 2415 _, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, channelScheme.Id) 2416 CheckNotImplementedStatus(t, resp) 2417 th.App.SetLicense(model.NewTestLicense("")) 2418 2419 // Test an invalid scheme scope. 2420 _, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, teamScheme.Id) 2421 CheckBadRequestStatus(t, resp) 2422 2423 // Test that an unauthenticated user gets rejected. 2424 th.SystemAdminClient.Logout() 2425 _, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, channelScheme.Id) 2426 CheckUnauthorizedStatus(t, resp) 2427 } 2428 2429 func TestGetChannelMembersTimezones(t *testing.T) { 2430 th := Setup().InitBasic() 2431 defer th.TearDown() 2432 Client := th.Client 2433 2434 user := th.BasicUser 2435 user.Timezone["useAutomaticTimezone"] = "false" 2436 user.Timezone["manualTimezone"] = "XOXO/BLABLA" 2437 _, resp := Client.UpdateUser(user) 2438 CheckNoError(t, resp) 2439 2440 user2 := th.BasicUser2 2441 user2.Timezone["automaticTimezone"] = "NoWhere/Island" 2442 _, resp = th.SystemAdminClient.UpdateUser(user2) 2443 CheckNoError(t, resp) 2444 2445 timezone, resp := Client.GetChannelMembersTimezones(th.BasicChannel.Id) 2446 CheckNoError(t, resp) 2447 if len(timezone) != 2 { 2448 t.Fatal("should return 2 timezones") 2449 } 2450 2451 //both users have same timezone 2452 user2.Timezone["automaticTimezone"] = "XOXO/BLABLA" 2453 _, resp = th.SystemAdminClient.UpdateUser(user2) 2454 CheckNoError(t, resp) 2455 2456 timezone, resp = Client.GetChannelMembersTimezones(th.BasicChannel.Id) 2457 CheckNoError(t, resp) 2458 if len(timezone) != 1 { 2459 t.Fatal("should return 1 timezone") 2460 } 2461 2462 //no timezone set should return empty 2463 user2.Timezone["automaticTimezone"] = "" 2464 _, resp = th.SystemAdminClient.UpdateUser(user2) 2465 CheckNoError(t, resp) 2466 2467 user.Timezone["manualTimezone"] = "" 2468 _, resp = Client.UpdateUser(user) 2469 2470 timezone, resp = Client.GetChannelMembersTimezones(th.BasicChannel.Id) 2471 CheckNoError(t, resp) 2472 if len(timezone) > 0 { 2473 t.Fatal("should return 0 timezone") 2474 } 2475 2476 }