Skip to content

Commit

Permalink
fix: UpdateUser returns the entire updated resource (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz committed May 20, 2020
1 parent 017d978 commit 6fdb904
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion server/services/identity_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *identityServerImpl) UpdateUser(_ context.Context, in *pb.UpdateUserRequ
}

s.users[i] = userEntry{user: updated}
return u, nil
return updated, nil
}

// Deletes a user, their profile, and all of their authored messages.
Expand Down
35 changes: 21 additions & 14 deletions server/services/identity_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,39 @@ func Test_User_lifecycle(t *testing.T) {
t.Error("Expected to get created user.")
}

got.DisplayName = "musubi"
got.Nickname = proto.String("musu")
// Make a copy of the User value, then unset proto3_optional fields
// to scope the update to DisplayName and Nickname.
u := *got
u.DisplayName = "musubi"
u.Nickname = proto.String("musu")
u.Age = nil
u.HeightFeet = nil
u.EnableNotifications = nil
updated, err := s.UpdateUser(
context.Background(),
&pb.UpdateUserRequest{User: got, UpdateMask: nil})
&pb.UpdateUserRequest{User: &u, UpdateMask: nil})
if err != nil {
t.Errorf("Update: unexpected err %+v", err)
}
// Ensure the proto3_optional fields that were unset did not get updated.
if updated.Age == nil {
t.Errorf("UpdateUser().Age was unexpectedly set to nil")
}
if updated.HeightFeet == nil {
t.Errorf("UpdateUser().HeightFeet was unexpectedly set to nil")
}
if updated.EnableNotifications == nil {
t.Errorf("UpdateUser().EnableNotifications was unexpectedly set to nil")
}

got, err = s.GetUser(
context.Background(),
&pb.GetUserRequest{Name: updated.GetName()})
if err != nil {
t.Errorf("Get: unexpected err %+v", err)
}
// Cannot use proto.Equal here because the update time is changed on updates.
if updated.GetName() != got.GetName() ||
updated.GetDisplayName() != got.GetDisplayName() ||
updated.GetEmail() != got.GetEmail() ||
!proto.Equal(updated.GetCreateTime(), got.GetCreateTime()) ||
proto.Equal(updated.GetUpdateTime(), got.GetUpdateTime()) ||
updated.GetNickname() != got.GetNickname() ||
updated.GetAge() != got.GetAge() ||
updated.GetHeightFeet() != got.GetHeightFeet() ||
updated.GetEnableNotifications() != got.GetEnableNotifications() {
t.Error("Expected to get updated user.")
if !proto.Equal(updated, got) {
t.Errorf("UpdateUser() = %+v, want %+v", got, updated)
}

r, err := s.ListUsers(
Expand Down

0 comments on commit 6fdb904

Please sign in to comment.