MM-16872 - Extend Plugin API to set LHS bot icon (#11601)

* MM-16872 - Extend Plugin API to set LHS bot icon

* MM-16872 - Using ReadSeeker as opposed to Reader for reading svg image file

* MM-16872 - PR feedback

* MM-16872 - Using userId rather than bot.UserId

* MM-16872 - Minor stylistic changes

* MM-16872 - Removing DriverName check
Этот коммит содержится в:
Ali Farooq
2019-07-11 12:00:12 -04:00
коммит произвёл GitHub
родитель 76f4fccf8a
Коммит 5ed40a48c8
10 изменённых файлов: 388 добавлений и 57 удалений

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

@@ -5,6 +5,9 @@ package app
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -12,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
func TestCreateBot(t *testing.T) {
@@ -596,6 +600,165 @@ func TestConvertUserToBot(t *testing.T) {
})
}
func TestSetBotIconImage(t *testing.T) {
t.Run("invalid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
err := th.App.SetBotIconImage("invalid_bot_id", svgFile)
require.NotNil(t, err)
})
t.Run("valid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Set an icon image
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
expectedData, fileErr := ioutil.ReadAll(svgFile)
require.Nil(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
defer th.App.PermanentDeleteBot(bot.UserId)
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
exists, err := th.App.FileExists(fpath)
require.Nil(t, err)
require.False(t, exists, "icon.svg shouldn't exist for the bot")
svgFile.Seek(0, 0)
err = th.App.SetBotIconImage(bot.UserId, svgFile)
require.Nil(t, err)
exists, err = th.App.FileExists(fpath)
require.Nil(t, err)
require.True(t, exists, "icon.svg should exist for the bot")
actualData, err := th.App.ReadFile(fpath)
require.Nil(t, err)
require.NotNil(t, actualData)
require.Equal(t, expectedData, actualData)
})
}
func TestGetBotIconImage(t *testing.T) {
t.Run("invalid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
actualData, err := th.App.GetBotIconImage("invalid_bot_id")
require.NotNil(t, err)
require.Nil(t, actualData)
})
t.Run("valid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Set an icon image
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
expectedData, fileErr := ioutil.ReadAll(svgFile)
require.Nil(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
defer th.App.PermanentDeleteBot(bot.UserId)
svgFile.Seek(0, 0)
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
_, err = th.App.WriteFile(svgFile, fpath)
require.Nil(t, err)
actualBytes, err := th.App.GetBotIconImage(bot.UserId)
require.Nil(t, err)
require.NotNil(t, actualBytes)
actualData, err := th.App.ReadFile(fpath)
require.Nil(t, err)
require.NotNil(t, actualData)
require.Equal(t, expectedData, actualData)
})
}
func TestDeleteBotIconImage(t *testing.T) {
t.Run("invalid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
err := th.App.DeleteBotIconImage("invalid_bot_id")
require.NotNil(t, err)
})
t.Run("valid bot", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Set an icon image
path, _ := fileutils.FindDir("tests")
svgFile, fileErr := os.Open(filepath.Join(path, "test.svg"))
require.NoError(t, fileErr)
defer svgFile.Close()
expectedData, fileErr := ioutil.ReadAll(svgFile)
require.Nil(t, fileErr)
require.NotNil(t, expectedData)
bot, err := th.App.ConvertUserToBot(&model.User{
Username: "username",
Id: th.BasicUser.Id,
})
defer th.App.PermanentDeleteBot(bot.UserId)
// Set icon
svgFile.Seek(0, 0)
err = th.App.SetBotIconImage(bot.UserId, svgFile)
require.Nil(t, err)
// Get icon
actualData, err := th.App.GetBotIconImage(bot.UserId)
require.Nil(t, err)
require.NotNil(t, actualData)
require.Equal(t, expectedData, actualData)
// Bot icon should exist
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
exists, err := th.App.FileExists(fpath)
require.Nil(t, err)
require.True(t, exists, "icon.svg should exist for the bot")
// Delete icon
err = th.App.DeleteBotIconImage(bot.UserId)
require.Nil(t, err)
// Bot icon should not exist
exists, err = th.App.FileExists(fpath)
require.Nil(t, err)
require.False(t, exists, "icon.svg should be deleted for the bot")
})
}
func sToP(s string) *string {
return &s
}