diff options
Diffstat (limited to 'api4/channel_test.go')
-rw-r--r-- | api4/channel_test.go | 657 |
1 files changed, 654 insertions, 3 deletions
diff --git a/api4/channel_test.go b/api4/channel_test.go index cbdaef8a6..987678ee0 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -11,6 +11,7 @@ import ( "github.com/mattermost/platform/app" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" ) @@ -184,6 +185,78 @@ func TestCreateChannel(t *testing.T) { } } +func TestUpdateChannel(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + team := th.BasicTeam + + channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} + private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id} + + channel, resp := Client.CreateChannel(channel) + private, resp = Client.CreateChannel(private) + + //Update a open channel + channel.DisplayName = "My new display name" + channel.Header = "My fancy header" + channel.Purpose = "Mattermost ftw!" + + newChannel, resp := Client.UpdateChannel(channel) + CheckNoError(t, resp) + + if newChannel.DisplayName != channel.DisplayName { + t.Fatal("Update failed for DisplayName") + } + + if newChannel.Header != channel.Header { + t.Fatal("Update failed for Header") + } + + if newChannel.Purpose != channel.Purpose { + t.Fatal("Update failed for Purpose") + } + + //Update a private channel + private.DisplayName = "My new display name for private channel" + private.Header = "My fancy private header" + private.Purpose = "Mattermost ftw! in private mode" + + newPrivateChannel, resp := Client.UpdateChannel(private) + CheckNoError(t, resp) + + if newPrivateChannel.DisplayName != private.DisplayName { + t.Fatal("Update failed for DisplayName in private channel") + } + + if newPrivateChannel.Header != private.Header { + t.Fatal("Update failed for Header in private channel") + } + + if newPrivateChannel.Purpose != private.Purpose { + t.Fatal("Update failed for Purpose in private channel") + } + + //Non existing channel + channel1 := &model.Channel{DisplayName: "Test API Name for apiv4", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id} + _, resp = Client.UpdateChannel(channel1) + CheckNotFoundStatus(t, resp) + + //Try to update with not logged user + Client.Logout() + _, resp = Client.UpdateChannel(channel) + CheckUnauthorizedStatus(t, resp) + + //Try to update using another user + user := th.CreateUser() + Client.Login(user.Email, user.Password) + + channel.DisplayName = "Should not update" + _, resp = Client.UpdateChannel(channel) + CheckNotFoundStatus(t, resp) + +} + func TestCreateDirectChannel(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() @@ -248,9 +321,24 @@ func TestGetChannel(t *testing.T) { t.Fatal("ids did not match") } - _, resp = Client.GetChannel(model.NewId(), "") + Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) + _, resp = Client.GetChannel(th.BasicChannel.Id, "") + CheckNoError(t, resp) + + channel, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "") + CheckNoError(t, resp) + + if channel.Id != th.BasicPrivateChannel.Id { + t.Fatal("ids did not match") + } + + Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id) + _, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "") CheckForbiddenStatus(t, resp) + _, resp = Client.GetChannel(model.NewId(), "") + CheckNotFoundStatus(t, resp) + Client.Logout() _, resp = Client.GetChannel(th.BasicChannel.Id, "") CheckUnauthorizedStatus(t, resp) @@ -263,10 +351,331 @@ func TestGetChannel(t *testing.T) { _, resp = th.SystemAdminClient.GetChannel(th.BasicChannel.Id, "") CheckNoError(t, resp) + _, resp = th.SystemAdminClient.GetChannel(th.BasicPrivateChannel.Id, "") + CheckNoError(t, resp) + _, resp = th.SystemAdminClient.GetChannel(th.BasicUser.Id, "") CheckNotFoundStatus(t, resp) } +func TestGetPublicChannelsForTeam(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + team := th.BasicTeam + publicChannel1 := th.BasicChannel + publicChannel2 := th.BasicChannel2 + + channels, resp := Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 4 { + t.Fatal("wrong length") + } + + for i, c := range *channels { + if c.Type != model.CHANNEL_OPEN { + t.Fatal("should include open channel only") + } + + // only check the created 2 public channels + if i < 2 && !(c.DisplayName == publicChannel1.DisplayName || c.DisplayName == publicChannel2.DisplayName) { + t.Logf("channel %v: %v", i, c.DisplayName) + t.Fatal("should match public channel display name only") + } + } + + privateChannel := th.CreatePrivateChannel() + channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 4 { + t.Fatal("wrong length") + } + + for _, c := range *channels { + if c.Type != model.CHANNEL_OPEN { + t.Fatal("should not include private channel") + } + + if c.DisplayName == privateChannel.DisplayName { + t.Fatal("should not match private channel display name") + } + } + + channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 1, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be one channel per page") + } + + channels, resp = Client.GetPublicChannelsForTeam(team.Id, 1, 1, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be one channel per page") + } + + channels, resp = Client.GetPublicChannelsForTeam(team.Id, 10000, 100, "") + CheckNoError(t, resp) + if len(*channels) != 0 { + t.Fatal("should be no channel") + } + + _, resp = Client.GetPublicChannelsForTeam("junk", 0, 100, "") + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetPublicChannelsForTeam(model.NewId(), 0, 100, "") + CheckForbiddenStatus(t, resp) + + Client.Logout() + _, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") + CheckUnauthorizedStatus(t, resp) + + user := th.CreateUser() + Client.Login(user.Email, user.Password) + _, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "") + CheckForbiddenStatus(t, resp) + + _, resp = th.SystemAdminClient.GetPublicChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) +} + +func TestDeleteChannel(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + team := th.BasicTeam + user := th.BasicUser + user2 := th.BasicUser2 + + // successful delete of public channel + publicChannel1 := th.CreatePublicChannel() + pass, resp := Client.DeleteChannel(publicChannel1.Id) + CheckNoError(t, resp) + + if !pass { + t.Fatal("should have passed") + } + + if ch, err := app.GetChannel(publicChannel1.Id); err == nil && ch.DeleteAt == 0 { + t.Fatal("should have failed to get deleted channel") + } else if err := app.JoinChannel(ch, user2.Id, ""); err == nil { + t.Fatal("should have failed to join deleted channel") + } + + post1 := &model.Post{ChannelId: publicChannel1.Id, Message: "a" + GenerateTestId() + "a"} + if _, err := Client.CreatePost(post1); err == nil { + t.Fatal("should have failed to post to deleted channel") + } + + // successful delete of private channel + privateChannel2 := th.CreatePrivateChannel() + _, resp = Client.DeleteChannel(privateChannel2.Id) + CheckNoError(t, resp) + + // successful delete of channel with multiple members + publicChannel3 := th.CreatePublicChannel() + app.AddUserToChannel(user2, publicChannel3) + _, resp = Client.DeleteChannel(publicChannel3.Id) + CheckNoError(t, resp) + + // successful delete by TeamAdmin of channel created by user + publicChannel4 := th.CreatePublicChannel() + th.LoginTeamAdmin() + _, resp = Client.DeleteChannel(publicChannel4.Id) + CheckNoError(t, resp) + + // default channel cannot be deleted. + defaultChannel, _ := app.GetChannelByName(model.DEFAULT_CHANNEL, team.Id) + pass, resp = Client.DeleteChannel(defaultChannel.Id) + CheckBadRequestStatus(t, resp) + + if pass { + t.Fatal("should have failed") + } + + th.LoginBasic() + publicChannel5 := th.CreatePublicChannel() + Client.Logout() + + Client.Login(user2.Id, user2.Password) + _, resp = Client.DeleteChannel(publicChannel5.Id) + CheckUnauthorizedStatus(t, resp) + + _, resp = Client.DeleteChannel("junk") + CheckUnauthorizedStatus(t, resp) + + Client.Logout() + _, resp = Client.DeleteChannel(GenerateTestId()) + CheckUnauthorizedStatus(t, resp) + + _, resp = th.SystemAdminClient.DeleteChannel(publicChannel5.Id) + CheckNoError(t, resp) + + isLicensed := utils.IsLicensed + license := utils.License + restrictPublicChannel := *utils.Cfg.TeamSettings.RestrictPublicChannelManagement + restrictPrivateChannel := *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement + defer func() { + *utils.Cfg.TeamSettings.RestrictPublicChannelManagement = restrictPublicChannel + *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = restrictPrivateChannel + utils.IsLicensed = isLicensed + utils.License = license + utils.SetDefaultRolesBasedOnConfig() + }() + *utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_ALL + *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_ALL + utils.IsLicensed = true + utils.License = &model.License{Features: &model.Features{}} + utils.License.Features.SetDefaults() + utils.SetDefaultRolesBasedOnConfig() + + th = Setup().InitBasic().InitSystemAdmin() + Client = th.Client + team = th.BasicTeam + user = th.BasicUser + + // channels created by SystemAdmin + publicChannel6 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) + privateChannel7 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) + app.AddUserToChannel(user, publicChannel6) + app.AddUserToChannel(user, privateChannel7) + + // successful delete by user + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckNoError(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckNoError(t, resp) + + *utils.Cfg.TeamSettings.RestrictPublicChannelDeletion = model.PERMISSIONS_CHANNEL_ADMIN + *utils.Cfg.TeamSettings.RestrictPrivateChannelDeletion = model.PERMISSIONS_CHANNEL_ADMIN + utils.SetDefaultRolesBasedOnConfig() + + // channels created by SystemAdmin + publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) + privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) + app.AddUserToChannel(user, publicChannel6) + app.AddUserToChannel(user, privateChannel7) + + // cannot delete by user + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckForbiddenStatus(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckForbiddenStatus(t, resp) + + // successful delete by channel admin + MakeUserChannelAdmin(user, publicChannel6) + MakeUserChannelAdmin(user, privateChannel7) + store.ClearChannelCaches() + + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckNoError(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckNoError(t, resp) + + // // channels created by SystemAdmin + publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) + privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) + app.AddUserToChannel(user, publicChannel6) + app.AddUserToChannel(user, privateChannel7) + + // successful delete by team admin + UpdateUserToTeamAdmin(user, team) + app.InvalidateAllCaches() + + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckNoError(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckNoError(t, resp) + + *utils.Cfg.TeamSettings.RestrictPublicChannelDeletion = model.PERMISSIONS_TEAM_ADMIN + *utils.Cfg.TeamSettings.RestrictPrivateChannelDeletion = model.PERMISSIONS_TEAM_ADMIN + utils.SetDefaultRolesBasedOnConfig() + UpdateUserToNonTeamAdmin(user, team) + app.InvalidateAllCaches() + + // channels created by SystemAdmin + publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) + privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) + app.AddUserToChannel(user, publicChannel6) + app.AddUserToChannel(user, privateChannel7) + + // cannot delete by user + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckForbiddenStatus(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckForbiddenStatus(t, resp) + + // // cannot delete by channel admin + MakeUserChannelAdmin(user, publicChannel6) + MakeUserChannelAdmin(user, privateChannel7) + store.ClearChannelCaches() + + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckForbiddenStatus(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckForbiddenStatus(t, resp) + + // successful delete by team admin + UpdateUserToTeamAdmin(th.BasicUser, team) + app.InvalidateAllCaches() + + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckNoError(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckNoError(t, resp) + + *utils.Cfg.TeamSettings.RestrictPublicChannelDeletion = model.PERMISSIONS_SYSTEM_ADMIN + *utils.Cfg.TeamSettings.RestrictPrivateChannelDeletion = model.PERMISSIONS_SYSTEM_ADMIN + utils.SetDefaultRolesBasedOnConfig() + + // channels created by SystemAdmin + publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN) + privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE) + app.AddUserToChannel(user, publicChannel6) + app.AddUserToChannel(user, privateChannel7) + + // cannot delete by user + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckForbiddenStatus(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckForbiddenStatus(t, resp) + + // cannot delete by channel admin + MakeUserChannelAdmin(user, publicChannel6) + MakeUserChannelAdmin(user, privateChannel7) + store.ClearChannelCaches() + + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckForbiddenStatus(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckForbiddenStatus(t, resp) + + // cannot delete by team admin + UpdateUserToTeamAdmin(th.BasicUser, team) + app.InvalidateAllCaches() + + _, resp = Client.DeleteChannel(publicChannel6.Id) + CheckForbiddenStatus(t, resp) + + _, resp = Client.DeleteChannel(privateChannel7.Id) + CheckForbiddenStatus(t, resp) + + // successful delete by SystemAdmin + _, resp = th.SystemAdminClient.DeleteChannel(publicChannel6.Id) + CheckNoError(t, resp) + + _, resp = th.SystemAdminClient.DeleteChannel(privateChannel7.Id) + CheckNoError(t, resp) +} + func TestGetChannelByName(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() @@ -279,9 +688,27 @@ func TestGetChannelByName(t *testing.T) { t.Fatal("names did not match") } + channel, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "") + CheckNoError(t, resp) + + if channel.Name != th.BasicPrivateChannel.Name { + t.Fatal("names did not match") + } + + Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) + _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") + CheckNoError(t, resp) + + Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id) + _, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "") + CheckForbiddenStatus(t, resp) + _, resp = Client.GetChannelByName(GenerateTestChannelName(), th.BasicTeam.Id, "") CheckNotFoundStatus(t, resp) + _, resp = Client.GetChannelByName(GenerateTestChannelName(), "junk", "") + CheckBadRequestStatus(t, resp) + Client.Logout() _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") CheckUnauthorizedStatus(t, resp) @@ -381,6 +808,53 @@ func TestGetChannelMembers(t *testing.T) { CheckNoError(t, resp) } +func TestGetChannelMembersByIds(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + cm, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id}) + CheckNoError(t, resp) + + if (*cm)[0].UserId != th.BasicUser.Id { + t.Fatal("returned wrong user") + } + + _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{}) + CheckBadRequestStatus(t, resp) + + cm1, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk"}) + CheckNoError(t, resp) + if len(*cm1) > 0 { + t.Fatal("no users should be returned") + } + + cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk", th.BasicUser.Id}) + CheckNoError(t, resp) + if len(*cm1) != 1 { + t.Fatal("1 member should be returned") + } + + cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id}) + CheckNoError(t, resp) + if len(*cm1) != 2 { + t.Fatal("2 members should be returned") + } + + _, resp = Client.GetChannelMembersByIds("junk", []string{th.BasicUser.Id}) + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetChannelMembersByIds(model.NewId(), []string{th.BasicUser.Id}) + CheckForbiddenStatus(t, resp) + + Client.Logout() + _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id}) + CheckUnauthorizedStatus(t, resp) + + _, resp = th.SystemAdminClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id}) + CheckNoError(t, resp) +} + func TestGetChannelMember(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() @@ -436,8 +910,8 @@ func TestGetChannelMembersForUser(t *testing.T) { members, resp := Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "") CheckNoError(t, resp) - if len(*members) != 4 { - t.Fatal("should have 4 members on team") + if len(*members) != 5 { + t.Fatal("should have 5 members on team") } _, resp = Client.GetChannelMembersForUser("", th.BasicTeam.Id, "") @@ -536,6 +1010,84 @@ func TestViewChannel(t *testing.T) { CheckNoError(t, resp) } +func TestGetChannelUnread(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + user := th.BasicUser + channel := th.BasicChannel + + channelUnread, resp := Client.GetChannelUnread(channel.Id, user.Id) + CheckNoError(t, resp) + if channelUnread.TeamId != th.BasicTeam.Id { + t.Fatal("wrong team id returned for a regular user call") + } else if channelUnread.ChannelId != channel.Id { + t.Fatal("wrong team id returned for a regular user call") + } + + _, resp = Client.GetChannelUnread("junk", user.Id) + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetChannelUnread(channel.Id, "junk") + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetChannelUnread(channel.Id, model.NewId()) + CheckForbiddenStatus(t, resp) + + _, resp = Client.GetChannelUnread(model.NewId(), user.Id) + CheckForbiddenStatus(t, resp) + + newUser := th.CreateUser() + Client.Login(newUser.Email, newUser.Password) + _, resp = Client.GetChannelUnread(th.BasicChannel.Id, user.Id) + CheckForbiddenStatus(t, resp) + + Client.Logout() + + _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, user.Id) + CheckNoError(t, resp) + + _, resp = th.SystemAdminClient.GetChannelUnread(model.NewId(), user.Id) + CheckNotFoundStatus(t, resp) + + _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, model.NewId()) + CheckNotFoundStatus(t, resp) +} + +func TestGetChannelStats(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + channel := th.CreatePrivateChannel() + + stats, resp := Client.GetChannelStats(channel.Id, "") + CheckNoError(t, resp) + + if stats.ChannelId != channel.Id { + t.Fatal("couldnt't get extra info") + } else if stats.MemberCount != 1 { + t.Fatal("got incorrect member count") + } + + _, resp = Client.GetChannelStats("junk", "") + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetChannelStats(model.NewId(), "") + CheckForbiddenStatus(t, resp) + + Client.Logout() + _, resp = Client.GetChannelStats(channel.Id, "") + CheckUnauthorizedStatus(t, resp) + + th.LoginBasic2() + + _, resp = Client.GetChannelStats(channel.Id, "") + CheckForbiddenStatus(t, resp) + + _, resp = th.SystemAdminClient.GetChannelStats(channel.Id, "") + CheckNoError(t, resp) +} + func TestUpdateChannelRoles(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() @@ -615,6 +1167,98 @@ func TestUpdateChannelRoles(t *testing.T) { CheckForbiddenStatus(t, resp) } +func TestAddChannelMember(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + user := th.BasicUser + user2 := th.BasicUser2 + publicChannel := th.CreatePublicChannel() + privateChannel := th.CreatePrivateChannel() + + cm, resp := Client.AddChannelMember(publicChannel.Id, user2.Id) + CheckNoError(t, resp) + + if cm.ChannelId != publicChannel.Id { + t.Fatal("should have returned exact channel") + } + + if cm.UserId != user2.Id { + t.Fatal("should have returned exact user added to public channel") + } + + cm, resp = Client.AddChannelMember(privateChannel.Id, user2.Id) + CheckNoError(t, resp) + + if cm.ChannelId != privateChannel.Id { + t.Fatal("should have returned exact channel") + } + + if cm.UserId != user2.Id { + t.Fatal("should have returned exact user added to private channel") + } + + Client.RemoveUserFromChannel(publicChannel.Id, user.Id) + _, resp = Client.AddChannelMember(publicChannel.Id, user.Id) + CheckNoError(t, resp) + + cm, resp = Client.AddChannelMember(publicChannel.Id, "junk") + CheckBadRequestStatus(t, resp) + + if cm != nil { + t.Fatal("should return nothing") + } + + _, resp = Client.AddChannelMember(publicChannel.Id, GenerateTestId()) + CheckNotFoundStatus(t, resp) + + _, resp = Client.AddChannelMember("junk", user2.Id) + CheckBadRequestStatus(t, resp) + + _, resp = Client.AddChannelMember(GenerateTestId(), user2.Id) + CheckNotFoundStatus(t, resp) + + otherUser := th.CreateUser() + otherChannel := th.CreatePublicChannel() + Client.Logout() + Client.Login(user2.Id, user2.Password) + + _, resp = Client.AddChannelMember(publicChannel.Id, otherUser.Id) + CheckUnauthorizedStatus(t, resp) + + _, resp = Client.AddChannelMember(privateChannel.Id, otherUser.Id) + CheckUnauthorizedStatus(t, resp) + + _, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id) + CheckUnauthorizedStatus(t, resp) + + Client.Logout() + Client.Login(user.Id, user.Password) + + // should fail adding user who is not a member of the team + _, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id) + CheckUnauthorizedStatus(t, resp) + + Client.DeleteChannel(otherChannel.Id) + + // should fail adding user to a deleted channel + _, resp = Client.AddChannelMember(otherChannel.Id, user2.Id) + CheckUnauthorizedStatus(t, resp) + + Client.Logout() + _, resp = Client.AddChannelMember(publicChannel.Id, user2.Id) + CheckUnauthorizedStatus(t, resp) + + _, resp = Client.AddChannelMember(privateChannel.Id, user2.Id) + CheckUnauthorizedStatus(t, resp) + + _, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user2.Id) + CheckNoError(t, resp) + + _, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id) + CheckNoError(t, resp) +} + func TestRemoveChannelMember(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() @@ -636,6 +1280,9 @@ func TestRemoveChannelMember(t *testing.T) { _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, model.NewId()) CheckNotFoundStatus(t, resp) + _, resp = Client.RemoveUserFromChannel(model.NewId(), th.BasicUser2.Id) + CheckNotFoundStatus(t, resp) + th.LoginBasic2() _, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) CheckForbiddenStatus(t, resp) @@ -657,6 +1304,10 @@ func TestRemoveChannelMember(t *testing.T) { _, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser2.Id) CheckNoError(t, resp) + th.LoginBasic2() + _, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser.Id) + CheckForbiddenStatus(t, resp) + _, resp = th.SystemAdminClient.RemoveUserFromChannel(private.Id, th.BasicUser.Id) CheckNoError(t, resp) } |