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
Этот коммит содержится в:
Jesús Espino
2019-03-11 09:26:31 +01:00
коммит произвёл GitHub
родитель 9ef8c1e8b1
Коммит 86a3bd064b
3 изменённых файлов: 81 добавлений и 0 удалений

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

@@ -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
}

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

@@ -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()

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

@@ -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."