Add All route & operation for Chronograf Users
Signed-off-by: Jared Scheib <jared.scheib@gmail.com>pull/10616/head
parent
eb6c212023
commit
b8970c8ed1
|
@ -146,7 +146,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
|
|||
// Users associated with Chronograf
|
||||
router.GET("/chronograf/v1/me", service.Me)
|
||||
|
||||
// router.GET("/chronograf/v1/users", service.Users)
|
||||
router.GET("/chronograf/v1/users", service.Users)
|
||||
router.POST("/chronograf/v1/users", service.NewUser)
|
||||
|
||||
router.GET("/chronograf/v1/users/:id", service.UserID)
|
||||
|
|
|
@ -82,7 +82,7 @@ func (h *Service) SourceUsers(w http.ResponseWriter, r *http.Request) {
|
|||
ur[i] = *usr
|
||||
}
|
||||
|
||||
res := usersResponse{
|
||||
res := sourceUsersResponse{
|
||||
Users: ur,
|
||||
}
|
||||
|
||||
|
@ -240,7 +240,7 @@ func (r *sourceUserRequest) ValidCreate() error {
|
|||
return validPermissions(&r.Permissions)
|
||||
}
|
||||
|
||||
type usersResponse struct {
|
||||
type sourceUsersResponse struct {
|
||||
Users []sourceUserResponse `json:"users"`
|
||||
}
|
||||
|
||||
|
|
|
@ -57,6 +57,24 @@ func newUserResponse(u *chronograf.User) *userResponse {
|
|||
}
|
||||
}
|
||||
|
||||
type usersResponse struct {
|
||||
Links selfLinks `json:"links"`
|
||||
Users []*userResponse `json:"users"`
|
||||
}
|
||||
|
||||
func newUsersResponse(users []chronograf.User) *usersResponse {
|
||||
usersResp := make([]*userResponse, len(users))
|
||||
for i, user := range users {
|
||||
usersResp[i] = newUserResponse(&user)
|
||||
}
|
||||
return &usersResponse{
|
||||
Users: usersResp,
|
||||
Links: selfLinks{
|
||||
Self: "/chronograf/v1/users",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) UserID(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
|
@ -151,3 +169,16 @@ func (s *Service) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
|||
w.Header().Add("Location", cu.Links.Self)
|
||||
encodeJSON(w, http.StatusOK, cu, s.Logger)
|
||||
}
|
||||
|
||||
func (s *Service) Users(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
users, err := s.UsersStore.All(ctx)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
||||
return
|
||||
}
|
||||
|
||||
res := newUsersResponse(users)
|
||||
encodeJSON(w, http.StatusOK, res, s.Logger)
|
||||
}
|
||||
|
|
|
@ -371,6 +371,86 @@ func TestService_UpdateUser(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestService_Users(t *testing.T) {
|
||||
type fields struct {
|
||||
UsersStore chronograf.UsersStore
|
||||
Logger chronograf.Logger
|
||||
}
|
||||
type args struct {
|
||||
w *httptest.ResponseRecorder
|
||||
r *http.Request
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantStatus int
|
||||
wantContentType string
|
||||
wantBody string
|
||||
}{
|
||||
{
|
||||
name: "Get all Chronograf users",
|
||||
fields: fields{
|
||||
Logger: log.New(log.DebugLevel),
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
},
|
||||
{
|
||||
ID: 1338,
|
||||
Name: "bobbettastuhvetta",
|
||||
Provider: "Auth0",
|
||||
Scheme: "LDAP",
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
w: httptest.NewRecorder(),
|
||||
r: httptest.NewRequest(
|
||||
"GET",
|
||||
"http://any.url", // can be any valid URL as we are bypassing mux
|
||||
nil,
|
||||
),
|
||||
},
|
||||
wantStatus: http.StatusOK,
|
||||
wantContentType: "application/json",
|
||||
wantBody: `{"users":[{"id":1337,"name":"billysteve","provider":"Google","scheme":"OAuth2","links":{"self":"/chronograf/v1/users/1337"}},{"id":1338,"name":"bobbettastuhvetta","provider":"Auth0","scheme":"LDAP","links":{"self":"/chronograf/v1/users/1338"}}],"links":{"self":"/chronograf/v1/users"}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := &Service{
|
||||
UsersStore: tt.fields.UsersStore,
|
||||
Logger: tt.fields.Logger,
|
||||
}
|
||||
|
||||
s.Users(tt.args.w, tt.args.r)
|
||||
|
||||
resp := tt.args.w.Result()
|
||||
content := resp.Header.Get("Content-Type")
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if resp.StatusCode != tt.wantStatus {
|
||||
t.Errorf("%q. Users() = %v, want %v", tt.name, resp.StatusCode, tt.wantStatus)
|
||||
}
|
||||
if tt.wantContentType != "" && content != tt.wantContentType {
|
||||
t.Errorf("%q. Users() = %v, want %v", tt.name, content, tt.wantContentType)
|
||||
}
|
||||
if eq, _ := jsonEqual(string(body), tt.wantBody); tt.wantBody != "" && !eq {
|
||||
t.Errorf("%q. Users() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wantBody)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func jsonEqual(s1, s2 string) (eq bool, err error) {
|
||||
var o1, o2 interface{}
|
||||
|
||||
|
|
Loading…
Reference in New Issue