diff --git a/server/.golangci.yml b/server/.golangci.yml index e4c610dde7..315362a967 100644 --- a/server/.golangci.yml +++ b/server/.golangci.yml @@ -95,7 +95,6 @@ issues: channels/app/permissions_test.go|\ channels/app/platform/helper_test.go|\ channels/app/platform/license.go|\ - channels/app/slashcommands/helper_test.go|\ channels/store/localcachelayer/channel_layer.go|\ channels/store/localcachelayer/channel_layer_test.go|\ channels/store/localcachelayer/emoji_layer.go|\ diff --git a/server/channels/app/slashcommands/command_channel_header_test.go b/server/channels/app/slashcommands/command_channel_header_test.go index 43c8c1ca2d..fdeccc0d84 100644 --- a/server/channels/app/slashcommands/command_channel_header_test.go +++ b/server/channels/app/slashcommands/command_channel_header_test.go @@ -12,12 +12,11 @@ import ( ) func TestHeaderProviderDoCommand(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) hp := HeaderProvider{} - th.addPermissionToRole(model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) + th.addPermissionToRole(t, model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) // Try a public channel *with* permission. args := &model.CommandArgs{ @@ -34,7 +33,7 @@ func TestHeaderProviderDoCommand(t *testing.T) { assert.Equal(t, expected, actual) } - th.removePermissionFromRole(model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) + th.removePermissionFromRole(t, model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) // Try a public channel *without* permission. args = &model.CommandArgs{ @@ -46,10 +45,10 @@ func TestHeaderProviderDoCommand(t *testing.T) { actual := hp.DoCommand(th.App, th.Context, args, "hello").Text assert.Equal(t, "api.command_channel_header.permission.app_error", actual) - th.addPermissionToRole(model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) + th.addPermissionToRole(t, model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) // Try a private channel *with* permission. - privateChannel := th.createPrivateChannel(th.BasicTeam) + privateChannel := th.createPrivateChannel(t, th.BasicTeam) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -60,7 +59,7 @@ func TestHeaderProviderDoCommand(t *testing.T) { actual = hp.DoCommand(th.App, th.Context, args, "hello").Text assert.Equal(t, "", actual) - th.removePermissionFromRole(model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) + th.removePermissionFromRole(t, model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) // Try a private channel *without* permission. args = &model.CommandArgs{ @@ -73,11 +72,11 @@ func TestHeaderProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_header.permission.app_error", actual) // Try a group channel *with* being a member. - user1 := th.createUser() - user2 := th.createUser() - user3 := th.createUser() + user1 := th.createUser(t) + user2 := th.createUser(t) + user3 := th.createUser(t) - groupChannel := th.createGroupChannel(user1, user2) + groupChannel := th.createGroupChannel(t, user1, user2) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -99,7 +98,7 @@ func TestHeaderProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_header.permission.app_error", actual) // Try a direct channel *with* being a member. - directChannel := th.createDmChannel(user1) + directChannel := th.createDmChannel(t, user1) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, diff --git a/server/channels/app/slashcommands/command_channel_purpose_test.go b/server/channels/app/slashcommands/command_channel_purpose_test.go index de428b2f9d..6f6fca400b 100644 --- a/server/channels/app/slashcommands/command_channel_purpose_test.go +++ b/server/channels/app/slashcommands/command_channel_purpose_test.go @@ -12,13 +12,12 @@ import ( ) func TestPurposeProviderDoCommand(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) pp := PurposeProvider{} // Try a public channel *with* permission. - th.addPermissionToRole(model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) + th.addPermissionToRole(t, model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) args := &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -35,7 +34,7 @@ func TestPurposeProviderDoCommand(t *testing.T) { } // Try a public channel *without* permission. - th.removePermissionFromRole(model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) + th.removePermissionFromRole(t, model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -46,9 +45,9 @@ func TestPurposeProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_purpose.permission.app_error", actual) // Try a private channel *with* permission. - privateChannel := th.createPrivateChannel(th.BasicTeam) + privateChannel := th.createPrivateChannel(t, th.BasicTeam) - th.addPermissionToRole(model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) + th.addPermissionToRole(t, model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -60,7 +59,7 @@ func TestPurposeProviderDoCommand(t *testing.T) { assert.Equal(t, "", actual) // Try a private channel *without* permission. - th.removePermissionFromRole(model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) + th.removePermissionFromRole(t, model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -71,10 +70,10 @@ func TestPurposeProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_purpose.permission.app_error", actual) // Try a group channel *with* being a member. - user1 := th.createUser() - user2 := th.createUser() + user1 := th.createUser(t) + user2 := th.createUser(t) - groupChannel := th.createGroupChannel(user1, user2) + groupChannel := th.createGroupChannel(t, user1, user2) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -85,7 +84,7 @@ func TestPurposeProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_purpose.direct_group.app_error", actual) // Try a direct channel *with* being a member. - directChannel := th.createDmChannel(user1) + directChannel := th.createDmChannel(t, user1) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, diff --git a/server/channels/app/slashcommands/command_channel_rename_test.go b/server/channels/app/slashcommands/command_channel_rename_test.go index 901015ca86..14f5d35415 100644 --- a/server/channels/app/slashcommands/command_channel_rename_test.go +++ b/server/channels/app/slashcommands/command_channel_rename_test.go @@ -13,10 +13,9 @@ import ( ) func TestRenameProviderDoCommand(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) - th.addPermissionToRole(model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) + th.addPermissionToRole(t, model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) rp := RenameProvider{} args := &model.CommandArgs{ @@ -38,7 +37,7 @@ func TestRenameProviderDoCommand(t *testing.T) { } // Try a public channel *without* permission. - th.removePermissionFromRole(model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) + th.removePermissionFromRole(t, model.PermissionManagePublicChannelProperties.Id, model.ChannelUserRoleId) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -50,9 +49,9 @@ func TestRenameProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_rename.permission.app_error", actual) // Try a private channel *with* permission. - privateChannel := th.createPrivateChannel(th.BasicTeam) + privateChannel := th.createPrivateChannel(t, th.BasicTeam) - th.addPermissionToRole(model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) + th.addPermissionToRole(t, model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -64,7 +63,7 @@ func TestRenameProviderDoCommand(t *testing.T) { assert.Equal(t, "", actual) // Try a private channel *without* permission. - th.removePermissionFromRole(model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) + th.removePermissionFromRole(t, model.PermissionManagePrivateChannelProperties.Id, model.ChannelUserRoleId) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -76,10 +75,10 @@ func TestRenameProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_rename.permission.app_error", actual) // Try a group channel *with* being a member. - user1 := th.createUser() - user2 := th.createUser() + user1 := th.createUser(t) + user2 := th.createUser(t) - groupChannel := th.createGroupChannel(user1, user2) + groupChannel := th.createGroupChannel(t, user1, user2) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -91,7 +90,7 @@ func TestRenameProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_channel_rename.direct_group.app_error", actual) // Try a direct channel *with* being a member. - directChannel := th.createDmChannel(user1) + directChannel := th.createDmChannel(t, user1) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, diff --git a/server/channels/app/slashcommands/command_groupmsg_test.go b/server/channels/app/slashcommands/command_groupmsg_test.go index adae67e5c8..cd211d3fa6 100644 --- a/server/channels/app/slashcommands/command_groupmsg_test.go +++ b/server/channels/app/slashcommands/command_groupmsg_test.go @@ -54,17 +54,16 @@ func TestGroupMsgUsernames(t *testing.T) { } func TestGroupMsgProvider(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) - user3 := th.createUser() + user3 := th.createUser(t) targetUsers := "@" + th.BasicUser2.Username + ",@" + user3.Username + " " - team := th.createTeam() - th.linkUserToTeam(th.BasicUser, team) + team := th.createTeam(t) + th.linkUserToTeam(t, th.BasicUser, team) cmd := &groupmsgProvider{} - th.removePermissionFromRole(model.PermissionCreateGroupChannel.Id, model.SystemUserRoleId) + th.removePermissionFromRole(t, model.PermissionCreateGroupChannel.Id, model.SystemUserRoleId) t.Run("Check without permission to create a GM channel.", func(t *testing.T) { resp := cmd.DoCommand(th.App, th.Context, &model.CommandArgs{ @@ -78,11 +77,11 @@ func TestGroupMsgProvider(t *testing.T) { assert.Equal(t, "", resp.GotoLocation) }) - th.addPermissionToRole(model.PermissionCreateGroupChannel.Id, model.SystemUserRoleId) + th.addPermissionToRole(t, model.PermissionCreateGroupChannel.Id, model.SystemUserRoleId) t.Run("Check without permissions to view a user in the list.", func(t *testing.T) { - th.removePermissionFromRole(model.PermissionViewMembers.Id, model.SystemUserRoleId) - defer th.addPermissionToRole(model.PermissionViewMembers.Id, model.SystemUserRoleId) + th.removePermissionFromRole(t, model.PermissionViewMembers.Id, model.SystemUserRoleId) + t.Cleanup(func() { th.addPermissionToRole(t, model.PermissionViewMembers.Id, model.SystemUserRoleId) }) resp := cmd.DoCommand(th.App, th.Context, &model.CommandArgs{ T: i18n.IdentityTfunc(), SiteURL: "http://test.url", diff --git a/server/channels/app/slashcommands/command_invite_people_test.go b/server/channels/app/slashcommands/command_invite_people_test.go index 62fc14d059..7bba88f465 100644 --- a/server/channels/app/slashcommands/command_invite_people_test.go +++ b/server/channels/app/slashcommands/command_invite_people_test.go @@ -12,8 +12,7 @@ import ( ) func TestInvitePeopleProvider(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.EmailSettings.SendEmailNotifications = true @@ -22,7 +21,7 @@ func TestInvitePeopleProvider(t *testing.T) { cmd := InvitePeopleProvider{} - notTeamUser := th.createUser() + notTeamUser := th.createUser(t) // Test without required permissions args := &model.CommandArgs{ diff --git a/server/channels/app/slashcommands/command_invite_test.go b/server/channels/app/slashcommands/command_invite_test.go index 5b4b4d516c..974465555b 100644 --- a/server/channels/app/slashcommands/command_invite_test.go +++ b/server/channels/app/slashcommands/command_invite_test.go @@ -14,8 +14,7 @@ import ( ) func TestInviteProvider(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) inviteProvider := InviteProvider{} args := &model.CommandArgs{ @@ -52,7 +51,7 @@ func TestInviteProvider(t *testing.T) { }) t.Run("add user to another channel not the current", func(t *testing.T) { - channel := th.createChannel(th.BasicTeam, model.ChannelTypeOpen) + channel := th.createChannel(t, th.BasicTeam, model.ChannelTypeOpen) msg := "@" + th.BasicUser2.Username + " ~" + channel.Name + " " runCmd(msg, "api.command_invite.success") @@ -60,7 +59,7 @@ func TestInviteProvider(t *testing.T) { }) t.Run("add a user to a private channel", func(t *testing.T) { - privateChannel := th.createChannel(th.BasicTeam, model.ChannelTypePrivate) + privateChannel := th.createChannel(t, th.BasicTeam, model.ChannelTypePrivate) msg := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name runCmd(msg, "api.command_invite.success") @@ -68,10 +67,10 @@ func TestInviteProvider(t *testing.T) { }) t.Run("add multiple users to multiple channels", func(t *testing.T) { - anotherUser := th.createUser() - th.linkUserToTeam(anotherUser, th.BasicTeam) - channel1 := th.createChannel(th.BasicTeam, model.ChannelTypeOpen) - channel2 := th.createChannel(th.BasicTeam, model.ChannelTypeOpen) + anotherUser := th.createUser(t) + th.linkUserToTeam(t, anotherUser, th.BasicTeam) + channel1 := th.createChannel(t, th.BasicTeam, model.ChannelTypeOpen) + channel2 := th.createChannel(t, th.BasicTeam, model.ChannelTypeOpen) msg := "@" + th.BasicUser2.Username + " @" + anotherUser.Username + " ~" + channel1.Name + " ~" + channel2.Name expected := "api.command_invite.success\napi.command_invite.success" @@ -83,13 +82,13 @@ func TestInviteProvider(t *testing.T) { }) t.Run("adds multiple users even when some are invalid or already members", func(t *testing.T) { - channel := th.createChannel(th.BasicTeam, model.ChannelTypeOpen) - userAlreadyInChannel := th.createUser() - th.linkUserToTeam(userAlreadyInChannel, th.BasicTeam) - th.addUserToChannel(userAlreadyInChannel, channel) - userInTeam := th.createUser() - th.linkUserToTeam(userInTeam, th.BasicTeam) - userNotInTeam := th.createUser() + channel := th.createChannel(t, th.BasicTeam, model.ChannelTypeOpen) + userAlreadyInChannel := th.createUser(t) + th.linkUserToTeam(t, userAlreadyInChannel, th.BasicTeam) + th.addUserToChannel(t, userAlreadyInChannel, channel) + userInTeam := th.createUser(t) + th.linkUserToTeam(t, userInTeam, th.BasicTeam) + userNotInTeam := th.createUser(t) msg := "@invalidUser123 @" + userAlreadyInChannel.Username + " @" + userInTeam.Username + " @" + userNotInTeam.Username + " ~" + channel.Name expected := "api.command_invite.missing_user.app_error\n" @@ -101,9 +100,9 @@ func TestInviteProvider(t *testing.T) { }) t.Run("try to add a user to a direct channel", func(t *testing.T) { - anotherUser := th.createUser() - th.linkUserToTeam(anotherUser, th.BasicTeam) - directChannel := th.createDmChannel(th.BasicUser2) + anotherUser := th.createUser(t) + th.linkUserToTeam(t, anotherUser, th.BasicTeam) + directChannel := th.createDmChannel(t, th.BasicUser2) msg := "@" + anotherUser.Username + " ~" + directChannel.Name runCmd(msg, "api.command_invite.directchannel.app_error") @@ -116,7 +115,7 @@ func TestInviteProvider(t *testing.T) { }) t.Run("try to add a user using channel's display name", func(t *testing.T) { - channel := th.createChannel(th.BasicTeam, model.ChannelTypeOpen) + channel := th.createChannel(t, th.BasicTeam, model.ChannelTypeOpen) msg := "@" + th.BasicUser2.Username + " ~" + channel.DisplayName runCmd(msg, "api.command_invite.channel.error") @@ -134,7 +133,7 @@ func TestInviteProvider(t *testing.T) { }) t.Run("try to add a user which is not part of the team", func(t *testing.T) { - anotherUser := th.createUser() + anotherUser := th.createUser(t) // Do not add user to the team msg := anotherUser.Username @@ -142,7 +141,7 @@ func TestInviteProvider(t *testing.T) { }) t.Run("try to add a user not part of the group to a group channel", func(t *testing.T) { - groupChannel := th.createChannel(th.BasicTeam, model.ChannelTypePrivate) + groupChannel := th.createChannel(t, th.BasicTeam, model.ChannelTypePrivate) _, err := th.App.AddChannelMember(th.Context, th.BasicUser.Id, groupChannel, app.ChannelMemberOpts{}) require.Nil(t, err) groupChannel.GroupConstrained = model.NewPointer(true) @@ -154,9 +153,9 @@ func TestInviteProvider(t *testing.T) { }) t.Run("try to add a user to a private channel with no permission", func(t *testing.T) { - anotherUser := th.createUser() - th.linkUserToTeam(anotherUser, th.BasicTeam) - privateChannel := th.createChannelWithAnotherUser(th.BasicTeam, model.ChannelTypePrivate, th.BasicUser2.Id) + anotherUser := th.createUser(t) + th.linkUserToTeam(t, anotherUser, th.BasicTeam) + privateChannel := th.createChannelWithAnotherUser(t, th.BasicTeam, model.ChannelTypePrivate, th.BasicUser2.Id) msg := "@" + anotherUser.Username + " ~" + privateChannel.Name runCmd(msg, "api.command_invite.private_channel.app_error") @@ -164,8 +163,8 @@ func TestInviteProvider(t *testing.T) { }) t.Run("try to add a deleted user to a public channel", func(t *testing.T) { - channel := th.createChannel(th.BasicTeam, model.ChannelTypeOpen) - deactivatedUser := th.createUser() + channel := th.createChannel(t, th.BasicTeam, model.ChannelTypeOpen) + deactivatedUser := th.createUser(t) _, appErr := th.App.UpdateActive(th.Context, deactivatedUser, false) require.Nil(t, appErr) @@ -210,8 +209,7 @@ func TestInviteProvider(t *testing.T) { } func TestInviteGroup(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) th.BasicTeam.GroupConstrained = model.NewPointer(true) var err *model.AppError @@ -220,11 +218,11 @@ func TestInviteGroup(t *testing.T) { require.Nil(t, err) th.BasicTeam, _ = th.App.UpdateTeam(th.BasicTeam) - privateChannel := th.createChannel(th.BasicTeam, model.ChannelTypePrivate) + privateChannel := th.createChannel(t, th.BasicTeam, model.ChannelTypePrivate) groupChannelUser1 := "@" + th.BasicUser.Username + " ~" + privateChannel.Name groupChannelUser2 := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name - basicUser3 := th.createUser() + basicUser3 := th.createUser(t) groupChannelUser3 := "@" + basicUser3.Username + " ~" + privateChannel.Name InviteP := InviteProvider{} @@ -266,10 +264,9 @@ func TestInviteGroup(t *testing.T) { } func TestUserGroups(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) - privateChannel := th.createChannel(th.BasicTeam, model.ChannelTypePrivate) + privateChannel := th.createChannel(t, th.BasicTeam, model.ChannelTypePrivate) id := model.NewId() teamGroup, err := th.App.CreateGroup(&model.Group{ @@ -288,8 +285,8 @@ func TestUserGroups(t *testing.T) { require.Nil(t, upsertErr) assert.Len(t, groupMembers, 1) - basicUser3 := th.createUser() - basicUser4 := th.createUser() + basicUser3 := th.createUser(t) + basicUser4 := th.createUser(t) id2 := model.NewId() nonTeamGroup, err := th.App.CreateGroup(&model.Group{ DisplayName: "dn_" + id2, diff --git a/server/channels/app/slashcommands/command_join_test.go b/server/channels/app/slashcommands/command_join_test.go index 9abfe68e1d..4bfbc5e07a 100644 --- a/server/channels/app/slashcommands/command_join_test.go +++ b/server/channels/app/slashcommands/command_join_test.go @@ -13,8 +13,7 @@ import ( ) func TestJoinCommandNoChannel(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() @@ -32,8 +31,7 @@ func TestJoinCommandNoChannel(t *testing.T) { } func TestJoinCommandForExistingChannel(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() @@ -60,8 +58,7 @@ func TestJoinCommandForExistingChannel(t *testing.T) { } func TestJoinCommandWithTilde(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() @@ -88,8 +85,7 @@ func TestJoinCommandWithTilde(t *testing.T) { } func TestJoinCommandPermissions(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) channel2, _ := th.App.CreateChannel(th.Context, &model.Channel{ DisplayName: "AA", @@ -101,7 +97,7 @@ func TestJoinCommandPermissions(t *testing.T) { cmd := &JoinProvider{} - user3 := th.createUser() + user3 := th.createUser(t) // Try a public channel *without* permission. args := &model.CommandArgs{ diff --git a/server/channels/app/slashcommands/command_leave_test.go b/server/channels/app/slashcommands/command_leave_test.go index 63e23d3346..e981bba160 100644 --- a/server/channels/app/slashcommands/command_leave_test.go +++ b/server/channels/app/slashcommands/command_leave_test.go @@ -13,8 +13,7 @@ import ( ) func TestLeaveProviderDoCommand(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) lp := LeaveProvider{} @@ -37,7 +36,7 @@ func TestLeaveProviderDoCommand(t *testing.T) { defaultChannel, err := th.App.GetChannelByName(th.Context, model.DefaultChannelName, th.BasicTeam.Id, false) require.Nil(t, err) - guest := th.createGuest() + guest := th.createGuest(t) _, _, appErr := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, th.BasicUser.Id, th.BasicUser.Id) require.Nil(t, appErr) diff --git a/server/channels/app/slashcommands/command_marketplace_test.go b/server/channels/app/slashcommands/command_marketplace_test.go index b6c4c92af5..e1ab92b7c0 100644 --- a/server/channels/app/slashcommands/command_marketplace_test.go +++ b/server/channels/app/slashcommands/command_marketplace_test.go @@ -12,8 +12,7 @@ import ( ) func TestMarketplaceProviderGetCommand(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) mp := MarketplaceProvider{} diff --git a/server/channels/app/slashcommands/command_me_test.go b/server/channels/app/slashcommands/command_me_test.go index 840fcc0123..01141cf333 100644 --- a/server/channels/app/slashcommands/command_me_test.go +++ b/server/channels/app/slashcommands/command_me_test.go @@ -13,7 +13,6 @@ import ( func TestMeProviderDoCommand(t *testing.T) { th := setup(t) - defer th.tearDown() mp := MeProvider{} diff --git a/server/channels/app/slashcommands/command_msg_test.go b/server/channels/app/slashcommands/command_msg_test.go index c8d3808364..e50822e741 100644 --- a/server/channels/app/slashcommands/command_msg_test.go +++ b/server/channels/app/slashcommands/command_msg_test.go @@ -13,14 +13,13 @@ import ( ) func TestMsgProvider(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) - team := th.createTeam() - th.linkUserToTeam(th.BasicUser, team) + team := th.createTeam(t) + th.linkUserToTeam(t, th.BasicUser, team) cmd := &msgProvider{} - th.removePermissionFromRole(model.PermissionCreateDirectChannel.Id, model.SystemUserRoleId) + th.removePermissionFromRole(t, model.PermissionCreateDirectChannel.Id, model.SystemUserRoleId) // Check without permission to create a DM channel. resp := cmd.DoCommand(th.App, th.Context, &model.CommandArgs{ @@ -34,7 +33,7 @@ func TestMsgProvider(t *testing.T) { assert.Equal(t, "api.command_msg.permission.app_error", resp.Text) assert.Equal(t, "", resp.GotoLocation) - th.addPermissionToRole(model.PermissionCreateDirectChannel.Id, model.SystemUserRoleId) + th.addPermissionToRole(t, model.PermissionCreateDirectChannel.Id, model.SystemUserRoleId) // Check with permission to create a DM channel. resp = cmd.DoCommand(th.App, th.Context, &model.CommandArgs{ @@ -59,12 +58,12 @@ func TestMsgProvider(t *testing.T) { assert.Equal(t, "http://test.url/"+team.Name+"/channels/"+channelName, resp.GotoLocation) // Check that a guest user cannot message a user who is not in a channel/team with him - guest := th.createGuest() - user := th.createUser() + guest := th.createGuest(t) + user := th.createUser(t) - th.linkUserToTeam(user, team) - th.linkUserToTeam(guest, th.BasicTeam) - th.addUserToChannel(guest, th.BasicChannel) + th.linkUserToTeam(t, user, team) + th.linkUserToTeam(t, guest, th.BasicTeam) + th.addUserToChannel(t, guest, th.BasicChannel) resp = cmd.DoCommand(th.App, th.Context, &model.CommandArgs{ T: i18n.IdentityTfunc(), @@ -77,8 +76,8 @@ func TestMsgProvider(t *testing.T) { assert.Equal(t, "", resp.GotoLocation) // Check that a guest user can message a user who is in a channel/team with him - th.linkUserToTeam(user, th.BasicTeam) - th.addUserToChannel(user, th.BasicChannel) + th.linkUserToTeam(t, user, th.BasicTeam) + th.addUserToChannel(t, user, th.BasicChannel) resp = cmd.DoCommand(th.App, th.Context, &model.CommandArgs{ T: i18n.IdentityTfunc(), diff --git a/server/channels/app/slashcommands/command_mute_test.go b/server/channels/app/slashcommands/command_mute_test.go index 24c009e041..4e93621980 100644 --- a/server/channels/app/slashcommands/command_mute_test.go +++ b/server/channels/app/slashcommands/command_mute_test.go @@ -14,8 +14,7 @@ import ( ) func TestMuteCommandNoChannel(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() @@ -41,8 +40,7 @@ func TestMuteCommandNoChannel(t *testing.T) { } func TestMuteCommandNoArgs(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) channel1 := th.BasicChannel channel1M, _ := th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id) @@ -71,8 +69,7 @@ func TestMuteCommandNoArgs(t *testing.T) { } func TestMuteCommandSpecificChannel(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() @@ -116,8 +113,7 @@ func TestMuteCommandSpecificChannel(t *testing.T) { } func TestMuteCommandNotMember(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() @@ -144,8 +140,7 @@ func TestMuteCommandNotMember(t *testing.T) { } func TestMuteCommandNotChannel(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() @@ -165,8 +160,7 @@ func TestMuteCommandNotChannel(t *testing.T) { } func TestMuteCommandDMChannel(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) if testing.Short() { t.SkipNow() diff --git a/server/channels/app/slashcommands/command_remove_test.go b/server/channels/app/slashcommands/command_remove_test.go index b0dc77ceba..b9ab7dafc6 100644 --- a/server/channels/app/slashcommands/command_remove_test.go +++ b/server/channels/app/slashcommands/command_remove_test.go @@ -13,8 +13,7 @@ import ( ) func TestRemoveProviderDoCommand(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) rp := RemoveProvider{} @@ -34,7 +33,7 @@ func TestRemoveProviderDoCommand(t *testing.T) { CreatorId: th.BasicUser.Id, }, false) - targetUser := th.createUser() + targetUser := th.createUser(t) _, _, err := th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, targetUser.Id, targetUser.Id) require.Nil(t, err) _, err = th.App.AddUserToChannel(th.Context, targetUser, publicChannel, false) @@ -87,10 +86,10 @@ func TestRemoveProviderDoCommand(t *testing.T) { assert.Equal(t, "", actual) // Try a group channel - user1 := th.createUser() - user2 := th.createUser() + user1 := th.createUser(t) + user2 := th.createUser(t) - groupChannel := th.createGroupChannel(user1, user2) + groupChannel := th.createGroupChannel(t, user1, user2) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -102,7 +101,7 @@ func TestRemoveProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_remove.direct_group.app_error", actual) // Try a direct channel *with* being a member. - directChannel := th.createDmChannel(user1) + directChannel := th.createDmChannel(t, user1) args = &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -114,7 +113,7 @@ func TestRemoveProviderDoCommand(t *testing.T) { assert.Equal(t, "api.command_remove.direct_group.app_error", actual) // Try a public channel with a deactivated user. - deactivatedUser := th.createUser() + deactivatedUser := th.createUser(t) _, _, err = th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, deactivatedUser.Id, deactivatedUser.Id) require.Nil(t, err) _, err = th.App.AddUserToChannel(th.Context, deactivatedUser, publicChannel, false) diff --git a/server/channels/app/slashcommands/command_share_test.go b/server/channels/app/slashcommands/command_share_test.go index c87aee6381..1a92f9a6f4 100644 --- a/server/channels/app/slashcommands/command_share_test.go +++ b/server/channels/app/slashcommands/command_share_test.go @@ -27,10 +27,9 @@ func setupForSharedChannels(tb testing.TB) *TestHelper { func TestShareProviderDoCommand(t *testing.T) { t.Run("share command sends a websocket channel updated event", func(t *testing.T) { - th := setupForSharedChannels(t).initBasic() - defer th.tearDown() + th := setupForSharedChannels(t).initBasic(t) - th.addPermissionToRole(model.PermissionManageSharedChannels.Id, th.BasicUser.Roles) + th.addPermissionToRole(t, model.PermissionManageSharedChannels.Id, th.BasicUser.Roles) mockSyncService := app.NewMockSharedChannelService(th.Server.GetSharedChannelSyncService()) th.Server.SetSharedChannelSyncService(mockSyncService) @@ -39,7 +38,7 @@ func TestShareProviderDoCommand(t *testing.T) { th.Server.Platform().SetCluster(testCluster) commandProvider := ShareProvider{} - channel := th.CreateChannel(th.BasicTeam, WithShared(false)) + channel := th.CreateChannel(t, th.BasicTeam, WithShared(false)) args := &model.CommandArgs{ T: func(s string, args ...any) string { return s }, @@ -60,10 +59,9 @@ func TestShareProviderDoCommand(t *testing.T) { }) t.Run("unshare command sends a websocket channel updated event", func(t *testing.T) { - th := setupForSharedChannels(t).initBasic() - defer th.tearDown() + th := setupForSharedChannels(t).initBasic(t) - th.addPermissionToRole(model.PermissionManageSharedChannels.Id, th.BasicUser.Roles) + th.addPermissionToRole(t, model.PermissionManageSharedChannels.Id, th.BasicUser.Roles) mockSyncService := app.NewMockSharedChannelService(th.Server.GetSharedChannelSyncService()) th.Server.SetSharedChannelSyncService(mockSyncService) @@ -72,7 +70,7 @@ func TestShareProviderDoCommand(t *testing.T) { th.Server.Platform().SetCluster(testCluster) commandProvider := ShareProvider{} - channel := th.CreateChannel(th.BasicTeam, WithShared(true)) + channel := th.CreateChannel(t, th.BasicTeam, WithShared(true)) args := &model.CommandArgs{ T: func(s string, args ...any) string { return s }, ChannelId: channel.Id, @@ -92,10 +90,9 @@ func TestShareProviderDoCommand(t *testing.T) { }) t.Run("invite remote to channel shared with us", func(t *testing.T) { - th := setupForSharedChannels(t).initBasic() - defer th.tearDown() + th := setupForSharedChannels(t).initBasic(t) - th.addPermissionToRole(model.PermissionManageSharedChannels.Id, th.BasicUser.Roles) + th.addPermissionToRole(t, model.PermissionManageSharedChannels.Id, th.BasicUser.Roles) mockSyncService := app.NewMockSharedChannelService(th.Server.GetSharedChannelSyncService()) th.Server.SetSharedChannelSyncService(mockSyncService) @@ -116,7 +113,7 @@ func TestShareProviderDoCommand(t *testing.T) { require.Nil(t, err) commandProvider := ShareProvider{} - channel := th.CreateChannel(th.BasicTeam, WithShared(true)) // will create with generated remoteID + channel := th.CreateChannel(t, th.BasicTeam, WithShared(true)) // will create with generated remoteID args := &model.CommandArgs{ T: func(s string, args ...any) string { return s }, ChannelId: channel.Id, diff --git a/server/channels/app/slashcommands/command_test.go b/server/channels/app/slashcommands/command_test.go index 4c93a103a7..c41f0256f5 100644 --- a/server/channels/app/slashcommands/command_test.go +++ b/server/channels/app/slashcommands/command_test.go @@ -33,16 +33,15 @@ func (r InfiniteReader) Read(p []byte) (n int, err error) { func TestMoveCommand(t *testing.T) { th := setup(t) - defer th.tearDown() - sourceTeam := th.createTeam() - targetTeam := th.createTeam() - defer func() { + sourceTeam := th.createTeam(t) + targetTeam := th.createTeam(t) + t.Cleanup(func() { appErr := th.App.PermanentDeleteTeam(th.Context, sourceTeam) require.Nil(t, appErr) appErr = th.App.PermanentDeleteTeam(th.Context, targetTeam) require.Nil(t, appErr) - }() + }) command := &model.Command{} command.CreatorId = model.NewId() @@ -68,8 +67,7 @@ func TestMoveCommand(t *testing.T) { } func TestCreateCommandPost(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) post := &model.Post{ ChannelId: th.BasicChannel.Id, @@ -88,8 +86,7 @@ func TestCreateCommandPost(t *testing.T) { } func TestExecuteCommand(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) t.Run("valid tests with different whitespace characters", func(t *testing.T) { TestCases := map[string]string{ @@ -136,8 +133,7 @@ func TestExecuteCommand(t *testing.T) { } func TestHandleCommandResponsePost(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) command := &model.Command{} args := &model.CommandArgs{ @@ -177,9 +173,9 @@ func TestHandleCommandResponsePost(t *testing.T) { builtIn = true // Channel id is specified by response, it should override the command args value. - channel := th.CreateChannel(th.BasicTeam) + channel := th.CreateChannel(t, th.BasicTeam) resp.ChannelId = channel.Id - th.addUserToChannel(th.BasicUser, channel) + th.addUserToChannel(t, th.BasicUser, channel) post, err = th.App.HandleCommandResponsePost(th.Context, command, args, resp, builtIn) assert.Nil(t, err) @@ -262,7 +258,7 @@ func TestHandleCommandResponsePost(t *testing.T) { } assert.Equal(t, "true", post.GetProp(model.PostPropsFromWebhook)) - channel = th.createPrivateChannel(th.BasicTeam) + channel = th.createPrivateChannel(t, th.BasicTeam) resp.ChannelId = channel.Id args.UserId = th.BasicUser2.Id _, err = th.App.HandleCommandResponsePost(th.Context, command, args, resp, builtIn) @@ -291,8 +287,7 @@ func TestHandleCommandResponsePost(t *testing.T) { } func TestHandleCommandResponse(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) command := &model.Command{} @@ -350,7 +345,6 @@ func TestHandleCommandResponse(t *testing.T) { func TestDoCommandRequest(t *testing.T) { th := setup(t) - defer th.tearDown() th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.AllowedUntrustedInternalConnections = model.NewPointer("127.0.0.1") @@ -362,7 +356,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, strings.NewReader("Hello, World!")) require.NoError(t, err) })) - defer server.Close() + t.Cleanup(server.Close) _, resp, err := th.App.DoCommandRequest(th.Context, &model.Command{URL: server.URL}, url.Values{}) require.Nil(t, err) @@ -378,7 +372,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, strings.NewReader(`{"text": "Hello, World!"}`)) require.NoError(t, err) })) - defer server.Close() + t.Cleanup(server.Close) _, resp, err := th.App.DoCommandRequest(th.Context, &model.Command{URL: server.URL}, url.Values{}) require.Nil(t, err) @@ -392,7 +386,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, InfiniteReader{}) require.Error(t, err) // InfiniteReader never returns EOF, so this will error })) - defer server.Close() + t.Cleanup(server.Close) // Since we limit the length of the response, no error will be returned and resp.Text will be a finite string @@ -408,7 +402,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, io.MultiReader(strings.NewReader(`{"text": "`), InfiniteReader{}, strings.NewReader(`"}`))) require.Error(t, err) // InfiniteReader never returns EOF, so this will error })) - defer server.Close() + t.Cleanup(server.Close) _, _, err := th.App.DoCommandRequest(th.Context, &model.Command{URL: server.URL}, url.Values{}) require.NotNil(t, err) @@ -422,7 +416,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, InfiniteReader{}) require.Error(t, err) // InfiniteReader never returns EOF, so this will error })) - defer server.Close() + t.Cleanup(server.Close) _, _, err := th.App.DoCommandRequest(th.Context, &model.Command{URL: server.URL}, url.Values{}) require.NotNil(t, err) @@ -436,7 +430,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, strings.NewReader("Hello, World!")) require.NoError(t, err) })) - defer server.Close() + t.Cleanup(server.Close) th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.OutgoingIntegrationRequestsTimeout = model.NewPointer(int64(1)) @@ -455,7 +449,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, strings.NewReader("Hello, World!")) require.NoError(t, err) })) - defer server.Close() + t.Cleanup(server.Close) th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.OutgoingIntegrationRequestsTimeout = model.NewPointer(int64(2)) @@ -482,7 +476,7 @@ func TestDoCommandRequest(t *testing.T) { _, err := io.Copy(w, strings.NewReader(r.Header.Get("Authorization"))) require.NoError(t, err) })) - defer serverCommand.Close() + t.Cleanup(serverCommand.Close) connection := &model.OutgoingOAuthConnection{ Id: model.NewId(), @@ -515,12 +509,11 @@ func TestDoCommandRequest(t *testing.T) { } func TestMentionsToTeamMembers(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) - otherTeam := th.createTeam() - otherUser := th.createUser() - th.linkUserToTeam(otherUser, otherTeam) + otherTeam := th.createTeam(t) + otherUser := th.createUser(t) + th.linkUserToTeam(t, otherUser, otherTeam) fixture := []struct { message string @@ -601,11 +594,10 @@ func TestMentionsToTeamMembers(t *testing.T) { } func TestMentionsToPublicChannels(t *testing.T) { - th := setup(t).initBasic() - defer th.tearDown() + th := setup(t).initBasic(t) - otherPublicChannel := th.CreateChannel(th.BasicTeam) - privateChannel := th.createPrivateChannel(th.BasicTeam) + otherPublicChannel := th.CreateChannel(t, th.BasicTeam) + privateChannel := th.createPrivateChannel(t, th.BasicTeam) fixture := []struct { message string diff --git a/server/channels/app/slashcommands/helper_test.go b/server/channels/app/slashcommands/helper_test.go index 47447563bc..503b839b52 100644 --- a/server/channels/app/slashcommands/helper_test.go +++ b/server/channels/app/slashcommands/helper_test.go @@ -9,10 +9,11 @@ import ( "os" "path/filepath" "strings" - "sync" "testing" "time" + "github.com/stretchr/testify/require" + "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/shared/mlog" "github.com/mattermost/mattermost/server/public/shared/request" @@ -35,15 +36,12 @@ type TestHelper struct { LogBuffer *bytes.Buffer TestLogger *mlog.Logger IncludeCacheLayer bool - - tempWorkspace string + tempWorkspace string } func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer bool, tb testing.TB, configSet func(*model.Config)) *TestHelper { tempWorkspace, err := os.MkdirTemp("", "apptest") - if err != nil { - panic(err) - } + require.NoError(tb, err) memoryStore := config.NewTestMemoryStore() @@ -57,7 +55,8 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo *memoryConfig.PluginSettings.AutomaticPrepackagedPlugins = false *memoryConfig.LogSettings.EnableSentry = false // disable error reporting during tests *memoryConfig.LogSettings.ConsoleLevel = mlog.LvlStdLog.Name - memoryStore.Set(memoryConfig) + _, _, err = memoryStore.Set(memoryConfig) + require.NoError(tb, err) buffer := &bytes.Buffer{} @@ -69,19 +68,18 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo options = append(options, app.StoreOverride(dbStore)) } - testLogger, _ := mlog.NewLogger() - logCfg, _ := config.MloggerConfigFromLoggerConfig(&memoryConfig.LogSettings, nil, config.GetLogFileLocation) - if errCfg := testLogger.ConfigureTargets(logCfg, nil); errCfg != nil { - panic("failed to configure test logger: " + errCfg.Error()) - } + testLogger, err := mlog.NewLogger() + require.NoError(tb, err) + logCfg, err := config.MloggerConfigFromLoggerConfig(&memoryConfig.LogSettings, nil, config.GetLogFileLocation) + require.NoError(tb, err) + errCfg := testLogger.ConfigureTargets(logCfg, nil) + require.NoError(tb, errCfg, "failed to configure test logger") // lock logger config so server init cannot override it during testing. testLogger.LockConfiguration() options = append(options, app.SetLogger(testLogger)) s, err := app.NewServer(options...) - if err != nil { - panic(err) - } + require.NoError(tb, err) th := &TestHelper{ App: app.New(app.ServerConnector(s.Channels())), @@ -93,13 +91,17 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo } if enterprise { - th.App.Srv().Jobs.StopWorkers() - th.App.Srv().Jobs.StopSchedulers() + stopErr := th.App.Srv().Jobs.StopWorkers() + require.NoError(tb, stopErr) + stopErr = th.App.Srv().Jobs.StopSchedulers() + require.NoError(tb, stopErr) th.App.Srv().SetLicense(model.NewTestLicense()) - th.App.Srv().Jobs.StartWorkers() - th.App.Srv().Jobs.StartSchedulers() + startErr := th.App.Srv().Jobs.StartWorkers() + require.NoError(tb, startErr) + startErr = th.App.Srv().Jobs.StartSchedulers() + require.NoError(tb, startErr) } else { th.App.Srv().SetLicense(getLicense(false, memoryConfig)) } @@ -109,9 +111,7 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = "localhost:0" }) serverErr := th.Server.Start() - if serverErr != nil { - panic(serverErr) - } + require.NoError(tb, serverErr) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress }) @@ -134,6 +134,31 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo th.tempWorkspace = tempWorkspace } + tb.Cleanup(func() { + if th.IncludeCacheLayer { + // Clean all the caches + appErr := th.App.Srv().InvalidateAllCaches() + require.Nil(tb, appErr) + } + + done := make(chan bool) + go func() { + th.Server.Shutdown() + close(done) + }() + + select { + case <-done: + case <-time.After(30 * time.Second): + // Use require.FailNow to terminate all tests in this package, otherwise the + // still running App could spuriously fail subsequent tests. + require.FailNow(tb, "failed to shutdown App within 30 seconds") + } + if th.tempWorkspace != "" { + os.RemoveAll(th.tempWorkspace) + } + }) + return th } @@ -169,46 +194,23 @@ func setupConfig(tb testing.TB, updateConfig func(cfg *model.Config)) *TestHelpe return setupTestHelper(dbStore, false, true, tb, updateConfig) } -var initBasicOnce sync.Once -var userCache struct { - SystemAdminUser *model.User - BasicUser *model.User - BasicUser2 *model.User -} +func (th *TestHelper) initBasic(tb testing.TB) *TestHelper { + th.SystemAdminUser = th.createUser(tb) + _, appErr := th.App.UpdateUserRoles(th.Context, th.SystemAdminUser.Id, model.SystemUserRoleId+" "+model.SystemAdminRoleId, false) + require.Nil(tb, appErr) -func (th *TestHelper) initBasic() *TestHelper { - // create users once and cache them because password hashing is slow - initBasicOnce.Do(func() { - th.SystemAdminUser = th.createUser() - th.App.UpdateUserRoles(th.Context, th.SystemAdminUser.Id, model.SystemUserRoleId+" "+model.SystemAdminRoleId, false) - th.SystemAdminUser, _ = th.App.GetUser(th.SystemAdminUser.Id) - userCache.SystemAdminUser = th.SystemAdminUser.DeepCopy() + th.BasicUser = th.createUser(tb) + th.BasicUser2 = th.createUser(tb) + th.BasicTeam = th.createTeam(tb) - th.BasicUser = th.createUser() - th.BasicUser, _ = th.App.GetUser(th.BasicUser.Id) - userCache.BasicUser = th.BasicUser.DeepCopy() - - th.BasicUser2 = th.createUser() - th.BasicUser2, _ = th.App.GetUser(th.BasicUser2.Id) - userCache.BasicUser2 = th.BasicUser2.DeepCopy() - }) - // restore cached users - th.SystemAdminUser = userCache.SystemAdminUser.DeepCopy() - th.BasicUser = userCache.BasicUser.DeepCopy() - th.BasicUser2 = userCache.BasicUser2.DeepCopy() - users := []*model.User{th.SystemAdminUser, th.BasicUser, th.BasicUser2} - mainHelper.GetSQLStore().User().InsertUsers(users) - - th.BasicTeam = th.createTeam() - - th.linkUserToTeam(th.BasicUser, th.BasicTeam) - th.linkUserToTeam(th.BasicUser2, th.BasicTeam) - th.BasicChannel = th.CreateChannel(th.BasicTeam) - th.BasicPost = th.createPost(th.BasicChannel) + th.linkUserToTeam(tb, th.BasicUser, th.BasicTeam) + th.linkUserToTeam(tb, th.BasicUser2, th.BasicTeam) + th.BasicChannel = th.CreateChannel(tb, th.BasicTeam) + th.BasicPost = th.createPost(tb, th.BasicChannel) return th } -func (th *TestHelper) createTeam() *model.Team { +func (th *TestHelper) createTeam(tb testing.TB) *model.Team { id := model.NewId() team := &model.Team{ DisplayName: "dn_" + id, @@ -217,23 +219,21 @@ func (th *TestHelper) createTeam() *model.Team { Type: model.TeamOpen, } - var err *model.AppError - if team, err = th.App.CreateTeam(th.Context, team); err != nil { - panic(err) - } + team, appErr := th.App.CreateTeam(th.Context, team) + require.Nil(tb, appErr) return team } -func (th *TestHelper) createUser() *model.User { - return th.createUserOrGuest(false) +func (th *TestHelper) createUser(tb testing.TB) *model.User { + return th.createUserOrGuest(tb, false) } -func (th *TestHelper) createGuest() *model.User { - return th.createUserOrGuest(true) +func (th *TestHelper) createGuest(tb testing.TB) *model.User { + return th.createUserOrGuest(tb, true) } -func (th *TestHelper) createUserOrGuest(guest bool) *model.User { +func (th *TestHelper) createUserOrGuest(tb testing.TB, guest bool) *model.User { id := model.NewId() user := &model.User{ @@ -244,15 +244,13 @@ func (th *TestHelper) createUserOrGuest(guest bool) *model.User { EmailVerified: true, } - var err *model.AppError + var appErr *model.AppError if guest { - if user, err = th.App.CreateGuest(th.Context, user); err != nil { - panic(err) - } + user, appErr = th.App.CreateGuest(th.Context, user) + require.Nil(tb, appErr) } else { - if user, err = th.App.CreateUser(th.Context, user); err != nil { - panic(err) - } + user, appErr = th.App.CreateUser(th.Context, user) + require.Nil(tb, appErr) } return user } @@ -265,15 +263,15 @@ func WithShared(v bool) ChannelOption { } } -func (th *TestHelper) CreateChannel(team *model.Team, options ...ChannelOption) *model.Channel { - return th.createChannel(team, model.ChannelTypeOpen, options...) +func (th *TestHelper) CreateChannel(tb testing.TB, team *model.Team, options ...ChannelOption) *model.Channel { + return th.createChannel(tb, team, model.ChannelTypeOpen, options...) } -func (th *TestHelper) createPrivateChannel(team *model.Team) *model.Channel { - return th.createChannel(team, model.ChannelTypePrivate) +func (th *TestHelper) createPrivateChannel(tb testing.TB, team *model.Team) *model.Channel { + return th.createChannel(tb, team, model.ChannelTypePrivate) } -func (th *TestHelper) createChannel(team *model.Team, channelType model.ChannelType, options ...ChannelOption) *model.Channel { +func (th *TestHelper) createChannel(tb testing.TB, team *model.Team, channelType model.ChannelType, options ...ChannelOption) *model.Channel { id := model.NewId() channel := &model.Channel{ @@ -288,10 +286,8 @@ func (th *TestHelper) createChannel(team *model.Team, channelType model.ChannelT option(channel) } - var err *model.AppError - if channel, err = th.App.CreateChannel(th.Context, channel, true); err != nil { - panic(err) - } + channel, appErr := th.App.CreateChannel(th.Context, channel, true) + require.Nil(tb, appErr) if channel.IsShared() { id := model.NewId() @@ -305,14 +301,12 @@ func (th *TestHelper) createChannel(team *model.Team, channelType model.ChannelT CreatorId: th.BasicUser.Id, RemoteId: model.NewId(), }) - if err != nil { - panic(err) - } + require.NoError(tb, err) } return channel } -func (th *TestHelper) createChannelWithAnotherUser(team *model.Team, channelType model.ChannelType, userID string) *model.Channel { +func (th *TestHelper) createChannelWithAnotherUser(tb testing.TB, team *model.Team, channelType model.ChannelType, userID string) *model.Channel { id := model.NewId() channel := &model.Channel{ @@ -323,32 +317,24 @@ func (th *TestHelper) createChannelWithAnotherUser(team *model.Team, channelType CreatorId: userID, } - var err *model.AppError - if channel, err = th.App.CreateChannel(th.Context, channel, true); err != nil { - panic(err) - } + channel, appErr := th.App.CreateChannel(th.Context, channel, true) + require.Nil(tb, appErr) return channel } -func (th *TestHelper) createDmChannel(user *model.User) *model.Channel { - var err *model.AppError - var channel *model.Channel - if channel, err = th.App.GetOrCreateDirectChannel(th.Context, th.BasicUser.Id, user.Id); err != nil { - panic(err) - } +func (th *TestHelper) createDmChannel(tb testing.TB, user *model.User) *model.Channel { + channel, appErr := th.App.GetOrCreateDirectChannel(th.Context, th.BasicUser.Id, user.Id) + require.Nil(tb, appErr) return channel } -func (th *TestHelper) createGroupChannel(user1 *model.User, user2 *model.User) *model.Channel { - var err *model.AppError - var channel *model.Channel - if channel, err = th.App.CreateGroupChannel(th.Context, []string{th.BasicUser.Id, user1.Id, user2.Id}, th.BasicUser.Id); err != nil { - panic(err) - } +func (th *TestHelper) createGroupChannel(tb testing.TB, user1 *model.User, user2 *model.User) *model.Channel { + channel, appErr := th.App.CreateGroupChannel(th.Context, []string{th.BasicUser.Id, user1.Id, user2.Id}, th.BasicUser.Id) + require.Nil(tb, appErr) return channel } -func (th *TestHelper) createPost(channel *model.Channel) *model.Post { +func (th *TestHelper) createPost(tb testing.TB, channel *model.Channel) *model.Post { id := model.NewId() post := &model.Post{ @@ -358,60 +344,25 @@ func (th *TestHelper) createPost(channel *model.Channel) *model.Post { CreateAt: model.GetMillis() - 10000, } - var err *model.AppError - if post, err = th.App.CreatePost(th.Context, post, channel, model.CreatePostFlags{SetOnline: true}); err != nil { - panic(err) - } + post, appErr := th.App.CreatePost(th.Context, post, channel, model.CreatePostFlags{SetOnline: true}) + require.Nil(tb, appErr) return post } -func (th *TestHelper) linkUserToTeam(user *model.User, team *model.Team) { - _, err := th.App.JoinUserToTeam(th.Context, team, user, "") - if err != nil { - panic(err) - } +func (th *TestHelper) linkUserToTeam(tb testing.TB, user *model.User, team *model.Team) { + _, appErr := th.App.JoinUserToTeam(th.Context, team, user, "") + require.Nil(tb, appErr) } -func (th *TestHelper) addUserToChannel(user *model.User, channel *model.Channel) *model.ChannelMember { - member, err := th.App.AddUserToChannel(th.Context, user, channel, false) - if err != nil { - panic(err) - } +func (th *TestHelper) addUserToChannel(tb testing.TB, user *model.User, channel *model.Channel) *model.ChannelMember { + member, appErr := th.App.AddUserToChannel(th.Context, user, channel, false) + require.Nil(tb, appErr) return member } -func (th *TestHelper) shutdownApp() { - done := make(chan bool) - go func() { - th.Server.Shutdown() - close(done) - }() - - select { - case <-done: - case <-time.After(30 * time.Second): - // panic instead of fatal to terminate all tests in this package, otherwise the - // still running App could spuriously fail subsequent tests. - panic("failed to shutdown App within 30 seconds") - } -} - -func (th *TestHelper) tearDown() { - if th.IncludeCacheLayer { - // Clean all the caches - th.App.Srv().InvalidateAllCaches() - } - th.shutdownApp() - if th.tempWorkspace != "" { - os.RemoveAll(th.tempWorkspace) - } -} - -func (th *TestHelper) removePermissionFromRole(permission string, roleName string) { - role, err1 := th.App.GetRoleByName(context.Background(), roleName) - if err1 != nil { - panic(err1) - } +func (th *TestHelper) removePermissionFromRole(tb testing.TB, permission string, roleName string) { + role, appErr := th.App.GetRoleByName(context.Background(), roleName) + require.Nil(tb, appErr) var newPermissions []string for _, p := range role.Permissions { @@ -426,17 +377,13 @@ func (th *TestHelper) removePermissionFromRole(permission string, roleName strin role.Permissions = newPermissions - _, err2 := th.App.UpdateRole(role) - if err2 != nil { - panic(err2) - } + _, appErr = th.App.UpdateRole(role) + require.Nil(tb, appErr) } -func (th *TestHelper) addPermissionToRole(permission string, roleName string) { - role, err1 := th.App.GetRoleByName(context.Background(), roleName) - if err1 != nil { - panic(err1) - } +func (th *TestHelper) addPermissionToRole(tb testing.TB, permission string, roleName string) { + role, appErr := th.App.GetRoleByName(context.Background(), roleName) + require.Nil(tb, appErr) for _, existingPermission := range role.Permissions { if existingPermission == permission { @@ -446,8 +393,6 @@ func (th *TestHelper) addPermissionToRole(permission string, roleName string) { role.Permissions = append(role.Permissions, permission) - _, err2 := th.App.UpdateRole(role) - if err2 != nil { - panic(err2) - } + _, appErr = th.App.UpdateRole(role) + require.Nil(tb, appErr) }