Profile image from bytes (#26610)

* ProfileImageBytes for EnsureBotOptions

* leverage plugintest.NewAPI

* fix linting

* clarify ProfileImage* godoc
This commit is contained in:
Jesse Hallam 2024-03-28 07:56:55 -03:00 committed by GitHub
parent ed00cf78ff
commit 22c978e223
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 70 additions and 31 deletions

View File

@ -112,14 +112,29 @@ func (b *BotService) DeletePermanently(botUserID string) error {
}
type ensureBotOptions struct {
ProfileImagePath string
ProfileImagePath string
ProfileImageBytes []byte
}
type EnsureBotOption func(*ensureBotOptions)
// ProfileImagePath configures EnsureBot to set a profile image from the given path.
//
// Using this option overrides any previously set ProfileImageBytes option.
func ProfileImagePath(path string) EnsureBotOption {
return func(args *ensureBotOptions) {
args.ProfileImagePath = path
args.ProfileImageBytes = nil
}
}
// ProfileImageBytes configures EnsureBot to set a profile image from the given bytes.
//
// Using this option overrides any previously set ProfileImagePath option.
func ProfileImageBytes(bytes []byte) EnsureBotOption {
return func(args *ensureBotOptions) {
args.ProfileImageBytes = bytes
args.ProfileImagePath = ""
}
}
@ -172,6 +187,11 @@ func (b *BotService) ensureBot(m mutex, bot *model.Bot, options ...EnsureBotOpti
if appErr != nil {
return "", errors.Wrap(appErr, "failed to set profile image")
}
} else if len(o.ProfileImageBytes) > 0 {
appErr := b.api.SetProfileImage(botID, o.ProfileImageBytes)
if appErr != nil {
return "", errors.Wrap(appErr, "failed to set profile image")
}
}
return botID, nil

View File

@ -14,8 +14,7 @@ import (
func TestCreateBot(t *testing.T) {
t.Run("success", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
api.On("CreateBot", &model.Bot{Username: "1"}).Return(&model.Bot{Username: "1", UserId: "2"}, nil)
@ -27,8 +26,7 @@ func TestCreateBot(t *testing.T) {
})
t.Run("failure", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
appErr := newAppError()
@ -44,8 +42,7 @@ func TestCreateBot(t *testing.T) {
func TestUpdateBotStatus(t *testing.T) {
t.Run("success", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
api.On("UpdateBotActive", "1", true).Return(&model.Bot{UserId: "2"}, nil)
@ -56,8 +53,7 @@ func TestUpdateBotStatus(t *testing.T) {
})
t.Run("failure", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
appErr := newAppError()
@ -72,8 +68,7 @@ func TestUpdateBotStatus(t *testing.T) {
func TestGetBot(t *testing.T) {
t.Run("success", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
api.On("GetBot", "1", true).Return(&model.Bot{UserId: "2"}, nil)
@ -84,8 +79,7 @@ func TestGetBot(t *testing.T) {
})
t.Run("failure", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
appErr := newAppError()
@ -179,7 +173,7 @@ func TestListBot(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
api := &plugintest.API{}
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
api.On("GetBots", test.expectedOptions).Return(test.bots, test.err)
@ -191,16 +185,13 @@ func TestListBot(t *testing.T) {
require.NoError(t, err, test.name)
}
require.Equal(t, test.bots, bots, test.name)
api.AssertExpectations(t)
})
}
}
func TestDeleteBotPermanently(t *testing.T) {
t.Run("success", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
api.On("PermanentDeleteBot", "1").Return(nil)
@ -210,8 +201,7 @@ func TestDeleteBotPermanently(t *testing.T) {
})
t.Run("failure", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
appErr := newAppError()
@ -233,8 +223,7 @@ func TestEnsureBot(t *testing.T) {
m := testMutex{}
t.Run("server version incompatible", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
api.On("GetServerVersion").Return("5.9.0")
@ -249,8 +238,7 @@ func TestEnsureBot(t *testing.T) {
t.Run("if bot already exists", func(t *testing.T) {
t.Run("should find and return the existing bot ID", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
expectedBotID := model.NewId()
@ -263,9 +251,8 @@ func TestEnsureBot(t *testing.T) {
assert.Equal(t, expectedBotID, botID)
})
t.Run("should set the bot profile image when specified", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
t.Run("should set the bot profile image when specified from a file", func(t *testing.T) {
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
expectedBotID := model.NewId()
@ -287,9 +274,42 @@ func TestEnsureBot(t *testing.T) {
assert.Equal(t, expectedBotID, botID)
})
t.Run("should set the bot profile image when specified from bytes", func(t *testing.T) {
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
expectedBotID := model.NewId()
profileImageBytes := []byte("profile image")
api.On("EnsureBotUser", testbot).Return(expectedBotID, nil)
api.On("SetProfileImage", expectedBotID, profileImageBytes).Return(nil)
api.On("GetServerVersion").Return("5.10.0")
botID, err := client.Bot.ensureBot(m, testbot, ProfileImageBytes(profileImageBytes))
require.NoError(t, err)
assert.Equal(t, expectedBotID, botID)
})
t.Run("the last bot profile image configuration should take precedence", func(t *testing.T) {
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
expectedBotID := model.NewId()
profileImageBytes := []byte("profile image")
api.On("EnsureBotUser", testbot).Return(expectedBotID, nil)
api.On("SetProfileImage", expectedBotID, profileImageBytes).Return(nil)
api.On("GetServerVersion").Return("5.10.0")
botID, err := client.Bot.ensureBot(m, testbot, ProfileImagePath("does not exist"), ProfileImageBytes(profileImageBytes))
require.NoError(t, err)
assert.Equal(t, expectedBotID, botID)
})
t.Run("should find and update the bot with new bot details", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
expectedBotID := model.NewId()
@ -332,8 +352,7 @@ func TestEnsureBot(t *testing.T) {
t.Run("if bot doesn't exist", func(t *testing.T) {
t.Run("should create bot and set the bot profile image when specified", func(t *testing.T) {
api := &plugintest.API{}
defer api.AssertExpectations(t)
api := plugintest.NewAPI(t)
client := NewClient(api, &plugintest.Driver{})
expectedBotID := model.NewId()