[MM-37716] Drop support for LHS specific bot icons (#18087)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
99bb6084b3
Коммит
225565f412
202
api4/bot_test.go
202
api4/bot_test.go
@@ -5,19 +5,13 @@ package api4
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/utils/fileutils"
|
||||
"github.com/mattermost/mattermost-server/v6/utils/testutils"
|
||||
)
|
||||
|
||||
func TestCreateBot(t *testing.T) {
|
||||
@@ -1212,202 +1206,6 @@ func TestAssignBot(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetBotIconImage(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
user := th.BasicUser
|
||||
|
||||
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
||||
|
||||
th.AddPermissionToRole(model.PermissionCreateBot.Id, model.SystemUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionManageBots.Id, model.SystemUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionReadBots.Id, model.SystemUserRoleId)
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
||||
})
|
||||
|
||||
bot := &model.Bot{
|
||||
Username: GenerateTestUsername(),
|
||||
Description: "bot",
|
||||
}
|
||||
bot, resp := th.Client.CreateBot(bot)
|
||||
CheckCreatedStatus(t, resp)
|
||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||
|
||||
badData, err := testutils.ReadTestFile("test.png")
|
||||
require.NoError(t, err)
|
||||
|
||||
goodData, err := testutils.ReadTestFile("test.svg")
|
||||
require.NoError(t, err)
|
||||
|
||||
// SetBotIconImage only allowed for bots
|
||||
_, resp = th.SystemAdminClient.SetBotIconImage(user.Id, goodData)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
// png/jpg is not allowed
|
||||
ok, resp := th.Client.SetBotIconImage(bot.UserId, badData)
|
||||
require.False(t, ok, "Should return false, set icon image only allows svg")
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
ok, resp = th.Client.SetBotIconImage(model.NewId(), badData)
|
||||
require.False(t, ok, "Should return false, set icon image not allowed")
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
_, resp = th.Client.SetBotIconImage(bot.UserId, goodData)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
// status code returns either forbidden or unauthorized
|
||||
// note: forbidden is set as default at Client4.SetBotIconImage when request is terminated early by server
|
||||
th.Client.Logout()
|
||||
_, resp = th.Client.SetBotIconImage(bot.UserId, badData)
|
||||
if resp.StatusCode == http.StatusForbidden {
|
||||
CheckForbiddenStatus(t, resp)
|
||||
} else if resp.StatusCode == http.StatusUnauthorized {
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
} else {
|
||||
require.Fail(t, "Should have failed either forbidden or unauthorized")
|
||||
}
|
||||
|
||||
_, resp = th.SystemAdminClient.SetBotIconImage(bot.UserId, goodData)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
|
||||
actualData, appErr := th.App.ReadFile(fpath)
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, actualData)
|
||||
require.Equal(t, goodData, actualData)
|
||||
|
||||
info := &model.FileInfo{Path: fpath}
|
||||
err = th.cleanupTestFile(info)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetBotIconImage(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
||||
|
||||
th.AddPermissionToRole(model.PermissionCreateBot.Id, model.SystemUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionManageBots.Id, model.SystemUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionReadBots.Id, model.SystemUserRoleId)
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
||||
})
|
||||
|
||||
bot := &model.Bot{
|
||||
Username: GenerateTestUsername(),
|
||||
Description: "bot",
|
||||
}
|
||||
bot, resp := th.Client.CreateBot(bot)
|
||||
CheckCreatedStatus(t, resp)
|
||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||
|
||||
// Get icon image for user with no icon
|
||||
data, resp := th.Client.GetBotIconImage(bot.UserId)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
require.Equal(t, 0, len(data))
|
||||
|
||||
// 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, err := ioutil.ReadAll(svgFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
svgFile.Seek(0, 0)
|
||||
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
|
||||
_, appErr := th.App.WriteFile(svgFile, fpath)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
data, resp = th.Client.GetBotIconImage(bot.UserId)
|
||||
CheckNoError(t, resp)
|
||||
require.Equal(t, expectedData, data)
|
||||
|
||||
_, resp = th.Client.GetBotIconImage("junk")
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = th.Client.GetBotIconImage(model.NewId())
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
th.Client.Logout()
|
||||
_, resp = th.Client.GetBotIconImage(bot.UserId)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
_, resp = th.SystemAdminClient.GetBotIconImage(bot.UserId)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
info := &model.FileInfo{Path: "/bots/" + bot.UserId + "/icon.svg"}
|
||||
err = th.cleanupTestFile(info)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDeleteBotIconImage(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
||||
|
||||
th.AddPermissionToRole(model.PermissionCreateBot.Id, model.SystemUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionManageBots.Id, model.SystemUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionReadBots.Id, model.SystemUserRoleId)
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.EnableBotAccountCreation = true
|
||||
})
|
||||
|
||||
bot := &model.Bot{
|
||||
Username: GenerateTestUsername(),
|
||||
Description: "bot",
|
||||
}
|
||||
bot, resp := th.Client.CreateBot(bot)
|
||||
CheckCreatedStatus(t, resp)
|
||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||
|
||||
// Get icon image for user with no icon
|
||||
data, resp := th.Client.GetBotIconImage(bot.UserId)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
require.Equal(t, 0, len(data))
|
||||
|
||||
// Set an icon image
|
||||
svgData, err := testutils.ReadTestFile("test.svg")
|
||||
require.NoError(t, err)
|
||||
|
||||
_, resp = th.Client.SetBotIconImage(bot.UserId, svgData)
|
||||
CheckNoError(t, resp)
|
||||
|
||||
fpath := fmt.Sprintf("/bots/%v/icon.svg", bot.UserId)
|
||||
exists, appErr := th.App.FileExists(fpath)
|
||||
require.Nil(t, appErr)
|
||||
require.True(t, exists, "icon.svg needs to exist for the user")
|
||||
|
||||
data, resp = th.Client.GetBotIconImage(bot.UserId)
|
||||
CheckNoError(t, resp)
|
||||
require.Equal(t, svgData, data)
|
||||
|
||||
success, resp := th.Client.DeleteBotIconImage("junk")
|
||||
CheckBadRequestStatus(t, resp)
|
||||
require.False(t, success)
|
||||
|
||||
success, resp = th.Client.DeleteBotIconImage(model.NewId())
|
||||
CheckNotFoundStatus(t, resp)
|
||||
require.False(t, success)
|
||||
|
||||
success, resp = th.Client.DeleteBotIconImage(bot.UserId)
|
||||
CheckNoError(t, resp)
|
||||
require.True(t, success)
|
||||
|
||||
th.Client.Logout()
|
||||
success, resp = th.Client.DeleteBotIconImage(bot.UserId)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
require.False(t, success)
|
||||
|
||||
exists, appErr = th.App.FileExists(fpath)
|
||||
require.Nil(t, appErr)
|
||||
require.False(t, exists, "icon.svg should not for the user")
|
||||
}
|
||||
|
||||
func TestConvertBotToUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
Ссылка в новой задаче
Block a user