2021-09-01 00:08:18 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
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 15:24:34 +01:00
|
|
|
testBoardID = "test-board-id"
|
|
|
|
testFilePath = "1/test-board-id/temp-file-name"
|
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) {
|
|
|
|
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 15:24:34 +01: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 15:24:34 +01: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 15:24:34 +01: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-03-22 15:24:34 +01:00
|
|
|
filePath := "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 15:24:34 +01: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-03-22 15:24:34 +01:00
|
|
|
filePath := "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 15:24:34 +01: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
|
|
|
|
|
|
|
|
writeFileFunc := func(reader io.Reader, path string) int64 {
|
|
|
|
paths := strings.Split(path, "/")
|
|
|
|
assert.Equal(t, "1", paths[0])
|
2022-03-22 15:24:34 +01:00
|
|
|
assert.Equal(t, testBoardID, 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)
|
2022-03-22 15:24:34 +01:00
|
|
|
actual, err := th.App.SaveFile(mockedReadCloseSeek, "1", testBoardID, fileName)
|
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
|
|
|
|
|
|
|
|
writeFileFunc := func(reader io.Reader, path string) int64 {
|
|
|
|
paths := strings.Split(path, "/")
|
|
|
|
assert.Equal(t, "1", paths[0])
|
2022-03-22 15:24:34 +01:00
|
|
|
assert.Equal(t, "test-board-id", 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)
|
2022-03-22 15:24:34 +01:00
|
|
|
actual, err := th.App.SaveFile(mockedReadCloseSeek, "1", "test-board-id", fileName)
|
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 {
|
|
|
|
paths := strings.Split(path, "/")
|
|
|
|
assert.Equal(t, "1", paths[0])
|
2022-03-22 15:24:34 +01:00
|
|
|
assert.Equal(t, "test-board-id", 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)
|
2022-03-22 15:24:34 +01:00
|
|
|
actual, err := th.App.SaveFile(mockedReadCloseSeek, "1", "test-board-id", fileName)
|
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())
|
|
|
|
})
|
|
|
|
}
|