Fixed errcheck issues in server/channels/api4/post_test.go (#29201)

Этот коммит содержится в:
Arya Khochare
2024-11-12 14:37:19 +05:30
коммит произвёл GitHub
родитель 7c3d71c089
Коммит 5eef415a39
2 изменённых файлов: 163 добавлений и 83 удалений

Просмотреть файл

@@ -70,7 +70,6 @@ issues:
channels/api4/config_local.go|\ channels/api4/config_local.go|\
channels/api4/config_test.go|\ channels/api4/config_test.go|\
channels/api4/data_retention.go|\ channels/api4/data_retention.go|\
channels/api4/post_test.go|\
channels/api4/preference_test.go|\ channels/api4/preference_test.go|\
channels/api4/reaction_test.go|\ channels/api4/reaction_test.go|\
channels/api4/role.go|\ channels/api4/role.go|\

Просмотреть файл

@@ -222,7 +222,8 @@ func TestCreatePost(t *testing.T) {
require.Error(t, err) require.Error(t, err)
require.Equal(t, http.StatusBadRequest, r.StatusCode) require.Equal(t, http.StatusBadRequest, r.StatusCode)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.CreatePost(context.Background(), post) _, resp, err = client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -302,7 +303,8 @@ func TestCreatePostForPriority(t *testing.T) {
}) })
t.Run("should return statusNotImplemented when min. pro. license not available", func(t *testing.T) { t.Run("should return statusNotImplemented when min. pro. license not available", func(t *testing.T) {
th.App.Srv().RemoveLicense() appErr := th.App.Srv().RemoveLicense()
require.Nil(t, appErr)
defer th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuProfessional)) defer th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuProfessional))
// for Acknowledment // for Acknowledment
p1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "test", Metadata: &model.PostMetadata{ p1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "test", Metadata: &model.PostMetadata{
@@ -371,7 +373,10 @@ func TestCreatePostForPriority(t *testing.T) {
appErr := th.App.DemoteUserToGuest(th.Context, th.BasicUser) appErr := th.App.DemoteUserToGuest(th.Context, th.BasicUser)
require.Nil(t, appErr) require.Nil(t, appErr)
defer th.App.PromoteGuestToUser(th.Context, th.BasicUser, th.SystemAdminUser.Id) defer func() {
appErr = th.App.PromoteGuestToUser(th.Context, th.BasicUser, th.SystemAdminUser.Id)
require.Nil(t, appErr)
}()
p1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "test", Metadata: &model.PostMetadata{ p1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "test", Metadata: &model.PostMetadata{
Priority: &model.PostPriority{ Priority: &model.PostPriority{
@@ -509,7 +514,8 @@ func TestCreatePostEphemeral(t *testing.T) {
require.Error(t, err) require.Error(t, err)
require.Equal(t, http.StatusBadRequest, r.StatusCode) require.Equal(t, http.StatusBadRequest, r.StatusCode)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.CreatePostEphemeral(context.Background(), ephemeralPost) _, resp, err = client.CreatePostEphemeral(context.Background(), ephemeralPost)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -626,7 +632,8 @@ func testCreatePostWithOutgoingHook(
hookJSON, jsonErr := json.Marshal(outGoingHookResponse) hookJSON, jsonErr := json.Marshal(outGoingHookResponse)
require.NoError(t, jsonErr) require.NoError(t, jsonErr)
w.Write(hookJSON) _, err := w.Write(hookJSON)
require.NoError(t, err)
success <- true success <- true
})) }))
defer ts.Close() defer ts.Close()
@@ -1041,16 +1048,20 @@ func TestCreatePostPublic(t *testing.T) {
ruser, _, err := client.CreateUser(context.Background(), &user) ruser, _, err := client.CreateUser(context.Background(), &user)
require.NoError(t, err) require.NoError(t, err)
client.Login(context.Background(), user.Email, user.Password) _, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
_, resp, err := client.CreatePost(context.Background(), post) _, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId+" "+model.SystemPostAllPublicRoleId, false) _, appErr := th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId+" "+model.SystemPostAllPublicRoleId, false)
th.App.Srv().InvalidateAllCaches() require.Nil(t, appErr)
appErr = th.App.Srv().InvalidateAllCaches()
require.Nil(t, appErr)
client.Login(context.Background(), user.Email, user.Password) _, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
_, _, err = client.CreatePost(context.Background(), post) _, _, err = client.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
@@ -1060,12 +1071,17 @@ func TestCreatePostPublic(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId, false) _, appErr = th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId, false)
th.App.JoinUserToTeam(th.Context, th.BasicTeam, ruser, "") require.Nil(t, appErr)
th.App.UpdateTeamMemberRoles(th.Context, th.BasicTeam.Id, ruser.Id, model.TeamUserRoleId+" "+model.TeamPostAllPublicRoleId) _, appErr = th.App.JoinUserToTeam(th.Context, th.BasicTeam, ruser, "")
th.App.Srv().InvalidateAllCaches() require.Nil(t, appErr)
_, appErr = th.App.UpdateTeamMemberRoles(th.Context, th.BasicTeam.Id, ruser.Id, model.TeamUserRoleId+" "+model.TeamPostAllPublicRoleId)
require.Nil(t, appErr)
appErr = th.App.Srv().InvalidateAllCaches()
require.Nil(t, appErr)
client.Login(context.Background(), user.Email, user.Password) _, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
post.ChannelId = th.BasicPrivateChannel.Id post.ChannelId = th.BasicPrivateChannel.Id
_, resp, err = client.CreatePost(context.Background(), post) _, resp, err = client.CreatePost(context.Background(), post)
@@ -1091,16 +1107,20 @@ func TestCreatePostAll(t *testing.T) {
ruser, _, err := client.CreateUser(context.Background(), &user) ruser, _, err := client.CreateUser(context.Background(), &user)
require.NoError(t, err) require.NoError(t, err)
client.Login(context.Background(), user.Email, user.Password) _, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
_, resp, err := client.CreatePost(context.Background(), post) _, resp, err := client.CreatePost(context.Background(), post)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId+" "+model.SystemPostAllRoleId, false) _, appErr := th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId+" "+model.SystemPostAllRoleId, false)
th.App.Srv().InvalidateAllCaches() require.Nil(t, appErr)
appErr = th.App.Srv().InvalidateAllCaches()
require.Nil(t, appErr)
client.Login(context.Background(), user.Email, user.Password) _, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
_, _, err = client.CreatePost(context.Background(), post) _, _, err = client.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
@@ -1113,12 +1133,17 @@ func TestCreatePostAll(t *testing.T) {
_, _, err = client.CreatePost(context.Background(), post) _, _, err = client.CreatePost(context.Background(), post)
require.NoError(t, err) require.NoError(t, err)
th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId, false) _, appErr = th.App.UpdateUserRoles(th.Context, ruser.Id, model.SystemUserRoleId, false)
th.App.JoinUserToTeam(th.Context, th.BasicTeam, ruser, "") require.Nil(t, appErr)
th.App.UpdateTeamMemberRoles(th.Context, th.BasicTeam.Id, ruser.Id, model.TeamUserRoleId+" "+model.TeamPostAllRoleId) _, appErr = th.App.JoinUserToTeam(th.Context, th.BasicTeam, ruser, "")
th.App.Srv().InvalidateAllCaches() require.Nil(t, appErr)
_, appErr = th.App.UpdateTeamMemberRoles(th.Context, th.BasicTeam.Id, ruser.Id, model.TeamUserRoleId+" "+model.TeamPostAllRoleId)
require.Nil(t, appErr)
appErr = th.App.Srv().InvalidateAllCaches()
require.Nil(t, appErr)
client.Login(context.Background(), user.Email, user.Password) _, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
post.ChannelId = th.BasicPrivateChannel.Id post.ChannelId = th.BasicPrivateChannel.Id
_, _, err = client.CreatePost(context.Background(), post) _, _, err = client.CreatePost(context.Background(), post)
@@ -1145,7 +1170,8 @@ func TestCreatePostSendOutOfChannelMentions(t *testing.T) {
inChannelUser := th.CreateUser() inChannelUser := th.CreateUser()
th.LinkUserToTeam(inChannelUser, th.BasicTeam) th.LinkUserToTeam(inChannelUser, th.BasicTeam)
th.App.AddUserToChannel(th.Context, inChannelUser, th.BasicChannel, false) _, appErr := th.App.AddUserToChannel(th.Context, inChannelUser, th.BasicChannel, false)
require.Nil(t, appErr)
post1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + inChannelUser.Username} post1 := &model.Post{ChannelId: th.BasicChannel.Id, Message: "@" + inChannelUser.Username}
_, resp, err := client.CreatePost(context.Background(), post1) _, resp, err := client.CreatePost(context.Background(), post1)
@@ -1446,7 +1472,8 @@ func TestUpdatePost(t *testing.T) {
}) })
t.Run("logged out", func(t *testing.T) { t.Run("logged out", func(t *testing.T) {
client.Logout(context.Background()) _, err := client.Logout(context.Background())
require.NoError(t, err)
_, resp, err := client.UpdatePost(context.Background(), rpost.Id, rpost) _, resp, err := client.UpdatePost(context.Background(), rpost.Id, rpost)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -1458,7 +1485,8 @@ func TestUpdatePost(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
}) })
t.Run("different user, but team admin", func(t *testing.T) { t.Run("different user, but team admin", func(t *testing.T) {
@@ -1467,7 +1495,8 @@ func TestUpdatePost(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
}) })
t.Run("different user, but system admin", func(t *testing.T) { t.Run("different user, but system admin", func(t *testing.T) {
@@ -1561,7 +1590,8 @@ func TestPatchPost(t *testing.T) {
} }
patch2.Props = &model.StringInterface{"attachments": attachments} patch2.Props = &model.StringInterface{"attachments": attachments}
rpost2, _, err := client.PatchPost(context.Background(), post.Id, patch2) var rpost2 *model.Post
rpost2, _, err = client.PatchPost(context.Background(), post.Id, patch2)
require.NoError(t, err) require.NoError(t, err)
assert.NotEmpty(t, rpost2.GetProp("attachments")) assert.NotEmpty(t, rpost2.GetProp("attachments"))
assert.NotEqual(t, rpost.EditAt, rpost2.EditAt) assert.NotEqual(t, rpost.EditAt, rpost2.EditAt)
@@ -1577,25 +1607,29 @@ func TestPatchPost(t *testing.T) {
*cfg.ServiceSettings.EnableDeveloper = origEnableDeveloper *cfg.ServiceSettings.EnableDeveloper = origEnableDeveloper
}) })
r, err := client.DoAPIPut(context.Background(), "/posts/"+post.Id+"/patch", "garbage") var r *http.Response
r, err = client.DoAPIPut(context.Background(), "/posts/"+post.Id+"/patch", "garbage")
require.EqualError(t, err, "Invalid or missing post in request body., invalid character 'g' looking for beginning of value") require.EqualError(t, err, "Invalid or missing post in request body., invalid character 'g' looking for beginning of value")
require.Equal(t, http.StatusBadRequest, r.StatusCode, "wrong status code") require.Equal(t, http.StatusBadRequest, r.StatusCode, "wrong status code")
var resp *model.Response
patch := &model.PostPatch{} patch := &model.PostPatch{}
_, resp, err := client.PatchPost(context.Background(), "junk", patch) _, resp, err = client.PatchPost(context.Background(), "junk", patch)
require.Error(t, err) require.Error(t, err)
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
}) })
t.Run("unknown post", func(t *testing.T) { t.Run("unknown post", func(t *testing.T) {
var resp *model.Response
patch := &model.PostPatch{} patch := &model.PostPatch{}
_, resp, err := client.PatchPost(context.Background(), GenerateTestID(), patch) _, resp, err = client.PatchPost(context.Background(), GenerateTestID(), patch)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
}) })
t.Run("logged out", func(t *testing.T) { t.Run("logged out", func(t *testing.T) {
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
patch := &model.PostPatch{} patch := &model.PostPatch{}
_, resp, err := client.PatchPost(context.Background(), post.Id, patch) _, resp, err := client.PatchPost(context.Background(), post.Id, patch)
require.Error(t, err) require.Error(t, err)
@@ -1713,7 +1747,8 @@ func TestPinPost(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
resp, err = client.PinPost(context.Background(), post.Id) resp, err = client.PinPost(context.Background(), post.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -1743,7 +1778,8 @@ func TestUnpinPost(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
resp, err = client.UnpinPost(context.Background(), pinnedPost.Id) resp, err = client.UnpinPost(context.Background(), pinnedPost.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -1837,14 +1873,16 @@ func TestGetPostsForChannel(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckNotFoundStatus(t, resp) CheckNotFoundStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.GetPostsForChannel(context.Background(), model.NewId(), 0, 60, "", false, false) _, resp, err = client.GetPostsForChannel(context.Background(), model.NewId(), 0, 60, "", false, false)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
// more tests for next_post_id, prev_post_id, and order // more tests for next_post_id, prev_post_id, and order
// There are 12 posts composed of first 2 system messages and 10 created posts // There are 12 posts composed of first 2 system messages and 10 created posts
client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password) _, _, err = client.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password)
require.NoError(t, err)
th.CreatePost() // post6 th.CreatePost() // post6
post7 := th.CreatePost() post7 := th.CreatePost()
post8 := th.CreatePost() post8 := th.CreatePost()
@@ -1914,7 +1952,8 @@ func TestGetPostsForChannel(t *testing.T) {
th.TestForAllClients(t, func(t *testing.T, c *model.Client4) { th.TestForAllClients(t, func(t *testing.T, c *model.Client4) {
channel := th.CreatePublicChannel() channel := th.CreatePublicChannel()
th.CreatePostWithClient(th.SystemAdminClient, channel) th.CreatePostWithClient(th.SystemAdminClient, channel)
th.SystemAdminClient.DeleteChannel(context.Background(), channel.Id) _, err = th.SystemAdminClient.DeleteChannel(context.Background(), channel.Id)
require.NoError(t, err)
experimentalViewArchivedChannels := *th.App.Config().TeamSettings.ExperimentalViewArchivedChannels experimentalViewArchivedChannels := *th.App.Config().TeamSettings.ExperimentalViewArchivedChannels
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.ExperimentalViewArchivedChannels = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.ExperimentalViewArchivedChannels = true })
@@ -1933,8 +1972,10 @@ func TestGetPostsForChannel(t *testing.T) {
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
}, "Should forbid to retrieve posts if the channel is archived and users are not allowed to view archived messages") }, "Should forbid to retrieve posts if the channel is archived and users are not allowed to view archived messages")
client.DeletePost(context.Background(), post10.Id) _, err = client.DeletePost(context.Background(), post10.Id)
client.DeletePost(context.Background(), post8.Id) require.NoError(t, err)
_, err = client.DeletePost(context.Background(), post8.Id)
require.NoError(t, err)
// include deleted posts for non-admin users. // include deleted posts for non-admin users.
_, resp, err = client.GetPostsForChannel(context.Background(), th.BasicChannel.Id, 0, 100, "", false, true) _, resp, err = client.GetPostsForChannel(context.Background(), th.BasicChannel.Id, 0, 100, "", false, true)
@@ -2037,7 +2078,8 @@ func TestGetFlaggedPostsForUser(t *testing.T) {
post4 := th.CreatePostWithClient(client, channel3) post4 := th.CreatePostWithClient(client, channel3)
preference.Name = post4.Id preference.Name = post4.Id
client.UpdatePreferences(context.Background(), user.Id, model.Preferences{preference}) _, err = client.UpdatePreferences(context.Background(), user.Id, model.Preferences{preference})
require.NoError(t, err)
opl.AddPost(post4) opl.AddPost(post4)
opl.AddOrder(post4.Id) opl.AddOrder(post4.Id)
@@ -2109,7 +2151,8 @@ func TestGetFlaggedPostsForUser(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.GetFlaggedPostsForUserInChannel(context.Background(), user.Id, channel1.Id, 0, 10) _, resp, err = client.GetFlaggedPostsForUserInChannel(context.Background(), user.Id, channel1.Id, 0, 10)
require.Error(t, err) require.Error(t, err)
@@ -2302,8 +2345,10 @@ func TestGetPostsBefore(t *testing.T) {
require.Equal(t, nonExistentPostId, posts.NextPostId, "should return nonExistentPostId as NextPostId") require.Equal(t, nonExistentPostId, posts.NextPostId, "should return nonExistentPostId as NextPostId")
require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId") require.Equal(t, "", posts.PrevPostId, "should return an empty PrevPostId")
client.DeletePost(context.Background(), post9.Id) _, err = client.DeletePost(context.Background(), post9.Id)
client.DeletePost(context.Background(), post8.Id) require.NoError(t, err)
_, err = client.DeletePost(context.Background(), post8.Id)
require.NoError(t, err)
// include deleted posts for non-admin users. // include deleted posts for non-admin users.
_, resp, err = client.GetPostsBefore(context.Background(), th.BasicChannel.Id, post9.Id, 0, 60, "", false, true) _, resp, err = client.GetPostsBefore(context.Background(), th.BasicChannel.Id, post9.Id, 0, 60, "", false, true)
@@ -2448,8 +2493,10 @@ func TestGetPostsAfter(t *testing.T) {
require.Equal(t, "", posts.NextPostId, "should return an empty NextPostId") require.Equal(t, "", posts.NextPostId, "should return an empty NextPostId")
require.Equal(t, nonExistentPostId, posts.PrevPostId, "should return nonExistentPostId as PrevPostId") require.Equal(t, nonExistentPostId, posts.PrevPostId, "should return nonExistentPostId as PrevPostId")
client.DeletePost(context.Background(), post10.Id) _, err = client.DeletePost(context.Background(), post10.Id)
client.DeletePost(context.Background(), post9.Id) require.NoError(t, err)
_, err = client.DeletePost(context.Background(), post9.Id)
require.NoError(t, err)
// include deleted posts for non-admin users. // include deleted posts for non-admin users.
_, resp, err = client.GetPostsAfter(context.Background(), th.BasicChannel.Id, post1.Id, 0, 60, "", false, true) _, resp, err = client.GetPostsAfter(context.Background(), th.BasicChannel.Id, post1.Id, 0, 60, "", false, true)
@@ -2756,7 +2803,8 @@ func TestGetPost(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckNotFoundStatus(t, resp) CheckNotFoundStatus(t, resp)
client.RemoveUserFromChannel(context.Background(), th.BasicChannel.Id, th.BasicUser.Id) _, err = client.RemoveUserFromChannel(context.Background(), th.BasicChannel.Id, th.BasicUser.Id)
require.NoError(t, err)
// Channel is public, should be able to read post // Channel is public, should be able to read post
_, _, err = c.GetPost(context.Background(), th.BasicPost.Id, "") _, _, err = c.GetPost(context.Background(), th.BasicPost.Id, "")
@@ -2768,7 +2816,8 @@ func TestGetPost(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
}) })
client.RemoveUserFromChannel(context.Background(), th.BasicPrivateChannel.Id, th.BasicUser.Id) _, err := client.RemoveUserFromChannel(context.Background(), th.BasicPrivateChannel.Id, th.BasicUser.Id)
require.NoError(t, err)
// Channel is private, should not be able to read post // Channel is private, should not be able to read post
_, resp, err := client.GetPost(context.Background(), privatePost.Id, "") _, resp, err := client.GetPost(context.Background(), privatePost.Id, "")
@@ -2780,7 +2829,8 @@ func TestGetPost(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Delete post // Delete post
th.SystemAdminClient.DeletePost(context.Background(), th.BasicPost.Id) _, err = th.SystemAdminClient.DeletePost(context.Background(), th.BasicPost.Id)
require.NoError(t, err)
// Normal client should get 404 when trying to access deleted post normally // Normal client should get 404 when trying to access deleted post normally
_, resp, err = client.GetPost(context.Background(), th.BasicPost.Id, "") _, resp, err = client.GetPost(context.Background(), th.BasicPost.Id, "")
@@ -2802,7 +2852,8 @@ func TestGetPost(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, th.BasicPost.Id, post.Id) require.Equal(t, th.BasicPost.Id, post.Id)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
// Normal client should get unauthorized, but local client should get 404. // Normal client should get unauthorized, but local client should get 404.
_, resp, err = client.GetPost(context.Background(), model.NewId(), "") _, resp, err = client.GetPost(context.Background(), model.NewId(), "")
@@ -2838,7 +2889,8 @@ func TestDeletePost(t *testing.T) {
}) })
t.Run("Try to delete a post across different user roles", func(t *testing.T) { t.Run("Try to delete a post across different user roles", func(t *testing.T) {
client.Login(context.Background(), th.TeamAdminUser.Email, th.TeamAdminUser.Password) _, _, err := client.Login(context.Background(), th.TeamAdminUser.Email, th.TeamAdminUser.Password)
require.NoError(t, err)
_, cErr := client.DeletePost(context.Background(), th.BasicPost.Id) _, cErr := client.DeletePost(context.Background(), th.BasicPost.Id)
require.NoError(t, cErr) require.NoError(t, cErr)
@@ -2846,14 +2898,17 @@ func TestDeletePost(t *testing.T) {
post2 := th.CreatePost() post2 := th.CreatePost()
user := th.CreateUser() user := th.CreateUser()
client.Logout(context.Background()) _, err = client.Logout(context.Background())
client.Login(context.Background(), user.Email, user.Password) require.NoError(t, err)
_, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
resp, err := client.DeletePost(context.Background(), post.Id) resp, err := client.DeletePost(context.Background(), post.Id)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
resp, err = client.DeletePost(context.Background(), model.NewId()) resp, err = client.DeletePost(context.Background(), model.NewId())
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -2910,7 +2965,8 @@ func TestPermanentDeletePost(t *testing.T) {
}) })
t.Run("Try to permanently delete a post across different user roles", func(t *testing.T) { t.Run("Try to permanently delete a post across different user roles", func(t *testing.T) {
client.Login(context.Background(), th.TeamAdminUser.Email, th.TeamAdminUser.Password) _, _, err := client.Login(context.Background(), th.TeamAdminUser.Email, th.TeamAdminUser.Password)
require.NoError(t, err)
resp, err := client.PermanentDeletePost(context.Background(), th.BasicPost.Id) resp, err := client.PermanentDeletePost(context.Background(), th.BasicPost.Id)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
@@ -2919,14 +2975,17 @@ func TestPermanentDeletePost(t *testing.T) {
post2 := th.CreatePost() post2 := th.CreatePost()
user := th.CreateUser() user := th.CreateUser()
client.Logout(context.Background()) _, err = client.Logout(context.Background())
client.Login(context.Background(), user.Email, user.Password) require.NoError(t, err)
_, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
resp, err = client.PermanentDeletePost(context.Background(), post.Id) resp, err = client.PermanentDeletePost(context.Background(), post.Id)
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
resp, err = client.PermanentDeletePost(context.Background(), post.Id) resp, err = client.PermanentDeletePost(context.Background(), post.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -3126,7 +3185,8 @@ func TestDeletePostEvent(t *testing.T) {
func TestDeletePostMessage(t *testing.T) { func TestDeletePostMessage(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
th.LinkUserToTeam(th.SystemAdminUser, th.BasicTeam) th.LinkUserToTeam(th.SystemAdminUser, th.BasicTeam)
th.App.AddUserToChannel(th.Context, th.SystemAdminUser, th.BasicChannel, false) _, appErr := th.App.AddUserToChannel(th.Context, th.SystemAdminUser, th.BasicChannel, false)
require.Nil(t, appErr)
defer th.TearDown() defer th.TearDown()
@@ -3202,7 +3262,8 @@ func TestGetPostThread(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckNotFoundStatus(t, resp) CheckNotFoundStatus(t, resp)
client.RemoveUserFromChannel(context.Background(), th.BasicChannel.Id, th.BasicUser.Id) _, err = client.RemoveUserFromChannel(context.Background(), th.BasicChannel.Id, th.BasicUser.Id)
require.NoError(t, err)
// Channel is public, should be able to read post // Channel is public, should be able to read post
_, _, err = client.GetPostThread(context.Background(), th.BasicPost.Id, "", false) _, _, err = client.GetPostThread(context.Background(), th.BasicPost.Id, "", false)
@@ -3213,7 +3274,8 @@ func TestGetPostThread(t *testing.T) {
_, _, err = client.GetPostThread(context.Background(), privatePost.Id, "", false) _, _, err = client.GetPostThread(context.Background(), privatePost.Id, "", false)
require.NoError(t, err) require.NoError(t, err)
client.RemoveUserFromChannel(context.Background(), th.BasicPrivateChannel.Id, th.BasicUser.Id) _, err = client.RemoveUserFromChannel(context.Background(), th.BasicPrivateChannel.Id, th.BasicUser.Id)
require.NoError(t, err)
// Channel is private, should not be able to read post // Channel is private, should not be able to read post
_, resp, err = client.GetPostThread(context.Background(), privatePost.Id, "", false) _, resp, err = client.GetPostThread(context.Background(), privatePost.Id, "", false)
@@ -3236,7 +3298,8 @@ func TestGetPostThread(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.GetPostThread(context.Background(), model.NewId(), "", false) _, resp, err = client.GetPostThread(context.Background(), model.NewId(), "", false)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -3275,7 +3338,8 @@ func TestSearchPosts(t *testing.T) {
archivedChannel := th.CreatePublicChannel() archivedChannel := th.CreatePublicChannel()
_ = th.CreateMessagePostWithClient(th.Client, archivedChannel, "#hashtag for post3") _ = th.CreateMessagePostWithClient(th.Client, archivedChannel, "#hashtag for post3")
th.Client.DeleteChannel(context.Background(), archivedChannel.Id) _, err := th.Client.DeleteChannel(context.Background(), archivedChannel.Id)
require.NoError(t, err)
otherTeam := th.CreateTeam() otherTeam := th.CreateTeam()
channelInOtherTeam := th.CreateChannelWithClientAndTeam(th.Client, model.ChannelTypeOpen, otherTeam.Id) channelInOtherTeam := th.CreateChannelWithClientAndTeam(th.Client, model.ChannelTypeOpen, otherTeam.Id)
@@ -3389,7 +3453,8 @@ func TestSearchPosts(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.SearchPosts(context.Background(), th.BasicTeam.Id, "#sgtitlereview", false) _, resp, err = client.SearchPosts(context.Background(), th.BasicTeam.Id, "#sgtitlereview", false)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -3414,7 +3479,8 @@ func TestSearchHashtagPosts(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Len(t, posts.Order, 2, "wrong search results") require.Len(t, posts.Order, 2, "wrong search results")
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err := client.SearchPosts(context.Background(), th.BasicTeam.Id, "#sgtitlereview", false) _, resp, err := client.SearchPosts(context.Background(), th.BasicTeam.Id, "#sgtitlereview", false)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -3482,13 +3548,16 @@ func TestSearchPostsFromUser(t *testing.T) {
th.LoginTeamAdmin() th.LoginTeamAdmin()
user := th.CreateUser() user := th.CreateUser()
th.LinkUserToTeam(user, th.BasicTeam) th.LinkUserToTeam(user, th.BasicTeam)
th.App.AddUserToChannel(th.Context, user, th.BasicChannel, false) _, appErr := th.App.AddUserToChannel(th.Context, user, th.BasicChannel, false)
th.App.AddUserToChannel(th.Context, user, th.BasicChannel2, false) require.Nil(t, appErr)
_, appErr = th.App.AddUserToChannel(th.Context, user, th.BasicChannel2, false)
require.Nil(t, appErr)
message := "sgtitlereview with space" message := "sgtitlereview with space"
_ = th.CreateMessagePost(message) _ = th.CreateMessagePost(message)
client.Logout(context.Background()) _, err := client.Logout(context.Background())
require.NoError(t, err)
th.LoginBasic2() th.LoginBasic2()
message = "sgtitlereview\n with return" message = "sgtitlereview\n with return"
@@ -3513,7 +3582,8 @@ func TestSearchPostsFromUser(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Len(t, posts.Order, 1, "wrong number of posts for search 'from: %v in:", th.BasicUser2.Username, th.BasicChannel.Name) require.Len(t, posts.Order, 1, "wrong number of posts for search 'from: %v in:", th.BasicUser2.Username, th.BasicChannel.Name)
client.Login(context.Background(), user.Email, user.Password) _, _, err = client.Login(context.Background(), user.Email, user.Password)
require.NoError(t, err)
// wait for the join/leave messages to be created for user3 since they're done asynchronously // wait for the join/leave messages to be created for user3 since they're done asynchronously
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
@@ -3638,7 +3708,8 @@ func TestGetFileInfosForPost(t *testing.T) {
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
// Delete post // Delete post
th.SystemAdminClient.DeletePost(context.Background(), post.Id) _, err = th.SystemAdminClient.DeletePost(context.Background(), post.Id)
require.NoError(t, err)
// Normal client should get 404 when trying to access deleted post normally // Normal client should get 404 when trying to access deleted post normally
_, resp, err = client.GetFileInfosForPost(context.Background(), post.Id, "") _, resp, err = client.GetFileInfosForPost(context.Background(), post.Id, "")
@@ -3670,7 +3741,8 @@ func TestGetFileInfosForPost(t *testing.T) {
require.True(t, found, "missing file info") require.True(t, found, "missing file info")
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.GetFileInfosForPost(context.Background(), model.NewId(), "") _, resp, err = client.GetFileInfosForPost(context.Background(), model.NewId(), "")
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -3686,7 +3758,8 @@ func TestSetChannelUnread(t *testing.T) {
u1 := th.BasicUser u1 := th.BasicUser
u2 := th.BasicUser2 u2 := th.BasicUser2
s2, _ := th.App.GetSession(th.Client.AuthToken) s2, _ := th.App.GetSession(th.Client.AuthToken)
th.Client.Login(context.Background(), u1.Email, u1.Password) _, _, err := th.Client.Login(context.Background(), u1.Email, u1.Password)
require.NoError(t, err)
c1 := th.BasicChannel c1 := th.BasicChannel
c1toc2 := &model.ChannelView{ChannelId: th.BasicChannel2.Id, PrevChannelId: c1.Id} c1toc2 := &model.ChannelView{ChannelId: th.BasicChannel2.Id, PrevChannelId: c1.Id}
now := utils.MillisFromTime(time.Now()) now := utils.MillisFromTime(time.Now())
@@ -3700,10 +3773,10 @@ func TestSetChannelUnread(t *testing.T) {
require.NotNil(t, pp2) require.NotNil(t, pp2)
// Ensure that post have been read // Ensure that post have been read
unread, err := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id)
require.Nil(t, err) require.Nil(t, appErr)
require.Equal(t, int64(4), unread.MsgCount) require.Equal(t, int64(4), unread.MsgCount)
unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u2.Id) unread, appErr = th.App.GetChannelUnread(th.Context, c1.Id, u2.Id)
require.Nil(t, appErr) require.Nil(t, appErr)
require.Equal(t, int64(4), unread.MsgCount) require.Equal(t, int64(4), unread.MsgCount)
_, appErr = th.App.ViewChannel(th.Context, c1toc2, u2.Id, s2.Id, false) _, appErr = th.App.ViewChannel(th.Context, c1toc2, u2.Id, s2.Id, false)
@@ -3713,7 +3786,8 @@ func TestSetChannelUnread(t *testing.T) {
require.Equal(t, int64(0), unread.MsgCount) require.Equal(t, int64(0), unread.MsgCount)
t.Run("Unread last one", func(t *testing.T) { t.Run("Unread last one", func(t *testing.T) {
r, err := th.Client.SetPostUnread(context.Background(), u1.Id, p2.Id, true) var r *model.Response
r, err = th.Client.SetPostUnread(context.Background(), u1.Id, p2.Id, true)
require.NoError(t, err) require.NoError(t, err)
CheckOKStatus(t, r) CheckOKStatus(t, r)
unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id) unread, appErr := th.App.GetChannelUnread(th.Context, c1.Id, u1.Id)
@@ -3829,7 +3903,8 @@ func TestSetChannelUnread(t *testing.T) {
// let's create another user to test permissions // let's create another user to test permissions
u3 := th.CreateUser() u3 := th.CreateUser()
c3 := th.CreateClient() c3 := th.CreateClient()
c3.Login(context.Background(), u3.Email, u3.Password) _, _, err = c3.Login(context.Background(), u3.Email, u3.Password)
require.NoError(t, err)
t.Run("Can't unread channels you don't belong to", func(t *testing.T) { t.Run("Can't unread channels you don't belong to", func(t *testing.T) {
r, _ := c3.SetPostUnread(context.Background(), u3.Id, pp1.Id, true) r, _ := c3.SetPostUnread(context.Background(), u3.Id, pp1.Id, true)
@@ -3842,7 +3917,8 @@ func TestSetChannelUnread(t *testing.T) {
}) })
t.Run("Can't unread if user is not logged in", func(t *testing.T) { t.Run("Can't unread if user is not logged in", func(t *testing.T) {
th.Client.Logout(context.Background()) _, err := th.Client.Logout(context.Background())
require.NoError(t, err)
response, err := th.Client.SetPostUnread(context.Background(), u1.Id, p2.Id, true) response, err := th.Client.SetPostUnread(context.Background(), u1.Id, p2.Id, true)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, response) CheckUnauthorizedStatus(t, response)
@@ -4023,7 +4099,8 @@ func TestGetEditHistoryForPost(t *testing.T) {
}) })
t.Run("logged out", func(t *testing.T) { t.Run("logged out", func(t *testing.T) {
client.Logout(context.Background()) _, err := client.Logout(context.Background())
require.NoError(t, err)
_, resp, err := client.GetEditHistoryForPost(context.Background(), rpost.Id) _, resp, err := client.GetEditHistoryForPost(context.Background(), rpost.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -4318,11 +4395,13 @@ func TestPostGetInfo(t *testing.T) {
client := th.Client client := th.Client
sysadminClient := th.SystemAdminClient sysadminClient := th.SystemAdminClient
sysadminClient.AddTeamMember(context.Background(), th.BasicTeam.Id, th.SystemAdminUser.Id) _, _, err := sysadminClient.AddTeamMember(context.Background(), th.BasicTeam.Id, th.SystemAdminUser.Id)
require.NoError(t, err)
openChannel, _, err := client.CreateChannel(context.Background(), &model.Channel{TeamId: th.BasicTeam.Id, Type: model.ChannelTypeOpen, Name: "open-channel", DisplayName: "Open Channel"}) openChannel, _, err := client.CreateChannel(context.Background(), &model.Channel{TeamId: th.BasicTeam.Id, Type: model.ChannelTypeOpen, Name: "open-channel", DisplayName: "Open Channel"})
require.NoError(t, err) require.NoError(t, err)
sysadminClient.AddChannelMember(context.Background(), openChannel.Id, th.SystemAdminUser.Id) _, _, err = sysadminClient.AddChannelMember(context.Background(), openChannel.Id, th.SystemAdminUser.Id)
require.NoError(t, err)
openPost, _, err := client.CreatePost(context.Background(), &model.Post{ChannelId: openChannel.Id}) openPost, _, err := client.CreatePost(context.Background(), &model.Post{ChannelId: openChannel.Id})
require.NoError(t, err) require.NoError(t, err)
@@ -4604,7 +4683,8 @@ func TestAcknowledgePost(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
_, resp, err = client.AcknowledgePost(context.Background(), post.Id, th.BasicUser.Id) _, resp, err = client.AcknowledgePost(context.Background(), post.Id, th.BasicUser.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)
@@ -4651,7 +4731,8 @@ func TestUnacknowledgePost(t *testing.T) {
require.Nil(t, appErr) require.Nil(t, appErr)
require.Len(t, acks, 0) require.Len(t, acks, 0)
client.Logout(context.Background()) _, err = client.Logout(context.Background())
require.NoError(t, err)
resp, err = client.UnacknowledgePost(context.Background(), post.Id, th.BasicUser.Id) resp, err = client.UnacknowledgePost(context.Background(), post.Id, th.BasicUser.Id)
require.Error(t, err) require.Error(t, err)
CheckUnauthorizedStatus(t, resp) CheckUnauthorizedStatus(t, resp)