Skip to content

Commit

Permalink
feat: Added new ListRolesApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifosmin Simon authored and Lifosmin Simon committed Sep 1, 2023
1 parent 1740dd4 commit 030da00
Show file tree
Hide file tree
Showing 14 changed files with 1,700 additions and 1,208 deletions.
Binary file added __debug_bin867358792
Binary file not shown.
18 changes: 18 additions & 0 deletions api/handler/v1beta1/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,21 @@ 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")
}

filter := domain.ListGrantsFilter{
Owner: user,
}
roles, err := s.grantService.ListUserRoles(ctx, filter)
if err != nil {
return nil, err
}
return &guardianv1beta1.ListUserRolesResponse{
Roles: roles,
}, nil
}
57 changes: 57 additions & 0 deletions api/handler/v1beta1/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,63 @@ 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"), domain.ListGrantsFilter{
Owner: "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.

// Assert that the response's Roles field matches the expected roles.
// You can use reflect.DeepEqual or other comparison methods.
s.Equal(expectedResponse.Roles, res.Roles)

})
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, _ := s.grpcServer.ListUserRoles(context.Background(), req)

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, _ := 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, domain.ListGrantsFilter) ([]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 030da00

Please sign in to comment.