2021-09-01 00:08:18 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2022-06-13 10:05:42 +02:00
|
|
|
"errors"
|
2021-09-01 00:08:18 +02:00
|
|
|
"io"
|
2022-04-13 19:17:04 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-09-01 00:08:18 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2023-03-07 06:51:53 +02:00
|
|
|
"time"
|
2021-09-01 00:08:18 +02:00
|
|
|
|
2022-06-13 10:05:42 +02:00
|
|
|
"github.com/golang/mock/gomock"
|
2022-03-22 16:24:34 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-05-22 18:31:24 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
mm_model "github.com/mattermost/mattermost-server/v6/model"
|
2021-09-01 00:08:18 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/plugin/plugintest/mock"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/filestore"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/filestore/mocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testFileName = "temp-file-name"
|
2022-03-22 16:24:34 +02:00
|
|
|
testBoardID = "test-board-id"
|
2023-05-22 18:31:24 +02:00
|
|
|
testPath = "/path/to/file/fileName.txt"
|
2021-09-01 00:08:18 +02:00
|
|
|
)
|
|
|
|
|
2022-06-13 10:05:42 +02:00
|
|
|
var errDummy = errors.New("hello")
|
|
|
|
|
2021-09-01 00:08:18 +02:00
|
|
|
type TestError struct{}
|
|
|
|
|
|
|
|
func (err *TestError) Error() string { return "Mocked File backend error" }
|
|
|
|
|
|
|
|
func TestGetFileReader(t *testing.T) {
|
2022-04-13 19:17:04 +02:00
|
|
|
testFilePath := filepath.Join("1", "test-board-id", "temp-file-name")
|
|
|
|
|
2021-09-01 00:08:18 +02:00
|
|
|
th, _ := SetupTestHelper(t)
|
|
|
|
mockedReadCloseSeek := &mocks.ReadCloseSeeker{}
|
|
|
|
t.Run("should get file reader from filestore successfully", func(t *testing.T) {
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
readerFunc := func(path string) filestore.ReadCloseSeeker {
|
|
|
|
return mockedReadCloseSeek
|
|
|
|
}
|
|
|
|
|
|
|
|
readerErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsFunc := func(path string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("Reader", testFilePath).Return(readerFunc, readerErrorFunc)
|
|
|
|
mockedFileBackend.On("FileExists", testFilePath).Return(fileExistsFunc, fileExistsErrorFunc)
|
2022-03-22 16:24:34 +02:00
|
|
|
actual, _ := th.App.GetFileReader("1", testBoardID, testFileName)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Equal(t, mockedReadCloseSeek, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should get error from filestore when file exists return error", func(t *testing.T) {
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedError := &TestError{}
|
|
|
|
readerFunc := func(path string) filestore.ReadCloseSeeker {
|
|
|
|
return mockedReadCloseSeek
|
|
|
|
}
|
|
|
|
|
|
|
|
readerErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsFunc := func(path string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsErrorFunc := func(path string) error {
|
|
|
|
return mockedError
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("Reader", testFilePath).Return(readerFunc, readerErrorFunc)
|
|
|
|
mockedFileBackend.On("FileExists", testFilePath).Return(fileExistsFunc, fileExistsErrorFunc)
|
2022-03-22 16:24:34 +02:00
|
|
|
actual, err := th.App.GetFileReader("1", testBoardID, testFileName)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Error(t, err, mockedError)
|
|
|
|
assert.Nil(t, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should return error, if get reader from file backend returns error", func(t *testing.T) {
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedError := &TestError{}
|
|
|
|
readerFunc := func(path string) filestore.ReadCloseSeeker {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
readerErrorFunc := func(path string) error {
|
|
|
|
return mockedError
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsFunc := func(path string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("Reader", testFilePath).Return(readerFunc, readerErrorFunc)
|
|
|
|
mockedFileBackend.On("FileExists", testFilePath).Return(fileExistsFunc, fileExistsErrorFunc)
|
2022-03-22 16:24:34 +02:00
|
|
|
actual, err := th.App.GetFileReader("1", testBoardID, testFileName)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Error(t, err, mockedError)
|
|
|
|
assert.Nil(t, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should move file from old filepath to new filepath, if file doesnot exists in new filepath and workspace id is 0", func(t *testing.T) {
|
2022-04-13 19:17:04 +02:00
|
|
|
filePath := filepath.Join("0", "test-board-id", "temp-file-name")
|
2021-09-01 00:08:18 +02:00
|
|
|
workspaceid := "0"
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
readerFunc := func(path string) filestore.ReadCloseSeeker {
|
|
|
|
return mockedReadCloseSeek
|
|
|
|
}
|
|
|
|
|
|
|
|
readerErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsFunc := func(path string) bool {
|
|
|
|
// return true for old path
|
|
|
|
return path == testFileName
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
moveFileFunc := func(oldFileName, newFileName string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("FileExists", filePath).Return(fileExistsFunc, fileExistsErrorFunc)
|
|
|
|
mockedFileBackend.On("FileExists", testFileName).Return(fileExistsFunc, fileExistsErrorFunc)
|
|
|
|
mockedFileBackend.On("MoveFile", testFileName, filePath).Return(moveFileFunc)
|
|
|
|
mockedFileBackend.On("Reader", filePath).Return(readerFunc, readerErrorFunc)
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
actual, _ := th.App.GetFileReader(workspaceid, testBoardID, testFileName)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Equal(t, mockedReadCloseSeek, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should return file reader, if file doesnot exists in new filepath and old file path", func(t *testing.T) {
|
2022-04-13 19:17:04 +02:00
|
|
|
filePath := filepath.Join("0", "test-board-id", "temp-file-name")
|
2021-09-01 00:08:18 +02:00
|
|
|
fileName := testFileName
|
|
|
|
workspaceid := "0"
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
readerFunc := func(path string) filestore.ReadCloseSeeker {
|
|
|
|
return mockedReadCloseSeek
|
|
|
|
}
|
|
|
|
|
|
|
|
readerErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsFunc := func(path string) bool {
|
|
|
|
// return true for old path
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fileExistsErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
moveFileFunc := func(oldFileName, newFileName string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("FileExists", filePath).Return(fileExistsFunc, fileExistsErrorFunc)
|
|
|
|
mockedFileBackend.On("FileExists", testFileName).Return(fileExistsFunc, fileExistsErrorFunc)
|
|
|
|
mockedFileBackend.On("MoveFile", fileName, filePath).Return(moveFileFunc)
|
|
|
|
mockedFileBackend.On("Reader", filePath).Return(readerFunc, readerErrorFunc)
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
actual, _ := th.App.GetFileReader(workspaceid, testBoardID, testFileName)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Equal(t, mockedReadCloseSeek, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveFile(t *testing.T) {
|
|
|
|
th, _ := SetupTestHelper(t)
|
|
|
|
mockedReadCloseSeek := &mocks.ReadCloseSeeker{}
|
|
|
|
t.Run("should save file to file store using file backend", func(t *testing.T) {
|
|
|
|
fileName := "temp-file-name.txt"
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
2022-06-13 10:05:42 +02:00
|
|
|
th.Store.EXPECT().SaveFileInfo(gomock.Any()).Return(nil)
|
2021-09-01 00:08:18 +02:00
|
|
|
|
|
|
|
writeFileFunc := func(reader io.Reader, path string) int64 {
|
2022-04-13 19:17:04 +02:00
|
|
|
paths := strings.Split(path, string(os.PathSeparator))
|
2023-03-07 06:51:53 +02:00
|
|
|
assert.Equal(t, "boards", paths[0])
|
|
|
|
assert.Equal(t, time.Now().Format("20060102"), paths[1])
|
2021-09-01 00:08:18 +02:00
|
|
|
fileName = paths[2]
|
|
|
|
return int64(10)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFileErrorFunc := func(reader io.Reader, filePath string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("WriteFile", mockedReadCloseSeek, mock.Anything).Return(writeFileFunc, writeFileErrorFunc)
|
2023-05-22 18:31:24 +02:00
|
|
|
actual, err := th.App.SaveFile(mockedReadCloseSeek, "1", testBoardID, fileName, false)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Equal(t, fileName, actual)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should save .jpeg file as jpg file to file store using file backend", func(t *testing.T) {
|
|
|
|
fileName := "temp-file-name.jpeg"
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
2022-06-13 10:05:42 +02:00
|
|
|
th.Store.EXPECT().SaveFileInfo(gomock.Any()).Return(nil)
|
2021-09-01 00:08:18 +02:00
|
|
|
|
|
|
|
writeFileFunc := func(reader io.Reader, path string) int64 {
|
2022-04-13 19:17:04 +02:00
|
|
|
paths := strings.Split(path, string(os.PathSeparator))
|
2023-03-07 06:51:53 +02:00
|
|
|
assert.Equal(t, "boards", paths[0])
|
|
|
|
assert.Equal(t, time.Now().Format("20060102"), paths[1])
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Equal(t, "jpg", strings.Split(paths[2], ".")[1])
|
|
|
|
return int64(10)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFileErrorFunc := func(reader io.Reader, filePath string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("WriteFile", mockedReadCloseSeek, mock.Anything).Return(writeFileFunc, writeFileErrorFunc)
|
2023-05-22 18:31:24 +02:00
|
|
|
actual, err := th.App.SaveFile(mockedReadCloseSeek, "1", "test-board-id", fileName, false)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should return error when fileBackend.WriteFile returns error", func(t *testing.T) {
|
|
|
|
fileName := "temp-file-name.jpeg"
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedError := &TestError{}
|
|
|
|
|
|
|
|
writeFileFunc := func(reader io.Reader, path string) int64 {
|
2022-04-13 19:17:04 +02:00
|
|
|
paths := strings.Split(path, string(os.PathSeparator))
|
2023-03-07 06:51:53 +02:00
|
|
|
assert.Equal(t, "boards", paths[0])
|
|
|
|
assert.Equal(t, time.Now().Format("20060102"), paths[1])
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Equal(t, "jpg", strings.Split(paths[2], ".")[1])
|
|
|
|
return int64(10)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFileErrorFunc := func(reader io.Reader, filePath string) error {
|
|
|
|
return mockedError
|
|
|
|
}
|
|
|
|
|
|
|
|
mockedFileBackend.On("WriteFile", mockedReadCloseSeek, mock.Anything).Return(writeFileFunc, writeFileErrorFunc)
|
2023-05-22 18:31:24 +02:00
|
|
|
actual, err := th.App.SaveFile(mockedReadCloseSeek, "1", "test-board-id", fileName, false)
|
2021-09-01 00:08:18 +02:00
|
|
|
assert.Equal(t, "", actual)
|
|
|
|
assert.Equal(t, "unable to store the file in the files storage: Mocked File backend error", err.Error())
|
|
|
|
})
|
|
|
|
}
|
2022-06-13 10:05:42 +02:00
|
|
|
|
|
|
|
func TestGetFileInfo(t *testing.T) {
|
|
|
|
th, _ := SetupTestHelper(t)
|
|
|
|
|
|
|
|
t.Run("should return file info", func(t *testing.T) {
|
2023-05-22 18:31:24 +02:00
|
|
|
fileInfo := &mm_model.FileInfo{
|
2022-06-13 10:05:42 +02:00
|
|
|
Id: "file_info_id",
|
|
|
|
Archived: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
th.Store.EXPECT().GetFileInfo("filename").Return(fileInfo, nil).Times(2)
|
|
|
|
|
|
|
|
fetchedFileInfo, err := th.App.GetFileInfo("Afilename")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "file_info_id", fetchedFileInfo.Id)
|
|
|
|
assert.False(t, fetchedFileInfo.Archived)
|
|
|
|
|
|
|
|
fetchedFileInfo, err = th.App.GetFileInfo("Afilename.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "file_info_id", fetchedFileInfo.Id)
|
|
|
|
assert.False(t, fetchedFileInfo.Archived)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should return archived file info", func(t *testing.T) {
|
2023-05-22 18:31:24 +02:00
|
|
|
fileInfo := &mm_model.FileInfo{
|
2022-06-13 10:05:42 +02:00
|
|
|
Id: "file_info_id",
|
|
|
|
Archived: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
th.Store.EXPECT().GetFileInfo("filename").Return(fileInfo, nil)
|
|
|
|
|
|
|
|
fetchedFileInfo, err := th.App.GetFileInfo("Afilename")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "file_info_id", fetchedFileInfo.Id)
|
|
|
|
assert.True(t, fetchedFileInfo.Archived)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("should return archived file infoerror", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetFileInfo("filename").Return(nil, errDummy)
|
|
|
|
|
|
|
|
fetchedFileInfo, err := th.App.GetFileInfo("Afilename")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, fetchedFileInfo)
|
|
|
|
})
|
|
|
|
}
|
2023-03-07 06:51:53 +02:00
|
|
|
|
|
|
|
func TestGetFile(t *testing.T) {
|
|
|
|
th, _ := SetupTestHelper(t)
|
2023-05-22 18:31:24 +02:00
|
|
|
t.Run("happy path, no errors", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileInfoID").Return(&mm_model.FileInfo{
|
2023-03-07 06:51:53 +02:00
|
|
|
Id: "fileInfoID",
|
2023-05-22 18:31:24 +02:00
|
|
|
Path: testPath,
|
2023-03-07 06:51:53 +02:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedReadCloseSeek := &mocks.ReadCloseSeeker{}
|
|
|
|
readerFunc := func(path string) filestore.ReadCloseSeeker {
|
|
|
|
return mockedReadCloseSeek
|
|
|
|
}
|
|
|
|
|
|
|
|
readerErrorFunc := func(path string) error {
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-22 18:31:24 +02:00
|
|
|
mockedFileBackend.On("Reader", testPath).Return(readerFunc, readerErrorFunc)
|
|
|
|
mockedFileBackend.On("FileExists", testPath).Return(true, nil)
|
2023-03-07 06:51:53 +02:00
|
|
|
|
|
|
|
fileInfo, seeker, err := th.App.GetFile("teamID", "boardID", "7fileInfoID.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, fileInfo)
|
|
|
|
assert.NotNil(t, seeker)
|
|
|
|
})
|
|
|
|
|
2023-05-22 18:31:24 +02:00
|
|
|
t.Run("when GetFilePath() throws error", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileInfoID").Return(nil, errDummy)
|
|
|
|
|
|
|
|
fileInfo, seeker, err := th.App.GetFile("teamID", "boardID", "7fileInfoID.txt")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, fileInfo)
|
|
|
|
assert.Nil(t, seeker)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when FileExists returns false", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileInfoID").Return(&mm_model.FileInfo{
|
|
|
|
Id: "fileInfoID",
|
|
|
|
Path: testPath,
|
|
|
|
}, nil)
|
2023-03-07 06:51:53 +02:00
|
|
|
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
2023-05-22 18:31:24 +02:00
|
|
|
mockedFileBackend.On("FileExists", testPath).Return(false, nil)
|
2023-03-07 06:51:53 +02:00
|
|
|
|
2023-05-22 18:31:24 +02:00
|
|
|
fileInfo, seeker, err := th.App.GetFile("teamID", "boardID", "7fileInfoID.txt")
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, fileInfo)
|
|
|
|
assert.Nil(t, seeker)
|
|
|
|
})
|
|
|
|
t.Run("when FileReader throws error", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileInfoID").Return(&mm_model.FileInfo{
|
|
|
|
Id: "fileInfoID",
|
|
|
|
Path: testPath,
|
|
|
|
}, nil)
|
2023-03-07 06:51:53 +02:00
|
|
|
|
2023-05-22 18:31:24 +02:00
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedFileBackend.On("Reader", testPath).Return(nil, errDummy)
|
|
|
|
mockedFileBackend.On("FileExists", testPath).Return(true, nil)
|
2023-03-07 06:51:53 +02:00
|
|
|
|
|
|
|
fileInfo, seeker, err := th.App.GetFile("teamID", "boardID", "7fileInfoID.txt")
|
2023-05-22 18:31:24 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, fileInfo)
|
|
|
|
assert.Nil(t, seeker)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFilePath(t *testing.T) {
|
|
|
|
th, _ := SetupTestHelper(t)
|
|
|
|
|
|
|
|
t.Run("when FileInfo exists", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileInfoID").Return(&mm_model.FileInfo{
|
|
|
|
Id: "fileInfoID",
|
|
|
|
Path: testPath,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
fileInfo, filePath, err := th.App.GetFilePath("teamID", "boardID", "7fileInfoID.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, fileInfo)
|
|
|
|
assert.Equal(t, testPath, filePath)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when FileInfo doesn't exist", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileInfoID").Return(nil, nil)
|
|
|
|
|
|
|
|
fileInfo, filePath, err := th.App.GetFilePath("teamID", "boardID", "7fileInfoID.txt")
|
2023-03-07 06:51:53 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, fileInfo)
|
2023-05-22 18:31:24 +02:00
|
|
|
assert.Equal(t, "teamID/boardID/7fileInfoID.txt", filePath)
|
2023-03-07 06:51:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("when FileInfo exists but FileInfo.Path is not set", func(t *testing.T) {
|
2023-05-22 18:31:24 +02:00
|
|
|
th.Store.EXPECT().GetFileInfo("fileInfoID").Return(&mm_model.FileInfo{
|
2023-03-07 06:51:53 +02:00
|
|
|
Id: "fileInfoID",
|
|
|
|
Path: "",
|
|
|
|
}, nil)
|
|
|
|
|
2023-05-22 18:31:24 +02:00
|
|
|
fileInfo, filePath, err := th.App.GetFilePath("teamID", "boardID", "7fileInfoID.txt")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, fileInfo)
|
|
|
|
assert.Equal(t, "teamID/boardID/7fileInfoID.txt", filePath)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyCard(t *testing.T) {
|
|
|
|
th, _ := SetupTestHelper(t)
|
|
|
|
imageBlock := &model.Block{
|
|
|
|
ID: "imageBlock",
|
|
|
|
ParentID: "c3zqnh6fsu3f4mr6hzq9hizwske",
|
|
|
|
CreatedBy: "6k6ynxdp47dujjhhojw9nqhmyh",
|
|
|
|
ModifiedBy: "6k6ynxdp47dujjhhojw9nqhmyh",
|
|
|
|
Schema: 1,
|
|
|
|
Type: "image",
|
|
|
|
Title: "",
|
|
|
|
Fields: map[string]interface{}{"fileId": "7fileName.jpg"},
|
|
|
|
CreateAt: 1680725585250,
|
|
|
|
UpdateAt: 1680725585250,
|
|
|
|
DeleteAt: 0,
|
|
|
|
BoardID: "boardID",
|
|
|
|
}
|
|
|
|
t.Run("Board doesn't exist", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetBoard("boardID").Return(nil, errDummy)
|
|
|
|
_, err := th.App.CopyCardFiles("boardID", []*model.Block{}, false)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Board exists, image block, with FileInfo", func(t *testing.T) {
|
|
|
|
fileInfo := &mm_model.FileInfo{
|
|
|
|
Id: "imageBlock",
|
|
|
|
Path: testPath,
|
|
|
|
}
|
|
|
|
th.Store.EXPECT().GetBoard("boardID").Return(&model.Board{
|
|
|
|
ID: "boardID",
|
|
|
|
IsTemplate: false,
|
|
|
|
}, nil)
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileName").Return(fileInfo, nil)
|
|
|
|
th.Store.EXPECT().SaveFileInfo(fileInfo).Return(nil)
|
|
|
|
|
2023-03-07 06:51:53 +02:00
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
2023-05-22 18:31:24 +02:00
|
|
|
mockedFileBackend.On("CopyFile", mock.Anything, mock.Anything).Return(nil)
|
2023-03-07 06:51:53 +02:00
|
|
|
|
2023-05-22 18:31:24 +02:00
|
|
|
updatedFileNames, err := th.App.CopyCardFiles("boardID", []*model.Block{imageBlock}, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "7fileName.jpg", imageBlock.Fields["fileId"])
|
|
|
|
assert.NotNil(t, updatedFileNames["7fileName.jpg"])
|
|
|
|
assert.NotNil(t, updatedFileNames[imageBlock.Fields["fileId"].(string)])
|
|
|
|
})
|
2023-03-07 06:51:53 +02:00
|
|
|
|
2023-05-22 18:31:24 +02:00
|
|
|
t.Run("Board exists, attachment block, with FileInfo", func(t *testing.T) {
|
|
|
|
attachmentBlock := &model.Block{
|
|
|
|
ID: "attachmentBlock",
|
|
|
|
ParentID: "c3zqnh6fsu3f4mr6hzq9hizwske",
|
|
|
|
CreatedBy: "6k6ynxdp47dujjhhojw9nqhmyh",
|
|
|
|
ModifiedBy: "6k6ynxdp47dujjhhojw9nqhmyh",
|
|
|
|
Schema: 1,
|
|
|
|
Type: "attachment",
|
|
|
|
Title: "",
|
|
|
|
Fields: map[string]interface{}{"fileId": "7fileName.jpg"},
|
|
|
|
CreateAt: 1680725585250,
|
|
|
|
UpdateAt: 1680725585250,
|
|
|
|
DeleteAt: 0,
|
|
|
|
BoardID: "boardID",
|
|
|
|
}
|
|
|
|
|
|
|
|
fileInfo := &mm_model.FileInfo{
|
|
|
|
Id: "attachmentBlock",
|
|
|
|
Path: testPath,
|
|
|
|
}
|
|
|
|
th.Store.EXPECT().GetBoard("boardID").Return(&model.Board{
|
|
|
|
ID: "boardID",
|
|
|
|
IsTemplate: false,
|
|
|
|
}, nil)
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileName").Return(fileInfo, nil)
|
|
|
|
th.Store.EXPECT().SaveFileInfo(fileInfo).Return(nil)
|
|
|
|
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedFileBackend.On("CopyFile", mock.Anything, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
updatedFileNames, err := th.App.CopyCardFiles("boardID", []*model.Block{attachmentBlock}, false)
|
2023-03-07 06:51:53 +02:00
|
|
|
assert.NoError(t, err)
|
2023-05-22 18:31:24 +02:00
|
|
|
assert.NotNil(t, updatedFileNames[imageBlock.Fields["fileId"].(string)])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Board exists, image block, without FileInfo", func(t *testing.T) {
|
|
|
|
th.Store.EXPECT().GetBoard("boardID").Return(&model.Board{
|
|
|
|
ID: "boardID",
|
|
|
|
IsTemplate: false,
|
|
|
|
}, nil)
|
|
|
|
th.Store.EXPECT().GetFileInfo(gomock.Any()).Return(nil, nil)
|
|
|
|
th.Store.EXPECT().SaveFileInfo(gomock.Any()).Return(nil)
|
|
|
|
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedFileBackend.On("CopyFile", mock.Anything, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
updatedFileNames, err := th.App.CopyCardFiles("boardID", []*model.Block{imageBlock}, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, imageBlock.Fields["fileId"].(string))
|
|
|
|
assert.NotNil(t, updatedFileNames[imageBlock.Fields["fileId"].(string)])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyAndUpdateCardFiles(t *testing.T) {
|
|
|
|
th, _ := SetupTestHelper(t)
|
|
|
|
imageBlock := &model.Block{
|
|
|
|
ID: "imageBlock",
|
|
|
|
ParentID: "c3zqnh6fsu3f4mr6hzq9hizwske",
|
|
|
|
CreatedBy: "6k6ynxdp47dujjhhojw9nqhmyh",
|
|
|
|
ModifiedBy: "6k6ynxdp47dujjhhojw9nqhmyh",
|
|
|
|
Schema: 1,
|
|
|
|
Type: "image",
|
|
|
|
Title: "",
|
|
|
|
Fields: map[string]interface{}{"fileId": "7fileName.jpg"},
|
|
|
|
CreateAt: 1680725585250,
|
|
|
|
UpdateAt: 1680725585250,
|
|
|
|
DeleteAt: 0,
|
|
|
|
BoardID: "boardID",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Board exists, image block, with FileInfo", func(t *testing.T) {
|
|
|
|
fileInfo := &mm_model.FileInfo{
|
|
|
|
Id: "imageBlock",
|
|
|
|
Path: testPath,
|
|
|
|
}
|
|
|
|
th.Store.EXPECT().GetBoard("boardID").Return(&model.Board{
|
|
|
|
ID: "boardID",
|
|
|
|
IsTemplate: false,
|
|
|
|
}, nil)
|
|
|
|
th.Store.EXPECT().GetFileInfo("fileName").Return(fileInfo, nil)
|
|
|
|
th.Store.EXPECT().SaveFileInfo(fileInfo).Return(nil)
|
|
|
|
th.Store.EXPECT().PatchBlocks(gomock.Any(), "userID").Return(nil)
|
|
|
|
|
|
|
|
mockedFileBackend := &mocks.FileBackend{}
|
|
|
|
th.App.filesBackend = mockedFileBackend
|
|
|
|
mockedFileBackend.On("CopyFile", mock.Anything, mock.Anything).Return(nil)
|
|
|
|
|
|
|
|
err := th.App.CopyAndUpdateCardFiles("boardID", "userID", []*model.Block{imageBlock}, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotEqual(t, testPath, imageBlock.Fields["fileId"])
|
2023-03-07 06:51:53 +02:00
|
|
|
})
|
|
|
|
}
|