Update only fields with diff; add test for that

Signed-off-by: Jared Scheib <jared.scheib@gmail.com>
pull/10616/head
Michael de Sa 2017-10-10 11:40:33 -07:00 committed by Jared Scheib
parent 526dca3cb4
commit 63d7a4d201
2 changed files with 50 additions and 4 deletions

View File

@ -162,10 +162,15 @@ func (s *Service) UpdateUser(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusNotFound, err.Error(), s.Logger)
}
// TODO: should we diff and only set non-nil fields?
u.Name = req.Name
u.Provider = req.Provider
u.Scheme = req.Scheme
if req.Name != "" {
u.Name = req.Name
}
if req.Provider != "" {
u.Provider = req.Provider
}
if req.Scheme != "" {
u.Scheme = req.Scheme
}
err = s.UsersStore.Update(ctx, u)
if err != nil {

View File

@ -334,6 +334,47 @@ func TestService_UpdateUser(t *testing.T) {
wantContentType: "application/json",
wantBody: `{"id":1336,"name":"bobbetta","provider":"Google","scheme":"OAuth2","links":{"self":"/chronograf/v1/users/1336"}}`,
},
{
name: "Update only one field of a Chronograf user",
fields: fields{
Logger: log.New(log.DebugLevel),
UsersStore: &mocks.UsersStore{
UpdateF: func(ctx context.Context, user *chronograf.User) error {
return nil
},
GetF: func(ctx context.Context, ID string) (*chronograf.User, error) {
switch ID {
case "1336":
return &chronograf.User{
ID: 1336,
Name: "bobbetta2",
Provider: "GitHub",
Scheme: "OAuth2",
}, nil
default:
return nil, fmt.Errorf("User with ID %v not found", ID)
}
},
},
},
args: args{
w: httptest.NewRecorder(),
r: httptest.NewRequest(
"PATCH",
"http://any.url",
nil,
),
user: &chronograf.User{
ID: 1336,
Name: "burnetta",
},
},
id: "1336",
wantStatus: http.StatusOK,
wantContentType: "application/json",
wantBody: `{"id":1336,"name":"burnetta","provider":"GitHub","scheme":"OAuth2","links":{"self":"/chronograf/v1/users/1336"}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {