From 3c57dd3165790467b06b5fc6374c431212a19271 Mon Sep 17 00:00:00 2001 From: David Norton Date: Mon, 8 Dec 2014 13:39:33 -0500 Subject: [PATCH] Wire up GET /db/:db/users --- handler.go | 24 +++++++++++++++++++++--- handler_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/handler.go b/handler.go index f3478b5275..5077e3619b 100644 --- a/handler.go +++ b/handler.go @@ -284,8 +284,6 @@ func (h *Handler) serveUpdateClusterAdmin(w http.ResponseWriter, r *http.Request func (h *Handler) serveDeleteClusterAdmin(w http.ResponseWriter, r *http.Request) { // TODO: Authentication - // DELETE /cluster_admins/:user - urlQry := r.URL.Query() if err := h.server.DeleteClusterAdmin(urlQry.Get(":user")); err == ErrClusterAdminNotFound { @@ -303,7 +301,27 @@ func (h *Handler) serveDeleteClusterAdmin(w http.ResponseWriter, r *http.Request func (h *Handler) serveAuthenticateDBUser(w http.ResponseWriter, r *http.Request) {} // serveDBUsers returns data about a single database user. -func (h *Handler) serveDBUsers(w http.ResponseWriter, r *http.Request) {} +func (h *Handler) serveDBUsers(w http.ResponseWriter, r *http.Request) { + // TODO: Authentication + + urlQry := r.URL.Query() + + db := h.server.Database(urlQry.Get(":db")) + if db == nil { + h.error(w, ErrDatabaseNotFound.Error(), http.StatusNotFound) + return + } + + dbUsers := db.Users() + jsonUsers := make([]*userJSON, 0, len(dbUsers)) + for _, dbUser := range dbUsers { + jsonUsers = append(jsonUsers, newUserJSONFromDBUser(dbUser)) + } + + if err := json.NewEncoder(w).Encode(jsonUsers); err != nil { + h.error(w, err.Error(), http.StatusInternalServerError) + } +} type userJSON struct { Name string `json:"name"` diff --git a/handler_test.go b/handler_test.go index c7422215c5..6230b3bce2 100644 --- a/handler_test.go +++ b/handler_test.go @@ -376,6 +376,55 @@ func TestHandler_Ping(t *testing.T) { } } +func TestHandler_Users_NoUsers(t *testing.T) { + srvr := OpenServer(NewMessagingClient()) + srvr.CreateDatabase("foo") + s := NewHTTPServer(srvr) + defer s.Close() + status, body := MustHTTP("GET", s.URL+`/db/foo/users`, "") + if status != http.StatusOK { + t.Fatalf("unexpected status: %d", status) + } else if body != "[]" { + t.Fatalf("unexpected body: %s", body) + } +} + +func TestHandler_Users_OneUser(t *testing.T) { + srvr := OpenServer(NewMessagingClient()) + srvr.CreateDatabase("foo") + db := srvr.Database("foo") + readFrom := []*influxdb.Matcher{{IsRegex: true, Name: ".*"}} + writeTo := []*influxdb.Matcher{{IsRegex: true, Name: ".*"}} + db.CreateUser("jdoe", "1337", readFrom, writeTo) + s := NewHTTPServer(srvr) + defer s.Close() + status, body := MustHTTP("GET", s.URL+`/db/foo/users`, "") + if status != http.StatusOK { + t.Fatalf("unexpected status: %d", status) + } else if body != `[{"name":"jdoe","password":"","isAdmin":false,"readFrom":[{"IsRegex":true,"Name":".*"}],"writeTo":[{"IsRegex":true,"Name":".*"}]}]` { + t.Fatalf("unexpected body: %s", body) + } +} + +func TestHandler_Users_MultipleUsers(t *testing.T) { + srvr := OpenServer(NewMessagingClient()) + srvr.CreateDatabase("foo") + db := srvr.Database("foo") + readFrom := []*influxdb.Matcher{{IsRegex: true, Name: ".*"}} + writeTo := []*influxdb.Matcher{{IsRegex: true, Name: ".*"}} + db.CreateUser("jdoe", "1337", readFrom, writeTo) + db.CreateUser("mclark", "1337", readFrom, writeTo) + db.CreateUser("csmith", "1337", readFrom, writeTo) + s := NewHTTPServer(srvr) + defer s.Close() + status, body := MustHTTP("GET", s.URL+`/db/foo/users`, "") + if status != http.StatusOK { + t.Fatalf("unexpected status: %d", status) + } else if body != `[{"name":"csmith","password":"","isAdmin":false,"readFrom":[{"IsRegex":true,"Name":".*"}],"writeTo":[{"IsRegex":true,"Name":".*"}]},{"name":"jdoe","password":"","isAdmin":false,"readFrom":[{"IsRegex":true,"Name":".*"}],"writeTo":[{"IsRegex":true,"Name":".*"}]},{"name":"mclark","password":"","isAdmin":false,"readFrom":[{"IsRegex":true,"Name":".*"}],"writeTo":[{"IsRegex":true,"Name":".*"}]}]` { + t.Fatalf("unexpected body: %s", body) + } +} + func TestHandler_CreateDBUser(t *testing.T) { srvr := OpenServer(NewMessagingClient()) srvr.CreateDatabase("foo")