From f0351f23f1c6cee17044c6bd2688aa791b1e9e93 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Wed, 14 Nov 2018 18:01:44 +0000 Subject: [PATCH] MM-12818: Delete FileInfos when deleting the post they belong to. (#9828) --- app/post.go | 2 +- app/post_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/post.go b/app/post.go index 019cf207f5..8f391325ce 100644 --- a/app/post.go +++ b/app/post.go @@ -594,7 +594,7 @@ func (a *App) DeleteFlaggedPosts(postId string) { } func (a *App) DeletePostFiles(post *model.Post) { - if len(post.FileIds) != 0 { + if len(post.FileIds) == 0 { return } diff --git a/app/post_test.go b/app/post_test.go index 3b5bc910cd..ed5cb76f2c 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -657,3 +657,48 @@ func TestMaxPostSize(t *testing.T) { }) } } + +func TestDeletePostWithFileAttachments(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + // Create a post with a file attachment. + teamId := th.BasicTeam.Id + channelId := th.BasicChannel.Id + userId := th.BasicUser.Id + filename := "test" + data := []byte("abcd") + + info1, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data) + if err != nil { + t.Fatal(err) + } else { + defer func() { + <-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id) + th.App.RemoveFile(info1.Path) + }() + } + + post := &model.Post{ + Message: "asd", + ChannelId: channelId, + PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), + UserId: userId, + CreateAt: 0, + FileIds: []string{info1.Id}, + } + + post, err = th.App.CreatePost(post, th.BasicChannel, false) + assert.Nil(t, err) + + // Delete the post. + post, err = th.App.DeletePost(post.Id, userId) + assert.Nil(t, err) + + // Wait for the cleanup routine to finish. + time.Sleep(time.Millisecond * 100) + + // Check that the file can no longer be reached. + _, err = th.App.GetFileInfo(info1.Id) + assert.NotNil(t, err) +}