Use new chonograf.UserStore Get method when needed
Refactor tests that were dependent of old implementation of UsersStorepull/10616/head
parent
246e65e598
commit
a0d300d280
|
@ -73,7 +73,7 @@ func (s *UsersStore) Get(ctx context.Context, q chronograf.UserQuery) (*chronogr
|
|||
}
|
||||
|
||||
if user == nil {
|
||||
return nil, fmt.Errorf("user not found")
|
||||
return nil, chronograf.ErrUserNotFound
|
||||
}
|
||||
|
||||
return user, nil
|
||||
|
|
|
@ -78,19 +78,13 @@ func AuthorizedUser(store chronograf.UsersStore, useAuth bool, role string, logg
|
|||
return
|
||||
}
|
||||
|
||||
u, err := getUserBy(store, ctx, username, provider)
|
||||
u, err := store.Get(ctx, chronograf.UserQuery{Name: &username, Provider: &provider})
|
||||
if err != nil {
|
||||
log.Error("Error to retrieving user")
|
||||
Error(w, http.StatusUnauthorized, fmt.Sprintf("User %s is not authorized", username), logger)
|
||||
return
|
||||
}
|
||||
|
||||
if u == nil {
|
||||
log.Error("User not found")
|
||||
Error(w, http.StatusNotFound, fmt.Sprintf("User with name %s and provider %s not found", username, provider), logger)
|
||||
return
|
||||
}
|
||||
|
||||
if hasPrivelege(u, role) {
|
||||
next(w, r)
|
||||
return
|
||||
|
|
|
@ -129,16 +129,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with viewer role is viewer authorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.ViewerRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.ViewerRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -157,16 +155,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with editor role is viewer authorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.EditorRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.EditorRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -185,16 +181,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with admin role is viewer authorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.AdminRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.AdminRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -213,16 +207,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with viewer role is editor unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.ViewerRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.ViewerRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -241,16 +233,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with editor role is editor authorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.EditorRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.EditorRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -269,16 +259,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with admin role is editor authorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.AdminRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.AdminRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -297,16 +285,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with viewer role is admin unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.ViewerRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.ViewerRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -325,16 +311,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with editor role is admin unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.EditorRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.EditorRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -353,16 +337,14 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with admin role is admin authorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.AdminRole,
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
server.AdminRole,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
@ -381,15 +363,13 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with no role is viewer unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{},
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
|
@ -407,15 +387,13 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with no role is editor unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{},
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
|
@ -433,15 +411,13 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with no role is admin unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{},
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
|
@ -459,17 +435,15 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with unknown role is viewer unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
{
|
||||
Name: "sweet_role",
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
{
|
||||
Name: "sweet_role",
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
|
@ -489,17 +463,15 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with unknown role is editor unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
{
|
||||
Name: "sweet_role",
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
{
|
||||
Name: "sweet_role",
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
|
@ -519,17 +491,15 @@ func TestAuthorizedUser(t *testing.T) {
|
|||
name: "User with unknown role is admin unauthorized",
|
||||
fields: fields{
|
||||
UsersStore: &mocks.UsersStore{
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
{
|
||||
Name: "sweet_role",
|
||||
},
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
ID: 1337,
|
||||
Name: "billysteve",
|
||||
Provider: "Google",
|
||||
Scheme: "OAuth2",
|
||||
Roles: []chronograf.Role{
|
||||
{
|
||||
Name: "sweet_role",
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
|
|
21
server/me.go
21
server/me.go
|
@ -88,8 +88,8 @@ func (s *Service) Me(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
usr, err := getUserBy(s.UsersStore, ctx, username, provider)
|
||||
if err != nil {
|
||||
usr, err := s.UsersStore.Get(ctx, chronograf.UserQuery{Name: &username, Provider: &provider})
|
||||
if err != nil && err != chronograf.ErrUserNotFound {
|
||||
unknownErrorWithMessage(w, err, s.Logger)
|
||||
return
|
||||
}
|
||||
|
@ -115,20 +115,3 @@ func (s *Service) Me(w http.ResponseWriter, r *http.Request) {
|
|||
res := newMeResponse(newUser)
|
||||
encodeJSON(w, http.StatusOK, res, s.Logger)
|
||||
}
|
||||
|
||||
func getUserBy(store chronograf.UsersStore, ctx context.Context, name, provider string) (*chronograf.User, error) {
|
||||
usrs, err := store.All(ctx)
|
||||
if err != nil {
|
||||
msg := fmt.Errorf("error retrieving user with Username: %s, Provider: %s: %v", name, provider, err)
|
||||
return nil, msg
|
||||
}
|
||||
|
||||
for _, u := range usrs {
|
||||
if u.Name == name && u.Provider == provider {
|
||||
return &u, nil
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: this should really return an error
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ func TestService_Me(t *testing.T) {
|
|||
},
|
||||
fields: fields{
|
||||
UseAuth: true,
|
||||
Logger: log.New(log.DebugLevel),
|
||||
UsersStore: &mocks.UsersStore{
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return &chronograf.User{
|
||||
|
@ -51,20 +52,6 @@ func TestService_Me(t *testing.T) {
|
|||
Passwd: "hunter2",
|
||||
}, nil
|
||||
},
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
Name: "me",
|
||||
Provider: "GitHub",
|
||||
Passwd: "hunter2",
|
||||
},
|
||||
{
|
||||
Name: "billietta",
|
||||
Provider: "Google",
|
||||
Passwd: "billiettaspassword",
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
principal: oauth2.Principal{
|
||||
|
@ -84,27 +71,14 @@ func TestService_Me(t *testing.T) {
|
|||
},
|
||||
fields: fields{
|
||||
UseAuth: true,
|
||||
Logger: log.New(log.DebugLevel),
|
||||
UsersStore: &mocks.UsersStore{
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return nil, fmt.Errorf("Unknown User")
|
||||
return nil, chronograf.ErrUserNotFound
|
||||
},
|
||||
AddF: func(ctx context.Context, u *chronograf.User) (*chronograf.User, error) {
|
||||
return u, nil
|
||||
},
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
Name: "me",
|
||||
Provider: "GitHub",
|
||||
Passwd: "hunter2",
|
||||
},
|
||||
{
|
||||
Name: "billietta",
|
||||
Provider: "Google",
|
||||
Passwd: "billiettaspassword",
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
principal: oauth2.Principal{
|
||||
|
@ -126,25 +100,11 @@ func TestService_Me(t *testing.T) {
|
|||
UseAuth: true,
|
||||
UsersStore: &mocks.UsersStore{
|
||||
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
|
||||
return nil, fmt.Errorf("Unknown User")
|
||||
return nil, chronograf.ErrUserNotFound
|
||||
},
|
||||
AddF: func(ctx context.Context, u *chronograf.User) (*chronograf.User, error) {
|
||||
return nil, fmt.Errorf("Why Heavy?")
|
||||
},
|
||||
AllF: func(ctx context.Context) ([]chronograf.User, error) {
|
||||
return []chronograf.User{
|
||||
{
|
||||
Name: "me",
|
||||
Provider: "GitHub",
|
||||
Passwd: "hunter2",
|
||||
},
|
||||
{
|
||||
Name: "billietta",
|
||||
Provider: "Google",
|
||||
Passwd: "billiettaspassword",
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
Logger: log.New(log.DebugLevel),
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue