Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added new ListRolesApi #64

Merged
merged 8 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ COMMIT := $(shell git rev-parse --short HEAD)
TAG := "$(shell git rev-list --tags --max-count=1)"
VERSION := "$(shell git describe --tags ${TAG})-next"
BUILD_DIR=dist
PROTON_COMMIT := "8300157e4ac12a15f0dcc7f02b4b80c3c76be9fe"
PROTON_COMMIT := "5c19c2579d679e2a807dcde2fe4445f79233df91"

.PHONY: all build clean test tidy vet proto setup format generate

Expand Down
16 changes: 16 additions & 0 deletions api/handler/v1beta1/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (s *GRPCServer) ListGrants(ctx context.Context, req *guardianv1beta1.ListGr

func (s *GRPCServer) ListUserGrants(ctx context.Context, req *guardianv1beta1.ListUserGrantsRequest) (*guardianv1beta1.ListUserGrantsResponse, error) {
user, err := s.getUser(ctx)

if err != nil {
return nil, status.Error(codes.Unauthenticated, "failed to get metadata: user")
}
Expand Down Expand Up @@ -244,3 +245,18 @@ func (s *GRPCServer) ImportGrantsFromProvider(ctx context.Context, req *guardian
Grants: grantsProto,
}, nil
}

func (s *GRPCServer) ListUserRoles(ctx context.Context, req *guardianv1beta1.ListUserRolesRequest) (*guardianv1beta1.ListUserRolesResponse, error) {
user, err := s.getUser(ctx)
if err != nil {
return nil, status.Error(codes.Unauthenticated, "failed to get metadata: user")
}

roles, err := s.grantService.ListUserRoles(ctx, user)
if err != nil {
return nil, status.Error(codes.Internal, "Internal Error")
}
return &guardianv1beta1.ListUserRolesResponse{
Roles: roles,
}, nil
}
55 changes: 55 additions & 0 deletions api/handler/v1beta1/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,61 @@ func (s *GrpcHandlersSuite) TestGetGrant() {
})
}

func (s *GrpcHandlersSuite) TestListUserRoles() {
s.Run("should return roles", func() {
s.setup()
expectedResponse := &guardianv1beta1.ListUserRolesResponse{
Roles: []string{
"viewer",
},
}
expectedUser := "test-user"
s.grantService.EXPECT().
ListUserRoles(mock.AnythingOfType("*context.valueCtx"), "test-user").
Return(expectedResponse.Roles, nil).Once()

ctx := context.WithValue(context.Background(), authEmailTestContextKey{}, expectedUser)
req := &guardianv1beta1.ListUserRolesRequest{}
res, err := s.grpcServer.ListUserRoles(ctx, req)

s.Nil(err) // Check that there are no errors.
s.grantService.AssertExpectations(s.T())
s.Equal(expectedResponse.Roles, res.Roles)
s.Equal(codes.OK, status.Code(err))

rahmatrhd marked this conversation as resolved.
Show resolved Hide resolved
})
s.Run("should return unauthenticated user", func() {
s.setup()

s.grantService.EXPECT().
ListUserRoles(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).
Return(nil, nil).Once()

req := &guardianv1beta1.ListUserRolesRequest{}
res, err := s.grpcServer.ListUserRoles(context.Background(), req)

s.Equal(codes.Unauthenticated, status.Code(err))
s.Nil(res)

lifosmin marked this conversation as resolved.
Show resolved Hide resolved
})
s.Run("should return internal error if listroles returns an error", func() {
s.setup()

expectedError := errors.New("random error")
s.grantService.EXPECT().
ListUserRoles(mock.AnythingOfType("*context.valueCtx"), mock.Anything).
Return(nil, expectedError).Once()

req := &guardianv1beta1.ListUserRolesRequest{}
ctx := context.WithValue(context.Background(), authEmailTestContextKey{}, "test-user")
res, err := s.grpcServer.ListUserRoles(ctx, req)

s.Equal(codes.Internal, status.Code(err))
s.Nil(res)
s.grantService.AssertExpectations(s.T())
})
}

func (s *GrpcHandlersSuite) TestUpdateGrant() {
s.Run("should return grant details on succes", func() {
s.setup()
Expand Down
1 change: 1 addition & 0 deletions api/handler/v1beta1/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type approvalService interface {

//go:generate mockery --name=grantService --exported --with-expecter
type grantService interface {
ListUserRoles(context.Context, string) ([]string, error)
GetGrantsTotalCount(context.Context, domain.ListGrantsFilter) (int64, error)
List(context.Context, domain.ListGrantsFilter) ([]domain.Grant, error)
GetByID(context.Context, string) (*domain.Grant, error)
Expand Down
55 changes: 55 additions & 0 deletions api/handler/v1beta1/mocks/grantService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading