diff --git a/plugin/helpers_bots.go b/plugin/helpers_bots.go index e4702aae08..c0e30d9983 100644 --- a/plugin/helpers_bots.go +++ b/plugin/helpers_bots.go @@ -231,9 +231,21 @@ func (p *HelpersImpl) ensureBot(bot *model.Bot) (retBotID string, retErr error) return "", errors.Wrap(kvGetErr, "failed to get bot") } - // If the bot has already been created, there is nothing to do. + // If the bot has already been created, use it if botIDBytes != nil { botID := string(botIDBytes) + + // ensure existing bot is synced with what is being created + botPatch := &model.BotPatch{ + Username: &bot.Username, + DisplayName: &bot.DisplayName, + Description: &bot.Description, + } + + if _, err := p.API.PatchBot(botID, botPatch); err != nil { + return "", errors.Wrap(err, "failed to patch bot") + } + return botID, nil } diff --git a/plugin/helpers_bots_test.go b/plugin/helpers_bots_test.go index d76291f69e..1b331ee0b3 100644 --- a/plugin/helpers_bots_test.go +++ b/plugin/helpers_bots_test.go @@ -73,6 +73,11 @@ func TestEnsureBot(t *testing.T) { api := setupAPI() api.On("GetServerVersion").Return("5.10.0") api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotID), nil) + api.On("PatchBot", expectedBotID, &model.BotPatch{ + Username: &testbot.Username, + DisplayName: &testbot.DisplayName, + Description: &testbot.Description, + }).Return(nil, nil) defer api.AssertExpectations(t) p := &plugin.HelpersImpl{} @@ -111,6 +116,11 @@ func TestEnsureBot(t *testing.T) { api.On("GetBundlePath").Return("", nil) api.On("SetProfileImage", expectedBotID, imageBytes).Return(nil) api.On("GetServerVersion").Return("5.10.0") + api.On("PatchBot", expectedBotID, &model.BotPatch{ + Username: &testbot.Username, + DisplayName: &testbot.DisplayName, + Description: &testbot.Description, + }).Return(nil, nil) defer api.AssertExpectations(t) p := &plugin.HelpersImpl{} @@ -136,6 +146,11 @@ func TestEnsureBot(t *testing.T) { api.On("GetBundlePath").Return("", nil) api.On("SetBotIconImage", expectedBotID, imageBytes).Return(nil) api.On("GetServerVersion").Return("5.10.0") + api.On("PatchBot", expectedBotID, &model.BotPatch{ + Username: &testbot.Username, + DisplayName: &testbot.DisplayName, + Description: &testbot.Description, + }).Return(nil, nil) defer api.AssertExpectations(t) p := &plugin.HelpersImpl{} @@ -160,6 +175,11 @@ func TestEnsureBot(t *testing.T) { api.On("SetProfileImage", expectedBotID, imageBytes).Return(nil) api.On("SetBotIconImage", expectedBotID, imageBytes).Return(nil) api.On("GetServerVersion").Return("5.10.0") + api.On("PatchBot", expectedBotID, &model.BotPatch{ + Username: &testbot.Username, + DisplayName: &testbot.DisplayName, + Description: &testbot.Description, + }).Return(nil, nil) defer api.AssertExpectations(t) p := &plugin.HelpersImpl{} @@ -169,6 +189,45 @@ func TestEnsureBot(t *testing.T) { assert.Equal(t, expectedBotID, botID) assert.Nil(t, err) }) + + t.Run("should find and update the bot with new bot details", func(t *testing.T) { + expectedBotID := model.NewId() + + expectedBotUsername := "updated_testbot" + expectedBotDisplayName := "Updated Test Bot" + expectedBotDescription := "updated testbotdescription" + + testsDir, _ := fileutils.FindDir("tests") + testImage := filepath.Join(testsDir, "test.png") + imageBytes, err := ioutil.ReadFile(testImage) + assert.Nil(t, err) + + api := setupAPI() + api.On("GetServerVersion").Return("5.10.0") + api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotID), nil) + api.On("GetBundlePath").Return("", nil) + api.On("SetProfileImage", expectedBotID, imageBytes).Return(nil) + api.On("SetBotIconImage", expectedBotID, imageBytes).Return(nil) + api.On("PatchBot", expectedBotID, &model.BotPatch{ + Username: &expectedBotUsername, + DisplayName: &expectedBotDisplayName, + Description: &expectedBotDescription, + }).Return(nil, nil) + defer api.AssertExpectations(t) + + p := &plugin.HelpersImpl{} + p.API = api + + updatedTestbot := &model.Bot{ + Username: "updated_testbot", + DisplayName: "Updated Test Bot", + Description: "updated testbotdescription", + } + botID, err := p.EnsureBot(updatedTestbot, plugin.ProfileImagePath(testImage), plugin.IconImagePath(testImage)) + + assert.Equal(t, expectedBotID, botID) + assert.Nil(t, err) + }) }) t.Run("if bot doesn't exist", func(t *testing.T) {