diff --git a/kv/user.go b/kv/user.go index 705a4ec5e08..f4c4fd63f31 100644 --- a/kv/user.go +++ b/kv/user.go @@ -354,7 +354,7 @@ func (s *Service) updateUser(ctx context.Context, tx Tx, id influxdb.ID, upd inf } if upd.Name != nil { - if err := s.removeUserFromIndex(ctx, tx, id, *upd.Name); err != nil { + if err := s.removeUserFromIndex(ctx, tx, id, u.Name); err != nil { return nil, err } diff --git a/testing/user_service.go b/testing/user_service.go index 56e382cf147..01390f0026f 100644 --- a/testing/user_service.go +++ b/testing/user_service.go @@ -79,6 +79,10 @@ func UserService( name: "UpdateUser", fn: UpdateUser, }, + { + name: "UpdateUser_IndexHygiene", + fn: UpdateUser_IndexHygiene, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -977,3 +981,53 @@ func UpdateUser( }) } } + +func UpdateUser_IndexHygiene( + init func(UserFields, *testing.T) (platform.UserService, string, func()), + t *testing.T, +) { + + oldUserName := "user1" + users := UserFields{ + Users: []*platform.User{ + { + ID: MustIDBase16(userOneID), + Name: oldUserName, + Status: "active", + }, + }, + } + s, _, done := init(users, t) + defer done() + + newUserName := "user1Updated" + upd := platform.UserUpdate{ + Name: &newUserName, + } + + ctx := context.Background() + _, err := s.UpdateUser(ctx, MustIDBase16(userOneID), upd) + if err != nil { + t.Error(err) + } + + // Ensure we can find the user with the new name. + _, nerr := s.FindUser(ctx, platform.UserFilter{ + Name: &newUserName, + }) + if nerr != nil { + t.Error("unexpected error when finding user by name", nerr) + } + + // Ensure we cannot find a user with the old name. The index used when + // searching by name should have been cleared out by the UpdateUser + // operation. + _, oerr := s.FindUser(ctx, platform.UserFilter{ + Name: &oldUserName, + }) + ErrorsEqual(t, oerr, &platform.Error{ + Code: platform.ENotFound, + Op: platform.OpFindUser, + Msg: "user not found", + }) +}