Preventing plugin startup failure if bot account already exists as regular user. (#11077)

This commit is contained in:
Christopher Speller
2019-06-10 07:39:42 -07:00
committed by GitHub
parent b4d444319b
commit 31d695f951
2 changed files with 9 additions and 8 deletions

View File

@@ -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 <username> --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

View File

@@ -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) {