Wire up GET /db/:db/users
parent
7079d69f03
commit
3c57dd3165
24
handler.go
24
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"`
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue