Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kv): Ensure UpdateUser cleans up the index when updating names #17906

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kv/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
54 changes: 54 additions & 0 deletions testing/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
})
}