From a4afae5b3bfb03570422cbb924c9077350ce2507 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Wed, 22 May 2024 11:20:02 +0200 Subject: [PATCH] Adds RemoteId to the fields to clean when coming from the API (#27047) * Cleans the RemoteId when creating a user through the API * Replace bad usage of the API with an App call in tests --------- Co-authored-by: Mattermost Build --- server/channels/api4/user_test.go | 30 +++++++++++++++++++++++++++++- server/public/model/user.go | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/server/channels/api4/user_test.go b/server/channels/api4/user_test.go index eb60b2844e..a6c6595508 100644 --- a/server/channels/api4/user_test.go +++ b/server/channels/api4/user_test.go @@ -115,6 +115,33 @@ func TestCreateUser(t *testing.T) { _, appErr = th.App.GetUserByUsername(user3.Username) require.NotNil(t, appErr) }, "Should not be able to create two users with the same email but spaces in it") + + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + email := th.GenerateTestEmail() + newUser := &model.User{ + Id: model.NewId(), + RemoteId: model.NewString(model.NewId()), + Email: email, + Password: "Password1", + Username: GenerateTestUsername(), + EmailVerified: true, + } + + _, resp, err = client.CreateUser(context.Background(), newUser) + require.Error(t, err) + require.ErrorContains(t, err, "Must call update for existing user") + CheckBadRequestStatus(t, resp) + _, appErr := th.App.GetUserByEmail(email) + require.NotNil(t, appErr) + + newUser.Id = "" + _, resp, err = client.CreateUser(context.Background(), newUser) + require.NoError(t, err) + CheckCreatedStatus(t, resp) + createdUser, appErr := th.App.GetUserByEmail(email) + require.Nil(t, appErr) + require.Zero(t, *createdUser.RemoteId) + }, "Should not be able to define the RemoteID of a user through the API") } func TestCreateUserAudit(t *testing.T) { @@ -3845,7 +3872,8 @@ func TestLogin(t *testing.T) { t.Run("remote user login rejected", func(t *testing.T) { email := th.GenerateTestEmail() user := model.User{Email: email, Nickname: "Darth Vader", Password: "hello1", Username: GenerateTestUsername(), Roles: model.SystemAdminRoleId + " " + model.SystemUserRoleId, RemoteId: model.NewString("remote-id")} - ruser, _, _ := th.Client.CreateUser(context.Background(), &user) + ruser, appErr := th.App.CreateUser(th.Context, &user) + require.Nil(t, appErr) _, err := th.SystemAdminClient.UpdateUserPassword(context.Background(), ruser.Id, "", "password") require.NoError(t, err) diff --git a/server/public/model/user.go b/server/public/model/user.go index b367b80e3b..78e8b1503e 100644 --- a/server/public/model/user.go +++ b/server/public/model/user.go @@ -657,6 +657,7 @@ func (u *User) SanitizeInput(isAdmin bool) { u.AuthService = "" u.EmailVerified = false } + u.RemoteId = NewString("") u.DeleteAt = 0 u.LastPasswordUpdate = 0 u.LastPictureUpdate = 0