Skip to content

Commit

Permalink
feat: Added new ListRolesApi (#64)
Browse files Browse the repository at this point in the history
* feat: Added new ListRolesApi

* chore: Changed input and created error handling when receiving id

* test: fix

* test: fix testing

* fix: testing

* fix: makefile

---------

Co-authored-by: Lifosmin Simon <[email protected]>
  • Loading branch information
lifosmin and Lifosmin Simon authored Sep 12, 2023
1 parent 5bb71fc commit b7406c9
Show file tree
Hide file tree
Showing 14 changed files with 1,723 additions and 1,209 deletions.
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))

})
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)

})
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

0 comments on commit b7406c9

Please sign in to comment.