From 86a3bd064b9913e8565acfcf5f4fd9c4fae94322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Mon, 11 Mar 2019 09:26:31 +0100 Subject: [PATCH] MM-14481: Do not allow to edit or delete in archived channels (#10422) * MM-14481: Do not allow to edit or delete in archived channels * Fixing govet * Adding new tests --- app/post.go | 30 ++++++++++++++++++++++++++++++ app/post_test.go | 39 +++++++++++++++++++++++++++++++++++++++ i18n/en.json | 12 ++++++++++++ 3 files changed, 81 insertions(+) diff --git a/app/post.go b/app/post.go index ca06a04f70..b2d4594529 100644 --- a/app/post.go +++ b/app/post.go @@ -480,6 +480,16 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model } } + channel, err := a.GetChannel(oldPost.ChannelId) + if err != nil { + return nil, err + } + + if channel.DeleteAt != 0 { + err := model.NewAppError("UpdatePost", "api.post.update_post.can_not_update_post_in_deleted.error", nil, "", http.StatusBadRequest) + return nil, err + } + newPost := &model.Post{} *newPost = *oldPost @@ -559,6 +569,16 @@ func (a *App) PatchPost(postId string, patch *model.PostPatch) (*model.Post, *mo return nil, err } + channel, err := a.GetChannel(post.ChannelId) + if err != nil { + return nil, err + } + + if channel.DeleteAt != 0 { + err = model.NewAppError("PatchPost", "api.post.patch_post.can_not_update_post_in_deleted.error", nil, "", http.StatusBadRequest) + return nil, err + } + post.Patch(patch) updatedPost, err := a.UpdatePost(post, false) @@ -700,6 +720,16 @@ func (a *App) DeletePost(postId, deleteByID string) (*model.Post, *model.AppErro } post := result.Data.(*model.Post) + channel, err := a.GetChannel(post.ChannelId) + if err != nil { + return nil, err + } + + if channel.DeleteAt != 0 { + err := model.NewAppError("DeletePost", "api.post.delete_post.can_not_delete_post_in_deleted.error", nil, "", http.StatusBadRequest) + return nil, err + } + if result := <-a.Srv.Store.Post().Delete(postId, model.GetMillis(), deleteByID); result.Err != nil { return nil, result.Err } diff --git a/app/post_test.go b/app/post_test.go index 022658ee9e..7a54c46346 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -308,6 +308,19 @@ func TestUpdatePostTimeLimit(t *testing.T) { }) } +func TestUpdatePostInArchivedChannel(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + archivedChannel := th.CreateChannel(th.BasicTeam) + post := th.CreatePost(archivedChannel) + th.App.DeleteChannel(archivedChannel, "") + + _, err := th.App.UpdatePost(post, true) + require.NotNil(t, err) + require.Equal(t, "api.post.update_post.can_not_update_post_in_deleted.error", err.Id) +} + func TestPostReplyToPostWhereRootPosterLeftChannel(t *testing.T) { // This test ensures that when replying to a root post made by a user who has since left the channel, the reply // post completes successfully. This is a regression test for PLT-6523. @@ -639,6 +652,19 @@ func TestDeletePostWithFileAttachments(t *testing.T) { assert.NotNil(t, err) } +func TestDeletePostInArchivedChannel(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + archivedChannel := th.CreateChannel(th.BasicTeam) + post := th.CreatePost(archivedChannel) + th.App.DeleteChannel(archivedChannel, "") + + _, err := th.App.DeletePost(post.Id, "") + require.NotNil(t, err) + require.Equal(t, "api.post.delete_post.can_not_delete_post_in_deleted.error", err.Id) +} + func TestCreatePost(t *testing.T) { t.Run("call PreparePostForClient before returning", func(t *testing.T) { th := Setup(t).InitBasic() @@ -703,6 +729,19 @@ func TestPatchPost(t *testing.T) { }) } +func TestPatchPostInArchivedChannel(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + archivedChannel := th.CreateChannel(th.BasicTeam) + post := th.CreatePost(archivedChannel) + th.App.DeleteChannel(archivedChannel, "") + + _, err := th.App.PatchPost(post.Id, &model.PostPatch{IsPinned: model.NewBool(true)}) + require.NotNil(t, err) + require.Equal(t, "api.post.patch_post.can_not_update_post_in_deleted.error", err.Id) +} + func TestUpdatePost(t *testing.T) { t.Run("call PreparePostForClient before returning", func(t *testing.T) { th := Setup(t).InitBasic() diff --git a/i18n/en.json b/i18n/en.json index ad90a80fc8..4ec32f3caa 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1452,6 +1452,18 @@ "id": "api.post.create_post.town_square_read_only", "translation": "This channel is read-only. Only members with permission can post here." }, + { + "id": "api.post.patch_post.can_not_update_post_in_deleted.error", + "translation": "Can not update a post in a deleted channel." + }, + { + "id": "api.post.update_post.can_not_update_post_in_deleted.error", + "translation": "Can not update a post in a deleted channel." + }, + { + "id": "api.post.delete_post.can_not_delete_post_in_deleted.error", + "translation": "Can not delete a post in a deleted channel." + }, { "id": "api.post.save_is_pinned_post.town_square_read_only", "translation": "This channel is read-only. Only members with permission can pin or unpin posts here."