Files
mostlymatter/server/channels/app/post_file_change_test.go
Harshil Sharma 6e5a67caec Feature edit attachments (#29769)
* Updated patch/update post API to allow file modification (#29447)

* WIP

* WIP

* Atatched new files ton post

* WIP: deleting removed files

* Deleted removed files and invalidated file metadata cache

* removed file ignore logif from update post API

* Added TestFindExclusives

* Added tests for DeleteForPostByIds

* Added app layer tests

* Added tests

* Added API level tests

* test enhancements

* Fixed a test

* Edit history include file metadata (#29505)

* Send file metadata in edit history metadata

* Added app tests

* Added store tests

* Added tests for populateEditHistoryFileMetadata{

* Added cache to avoid repetitigve DB calls for edits with only message changes

* Added API tests

* i18m fix

* removed commented code

* Improved test helper

* Show attachments in edit history RHS (#29519)

* Send file metadata in edit history metadata

* Added app tests

* Added store tests

* Added tests for populateEditHistoryFileMetadata{

* Added cache to avoid repetitigve DB calls for edits with only message changes

* Added API tests

* i18m fix

* WIUP: displa files in edit

* removed commented code

* Displayed file in edit history

* Handled file icon

* Fixed closing history component on clicking on file

* Simplified selector

* Simplified selector

* Improved test helper

* Disabled action menu on edit history file

* Added tests

* Improved selector

* Updated snapshot

* review Fixes

* restructured componnets

* Updated test

* Updated test

* Restore post api (#29643)

* Restore post version API WIP

* Undelete files WIP

* Added store tests

* Created post restore API

* Updated updatepost safeUpdate signature

* review fixex and improvements

* Fixed an app test

* Added API laer tests

* Added API tests and OpenAPI specs

* Fixed a typo

* Allow editing files when editing posts (#29709)

* WIP - basic view files when editing post

* Cleanup

* bg color

* Added text editor tests for files

* WIP

* WIP

* removed debug log

* Allowed admin to add and remove files on someone else's post

* Handled drafts and scheduled posts

* linter fixes

* Updated snapshot

* server test fix

* CI

* Added doc

* Restore post api integration (#29719)

* WIP - basic view files when editing post

* Cleanup

* bg color

* Added text editor tests for files

* WIP

* WIP

* removed debug log

* Allowed admin to add and remove files on someone else's post

* Handled drafts and scheduled posts

* linter fixes

* Updated snapshot

* server test fix

* Used new API to restore post

* handled edut limit and undo

* lint fix

* added comments

* Fixed edit post item tests

* Fixed buttons

* Aded snapshots

* fix test

* Updated snapshot

* Minor fixes

* fixed snapshot

* Edit file dnd area (#29763)

* dnd wip

* DND continued

* Supported multiple unbind dragster funcs

* lint fixes

* Got center channel file drop working when editing a post

* file dnd working with center channel and rhs

* file dnd working with center channel and rhs

* removed unneeded stopPropogation calls

* cleanup

* DND overlay fix

* Lint fix

* Advanced text editor test updates for file upload overlay

* fixed use upload hook tests

* Updated some more snapshots

* minor cleanup

* Updated i18n

* removed need of array for dragster unbind events

* lint fixes

* edit history cursor

* Fixed bugu causing faliure to delete empty posts (#29778)

* Files in restore confirmation (#29781)

* Added files to restore post confirmation dialog

* Fixed post restore toast colors

* Fixed restore bug

* Fixed restore confirmation toast tests

* a11y improvement and modal width fix

* Edit attachment misc fixes (#29808)

* Removed single image actions in restore post confirmation dialog

* Fixed file drop overlay size and position

* Made edit indiator accessible

* Lint fix

* Added bunch of more tests

* ANother test migrated from enzyme to react testing library

* More test enhancements

* More test enhancements

* More test enhancements

* lint fixes

* Fixed  a test

* Added missing snapshots

* Test fixes
2025-01-13 18:16:56 +05:30

275 строки
8.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/stretchr/testify/require"
)
func TestProcessPostFileChanges(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
t.Run("no files", func(t *testing.T) {
oldPost := &model.Post{FileIds: []string{}}
newPost := &model.Post{FileIds: []string{}}
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 0, len(fileIds))
})
t.Run("have files but nothing changed", func(t *testing.T) {
oldPost := &model.Post{FileIds: []string{"file_id_1", "file_id_2"}}
newPost := &model.Post{FileIds: []string{"file_id_1", "file_id_2"}}
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 2, len(fileIds))
})
t.Run("one file deleted", func(t *testing.T) {
postId := model.NewId()
fileInfo1 := th.CreateFileInfo(th.BasicUser.Id, postId, th.BasicChannel.Id)
fileInfo2 := th.CreateFileInfo(th.BasicUser.Id, postId, th.BasicChannel.Id)
oldPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id, fileInfo2.Id},
}
newPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id},
}
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 1, len(fileIds))
require.Equal(t, fileInfo1.Id, fileIds[0])
// verify file2 was soft deleted
updatedFileInfos, err := th.App.Srv().Store().FileInfo().GetForPost(postId, true, true, false)
require.NoError(t, err)
require.Equal(t, 2, len(updatedFileInfos))
for _, fileInfo := range updatedFileInfos {
if fileInfo.Id == fileInfo1.Id {
require.Equal(t, int64(0), fileInfo.DeleteAt)
} else if fileInfo.Id == fileInfo2.Id {
require.Greater(t, fileInfo.DeleteAt, int64(0))
} else {
require.Fail(t, "unexpected file info")
}
}
})
t.Run("one file added", func(t *testing.T) {
postId := model.NewId()
fileInfo1 := th.CreateFileInfo(th.BasicUser.Id, postId, th.BasicChannel.Id)
fileInfo2 := th.CreateFileInfo(th.BasicUser.Id, "", th.BasicChannel.Id)
oldPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id},
}
newPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id, fileInfo2.Id},
}
th.Context.Session().UserId = th.BasicUser.Id
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 2, len(fileIds))
require.Contains(t, fileIds, fileInfo1.Id)
require.Contains(t, fileIds, fileInfo2.Id)
// verify file2 is attached to the post
updatedFileInfo2, err := th.App.Srv().Store().FileInfo().Get(fileInfo2.Id)
require.NoError(t, err)
require.Equal(t, postId, updatedFileInfo2.PostId)
})
t.Run("all files removed", func(t *testing.T) {
postId := model.NewId()
fileInfo1 := th.CreateFileInfo(th.BasicUser.Id, postId, th.BasicChannel.Id)
fileInfo2 := th.CreateFileInfo(th.BasicUser.Id, postId, th.BasicChannel.Id)
oldPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id, fileInfo2.Id},
}
newPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{},
}
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 0, len(fileIds))
// verify file2 was soft deleted
updatedFileInfos, err := th.App.Srv().Store().FileInfo().GetForPost(postId, true, true, false)
require.NoError(t, err)
require.Equal(t, 2, len(updatedFileInfos))
for _, fileInfo := range updatedFileInfos {
if fileInfo.Id == fileInfo1.Id || fileInfo.Id == fileInfo2.Id {
require.Greater(t, fileInfo.DeleteAt, int64(0))
} else {
require.Fail(t, "unexpected file info")
}
}
})
t.Run("files added when no files existed", func(t *testing.T) {
fileInfo1 := th.CreateFileInfo(th.BasicUser.Id, "", th.BasicChannel.Id)
fileInfo2 := th.CreateFileInfo(th.BasicUser.Id, "", th.BasicChannel.Id)
postId := model.NewId()
oldPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{},
}
newPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id, fileInfo2.Id},
}
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 2, len(fileIds))
require.Contains(t, fileIds, fileInfo1.Id)
require.Contains(t, fileIds, fileInfo2.Id)
updatedFileInfo1, err := th.App.Srv().Store().FileInfo().Get(fileInfo2.Id)
require.NoError(t, err)
require.Equal(t, postId, updatedFileInfo1.PostId)
updatedFileInfo2, err := th.App.Srv().Store().FileInfo().Get(fileInfo2.Id)
require.NoError(t, err)
require.Equal(t, postId, updatedFileInfo2.PostId)
})
t.Run("other post's attached file added", func(t *testing.T) {
postId := model.NewId()
fileInfo1 := th.CreateFileInfo(th.BasicUser.Id, postId, th.BasicChannel.Id)
fileInfo2 := th.CreateFileInfo(th.BasicUser.Id, model.NewId(), th.BasicChannel.Id)
oldPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id},
}
newPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id, fileInfo2.Id},
}
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 1, len(fileIds))
require.Equal(t, fileInfo1.Id, fileIds[0])
// verify file2 is attached to the post
updatedFileInfo2, err := th.App.Srv().Store().FileInfo().Get(fileInfo2.Id)
require.NoError(t, err)
require.NotEqual(t, postId, updatedFileInfo2.PostId)
})
t.Run("when admin adds a file to other user's post", func(t *testing.T) {
postId := model.NewId()
// admin uploads the files
fileInfo1 := th.CreateFileInfo(th.SystemAdminUser.Id, "", th.BasicChannel.Id)
fileInfo2 := th.CreateFileInfo(th.SystemAdminUser.Id, "", th.BasicChannel.Id)
// basic user's post
oldPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
}
newPost := &model.Post{
Id: postId,
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "Message",
CreateAt: model.GetMillis() - 10000,
FileIds: []string{fileInfo1.Id, fileInfo2.Id}, // admin attaching two files
}
// admin's session
th.Context.Session().UserId = th.SystemAdminUser.Id
fileIds, appErr := th.App.processPostFileChanges(th.Context, newPost, oldPost, nil)
require.Nil(t, appErr)
require.Equal(t, 2, len(fileIds))
require.Contains(t, fileIds, fileInfo1.Id)
require.Contains(t, fileIds, fileInfo2.Id)
// verify files are attached to the post and still belong tyo the admin
updatedFileInfo1, err := th.App.Srv().Store().FileInfo().Get(fileInfo1.Id)
require.NoError(t, err)
require.Equal(t, postId, updatedFileInfo1.PostId)
require.Equal(t, th.SystemAdminUser.Id, updatedFileInfo1.CreatorId)
updatedFileInfo2, err := th.App.Srv().Store().FileInfo().Get(fileInfo2.Id)
require.NoError(t, err)
require.Equal(t, postId, updatedFileInfo2.PostId)
require.Equal(t, th.SystemAdminUser.Id, updatedFileInfo2.CreatorId)
})
}