summaryrefslogtreecommitdiffstats
path: root/api4
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-02-03 09:30:57 -0500
committerCorey Hulen <corey@hulen.com>2017-02-03 09:30:57 -0500
commitccb034382850b7e8ea924a4559e47ef44203155c (patch)
tree72026c58e6bb8e8ad679ac07476906281ffbb1d5 /api4
parent177589b1e26fcabd7749dd0fbe8c39999c206609 (diff)
downloadchat-ccb034382850b7e8ea924a4559e47ef44203155c.tar.gz
chat-ccb034382850b7e8ea924a4559e47ef44203155c.tar.bz2
chat-ccb034382850b7e8ea924a4559e47ef44203155c.zip
Implement POST /users/ids endpoint for APIv4 (#5274)
Diffstat (limited to 'api4')
-rw-r--r--api4/user.go20
-rw-r--r--api4/user_test.go32
2 files changed, 52 insertions, 0 deletions
diff --git a/api4/user.go b/api4/user.go
index f68d01d33..19d3446fb 100644
--- a/api4/user.go
+++ b/api4/user.go
@@ -16,6 +16,8 @@ func InitUser() {
l4g.Debug(utils.T("api.user.init.debug"))
BaseRoutes.Users.Handle("", ApiHandler(createUser)).Methods("POST")
+ BaseRoutes.Users.Handle("/ids", ApiSessionRequired(getUsersByIds)).Methods("POST")
+
BaseRoutes.User.Handle("", ApiSessionRequired(getUser)).Methods("GET")
BaseRoutes.User.Handle("", ApiSessionRequired(updateUser)).Methods("PUT")
BaseRoutes.User.Handle("/roles", ApiSessionRequired(updateUserRoles)).Methods("PUT")
@@ -84,6 +86,24 @@ func getUser(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
+func getUsersByIds(c *Context, w http.ResponseWriter, r *http.Request) {
+ userIds := model.ArrayFromJson(r.Body)
+
+ if len(userIds) == 0 {
+ c.SetInvalidParam("user_ids")
+ return
+ }
+
+ // No permission check required
+
+ if users, err := app.GetUsersByIds(userIds, c.IsSystemAdmin()); err != nil {
+ c.Err = err
+ return
+ } else {
+ w.Write([]byte(model.UserListToJson(users)))
+ }
+}
+
func updateUser(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 501bb38e3..54aae4e49 100644
--- a/api4/user_test.go
+++ b/api4/user_test.go
@@ -131,6 +131,38 @@ func TestGetUser(t *testing.T) {
}
}
+func TestGetUsersByIds(t *testing.T) {
+ th := Setup().InitBasic()
+ Client := th.Client
+
+ users, resp := Client.GetUsersByIds([]string{th.BasicUser.Id})
+ CheckNoError(t, resp)
+
+ if users[0].Id != th.BasicUser.Id {
+ t.Fatal("returned wrong user")
+ }
+ CheckUserSanitization(t, users[0])
+
+ _, resp = Client.GetUsersByIds([]string{})
+ CheckBadRequestStatus(t, resp)
+
+ users, resp = Client.GetUsersByIds([]string{"junk"})
+ CheckNoError(t, resp)
+ if len(users) > 0 {
+ t.Fatal("no users should be returned")
+ }
+
+ users, resp = Client.GetUsersByIds([]string{"junk", th.BasicUser.Id})
+ CheckNoError(t, resp)
+ if len(users) != 1 {
+ t.Fatal("1 user should be returned")
+ }
+
+ Client.Logout()
+ _, resp = Client.GetUsersByIds([]string{th.BasicUser.Id})
+ CheckUnauthorizedStatus(t, resp)
+}
+
func TestUpdateUser(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()