Skip to content

Commit

Permalink
fix(kv): Ensure UpdateUser cleans up the index when updating names.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettbuddin committed Apr 29, 2020
1 parent 3c2ab1b commit af4f8e4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
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",
})
}

0 comments on commit af4f8e4

Please sign in to comment.