diff --git a/server/.golangci.yml b/server/.golangci.yml index a8a1ee5869..82066925d1 100644 --- a/server/.golangci.yml +++ b/server/.golangci.yml @@ -84,7 +84,6 @@ issues: channels/app/bot_test.go|\ channels/app/brand.go|\ channels/app/channel_bookmark_test.go|\ - channels/app/channel_test.go|\ channels/app/config_test.go|\ channels/app/export.go|\ channels/app/export_test.go|\ diff --git a/server/channels/app/channel_test.go b/server/channels/app/channel_test.go index d1bb81dc1a..25b9392d60 100644 --- a/server/channels/app/channel_test.go +++ b/server/channels/app/channel_test.go @@ -36,78 +36,87 @@ func TestPermanentDeleteChannel(t *testing.T) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true }) - channel, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "deletion-test", Name: "deletion-test", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) + channel, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "deletion-test", Name: "deletion-test", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) require.NotNil(t, channel, "Channel shouldn't be nil") - require.Nil(t, err) + require.Nil(t, appErr) defer func() { - th.App.PermanentDeleteChannel(th.Context, channel) + appErr = th.App.PermanentDeleteChannel(th.Context, channel) + require.Nil(t, appErr) }() - incoming, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, channel, &model.IncomingWebhook{ChannelId: channel.Id}) + incoming, appErr := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, channel, &model.IncomingWebhook{ChannelId: channel.Id}) require.NotNil(t, incoming, "incoming webhook should not be nil") - require.Nil(t, err, "Unable to create Incoming Webhook for Channel") - defer th.App.DeleteIncomingWebhook(incoming.Id) + require.Nil(t, appErr, "Unable to create Incoming Webhook for Channel") + defer func(hookID string) { + appErr = th.App.DeleteIncomingWebhook(hookID) + require.Nil(t, appErr) + }(incoming.Id) - incoming, err = th.App.GetIncomingWebhook(incoming.Id) + incoming, appErr = th.App.GetIncomingWebhook(incoming.Id) require.NotNil(t, incoming, "incoming webhook should not be nil") - require.Nil(t, err, "Unable to get new incoming webhook") + require.Nil(t, appErr, "Unable to get new incoming webhook") - outgoing, err := th.App.CreateOutgoingWebhook(&model.OutgoingWebhook{ + outgoing, appErr := th.App.CreateOutgoingWebhook(&model.OutgoingWebhook{ ChannelId: channel.Id, TeamId: channel.TeamId, CreatorId: th.BasicUser.Id, - CallbackURLs: []string{"http://foo"}, + CallbackURLs: []string{"https://foo"}, }) - require.Nil(t, err) - defer th.App.DeleteOutgoingWebhook(outgoing.Id) + require.Nil(t, appErr) + defer func(hookID string) { + appErr = th.App.DeleteOutgoingWebhook(hookID) + require.Nil(t, appErr) + }(outgoing.Id) - outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id) + outgoing, appErr = th.App.GetOutgoingWebhook(outgoing.Id) require.NotNil(t, outgoing, "Outgoing webhook should not be nil") - require.Nil(t, err, "Unable to get new outgoing webhook") + require.Nil(t, appErr, "Unable to get new outgoing webhook") - err = th.App.PermanentDeleteChannel(th.Context, channel) - require.Nil(t, err) + appErr = th.App.PermanentDeleteChannel(th.Context, channel) + require.Nil(t, appErr) - incoming, err = th.App.GetIncomingWebhook(incoming.Id) + incoming, appErr = th.App.GetIncomingWebhook(incoming.Id) require.Nil(t, incoming, "Incoming webhook should be nil") - require.NotNil(t, err, "Incoming webhook wasn't deleted") + require.NotNil(t, appErr, "Incoming webhook wasn't deleted") - outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id) + outgoing, appErr = th.App.GetOutgoingWebhook(outgoing.Id) require.Nil(t, outgoing, "Outgoing webhook should be nil") - require.NotNil(t, err, "Outgoing webhook wasn't deleted") + require.NotNil(t, appErr, "Outgoing webhook wasn't deleted") } func TestRemoveAllDeactivatedMembersFromChannel(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - var err *model.AppError + var appErr *model.AppError team := th.CreateTeam() channel := th.CreateChannel(th.Context, team) defer func() { - th.App.PermanentDeleteChannel(th.Context, channel) - th.App.PermanentDeleteTeam(th.Context, team) + appErr = th.App.PermanentDeleteChannel(th.Context, channel) + require.Nil(t, appErr) + appErr = th.App.PermanentDeleteTeam(th.Context, team) + require.Nil(t, appErr) }() - _, _, err = th.App.AddUserToTeam(th.Context, team.Id, th.BasicUser.Id, "") - require.Nil(t, err) + _, _, appErr = th.App.AddUserToTeam(th.Context, team.Id, th.BasicUser.Id, "") + require.Nil(t, appErr) deactivatedUser := th.CreateUser() - _, _, err = th.App.AddUserToTeam(th.Context, team.Id, deactivatedUser.Id, "") - require.Nil(t, err) - _, err = th.App.AddUserToChannel(th.Context, deactivatedUser, channel, false) - require.Nil(t, err) - channelMembers, err := th.App.GetChannelMembersPage(th.Context, channel.Id, 0, 10000000) - require.Nil(t, err) + _, _, appErr = th.App.AddUserToTeam(th.Context, team.Id, deactivatedUser.Id, "") + require.Nil(t, appErr) + _, appErr = th.App.AddUserToChannel(th.Context, deactivatedUser, channel, false) + require.Nil(t, appErr) + channelMembers, appErr := th.App.GetChannelMembersPage(th.Context, channel.Id, 0, 10000000) + require.Nil(t, appErr) require.Len(t, channelMembers, 2) - _, err = th.App.UpdateActive(th.Context, deactivatedUser, false) - require.Nil(t, err) + _, appErr = th.App.UpdateActive(th.Context, deactivatedUser, false) + require.Nil(t, appErr) - err = th.App.RemoveAllDeactivatedMembersFromChannel(th.Context, channel) - require.Nil(t, err) + appErr = th.App.RemoveAllDeactivatedMembersFromChannel(th.Context, channel) + require.Nil(t, appErr) - channelMembers, err = th.App.GetChannelMembersPage(th.Context, channel.Id, 0, 10000000) - require.Nil(t, err) + channelMembers, appErr = th.App.GetChannelMembersPage(th.Context, channel.Id, 0, 10000000) + require.Nil(t, appErr) require.Len(t, channelMembers, 1) } @@ -115,60 +124,68 @@ func TestMoveChannel(t *testing.T) { t.Run("should move channels between teams", func(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - var err *model.AppError + var appErr *model.AppError sourceTeam := th.CreateTeam() targetTeam := th.CreateTeam() channel1 := th.CreateChannel(th.Context, sourceTeam) defer func() { - th.App.PermanentDeleteChannel(th.Context, channel1) - th.App.PermanentDeleteTeam(th.Context, sourceTeam) - th.App.PermanentDeleteTeam(th.Context, targetTeam) + appErr = th.App.PermanentDeleteChannel(th.Context, channel1) + require.Nil(t, appErr) + + appErr = th.App.PermanentDeleteTeam(th.Context, sourceTeam) + require.Nil(t, appErr) + + appErr = th.App.PermanentDeleteTeam(th.Context, targetTeam) + require.Nil(t, appErr) }() - _, _, err = th.App.AddUserToTeam(th.Context, sourceTeam.Id, th.BasicUser.Id, "") - require.Nil(t, err) + _, _, appErr = th.App.AddUserToTeam(th.Context, sourceTeam.Id, th.BasicUser.Id, "") + require.Nil(t, appErr) - _, _, err = th.App.AddUserToTeam(th.Context, sourceTeam.Id, th.BasicUser2.Id, "") - require.Nil(t, err) + _, _, appErr = th.App.AddUserToTeam(th.Context, sourceTeam.Id, th.BasicUser2.Id, "") + require.Nil(t, appErr) - _, _, err = th.App.AddUserToTeam(th.Context, targetTeam.Id, th.BasicUser.Id, "") - require.Nil(t, err) + _, _, appErr = th.App.AddUserToTeam(th.Context, targetTeam.Id, th.BasicUser.Id, "") + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser, channel1, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser, channel1, false) + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser2, channel1, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser2, channel1, false) + require.Nil(t, appErr) - err = th.App.MoveChannel(th.Context, targetTeam, channel1, th.BasicUser) - require.NotNil(t, err, "Should have failed due to mismatched members.") + appErr = th.App.MoveChannel(th.Context, targetTeam, channel1, th.BasicUser) + require.NotNil(t, appErr, "Should have failed due to mismatched members.") - _, _, err = th.App.AddUserToTeam(th.Context, targetTeam.Id, th.BasicUser2.Id, "") - require.Nil(t, err) + _, _, appErr = th.App.AddUserToTeam(th.Context, targetTeam.Id, th.BasicUser2.Id, "") + require.Nil(t, appErr) - err = th.App.MoveChannel(th.Context, targetTeam, channel1, th.BasicUser) - require.Nil(t, err) + appErr = th.App.MoveChannel(th.Context, targetTeam, channel1, th.BasicUser) + require.Nil(t, appErr) // Test moving a channel with a deactivated user who isn't in the destination team. // It should fail, unless removeDeactivatedMembers is true. deactivatedUser := th.CreateUser() channel2 := th.CreateChannel(th.Context, sourceTeam) - defer th.App.PermanentDeleteChannel(th.Context, channel2) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channel2) + require.Nil(t, appErr) + }() - _, _, err = th.App.AddUserToTeam(th.Context, sourceTeam.Id, deactivatedUser.Id, "") - require.Nil(t, err) - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser, channel2, false) - require.Nil(t, err) + _, _, appErr = th.App.AddUserToTeam(th.Context, sourceTeam.Id, deactivatedUser.Id, "") + require.Nil(t, appErr) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser, channel2, false) + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, deactivatedUser, channel2, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, deactivatedUser, channel2, false) + require.Nil(t, appErr) - _, err = th.App.UpdateActive(th.Context, deactivatedUser, false) - require.Nil(t, err) + _, appErr = th.App.UpdateActive(th.Context, deactivatedUser, false) + require.Nil(t, appErr) - err = th.App.MoveChannel(th.Context, targetTeam, channel2, th.BasicUser) - require.NotNil(t, err, "Should have failed due to mismatched deactivated member.") + appErr = th.App.MoveChannel(th.Context, targetTeam, channel2, th.BasicUser) + require.NotNil(t, appErr, "Should have failed due to mismatched deactivated member.") // Test moving a channel with no members. channel3 := &model.Channel{ @@ -179,12 +196,15 @@ func TestMoveChannel(t *testing.T) { CreatorId: th.BasicUser.Id, } - channel3, err = th.App.CreateChannel(th.Context, channel3, false) - require.Nil(t, err) - defer th.App.PermanentDeleteChannel(th.Context, channel3) + channel3, appErr = th.App.CreateChannel(th.Context, channel3, false) + require.Nil(t, appErr) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channel3) + require.Nil(t, appErr) + }() - err = th.App.MoveChannel(th.Context, targetTeam, channel3, th.BasicUser) - assert.Nil(t, err) + appErr = th.App.MoveChannel(th.Context, targetTeam, channel3, th.BasicUser) + assert.Nil(t, appErr) }) t.Run("should remove sidebar entries when moving channels from one team to another", func(t *testing.T) { @@ -200,30 +220,30 @@ func TestMoveChannel(t *testing.T) { th.AddUserToChannel(th.BasicUser, channel) // Put the channel in a custom category so that it explicitly exists in SidebarChannels - category, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, sourceTeam.Id, &model.SidebarCategoryWithChannels{ + category, appErr := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, sourceTeam.Id, &model.SidebarCategoryWithChannels{ SidebarCategory: model.SidebarCategory{ DisplayName: "new category", }, Channels: []string{channel.Id}, }) - require.Nil(t, err) + require.Nil(t, appErr) require.Equal(t, []string{channel.Id}, category.Channels) - err = th.App.MoveChannel(th.Context, targetTeam, channel, th.BasicUser) - require.Nil(t, err) + appErr = th.App.MoveChannel(th.Context, targetTeam, channel, th.BasicUser) + require.Nil(t, appErr) - moved, err := th.App.GetChannel(th.Context, channel.Id) - require.Nil(t, err) + moved, appErr := th.App.GetChannel(th.Context, channel.Id) + require.Nil(t, appErr) require.Equal(t, targetTeam.Id, moved.TeamId) // The channel should no longer be on the old team - updatedCategory, err := th.App.GetSidebarCategory(th.Context, category.Id) - require.Nil(t, err) + updatedCategory, appErr := th.App.GetSidebarCategory(th.Context, category.Id) + require.Nil(t, appErr) assert.Equal(t, []string{}, updatedCategory.Channels) // And it should be on the new team instead - categories, err := th.App.GetSidebarCategoriesForTeamForUser(th.Context, th.BasicUser.Id, targetTeam.Id) - require.Nil(t, err) + categories, appErr := th.App.GetSidebarCategoriesForTeamForUser(th.Context, th.BasicUser.Id, targetTeam.Id) + require.Nil(t, appErr) require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type) assert.Contains(t, categories.Categories[1].Channels, channel.Id) }) @@ -237,28 +257,33 @@ func TestRemoveUsersFromChannelNotMemberOfTeam(t *testing.T) { team2 := th.CreateTeam() channel1 := th.CreateChannel(th.Context, team) defer func() { - th.App.PermanentDeleteChannel(th.Context, channel1) - th.App.PermanentDeleteTeam(th.Context, team) - th.App.PermanentDeleteTeam(th.Context, team2) + appErr := th.App.PermanentDeleteChannel(th.Context, channel1) + require.Nil(t, appErr) + + appErr = th.App.PermanentDeleteTeam(th.Context, team) + require.Nil(t, appErr) + + appErr = th.App.PermanentDeleteTeam(th.Context, team2) + require.Nil(t, appErr) }() - _, _, err := th.App.AddUserToTeam(th.Context, team.Id, th.BasicUser.Id, "") - require.Nil(t, err) - _, _, err = th.App.AddUserToTeam(th.Context, team2.Id, th.BasicUser.Id, "") - require.Nil(t, err) - _, _, err = th.App.AddUserToTeam(th.Context, team.Id, th.BasicUser2.Id, "") - require.Nil(t, err) + _, _, appErr := th.App.AddUserToTeam(th.Context, team.Id, th.BasicUser.Id, "") + require.Nil(t, appErr) + _, _, appErr = th.App.AddUserToTeam(th.Context, team2.Id, th.BasicUser.Id, "") + require.Nil(t, appErr) + _, _, appErr = th.App.AddUserToTeam(th.Context, team.Id, th.BasicUser2.Id, "") + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser, channel1, false) - require.Nil(t, err) - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser2, channel1, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser, channel1, false) + require.Nil(t, appErr) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser2, channel1, false) + require.Nil(t, appErr) - err = th.App.RemoveUsersFromChannelNotMemberOfTeam(th.Context, th.SystemAdminUser, channel1, team2) - require.Nil(t, err) + appErr = th.App.RemoveUsersFromChannelNotMemberOfTeam(th.Context, th.SystemAdminUser, channel1, team2) + require.Nil(t, appErr) - channelMembers, err := th.App.GetChannelMembersPage(th.Context, channel1.Id, 0, 10000000) - require.Nil(t, err) + channelMembers, appErr := th.App.GetChannelMembersPage(th.Context, channel1.Id, 0, 10000000) + require.Nil(t, appErr) require.Len(t, channelMembers, 1) members := make([]model.ChannelMember, len(channelMembers)) copy(members, channelMembers) @@ -279,7 +304,8 @@ func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordTownSquare(t *testi // create a new user that joins the default channels user := th.CreateUser() - th.App.JoinDefaultChannels(th.Context, th.BasicTeam.Id, user, false, "") + appErr := th.App.JoinDefaultChannels(th.Context, th.BasicTeam.Id, user, false, "") + require.Nil(t, appErr) // there should be a ChannelMemberHistory record for the user histories, nErr := th.App.Srv().Store().ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelID) @@ -310,7 +336,8 @@ func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordOffTopic(t *testing // create a new user that joins the default channels user := th.CreateUser() - th.App.JoinDefaultChannels(th.Context, th.BasicTeam.Id, user, false, "") + appError := th.App.JoinDefaultChannels(th.Context, th.BasicTeam.Id, user, false, "") + require.Nil(t, appError) // there should be a ChannelMemberHistory record for the user histories, nErr := th.App.Srv().Store().ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId) @@ -332,21 +359,24 @@ func TestJoinDefaultChannelsExperimentalDefaultChannels(t *testing.T) { defer th.TearDown() basicChannel2 := th.CreateChannel(th.Context, th.BasicTeam) - defer th.App.PermanentDeleteChannel(th.Context, basicChannel2) + defer func() { + appErr := th.App.PermanentDeleteChannel(th.Context, basicChannel2) + require.Nil(t, appErr) + }() defaultChannelList := []string{th.BasicChannel.Name, basicChannel2.Name, basicChannel2.Name} th.App.Config().TeamSettings.ExperimentalDefaultChannels = defaultChannelList user := th.CreateUser() - th.App.JoinDefaultChannels(th.Context, th.BasicTeam.Id, user, false, "") + appErr := th.App.JoinDefaultChannels(th.Context, th.BasicTeam.Id, user, false, "") + require.Nil(t, appErr) for _, channelName := range defaultChannelList { - channel, err := th.App.GetChannelByName(th.Context, channelName, th.BasicTeam.Id, false) - require.Nil(t, err, "Expected nil, didn't receive nil") - - member, err := th.App.GetChannelMember(th.Context, channel.Id, user.Id) + channel, appErr := th.App.GetChannelByName(th.Context, channelName, th.BasicTeam.Id, false) + require.Nil(t, appErr, "Expected nil, didn't receive nil") + member, appErr := th.App.GetChannelMember(th.Context, channel.Id, user.Id) + require.Nil(t, appErr, "Expected nil object, didn't receive nil") require.NotNil(t, member, "Expected member object, got nil") - require.Nil(t, err, "Expected nil object, didn't receive nil") } } @@ -355,7 +385,10 @@ func TestJoinDefaultChannelsExperimentalDefaultChannelsMissing(t *testing.T) { defer th.TearDown() basicChannel2 := th.CreateChannel(th.Context, th.BasicTeam) - defer th.App.PermanentDeleteChannel(th.Context, basicChannel2) + defer func() { + appErr := th.App.PermanentDeleteChannel(th.Context, basicChannel2) + require.Nil(t, appErr) + }() defaultChannelList := []string{th.BasicChannel.Name, basicChannel2.Name, "thischanneldoesnotexist", basicChannel2.Name} th.App.Config().TeamSettings.ExperimentalDefaultChannels = defaultChannelList @@ -367,13 +400,13 @@ func TestJoinDefaultChannelsExperimentalDefaultChannelsMissing(t *testing.T) { continue // skip the non-existent channel } - channel, err := th.App.GetChannelByName(th.Context, channelName, th.BasicTeam.Id, false) - require.Nil(t, err, "Expected nil, didn't receive nil") + channel, appErr := th.App.GetChannelByName(th.Context, channelName, th.BasicTeam.Id, false) + require.Nil(t, appErr, "Expected nil, didn't receive nil") - member, err := th.App.GetChannelMember(th.Context, channel.Id, user.Id) + member, appErr := th.App.GetChannelMember(th.Context, channel.Id, user.Id) require.NotNil(t, member, "Expected member object, got nil") - require.Nil(t, err, "Expected nil object, didn't receive nil") + require.Nil(t, appErr, "Expected nil object, didn't receive nil") } } @@ -410,9 +443,12 @@ func TestCreateChannelDisplayNameTrimsWhitespace(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - channel, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: " Public 1 ", Name: "public1", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) - defer th.App.PermanentDeleteChannel(th.Context, channel) - require.Nil(t, err) + channel, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: " Public 1 ", Name: "public1", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channel) + require.Nil(t, appErr) + }() + require.Nil(t, appErr) require.Equal(t, channel.DisplayName, "Public 1") } @@ -423,8 +459,8 @@ func TestUpdateChannelPrivacy(t *testing.T) { privateChannel := th.createChannel(th.Context, th.BasicTeam, model.ChannelTypePrivate) privateChannel.Type = model.ChannelTypeOpen - publicChannel, err := th.App.UpdateChannelPrivacy(th.Context, privateChannel, th.BasicUser) - require.Nil(t, err, "Failed to update channel privacy.") + publicChannel, appErr := th.App.UpdateChannelPrivacy(th.Context, privateChannel, th.BasicUser) + require.Nil(t, appErr, "Failed to update channel privacy.") assert.Equal(t, publicChannel.Id, privateChannel.Id) assert.Equal(t, publicChannel.Type, model.ChannelTypeOpen) } @@ -518,9 +554,9 @@ func TestCreateGroupChannelCreatesChannelMemberHistoryRecord(t *testing.T) { groupUserIds = append(groupUserIds, user2.Id) groupUserIds = append(groupUserIds, th.BasicUser.Id) - channel, err := th.App.CreateGroupChannel(th.Context, groupUserIds, th.BasicUser.Id) + channel, appErr := th.App.CreateGroupChannel(th.Context, groupUserIds, th.BasicUser.Id) - require.Nil(t, err, "Failed to create group channel.") + require.Nil(t, appErr, "Failed to create group channel.") histories, nErr := th.App.Srv().Store().ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id) require.NoError(t, nErr) assert.Len(t, histories, 3) @@ -543,8 +579,8 @@ func TestCreateDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) { user1 := th.CreateUser() user2 := th.CreateUser() - channel, err := th.App.GetOrCreateDirectChannel(th.Context, user1.Id, user2.Id) - require.Nil(t, err, "Failed to create direct channel.") + channel, appErr := th.App.GetOrCreateDirectChannel(th.Context, user1.Id, user2.Id) + require.Nil(t, appErr, "Failed to create direct channel.") histories, nErr := th.App.Srv().Store().ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id) require.NoError(t, nErr) @@ -570,8 +606,8 @@ func TestGetDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) { user2 := th.CreateUser() // this function call implicitly creates a direct channel between the two users if one doesn't already exist - channel, err := th.App.GetOrCreateDirectChannel(th.Context, user1.Id, user2.Id) - require.Nil(t, err, "Failed to create direct channel.") + channel, appErr := th.App.GetOrCreateDirectChannel(th.Context, user1.Id, user2.Id) + require.Nil(t, appErr, "Failed to create direct channel.") // there should be a ChannelMemberHistory record for both users histories, nErr := th.App.Srv().Store().ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id) @@ -596,8 +632,8 @@ func TestAddUserToChannelCreatesChannelMemberHistoryRecord(t *testing.T) { // create a user and add it to a channel user := th.CreateUser() - _, err := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, user.Id) - require.Nil(t, err, "Failed to add user to team.") + _, appErr := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, user.Id) + require.Nil(t, appErr, "Failed to add user to team.") groupUserIds := make([]string, 0) groupUserIds = append(groupUserIds, th.BasicUser.Id) @@ -605,8 +641,8 @@ func TestAddUserToChannelCreatesChannelMemberHistoryRecord(t *testing.T) { channel := th.createChannel(th.Context, th.BasicTeam, model.ChannelTypeOpen) - _, err = th.App.AddUserToChannel(th.Context, user, channel, false) - require.Nil(t, err, "Failed to add user to channel.") + _, appErr = th.App.AddUserToChannel(th.Context, user, channel, false) + require.Nil(t, appErr, "Failed to add user to channel.") // there should be a ChannelMemberHistory record for the user histories, nErr := th.App.Srv().Store().ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id) @@ -627,24 +663,24 @@ func TestLeaveDefaultChannel(t *testing.T) { guest := th.CreateGuest() th.LinkUserToTeam(guest, th.BasicTeam) - townSquare, err := th.App.GetChannelByName(th.Context, "town-square", th.BasicTeam.Id, false) - require.Nil(t, err) + townSquare, appErr := th.App.GetChannelByName(th.Context, "town-square", th.BasicTeam.Id, false) + require.Nil(t, appErr) th.AddUserToChannel(guest, townSquare) th.AddUserToChannel(th.BasicUser, townSquare) t.Run("User tries to leave the default channel", func(t *testing.T) { - err = th.App.LeaveChannel(th.Context, townSquare.Id, th.BasicUser.Id) - assert.NotNil(t, err, "It should fail to remove a regular user from the default channel") - assert.Equal(t, err.Id, "api.channel.remove.default.app_error") - _, err = th.App.GetChannelMember(th.Context, townSquare.Id, th.BasicUser.Id) - assert.Nil(t, err) + appErr = th.App.LeaveChannel(th.Context, townSquare.Id, th.BasicUser.Id) + assert.NotNil(t, appErr, "It should fail to remove a regular user from the default channel") + assert.Equal(t, appErr.Id, "api.channel.remove.default.app_error") + _, appErr = th.App.GetChannelMember(th.Context, townSquare.Id, th.BasicUser.Id) + assert.Nil(t, appErr) }) t.Run("Guest leaves the default channel", func(t *testing.T) { - err = th.App.LeaveChannel(th.Context, townSquare.Id, guest.Id) - assert.Nil(t, err, "It should allow to remove a guest user from the default channel") - _, err = th.App.GetChannelMember(th.Context, townSquare.Id, guest.Id) - assert.NotNil(t, err) + appErr = th.App.LeaveChannel(th.Context, townSquare.Id, guest.Id) + assert.Nil(t, appErr, "It should allow to remove a guest user from the default channel") + _, appErr = th.App.GetChannelMember(th.Context, townSquare.Id, guest.Id) + assert.NotNil(t, appErr) }) t.Run("Trying to leave the default channel should not delete thread memberships", func(t *testing.T) { @@ -653,8 +689,8 @@ func TestLeaveDefaultChannel(t *testing.T) { Message: "root post", UserId: th.BasicUser.Id, } - rpost, err := th.App.CreatePost(th.Context, post, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) - require.Nil(t, err) + rpost, appErr := th.App.CreatePost(th.Context, post, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) + require.Nil(t, appErr) reply := &model.Post{ ChannelId: townSquare.Id, @@ -662,19 +698,19 @@ func TestLeaveDefaultChannel(t *testing.T) { UserId: th.BasicUser.Id, RootId: rpost.Id, } - _, err = th.App.CreatePost(th.Context, reply, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) - require.Nil(t, err) + _, appErr = th.App.CreatePost(th.Context, reply, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) + require.Nil(t, appErr) - threads, err := th.App.GetThreadsForUser(th.BasicUser.Id, townSquare.TeamId, model.GetUserThreadsOpts{}) - require.Nil(t, err) + threads, appErr := th.App.GetThreadsForUser(th.BasicUser.Id, townSquare.TeamId, model.GetUserThreadsOpts{}) + require.Nil(t, appErr) require.Len(t, threads.Threads, 1) - err = th.App.LeaveChannel(th.Context, townSquare.Id, th.BasicUser.Id) - assert.NotNil(t, err, "It should fail to remove a regular user from the default channel") - assert.Equal(t, err.Id, "api.channel.remove.default.app_error") + appErr = th.App.LeaveChannel(th.Context, townSquare.Id, th.BasicUser.Id) + assert.NotNil(t, appErr, "It should fail to remove a regular user from the default channel") + assert.Equal(t, appErr.Id, "api.channel.remove.default.app_error") - threads, err = th.App.GetThreadsForUser(th.BasicUser.Id, townSquare.TeamId, model.GetUserThreadsOpts{}) - require.Nil(t, err) + threads, appErr = th.App.GetThreadsForUser(th.BasicUser.Id, townSquare.TeamId, model.GetUserThreadsOpts{}) + require.Nil(t, appErr) require.Len(t, threads.Threads, 1) }) } @@ -691,8 +727,8 @@ func TestLeaveChannel(t *testing.T) { UserId: th.BasicUser.Id, } - rpost, err := th.App.CreatePost(th.Context, post, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) - require.Nil(t, err) + rpost, appErr := th.App.CreatePost(th.Context, post, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) + require.Nil(t, appErr) reply := &model.Post{ ChannelId: channel.Id, @@ -700,8 +736,8 @@ func TestLeaveChannel(t *testing.T) { UserId: th.BasicUser.Id, RootId: rpost.Id, } - _, err = th.App.CreatePost(th.Context, reply, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) - require.Nil(t, err) + _, appErr = th.App.CreatePost(th.Context, reply, th.BasicChannel, model.CreatePostFlags{SetOnline: true}) + require.Nil(t, appErr) return rpost } @@ -711,18 +747,18 @@ func TestLeaveChannel(t *testing.T) { channel2 := th.createChannel(th.Context, th.BasicTeam, model.ChannelTypeOpen) createThread(channel2) - threads, err := th.App.GetThreadsForUser(th.BasicUser.Id, th.BasicChannel.TeamId, model.GetUserThreadsOpts{}) - require.Nil(t, err) + threads, appErr := th.App.GetThreadsForUser(th.BasicUser.Id, th.BasicChannel.TeamId, model.GetUserThreadsOpts{}) + require.Nil(t, appErr) require.Len(t, threads.Threads, 2) - err = th.App.LeaveChannel(th.Context, th.BasicChannel.Id, th.BasicUser.Id) - require.Nil(t, err) + appErr = th.App.LeaveChannel(th.Context, th.BasicChannel.Id, th.BasicUser.Id) + require.Nil(t, appErr) - _, err = th.App.GetChannelMember(th.Context, th.BasicChannel.Id, th.BasicUser.Id) - require.NotNil(t, err, "It should remove channel membership") + _, appErr = th.App.GetChannelMember(th.Context, th.BasicChannel.Id, th.BasicUser.Id) + require.NotNil(t, appErr, "It should remove channel membership") - threads, err = th.App.GetThreadsForUser(th.BasicUser.Id, th.BasicChannel.TeamId, model.GetUserThreadsOpts{}) - require.Nil(t, err) + threads, appErr = th.App.GetThreadsForUser(th.BasicUser.Id, th.BasicChannel.TeamId, model.GetUserThreadsOpts{}) + require.Nil(t, appErr) require.Len(t, threads.Threads, 1) }) } @@ -734,25 +770,25 @@ func TestLeaveLastChannel(t *testing.T) { guest := th.CreateGuest() th.LinkUserToTeam(guest, th.BasicTeam) - townSquare, err := th.App.GetChannelByName(th.Context, "town-square", th.BasicTeam.Id, false) - require.Nil(t, err) + townSquare, appErr := th.App.GetChannelByName(th.Context, "town-square", th.BasicTeam.Id, false) + require.Nil(t, appErr) th.AddUserToChannel(guest, townSquare) th.AddUserToChannel(guest, th.BasicChannel) t.Run("Guest leaves not last channel", func(t *testing.T) { - err = th.App.LeaveChannel(th.Context, townSquare.Id, guest.Id) - require.Nil(t, err) - _, err = th.App.GetTeamMember(th.Context, th.BasicTeam.Id, guest.Id) - assert.Nil(t, err, "It should maintain the team membership") + appErr = th.App.LeaveChannel(th.Context, townSquare.Id, guest.Id) + require.Nil(t, appErr) + _, appErr = th.App.GetTeamMember(th.Context, th.BasicTeam.Id, guest.Id) + assert.Nil(t, appErr, "It should maintain the team membership") }) t.Run("Guest leaves last channel", func(t *testing.T) { - err = th.App.LeaveChannel(th.Context, th.BasicChannel.Id, guest.Id) - assert.Nil(t, err, "It should allow to remove a guest user from the default channel") - _, err = th.App.GetChannelMember(th.Context, th.BasicChannel.Id, guest.Id) - assert.NotNil(t, err) - _, err = th.App.GetTeamMember(th.Context, th.BasicTeam.Id, guest.Id) - assert.Nil(t, err, "It should remove the team membership") + appErr = th.App.LeaveChannel(th.Context, th.BasicChannel.Id, guest.Id) + assert.Nil(t, appErr, "It should allow to remove a guest user from the default channel") + _, appErr = th.App.GetChannelMember(th.Context, th.BasicChannel.Id, guest.Id) + assert.NotNil(t, appErr) + _, appErr = th.App.GetTeamMember(th.Context, th.BasicTeam.Id, guest.Id) + assert.Nil(t, appErr, "It should remove the team membership") }) } @@ -762,8 +798,8 @@ func TestAddChannelMemberNoUserRequestor(t *testing.T) { // create a user and add it to a channel user := th.CreateUser() - _, err := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, user.Id) - require.Nil(t, err) + _, appErr := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, user.Id) + require.Nil(t, appErr) groupUserIds := make([]string, 0) groupUserIds = append(groupUserIds, th.BasicUser.Id) @@ -771,8 +807,8 @@ func TestAddChannelMemberNoUserRequestor(t *testing.T) { channel := th.createChannel(th.Context, th.BasicTeam, model.ChannelTypeOpen) - _, err = th.App.AddChannelMember(th.Context, user.Id, channel, ChannelMemberOpts{}) - require.Nil(t, err, "Failed to add user to channel.") + _, appErr = th.App.AddChannelMember(th.Context, user.Id, channel, ChannelMemberOpts{}) + require.Nil(t, appErr, "Failed to add user to channel.") // there should be a ChannelMemberHistory record for the user histories, nErr := th.App.Srv().Store().ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id) @@ -802,15 +838,15 @@ func TestAddChannelMemberDeletedUser(t *testing.T) { defer th.TearDown() user := th.CreateUser() - _, err := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, user.Id) - require.Nil(t, err) + _, appErr := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, user.Id) + require.Nil(t, appErr) - deactivated, err := th.App.UpdateActive(th.Context, user, false) + deactivated, appErr := th.App.UpdateActive(th.Context, user, false) require.Greater(t, deactivated.DeleteAt, int64(0)) - require.Nil(t, err) - _, err = th.App.AddChannelMember(th.Context, user.Id, th.BasicChannel, ChannelMemberOpts{}) - require.NotNil(t, err) + require.Nil(t, appErr) + _, appErr = th.App.AddChannelMember(th.Context, user.Id, th.BasicChannel, ChannelMemberOpts{}) + require.NotNil(t, appErr) } func TestAppUpdateChannelScheme(t *testing.T) { @@ -821,8 +857,8 @@ func TestAppUpdateChannelScheme(t *testing.T) { mockID := model.NewPointer("x") channel.SchemeId = mockID - updatedChannel, err := th.App.UpdateChannelScheme(th.Context, channel) - require.Nil(t, err) + updatedChannel, appErr := th.App.UpdateChannelScheme(th.Context, channel) + require.Nil(t, appErr) if updatedChannel.SchemeId != mockID { require.Fail(t, "Wrong Channel SchemeId") @@ -840,42 +876,42 @@ func TestSetChannelsMuted(t *testing.T) { th.AddUserToChannel(th.BasicUser, channel2) // Ensure that both channels start unmuted - member1, err := th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id) - require.Nil(t, err) + member1, appErr := th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.False(t, member1.IsChannelMuted()) - member2, err := th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id) - require.Nil(t, err) + member2, appErr := th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.False(t, member2.IsChannelMuted()) // Mute both channels - updated, err := th.App.setChannelsMuted(th.Context, []string{channel1.Id, channel2.Id}, th.BasicUser.Id, true) - require.Nil(t, err) + updated, appErr := th.App.setChannelsMuted(th.Context, []string{channel1.Id, channel2.Id}, th.BasicUser.Id, true) + require.Nil(t, appErr) assert.True(t, updated[0].IsChannelMuted()) assert.True(t, updated[1].IsChannelMuted()) // Verify that the channels are muted in the database - member1, err = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id) - require.Nil(t, err) + member1, appErr = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.True(t, member1.IsChannelMuted()) - member2, err = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id) - require.Nil(t, err) + member2, appErr = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.True(t, member2.IsChannelMuted()) // Unm both channels - updated, err = th.App.setChannelsMuted(th.Context, []string{channel1.Id, channel2.Id}, th.BasicUser.Id, false) - require.Nil(t, err) + updated, appErr = th.App.setChannelsMuted(th.Context, []string{channel1.Id, channel2.Id}, th.BasicUser.Id, false) + require.Nil(t, appErr) assert.False(t, updated[0].IsChannelMuted()) assert.False(t, updated[1].IsChannelMuted()) // Verify that the channels are muted in the database - member1, err = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id) - require.Nil(t, err) + member1, appErr = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.False(t, member1.IsChannelMuted()) - member2, err = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id) - require.Nil(t, err) + member2, appErr = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.False(t, member2.IsChannelMuted()) }) } @@ -884,17 +920,26 @@ func TestFillInChannelProps(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - channelPublic1, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Public 1", Name: "public1", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) - require.Nil(t, err) - defer th.App.PermanentDeleteChannel(th.Context, channelPublic1) + channelPublic1, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Public 1", Name: "public1", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) + require.Nil(t, appErr) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channelPublic1) + require.Nil(t, appErr) + }() - channelPublic2, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Public 2", Name: "public2", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) - require.Nil(t, err) - defer th.App.PermanentDeleteChannel(th.Context, channelPublic2) + channelPublic2, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Public 2", Name: "public2", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) + require.Nil(t, appErr) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channelPublic2) + require.Nil(t, appErr) + }() - channelPrivate, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Private", Name: "private", Type: model.ChannelTypePrivate, TeamId: th.BasicTeam.Id}, false) - require.Nil(t, err) - defer th.App.PermanentDeleteChannel(th.Context, channelPrivate) + channelPrivate, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Private", Name: "private", Type: model.ChannelTypePrivate, TeamId: th.BasicTeam.Id}, false) + require.Nil(t, appErr) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channelPrivate) + require.Nil(t, appErr) + }() otherTeamId := model.NewId() otherTeam := &model.Team{ @@ -903,13 +948,19 @@ func TestFillInChannelProps(t *testing.T) { Email: "success+" + otherTeamId + "@simulator.amazonses.com", Type: model.TeamOpen, } - otherTeam, err = th.App.CreateTeam(th.Context, otherTeam) - require.Nil(t, err) - defer th.App.PermanentDeleteTeam(th.Context, otherTeam) + otherTeam, appErr = th.App.CreateTeam(th.Context, otherTeam) + require.Nil(t, appErr) + defer func() { + appErr = th.App.PermanentDeleteTeam(th.Context, otherTeam) + require.Nil(t, appErr) + }() - channelOtherTeam, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Other Team Channel", Name: "other-team", Type: model.ChannelTypeOpen, TeamId: otherTeam.Id}, false) - require.Nil(t, err) - defer th.App.PermanentDeleteChannel(th.Context, channelOtherTeam) + channelOtherTeam, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "Other Team Channel", Name: "other-team", Type: model.ChannelTypeOpen, TeamId: otherTeam.Id}, false) + require.Nil(t, appErr) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channelOtherTeam) + require.Nil(t, appErr) + }() // Note that purpose is intentionally plaintext below. @@ -962,8 +1013,8 @@ func TestFillInChannelProps(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.Description, func(t *testing.T) { - err = th.App.FillInChannelProps(th.Context, testCase.Channel) - require.Nil(t, err) + appErr = th.App.FillInChannelProps(th.Context, testCase.Channel) + require.Nil(t, appErr) assert.Equal(t, testCase.ExpectedChannelProps, testCase.Channel.Props) }) @@ -1074,8 +1125,8 @@ func TestFillInChannelProps(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.Description, func(t *testing.T) { - err = th.App.FillInChannelsProps(th.Context, testCase.Channels) - require.Nil(t, err) + appErr = th.App.FillInChannelsProps(th.Context, testCase.Channels) + require.Nil(t, appErr) for _, channel := range testCase.Channels { assert.Equal(t, testCase.ExpectedChannelProps[channel.Name], channel.Props) @@ -1156,31 +1207,41 @@ func TestGetChannelMembersTimezones(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - _, err := th.App.AddChannelMember(th.Context, th.BasicUser2.Id, th.BasicChannel, ChannelMemberOpts{}) - require.Nil(t, err, "Failed to add user to channel.") + _, appErr := th.App.AddChannelMember(th.Context, th.BasicUser2.Id, th.BasicChannel, ChannelMemberOpts{}) + require.Nil(t, appErr, "Failed to add user to channel.") user := th.BasicUser user.Timezone["useAutomaticTimezone"] = "false" user.Timezone["manualTimezone"] = "XOXO/BLABLA" - th.App.UpdateUser(th.Context, user, false) + _, appErr = th.App.UpdateUser(th.Context, user, false) + require.Nil(t, appErr) user2 := th.BasicUser2 user2.Timezone["automaticTimezone"] = "NoWhere/Island" - th.App.UpdateUser(th.Context, user2, false) + _, appErr = th.App.UpdateUser(th.Context, user2, false) + require.Nil(t, appErr) user3 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} - ruser, _ := th.App.CreateUser(th.Context, &user3) - th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + ruser, appErr := th.App.CreateUser(th.Context, &user3) + require.Nil(t, appErr) + + _, _, appErr = th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") + require.Nil(t, appErr) + + _, appErr = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + require.Nil(t, appErr) ruser.Timezone["automaticTimezone"] = "NoWhere/Island" - th.App.UpdateUser(th.Context, ruser, false) + _, appErr = th.App.UpdateUser(th.Context, ruser, false) + require.Nil(t, appErr) user4 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ = th.App.CreateUser(th.Context, &user4) - th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + _, appErr = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + require.NotNil(t, appErr, "user should not be able to join the channel without being in the team.") - timezones, err := th.App.GetChannelMembersTimezones(th.Context, th.BasicChannel.Id) - require.Nil(t, err, "Failed to get the timezones for a channel.") + timezones, appErr := th.App.GetChannelMembersTimezones(th.Context, th.BasicChannel.Id) + require.Nil(t, appErr, "Failed to get the timezones for a channel.") assert.Equal(t, 2, len(timezones)) } @@ -1194,33 +1255,38 @@ func TestGetChannelsForUser(t *testing.T) { CreatorId: th.BasicUser.Id, TeamId: th.BasicTeam.Id, } - th.App.CreateChannel(th.Context, channel, true) - defer th.App.PermanentDeleteChannel(th.Context, channel) + _, appErr := th.App.CreateChannel(th.Context, channel, true) + require.Nil(t, appErr) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, channel) + require.Nil(t, appErr) + }() defer th.TearDown() - channelList, err := th.App.GetChannelsForTeamForUser(th.Context, th.BasicTeam.Id, th.BasicUser.Id, &model.ChannelSearchOpts{ + channelList, appErr := th.App.GetChannelsForTeamForUser(th.Context, th.BasicTeam.Id, th.BasicUser.Id, &model.ChannelSearchOpts{ IncludeDeleted: false, LastDeleteAt: 0, }) - require.Nil(t, err) + require.Nil(t, appErr) require.Len(t, channelList, 4) - th.App.DeleteChannel(th.Context, channel, th.BasicUser.Id) + appErr = th.App.DeleteChannel(th.Context, channel, th.BasicUser.Id) + require.Nil(t, appErr) // Now we get all the non-archived channels for the user - channelList, err = th.App.GetChannelsForTeamForUser(th.Context, th.BasicTeam.Id, th.BasicUser.Id, &model.ChannelSearchOpts{ + channelList, appErr = th.App.GetChannelsForTeamForUser(th.Context, th.BasicTeam.Id, th.BasicUser.Id, &model.ChannelSearchOpts{ IncludeDeleted: false, LastDeleteAt: 0, }) - require.Nil(t, err) + require.Nil(t, appErr) require.Len(t, channelList, 3) // Now we get all the channels, even though are archived, for the user - channelList, err = th.App.GetChannelsForTeamForUser(th.Context, th.BasicTeam.Id, th.BasicUser.Id, &model.ChannelSearchOpts{ + channelList, appErr = th.App.GetChannelsForTeamForUser(th.Context, th.BasicTeam.Id, th.BasicUser.Id, &model.ChannelSearchOpts{ IncludeDeleted: true, LastDeleteAt: 0, }) - require.Nil(t, err) + require.Nil(t, appErr) require.Len(t, channelList, 4) } @@ -1231,13 +1297,13 @@ func TestGetPublicChannelsForTeam(t *testing.T) { var expectedChannels []*model.Channel - townSquare, err := th.App.GetChannelByName(th.Context, "town-square", team.Id, false) - require.Nil(t, err) + townSquare, appErr := th.App.GetChannelByName(th.Context, "town-square", team.Id, false) + require.Nil(t, appErr) require.NotNil(t, townSquare) expectedChannels = append(expectedChannels, townSquare) - offTopic, err := th.App.GetChannelByName(th.Context, "off-topic", team.Id, false) - require.Nil(t, err) + offTopic, appErr := th.App.GetChannelByName(th.Context, "off-topic", team.Id, false) + require.Nil(t, appErr) require.NotNil(t, offTopic) expectedChannels = append(expectedChannels, offTopic) @@ -1249,20 +1315,23 @@ func TestGetPublicChannelsForTeam(t *testing.T) { TeamId: team.Id, } var rchannel *model.Channel - rchannel, err = th.App.CreateChannel(th.Context, &channel, false) - require.Nil(t, err) + rchannel, appErr = th.App.CreateChannel(th.Context, &channel, false) + require.Nil(t, appErr) require.NotNil(t, rchannel) - defer th.App.PermanentDeleteChannel(th.Context, rchannel) + defer func() { + appErr = th.App.PermanentDeleteChannel(th.Context, rchannel) + require.Nil(t, appErr) + }() // Store the user ids for comparison later expectedChannels = append(expectedChannels, rchannel) } // Fetch public channels multiple times - channelList, err := th.App.GetPublicChannelsForTeam(th.Context, team.Id, 0, 5) - require.Nil(t, err) - channelList2, err := th.App.GetPublicChannelsForTeam(th.Context, team.Id, 5, 5) - require.Nil(t, err) + channelList, appErr := th.App.GetPublicChannelsForTeam(th.Context, team.Id, 0, 5) + require.Nil(t, appErr) + channelList2, appErr := th.App.GetPublicChannelsForTeam(th.Context, team.Id, 5, 5) + require.Nil(t, appErr) channels := append(channelList, channelList2...) assert.ElementsMatch(t, expectedChannels, channels) @@ -1282,20 +1351,23 @@ func TestGetPrivateChannelsForTeam(t *testing.T) { TeamId: team.Id, } var rchannel *model.Channel - rchannel, err := th.App.CreateChannel(th.Context, &channel, false) - require.Nil(t, err) + rchannel, appErr := th.App.CreateChannel(th.Context, &channel, false) + require.Nil(t, appErr) require.NotNil(t, rchannel) - defer th.App.PermanentDeleteChannel(th.Context, rchannel) + defer func() { + appErr := th.App.PermanentDeleteChannel(th.Context, rchannel) + require.Nil(t, appErr) + }() // Store the user ids for comparison later expectedChannels = append(expectedChannels, rchannel) } // Fetch private channels multiple times - channelList, err := th.App.GetPrivateChannelsForTeam(th.Context, team.Id, 0, 5) - require.Nil(t, err) - channelList2, err := th.App.GetPrivateChannelsForTeam(th.Context, team.Id, 5, 5) - require.Nil(t, err) + channelList, appErr := th.App.GetPrivateChannelsForTeam(th.Context, team.Id, 0, 5) + require.Nil(t, appErr) + channelList2, appErr := th.App.GetPrivateChannelsForTeam(th.Context, team.Id, 5, 5) + require.Nil(t, appErr) channels := append(channelList, channelList2...) assert.ElementsMatch(t, expectedChannels, channels) @@ -1309,73 +1381,73 @@ func TestUpdateChannelMemberRolesChangingGuest(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := th.App.CreateGuest(th.Context, &user) - _, _, err := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") - require.Nil(t, err) + _, _, appErr := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + require.Nil(t, appErr) - _, err = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_user") - require.NotNil(t, err, "Should fail when try to modify the guest role") + _, appErr = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_user") + require.NotNil(t, appErr, "Should fail when try to modify the guest role") }) t.Run("from user to guest", func(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := th.App.CreateUser(th.Context, &user) - _, _, err := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") - require.Nil(t, err) + _, _, appErr := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + require.Nil(t, appErr) - _, err = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_guest") - require.NotNil(t, err, "Should fail when try to modify the guest role") + _, appErr = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_guest") + require.NotNil(t, appErr, "Should fail when try to modify the guest role") }) t.Run("from user to admin", func(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := th.App.CreateUser(th.Context, &user) - _, _, err := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") - require.Nil(t, err) + _, _, appErr := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + require.Nil(t, appErr) - _, err = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_user channel_admin") - require.Nil(t, err, "Should work when you not modify guest role") + _, appErr = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_user channel_admin") + require.Nil(t, appErr, "Should work when you not modify guest role") }) t.Run("from guest to guest plus custom", func(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := th.App.CreateGuest(th.Context, &user) - _, _, err := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") - require.Nil(t, err) + _, _, appErr := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + require.Nil(t, appErr) - _, err = th.App.CreateRole(&model.Role{Name: "custom", DisplayName: "custom", Description: "custom"}) - require.Nil(t, err) + _, appErr = th.App.CreateRole(&model.Role{Name: "custom", DisplayName: "custom", Description: "custom"}) + require.Nil(t, appErr) - _, err = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_guest custom") - require.Nil(t, err, "Should work when you not modify guest role") + _, appErr = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_guest custom") + require.Nil(t, appErr, "Should work when you not modify guest role") }) t.Run("a guest cant have user role", func(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := th.App.CreateGuest(th.Context, &user) - _, _, err := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") - require.Nil(t, err) + _, _, appErr := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, ruser.Id, "") + require.Nil(t, appErr) - _, err = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, ruser, th.BasicChannel, false) + require.Nil(t, appErr) - _, err = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_guest channel_user") - require.NotNil(t, err, "Should work when you not modify guest role") + _, appErr = th.App.UpdateChannelMemberRoles(th.Context, th.BasicChannel.Id, ruser.Id, "channel_guest channel_user") + require.NotNil(t, appErr, "Should work when you not modify guest role") }) } @@ -1400,26 +1472,30 @@ func TestSearchChannelsForUser(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - c1, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "test-dev-1", Name: "test-dev-1", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) - require.Nil(t, err) + c1, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "test-dev-1", Name: "test-dev-1", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) + require.Nil(t, appErr) - c2, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "test-dev-2", Name: "test-dev-2", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) - require.Nil(t, err) - - c3, err := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "dev-3", Name: "dev-3", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) - require.Nil(t, err) + c2, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "test-dev-2", Name: "test-dev-2", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) + require.Nil(t, appErr) + c3, appErr := th.App.CreateChannel(th.Context, &model.Channel{DisplayName: "dev-3", Name: "dev-3", Type: model.ChannelTypeOpen, TeamId: th.BasicTeam.Id}, false) + require.Nil(t, appErr) defer func() { - th.App.PermanentDeleteChannel(th.Context, c1) - th.App.PermanentDeleteChannel(th.Context, c2) - th.App.PermanentDeleteChannel(th.Context, c3) + appErr = th.App.PermanentDeleteChannel(th.Context, c1) + require.Nil(t, appErr) + + appErr = th.App.PermanentDeleteChannel(th.Context, c2) + require.Nil(t, appErr) + + appErr = th.App.PermanentDeleteChannel(th.Context, c3) + require.Nil(t, appErr) }() // add user to test-dev-1 and dev3 - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser, c1, false) - require.Nil(t, err) - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser, c3, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser, c1, false) + require.Nil(t, appErr) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser, c3, false) + require.Nil(t, appErr) searchAndCheck := func(t *testing.T, term string, expectedDisplayNames []string) { res, searchErr := th.App.SearchChannelsForUser(th.Context, th.BasicUser.Id, th.BasicTeam.Id, term) @@ -1442,8 +1518,8 @@ func TestSearchChannelsForUser(t *testing.T) { }) t.Run("After adding user to test-dev-2, search for dev, the three channels should be returned", func(t *testing.T) { - _, err = th.App.AddUserToChannel(th.Context, th.BasicUser, c2, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, th.BasicUser, c2, false) + require.Nil(t, appErr) searchAndCheck(t, "dev", []string{"test-dev-1", "test-dev-2", "dev-3"}) }) @@ -1469,106 +1545,107 @@ func TestMarkChannelAsUnreadFromPost(t *testing.T) { require.NotNil(t, pp1) pp2 := th.CreatePost(pc1) - unread, err := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) + require.Nil(t, appErr) require.Equal(t, int64(4), unread.MsgCount) - unread, err = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) - require.Nil(t, err) + unread, appErr = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) + require.Nil(t, appErr) require.Equal(t, int64(4), unread.MsgCount) - _, err = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u1.Id, "", false, false) - require.Nil(t, err) - _, err = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u2.Id, "", false, false) - require.Nil(t, err) - unread, err = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) - require.Nil(t, err) + _, appErr = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u1.Id, "", false, false) + require.Nil(t, appErr) + _, appErr = th.App.MarkChannelsAsViewed(th.Context, []string{c1.Id, pc1.Id}, u2.Id, "", false, false) + require.Nil(t, appErr) + unread, appErr = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) + require.Nil(t, appErr) require.Equal(t, int64(0), unread.MsgCount) t.Run("Unread but last one", func(t *testing.T) { - response, err := th.App.MarkChannelAsUnreadFromPost(th.Context, p2.Id, u1.Id, true) - require.Nil(t, err) + response, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, p2.Id, u1.Id, true) + require.Nil(t, appErr) require.NotNil(t, response) assert.Equal(t, int64(2), response.MsgCount) - unread, err := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) + require.Nil(t, appErr) assert.Equal(t, int64(2), unread.MsgCount) assert.Equal(t, p2.CreateAt-1, response.LastViewedAt) }) t.Run("Unread last one", func(t *testing.T) { - response, err := th.App.MarkChannelAsUnreadFromPost(th.Context, p3.Id, u1.Id, true) - require.Nil(t, err) + response, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, p3.Id, u1.Id, true) + require.Nil(t, appErr) require.NotNil(t, response) assert.Equal(t, int64(3), response.MsgCount) - unread, err := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) + require.Nil(t, appErr) assert.Equal(t, int64(1), unread.MsgCount) assert.Equal(t, p3.CreateAt-1, response.LastViewedAt) }) t.Run("Unread first one", func(t *testing.T) { - response, err := th.App.MarkChannelAsUnreadFromPost(th.Context, p1.Id, u1.Id, true) - require.Nil(t, err) + response, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, p1.Id, u1.Id, true) + require.Nil(t, appErr) require.NotNil(t, response) assert.Equal(t, int64(1), response.MsgCount) - unread, err := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) + require.Nil(t, appErr) assert.Equal(t, int64(3), unread.MsgCount) assert.Equal(t, p1.CreateAt-1, response.LastViewedAt) }) t.Run("Other users are unaffected", func(t *testing.T) { - unread, err := th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) + require.Nil(t, appErr) assert.Equal(t, int64(0), unread.MsgCount) }) t.Run("Unread on a private channel", func(t *testing.T) { - response, err := th.App.MarkChannelAsUnreadFromPost(th.Context, pp1.Id, u1.Id, true) - require.Nil(t, err) + response, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, pp1.Id, u1.Id, true) + require.Nil(t, appErr) require.NotNil(t, response) assert.Equal(t, int64(0), response.MsgCount) - unread, err := th.App.GetChannelUnread(th.Context, pc1.Id, u1.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, pc1.Id, u1.Id) + require.Nil(t, appErr) assert.Equal(t, int64(2), unread.MsgCount) assert.Equal(t, pp1.CreateAt-1, response.LastViewedAt) - response, err = th.App.MarkChannelAsUnreadFromPost(th.Context, pp2.Id, u1.Id, true) - assert.Nil(t, err) + response, appErr = th.App.MarkChannelAsUnreadFromPost(th.Context, pp2.Id, u1.Id, true) + assert.Nil(t, appErr) assert.Equal(t, int64(1), response.MsgCount) - unread, err = th.App.GetChannelUnread(th.Context, pc1.Id, u1.Id) - require.Nil(t, err) + unread, appErr = th.App.GetChannelUnread(th.Context, pc1.Id, u1.Id) + require.Nil(t, appErr) assert.Equal(t, int64(1), unread.MsgCount) assert.Equal(t, pp2.CreateAt-1, response.LastViewedAt) }) t.Run("Unread with mentions", func(t *testing.T) { c2 := th.CreateChannel(th.Context, th.BasicTeam) - _, err := th.App.AddUserToChannel(th.Context, u2, c2, false) - require.Nil(t, err) + _, appErr := th.App.AddUserToChannel(th.Context, u2, c2, false) + require.Nil(t, appErr) - p4, err := th.App.CreatePost(th.Context, &model.Post{ + p4, appErr := th.App.CreatePost(th.Context, &model.Post{ UserId: u2.Id, ChannelId: c2.Id, Message: "@" + u1.Username, }, c2, model.CreatePostFlags{SetOnline: true}) - require.Nil(t, err) + require.Nil(t, appErr) th.CreatePost(c2) - th.App.CreatePost(th.Context, &model.Post{ + _, appErr = th.App.CreatePost(th.Context, &model.Post{ UserId: u2.Id, ChannelId: c2.Id, RootId: p4.Id, Message: "@" + u1.Username, }, c2, model.CreatePostFlags{SetOnline: true}) + require.Nil(t, appErr) - response, err := th.App.MarkChannelAsUnreadFromPost(th.Context, p4.Id, u1.Id, true) - assert.Nil(t, err) + response, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, p4.Id, u1.Id, true) + assert.Nil(t, appErr) assert.Equal(t, int64(1), response.MsgCount) assert.Equal(t, int64(2), response.MentionCount) assert.Equal(t, int64(1), response.MentionCountRoot) - unread, err := th.App.GetChannelUnread(th.Context, c2.Id, u1.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, c2.Id, u1.Id) + require.Nil(t, appErr) assert.Equal(t, int64(2), unread.MsgCount) assert.Equal(t, int64(2), unread.MentionCount) assert.Equal(t, int64(1), unread.MentionCountRoot) @@ -1581,17 +1658,17 @@ func TestMarkChannelAsUnreadFromPost(t *testing.T) { th.CreatePost(dc) th.CreatePost(dc) - _, err := th.App.CreatePost(th.Context, &model.Post{ChannelId: dc.Id, UserId: th.BasicUser.Id, Message: "testReply", RootId: dm1.Id}, dc, model.CreatePostFlags{}) - assert.Nil(t, err) + _, appErr := th.App.CreatePost(th.Context, &model.Post{ChannelId: dc.Id, UserId: th.BasicUser.Id, Message: "testReply", RootId: dm1.Id}, dc, model.CreatePostFlags{}) + assert.Nil(t, appErr) - response, err := th.App.MarkChannelAsUnreadFromPost(th.Context, dm1.Id, u2.Id, true) - assert.Nil(t, err) + response, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, dm1.Id, u2.Id, true) + assert.Nil(t, appErr) assert.Equal(t, int64(0), response.MsgCount) assert.Equal(t, int64(4), response.MentionCount) assert.Equal(t, int64(3), response.MentionCountRoot) - unread, err := th.App.GetChannelUnread(th.Context, dc.Id, u2.Id) - require.Nil(t, err) + unread, appErr := th.App.GetChannelUnread(th.Context, dc.Id, u2.Id) + require.Nil(t, appErr) assert.Equal(t, int64(4), unread.MsgCount) assert.Equal(t, int64(4), unread.MentionCount) assert.Equal(t, int64(3), unread.MentionCountRoot) @@ -1610,79 +1687,92 @@ func TestAddUserToChannel(t *testing.T) { user1 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser1, _ := th.App.CreateUser(th.Context, &user1) - defer th.App.PermanentDeleteUser(th.Context, &user1) + defer func() { + appErr := th.App.PermanentDeleteUser(th.Context, &user1) + require.Nil(t, appErr) + }() bot := th.CreateBot() botUser, _ := th.App.GetUser(bot.UserId) - defer th.App.PermanentDeleteBot(th.Context, botUser.Id) + defer func() { + appErr := th.App.PermanentDeleteBot(th.Context, botUser.Id) + require.Nil(t, appErr) + }() - th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser1.Id) - th.App.AddTeamMember(th.Context, th.BasicTeam.Id, bot.UserId) + _, appErr := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser1.Id) + require.Nil(t, appErr) + _, appErr = th.App.AddTeamMember(th.Context, th.BasicTeam.Id, bot.UserId) + require.Nil(t, appErr) group := th.CreateGroup() - _, err := th.App.UpsertGroupMember(group.Id, user1.Id) - require.Nil(t, err) + _, appErr = th.App.UpsertGroupMember(group.Id, user1.Id) + require.Nil(t, appErr) - gs, err := th.App.UpsertGroupSyncable(&model.GroupSyncable{ + gs, appErr := th.App.UpsertGroupSyncable(&model.GroupSyncable{ AutoAdd: true, SyncableId: th.BasicChannel.Id, Type: model.GroupSyncableTypeChannel, GroupId: group.Id, SchemeAdmin: false, }) - require.Nil(t, err) + require.Nil(t, appErr) - err = th.App.JoinChannel(th.Context, th.BasicChannel, ruser1.Id) - require.Nil(t, err) + appErr = th.App.JoinChannel(th.Context, th.BasicChannel, ruser1.Id) + require.Nil(t, appErr) // verify user was added as a non-admin - cm1, err := th.App.GetChannelMember(th.Context, th.BasicChannel.Id, ruser1.Id) - require.Nil(t, err) + cm1, appErr := th.App.GetChannelMember(th.Context, th.BasicChannel.Id, ruser1.Id) + require.Nil(t, appErr) require.False(t, cm1.SchemeAdmin) user2 := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser2, _ := th.App.CreateUser(th.Context, &user2) - defer th.App.PermanentDeleteUser(th.Context, &user2) - th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser2.Id) + defer func() { + appErr = th.App.PermanentDeleteUser(th.Context, &user2) + require.Nil(t, appErr) + }() - _, err = th.App.UpsertGroupMember(group.Id, user2.Id) - require.Nil(t, err) + _, appErr = th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser2.Id) + require.Nil(t, appErr) + + _, appErr = th.App.UpsertGroupMember(group.Id, user2.Id) + require.Nil(t, appErr) gs.SchemeAdmin = true - _, err = th.App.UpdateGroupSyncable(gs) - require.Nil(t, err) + _, appErr = th.App.UpdateGroupSyncable(gs) + require.Nil(t, appErr) - err = th.App.JoinChannel(th.Context, th.BasicChannel, ruser2.Id) - require.Nil(t, err) + appErr = th.App.JoinChannel(th.Context, th.BasicChannel, ruser2.Id) + require.Nil(t, appErr) // Should allow a bot to be added to a public group synced channel - _, err = th.App.AddUserToChannel(th.Context, botUser, th.BasicChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, botUser, th.BasicChannel, false) + require.Nil(t, appErr) // verify user was added as an admin - cm2, err := th.App.GetChannelMember(th.Context, th.BasicChannel.Id, ruser2.Id) - require.Nil(t, err) + cm2, appErr := th.App.GetChannelMember(th.Context, th.BasicChannel.Id, ruser2.Id) + require.Nil(t, appErr) require.True(t, cm2.SchemeAdmin) privateChannel := th.CreatePrivateChannel(th.Context, th.BasicTeam) privateChannel.GroupConstrained = model.NewPointer(true) - _, err = th.App.UpdateChannel(th.Context, privateChannel) - require.Nil(t, err) + _, appErr = th.App.UpdateChannel(th.Context, privateChannel) + require.Nil(t, appErr) - _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ + _, appErr = th.App.UpsertGroupSyncable(&model.GroupSyncable{ GroupId: group.Id, SyncableId: privateChannel.Id, Type: model.GroupSyncableTypeChannel, }) - require.Nil(t, err) + require.Nil(t, appErr) // Should allow a group synced user to be added to a group synced private channel - _, err = th.App.AddUserToChannel(th.Context, ruser1, privateChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, ruser1, privateChannel, false) + require.Nil(t, appErr) // Should allow a bot to be added to a private group synced channel - _, err = th.App.AddUserToChannel(th.Context, botUser, privateChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, botUser, privateChannel, false) + require.Nil(t, appErr) } func TestRemoveUserFromChannel(t *testing.T) { @@ -1691,55 +1781,65 @@ func TestRemoveUserFromChannel(t *testing.T) { user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} ruser, _ := th.App.CreateUser(th.Context, &user) - defer th.App.PermanentDeleteUser(th.Context, ruser) + defer func() { + appErr := th.App.PermanentDeleteUser(th.Context, ruser) + require.Nil(t, appErr) + }() bot := th.CreateBot() botUser, _ := th.App.GetUser(bot.UserId) - defer th.App.PermanentDeleteBot(th.Context, botUser.Id) + defer func() { + appErr := th.App.PermanentDeleteBot(th.Context, botUser.Id) + require.Nil(t, appErr) + }() - th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser.Id) - th.App.AddTeamMember(th.Context, th.BasicTeam.Id, bot.UserId) + _, appErr := th.App.AddTeamMember(th.Context, th.BasicTeam.Id, ruser.Id) + require.Nil(t, appErr) + + _, appErr = th.App.AddTeamMember(th.Context, th.BasicTeam.Id, bot.UserId) + require.Nil(t, appErr) privateChannel := th.CreatePrivateChannel(th.Context, th.BasicTeam) - _, err := th.App.AddUserToChannel(th.Context, ruser, privateChannel, false) - require.Nil(t, err) - _, err = th.App.AddUserToChannel(th.Context, botUser, privateChannel, false) - require.Nil(t, err) + _, appErr = th.App.AddUserToChannel(th.Context, ruser, privateChannel, false) + require.Nil(t, appErr) + _, appErr = th.App.AddUserToChannel(th.Context, botUser, privateChannel, false) + require.Nil(t, appErr) group := th.CreateGroup() - _, err = th.App.UpsertGroupMember(group.Id, ruser.Id) - require.Nil(t, err) + _, appErr = th.App.UpsertGroupMember(group.Id, ruser.Id) + require.Nil(t, appErr) - _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ + _, appErr = th.App.UpsertGroupSyncable(&model.GroupSyncable{ GroupId: group.Id, SyncableId: privateChannel.Id, Type: model.GroupSyncableTypeChannel, }) - require.Nil(t, err) + require.Nil(t, appErr) privateChannel.GroupConstrained = model.NewPointer(true) - _, err = th.App.UpdateChannel(th.Context, privateChannel) - require.Nil(t, err) + _, appErr = th.App.UpdateChannel(th.Context, privateChannel) + require.Nil(t, appErr) // Should not allow a group synced user to be removed from channel - err = th.App.RemoveUserFromChannel(th.Context, ruser.Id, th.SystemAdminUser.Id, privateChannel) - assert.Equal(t, err.Id, "api.channel.remove_members.denied") + appErr = th.App.RemoveUserFromChannel(th.Context, ruser.Id, th.SystemAdminUser.Id, privateChannel) + assert.Equal(t, appErr.Id, "api.channel.remove_members.denied") // Should allow a user to remove themselves from group synced channel - err = th.App.RemoveUserFromChannel(th.Context, ruser.Id, ruser.Id, privateChannel) - require.Nil(t, err) + appErr = th.App.RemoveUserFromChannel(th.Context, ruser.Id, ruser.Id, privateChannel) + require.Nil(t, appErr) // Should allow a bot to be removed from a group synced channel - err = th.App.RemoveUserFromChannel(th.Context, botUser.Id, th.SystemAdminUser.Id, privateChannel) - require.Nil(t, err) + appErr = th.App.RemoveUserFromChannel(th.Context, botUser.Id, th.SystemAdminUser.Id, privateChannel) + require.Nil(t, appErr) } func TestPatchChannelModerationsForChannel(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown() - th.App.SetPhase2PermissionsMigrationStatus(true) + err := th.App.SetPhase2PermissionsMigrationStatus(true) + require.NoError(t, err) channel := th.BasicChannel user := th.BasicUser @@ -2066,21 +2166,29 @@ func TestPatchChannelModerationsForChannel(t *testing.T) { if higherScopedPermissionsOverridden { higherScopedGuestRoleName, higherScopedMemberRoleName, _, _ := th.App.GetTeamSchemeChannelRoles(th.Context, channel.TeamId) if tc.HigherScopedMemberPermissions != nil { - higherScopedMemberRole, err := th.App.GetRoleByName(context.Background(), higherScopedMemberRoleName) - require.Nil(t, err) + higherScopedMemberRole, appErr := th.App.GetRoleByName(context.Background(), higherScopedMemberRoleName) + require.Nil(t, appErr) originalPermissions := higherScopedMemberRole.Permissions - th.App.PatchRole(higherScopedMemberRole, &model.RolePatch{Permissions: &tc.HigherScopedMemberPermissions}) - defer th.App.PatchRole(higherScopedMemberRole, &model.RolePatch{Permissions: &originalPermissions}) + _, appErr = th.App.PatchRole(higherScopedMemberRole, &model.RolePatch{Permissions: &tc.HigherScopedMemberPermissions}) + require.Nil(t, appErr) + defer func() { + _, appErr := th.App.PatchRole(higherScopedMemberRole, &model.RolePatch{Permissions: &originalPermissions}) + require.Nil(t, appErr) + }() } if tc.HigherScopedGuestPermissions != nil { - higherScopedGuestRole, err := th.App.GetRoleByName(context.Background(), higherScopedGuestRoleName) - require.Nil(t, err) + higherScopedGuestRole, appErr := th.App.GetRoleByName(context.Background(), higherScopedGuestRoleName) + require.Nil(t, appErr) originalPermissions := higherScopedGuestRole.Permissions - th.App.PatchRole(higherScopedGuestRole, &model.RolePatch{Permissions: &tc.HigherScopedGuestPermissions}) - defer th.App.PatchRole(higherScopedGuestRole, &model.RolePatch{Permissions: &originalPermissions}) + _, appErr = th.App.PatchRole(higherScopedGuestRole, &model.RolePatch{Permissions: &tc.HigherScopedGuestPermissions}) + require.Nil(t, appErr) + defer func() { + _, appErr := th.App.PatchRole(higherScopedGuestRole, &model.RolePatch{Permissions: &originalPermissions}) + require.Nil(t, appErr) + }() } } @@ -2120,7 +2228,8 @@ func TestPatchChannelModerationsForChannel(t *testing.T) { } if tc.RevertChannelModerationsPatch != nil { - th.App.PatchChannelModerationsForChannel(th.Context, channel, tc.RevertChannelModerationsPatch) + _, appErr := th.App.PatchChannelModerationsForChannel(th.Context, channel, tc.RevertChannelModerationsPatch) + require.Nil(t, appErr) } }) } @@ -2149,15 +2258,19 @@ func TestPatchChannelModerationsForChannel(t *testing.T) { wg.Add(20) for i := 0; i < 10; i++ { go func() { - th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), addCreatePosts) - th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), removeCreatePosts) + _, appErr := th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), addCreatePosts) + require.Nil(t, appErr) + _, err = th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), removeCreatePosts) + require.Nil(t, appErr) wg.Done() }() } for i := 0; i < 10; i++ { go func() { - th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), addCreatePosts) - th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), removeCreatePosts) + _, appErr := th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), addCreatePosts) + require.Nil(t, appErr) + _, appErr = th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), removeCreatePosts) + require.Nil(t, appErr) wg.Done() }() } @@ -2190,12 +2303,12 @@ func TestPatchChannelModerationsForChannel(t *testing.T) { mockSession := model.Session{UserId: user.Id} - _, err := th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), addCreatePosts) - require.Nil(t, err) + _, appErr := th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), addCreatePosts) + require.Nil(t, appErr) require.True(t, th.App.SessionHasPermissionToChannel(th.Context, mockSession, channel.Id, model.PermissionCreatePost)) - _, err = th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), removeCreatePosts) - require.Nil(t, err) + _, appErr = th.App.PatchChannelModerationsForChannel(th.Context, channel.DeepCopy(), removeCreatePosts) + require.Nil(t, appErr) require.False(t, th.App.SessionHasPermissionToChannel(th.Context, mockSession, channel.Id, model.PermissionCreatePost)) }) } @@ -2240,8 +2353,8 @@ func TestGetMemberCountsByGroup(t *testing.T) { mockChannelStore.On("GetMemberCountsByGroup", context.Background(), "channelID", true).Return(cmc, nil) mockStore.On("Channel").Return(&mockChannelStore) mockStore.On("GetDBSchemaVersion").Return(1, nil) - resp, err := th.App.GetMemberCountsByGroup(th.Context, "channelID", true) - require.Nil(t, err) + resp, appErr := th.App.GetMemberCountsByGroup(th.Context, "channelID", true) + require.Nil(t, appErr) require.ElementsMatch(t, cmc, resp) } @@ -2258,8 +2371,8 @@ func TestGetChannelsMemberCount(t *testing.T) { mockChannelStore.On("GetChannelsMemberCount", []string{"channel1", "channel2"}).Return(channelsMemberCount, nil) mockStore.On("Channel").Return(&mockChannelStore) mockStore.On("GetDBSchemaVersion").Return(1, nil) - resp, err := th.App.GetChannelsMemberCount(th.Context, []string{"channel1", "channel2"}) - require.Nil(t, err) + resp, appErr := th.App.GetChannelsMemberCount(th.Context, []string{"channel1", "channel2"}) + require.Nil(t, appErr) require.Equal(t, channelsMemberCount, resp) } @@ -2389,12 +2502,12 @@ func TestMarkChannelAsUnreadFromPostCollapsedThreadsTurnedOff(t *testing.T) { require.Nil(t, appErr) t.Run("Mark reply post as unread", func(t *testing.T) { - _, err := th.App.MarkChannelAsUnreadFromPost(th.Context, replyPost1.Id, th.BasicUser.Id, true) - require.Nil(t, err) + _, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, replyPost1.Id, th.BasicUser.Id, true) + require.Nil(t, appErr) // Get channel unreads // Easier to reason with ChannelUnread now, than channelUnreadAt from the previous call - channelUnread, err := th.App.GetChannelUnread(th.Context, th.BasicChannel.Id, th.BasicUser.Id) - require.Nil(t, err) + channelUnread, appErr := th.App.GetChannelUnread(th.Context, th.BasicChannel.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.Equal(t, int64(3), channelUnread.MentionCount) // MentionCountRoot should be zero for a user that has CRT turned off @@ -2404,21 +2517,21 @@ func TestMarkChannelAsUnreadFromPostCollapsedThreadsTurnedOff(t *testing.T) { // MentionCountRoot should be zero for a user that has CRT turned off require.Equal(t, channelUnread.MsgCountRoot, int64(0)) - threadMembership, err := th.App.GetThreadMembershipForUser(th.BasicUser.Id, rootPost1.Id) - require.Nil(t, err) - thread, err := th.App.GetThreadForUser(threadMembership, false) - require.Nil(t, err) + threadMembership, appErr := th.App.GetThreadMembershipForUser(th.BasicUser.Id, rootPost1.Id) + require.Nil(t, appErr) + thread, appErr := th.App.GetThreadForUser(threadMembership, false) + require.Nil(t, appErr) require.Equal(t, int64(2), thread.UnreadMentions) require.Equal(t, int64(3), thread.UnreadReplies) }) t.Run("Mark root post as unread", func(t *testing.T) { - _, err := th.App.MarkChannelAsUnreadFromPost(th.Context, rootPost1.Id, th.BasicUser.Id, true) - require.Nil(t, err) + _, appErr := th.App.MarkChannelAsUnreadFromPost(th.Context, rootPost1.Id, th.BasicUser.Id, true) + require.Nil(t, appErr) // Get channel unreads // Easier to reason with ChannelUnread now, than channelUnreadAt from the previous call - channelUnread, err := th.App.GetChannelUnread(th.Context, th.BasicChannel.Id, th.BasicUser.Id) - require.Nil(t, err) + channelUnread, appErr := th.App.GetChannelUnread(th.Context, th.BasicChannel.Id, th.BasicUser.Id) + require.Nil(t, appErr) require.Equal(t, int64(4), channelUnread.MentionCount) require.Equal(t, int64(2), channelUnread.MentionCountRoot) @@ -2438,7 +2551,10 @@ func TestMarkUnreadCRTOffUpdatesThreads(t *testing.T) { t.Run("Mentions counted correctly if post is edited", func(t *testing.T) { user3 := th.CreateUser() - defer th.App.PermanentDeleteUser(th.Context, user3) + defer func() { + appErr := th.App.PermanentDeleteUser(th.Context, user3) + require.Nil(t, appErr) + }() rootPost, appErr := th.App.CreatePost(th.Context, &model.Post{UserId: th.BasicUser.Id, CreateAt: model.GetMillis(), ChannelId: th.BasicChannel.Id, Message: "root post"}, th.BasicChannel, model.CreatePostFlags{}) require.Nil(t, appErr) r1, appErr := th.App.CreatePost(th.Context, &model.Post{RootId: rootPost.Id, UserId: th.BasicUser2.Id, CreateAt: model.GetMillis(), ChannelId: th.BasicChannel.Id, Message: "reply 1"}, th.BasicChannel, model.CreatePostFlags{})