Return users in usersResponse in sorted order

Signed-off-by: Jared Scheib <jared.scheib@gmail.com>
pull/2099/head
Michael de Sa 2017-10-09 15:19:46 -07:00 committed by Jared Scheib
parent 1a6b446bf8
commit 990c4eee4d
2 changed files with 52 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"sort"
"github.com/bouk/httprouter"
"github.com/influxdata/chronograf"
@ -62,11 +63,26 @@ type usersResponse struct {
Users []*userResponse `json:"users"`
}
type usersResponseSlice []*userResponse
func (u usersResponseSlice) Len() int {
return len(u)
}
func (u usersResponseSlice) Less(i, j int) bool {
return u[i].ID < u[j].ID
}
func (u usersResponseSlice) Swap(i, j int) {
u[i], u[j] = u[j], u[i]
}
func newUsersResponse(users []chronograf.User) *usersResponse {
usersResp := make([]*userResponse, len(users))
usersResp := make(usersResponseSlice, len(users))
for i, user := range users {
usersResp[i] = newUserResponse(&user)
}
sort.Sort(usersResp)
return &usersResponse{
Users: usersResp,
Links: selfLinks{

View File

@ -423,6 +423,41 @@ func TestService_Users(t *testing.T) {
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"}}`,
},
{
name: "Get all Chronograf users, ensuring order of users in response",
fields: fields{
Logger: log.New(log.DebugLevel),
UsersStore: &mocks.UsersStore{
AllF: func(ctx context.Context) ([]chronograf.User, error) {
return []chronograf.User{
{
ID: 1338,
Name: "bobbettastuhvetta",
Provider: "Auth0",
Scheme: "LDAP",
},
{
ID: 1337,
Name: "billysteve",
Provider: "Google",
Scheme: "OAuth2",
},
}, 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 {