From fd356b62b4dd3318d2c8019d2310abdd6ce24c8c Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 20 Feb 2025 21:45:44 +0530 Subject: [PATCH] [AI assisted] MM-62837: (#30268) We did not invalidate the cache after converting a user to bot. That led to issues. See JIRA for more details. https://mattermost.atlassian.net/browse/MM-62837 ```release-note NONE ``` --- server/channels/api4/user_test.go | 19 +++++++++++++++++++ server/channels/app/bot.go | 1 + 2 files changed, 20 insertions(+) diff --git a/server/channels/api4/user_test.go b/server/channels/api4/user_test.go index 356d966d98..2b8146b0dd 100644 --- a/server/channels/api4/user_test.go +++ b/server/channels/api4/user_test.go @@ -6601,6 +6601,25 @@ func TestConvertUserToBot(t *testing.T) { require.NoError(t, err) require.NotNil(t, bot) }) + + t.Run("user cannot login after being converted to bot", func(t *testing.T) { + // Create a new user + user := th.CreateUser() + + // Login as the new user to verify login works initially + _, _, err := th.Client.Login(context.Background(), user.Email, user.Password) + require.NoError(t, err) + + // Convert user to bot + _, _, err = th.SystemAdminClient.ConvertUserToBot(context.Background(), user.Id) + require.NoError(t, err) + + // Try to login again - should fail + _, resp, err := th.Client.Login(context.Background(), user.Email, user.Password) + require.Error(t, err) + CheckErrorID(t, err, "api.user.login.bot_login_forbidden.app_error") + CheckUnauthorizedStatus(t, resp) + }) } func TestGetChannelMembersWithTeamData(t *testing.T) { diff --git a/server/channels/app/bot.go b/server/channels/app/bot.go index abecb70f09..27b5e6ce0c 100644 --- a/server/channels/app/bot.go +++ b/server/channels/app/bot.go @@ -617,6 +617,7 @@ func (a *App) ConvertUserToBot(rctx request.CTX, user *model.User) (*model.Bot, if err := a.RevokeAllSessions(rctx, user.Id); err != nil { return nil, err } + a.InvalidateCacheForUser(user.Id) return bot, nil }