diff --git a/plugin/helpers_bots.go b/plugin/helpers_bots.go index b83adc7f30..664a954d5d 100644 --- a/plugin/helpers_bots.go +++ b/plugin/helpers_bots.go @@ -46,10 +46,10 @@ func (p *HelpersImpl) EnsureBot(bot *model.Bot) (retBotId string, retErr error) if kvSetErr := p.API.KVSet(BOT_USER_KEY, []byte(user.Id)); kvSetErr != nil { p.API.LogWarn("Failed to set claimed bot user id.", "userid", user.Id, "err", kvSetErr) } - return user.Id, nil } else { - return "", errors.New("unable to create bot because user exists with the same name") + p.API.LogError("Plugin attempted to use an account that already exists. Convert user to a bot account in the CLI by running 'mattermost user convert --bot'. If the user is an existing user account you want to preserve, change its username and restart the Mattermost server, after which the plugin will create a bot account with that name. For more information about bot accounts, see https://mattermost.com/pl/default-bot-accounts", "username", bot.Username, "user_id", user.Id) } + return user.Id, nil } // Create a new bot user for the plugin diff --git a/plugin/helpers_bots_test.go b/plugin/helpers_bots_test.go index 30cd05685c..776c32a10b 100644 --- a/plugin/helpers_bots_test.go +++ b/plugin/helpers_bots_test.go @@ -9,6 +9,7 @@ import ( "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/plugin" "github.com/mattermost/mattermost-server/plugin/plugintest" + "github.com/mattermost/mattermost-server/plugin/plugintest/mock" "github.com/stretchr/testify/assert" ) @@ -115,13 +116,15 @@ func TestEnsureBot(t *testing.T) { assert.Nil(t, err) }) - t.Run("should fail if user exists with the same name and is not a bot", func(t *testing.T) { + t.Run("should return the non-bot account but log a message if user exists with the same name and is not a bot", func(t *testing.T) { + expectedBotId := model.NewId() api := setupAPI() api.On("KVGet", plugin.BOT_USER_KEY).Return(nil, nil) api.On("GetUserByUsername", testbot.Username).Return(&model.User{ - Id: "conflictingid", + Id: expectedBotId, IsBot: false, }, nil) + api.On("LogError", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() defer api.AssertExpectations(t) p := &plugin.HelpersImpl{} @@ -129,10 +132,8 @@ func TestEnsureBot(t *testing.T) { botId, err := p.EnsureBot(testbot) - t.Log(botId) - t.Log(err) - assert.Equal(t, "", botId) - assert.NotNil(t, err) + assert.Equal(t, expectedBotId, botId) + assert.Nil(t, err) }) t.Run("shoudl fail if create bot fails", func(t *testing.T) {