diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-02-16 09:46:55 -0500 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2017-02-16 09:46:55 -0500 |
commit | f87d42916f1ec4287daad29d6ffd4145dfc4b5cd (patch) | |
tree | dd0296f0023efe031dbacbb39b724540bed2d8a5 /api4 | |
parent | 8f262241595cc9377e4c3adce9c728eaff38c5f3 (diff) | |
download | chat-f87d42916f1ec4287daad29d6ffd4145dfc4b5cd.tar.gz chat-f87d42916f1ec4287daad29d6ffd4145dfc4b5cd.tar.bz2 chat-f87d42916f1ec4287daad29d6ffd4145dfc4b5cd.zip |
Implement PUT /users/{user_id}/patch endpoint for APIv4 (#5418)
Diffstat (limited to 'api4')
-rw-r--r-- | api4/user.go | 27 | ||||
-rw-r--r-- | api4/user_test.go | 67 |
2 files changed, 94 insertions, 0 deletions
diff --git a/api4/user.go b/api4/user.go index d8d071cd2..e394b9661 100644 --- a/api4/user.go +++ b/api4/user.go @@ -21,6 +21,7 @@ func InitUser() { BaseRoutes.User.Handle("", ApiSessionRequired(getUser)).Methods("GET") BaseRoutes.User.Handle("", ApiSessionRequired(updateUser)).Methods("PUT") + BaseRoutes.User.Handle("/patch", ApiSessionRequired(patchUser)).Methods("PUT") BaseRoutes.User.Handle("", ApiSessionRequired(deleteUser)).Methods("DELETE") BaseRoutes.User.Handle("/roles", ApiSessionRequired(updateUserRoles)).Methods("PUT") BaseRoutes.User.Handle("/password", ApiSessionRequired(updatePassword)).Methods("PUT") @@ -255,6 +256,32 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) { } } +func patchUser(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireUserId() + if c.Err != nil { + return + } + + patch := model.UserPatchFromJson(r.Body) + if patch == nil { + c.SetInvalidParam("user") + return + } + + if !app.SessionHasPermissionToUser(c.Session, c.Params.UserId) { + c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS) + return + } + + if ruser, err := app.PatchUser(c.Params.UserId, patch, c.GetSiteURL(), c.IsSystemAdmin()); err != nil { + c.Err = err + return + } else { + c.LogAudit("") + w.Write([]byte(ruser.ToJson())) + } +} + func deleteUser(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireUserId() if c.Err != nil { diff --git a/api4/user_test.go b/api4/user_test.go index 79589bdad..771a53cbe 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -349,6 +349,73 @@ func TestUpdateUser(t *testing.T) { CheckNoError(t, resp) } +func TestPatchUser(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + user := th.CreateUser() + Client.Login(user.Email, user.Password) + + patch := &model.UserPatch{} + + patch.Nickname = new(string) + *patch.Nickname = "Joram Wilander" + patch.FirstName = new(string) + *patch.FirstName = "Joram" + patch.LastName = new(string) + *patch.LastName = "Wilander" + patch.Position = new(string) + + ruser, resp := Client.PatchUser(user.Id, patch) + CheckNoError(t, resp) + CheckUserSanitization(t, ruser) + + if ruser.Nickname != "Joram Wilander" { + t.Fatal("Nickname did not update properly") + } + if ruser.FirstName != "Joram" { + t.Fatal("FirstName did not update properly") + } + if ruser.LastName != "Wilander" { + t.Fatal("LastName did not update properly") + } + if ruser.Position != "" { + t.Fatal("Position did not update properly") + } + if ruser.Username != user.Username { + t.Fatal("Username should not have updated") + } + + _, resp = Client.PatchUser("junk", patch) + CheckBadRequestStatus(t, resp) + + ruser.Id = model.NewId() + _, resp = Client.PatchUser(model.NewId(), patch) + CheckForbiddenStatus(t, resp) + + if r, err := Client.DoApiPut("/users/"+user.Id+"/patch", "garbage"); err == nil { + t.Fatal("should have errored") + } else { + if r.StatusCode != http.StatusBadRequest { + t.Log("actual: " + strconv.Itoa(r.StatusCode)) + t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) + t.Fatal("wrong status code") + } + } + + Client.Logout() + _, resp = Client.PatchUser(user.Id, patch) + CheckUnauthorizedStatus(t, resp) + + th.LoginBasic() + _, resp = Client.PatchUser(user.Id, patch) + CheckForbiddenStatus(t, resp) + + _, resp = th.SystemAdminClient.PatchUser(user.Id, patch) + CheckNoError(t, resp) +} + func TestDeleteUser(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() Client := th.Client |