diff --git a/server/channels/app/bot.go b/server/channels/app/bot.go index 1f95f04b7e..302e7951f3 100644 --- a/server/channels/app/bot.go +++ b/server/channels/app/bot.go @@ -65,18 +65,19 @@ func (a *App) EnsureBot(rctx request.CTX, pluginID string, bot *model.Bot) (stri if appErr := a.SetPluginKey(pluginID, botUserKey, []byte(user.Id)); appErr != nil { return "", fmt.Errorf("failed to set plugin key: %w", appErr) } - } else { - rctx.Logger().Error("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", mlog.String("username", - bot.Username), - mlog.String("user_id", - user.Id), - ) + return user.Id, nil } - return user.Id, nil + + rctx.Logger().Error("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", mlog.String("username", + bot.Username), + mlog.String("user_id", + user.Id), + ) + return "", fmt.Errorf("username %q is already taken by a non-bot user", bot.Username) } createdBot, err := a.CreateBot(rctx, bot) diff --git a/server/channels/app/bot_test.go b/server/channels/app/bot_test.go index 9dd4cf8156..aa68a4dedf 100644 --- a/server/channels/app/bot_test.go +++ b/server/channels/app/bot_test.go @@ -152,6 +152,23 @@ func TestEnsureBot(t *testing.T) { assert.Equal(t, "another bot", bot.Description) }) + t.Run("ensure bot should fail if username belongs to a non-bot user", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + pluginId := "pluginId" + + // th.BasicUser is a regular (non-bot) user created by InitBasic. + // EnsureBot must return an error — not the human user's ID. + botID, err := th.App.EnsureBot(th.Context, pluginId, &model.Bot{ + Username: th.BasicUser.Username, + Description: "a bot", + OwnerId: th.BasicUser.Id, + }) + require.Error(t, err) + assert.Empty(t, botID) + }) + t.Run("ensure bot should pass even after delete bot user", func(t *testing.T) { th := Setup(t).InitBasic() defer th.TearDown()