From 519f78a3dd3bea0b7368acb5bd721ab2dde59e5f Mon Sep 17 00:00:00 2001 From: Ishan Arya Date: Tue, 18 Jun 2024 19:04:49 +0530 Subject: [PATCH] test: fix --- internal/api/v1beta1/group.go | 14 ++ internal/api/v1beta1/group_test.go | 179 ++++++++++++------ .../api/v1beta1/mocks/relation_service.go | 80 ++++++-- internal/api/v1beta1/servicedata_test.go | 111 ++++++----- internal/api/v1beta1/user.go | 1 + 5 files changed, 265 insertions(+), 120 deletions(-) diff --git a/internal/api/v1beta1/group.go b/internal/api/v1beta1/group.go index 6cc531b6f..8868a9f13 100644 --- a/internal/api/v1beta1/group.go +++ b/internal/api/v1beta1/group.go @@ -39,10 +39,22 @@ type GroupService interface { } var grpcGroupNotFoundErr = status.Errorf(codes.NotFound, "group doesn't exist") +var grpcInvalidOrgIDErr = status.Errorf(codes.InvalidArgument, "ordIs is not valid uuid") func (h Handler) ListGroups(ctx context.Context, request *shieldv1beta1.ListGroupsRequest) (*shieldv1beta1.ListGroupsResponse, error) { logger := grpczap.Extract(ctx) + if request.GetOrgId() != "" { + if !uuid.IsValid(request.GetOrgId()) { + return nil, grpcInvalidOrgIDErr + } + + _, err := h.orgService.Get(ctx, request.GetOrgId()) + if err != nil { + return &shieldv1beta1.ListGroupsResponse{Groups: nil}, nil + } + } + var groups []*shieldv1beta1.Group currentUser, err := h.userService.FetchCurrentUser(ctx) @@ -99,6 +111,7 @@ func (h Handler) CreateGroup(ctx context.Context, request *shieldv1beta1.CreateG return nil, grpcInternalServerError } + //TODO: change this metaDataMap, err := metadata.Build(request.GetBody().GetMetadata().AsMap()) if err != nil { logger.Error(err.Error()) @@ -227,6 +240,7 @@ func (h Handler) UpdateGroup(ctx context.Context, request *shieldv1beta1.UpdateG return nil, grpcBadBodyError } + //TODO: change this implementation metaDataMap, err := metadata.Build(request.GetBody().GetMetadata().AsMap()) if err != nil { return nil, grpcBadBodyError diff --git a/internal/api/v1beta1/group_test.go b/internal/api/v1beta1/group_test.go index 20fdd5705..575536b49 100644 --- a/internal/api/v1beta1/group_test.go +++ b/internal/api/v1beta1/group_test.go @@ -7,6 +7,7 @@ import ( "github.com/goto/shield/core/group" "github.com/goto/shield/core/organization" + "github.com/goto/shield/core/project" "github.com/goto/shield/core/servicedata" "github.com/goto/shield/core/user" "github.com/goto/shield/internal/api/v1beta1/mocks" @@ -106,32 +107,25 @@ func TestHandler_ListGroups(t *testing.T) { randomID := uuid.NewString() tests := []struct { name string - setup func(gs *mocks.GroupService) + setup func(gs *mocks.GroupService, os *mocks.OrganizationService, ps *mocks.ProjectService, us *mocks.UserService, rs *mocks.RelationService) request *shieldv1beta1.ListGroupsRequest want *shieldv1beta1.ListGroupsResponse wantErr error }{ { name: "should return empty groups if query param org_id is not uuid", - setup: func(gs *mocks.GroupService) { - gs.EXPECT().List(mock.AnythingOfType("context.todoCtx"), group.Filter{ - OrganizationID: "some-id", - }).Return([]group.Group{}, nil) + setup: func(gs *mocks.GroupService, os *mocks.OrganizationService, ps *mocks.ProjectService, us *mocks.UserService, rs *mocks.RelationService) { }, request: &shieldv1beta1.ListGroupsRequest{ OrgId: "some-id", }, - want: &shieldv1beta1.ListGroupsResponse{ - Groups: nil, - }, - wantErr: nil, + want: nil, + wantErr: grpcInvalidOrgIDErr, }, { name: "should return empty groups if query param org_id is not exist", - setup: func(gs *mocks.GroupService) { - gs.EXPECT().List(mock.AnythingOfType("context.todoCtx"), group.Filter{ - OrganizationID: randomID, - }).Return([]group.Group{}, nil) + setup: func(gs *mocks.GroupService, os *mocks.OrganizationService, ps *mocks.ProjectService, us *mocks.UserService, rs *mocks.RelationService) { + os.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), randomID).Return(organization.Organization{}, errors.New("some error")) }, request: &shieldv1beta1.ListGroupsRequest{ OrgId: randomID, @@ -143,12 +137,29 @@ func TestHandler_ListGroups(t *testing.T) { }, { name: "should return all groups if no query param filter exist", - setup: func(gs *mocks.GroupService) { + setup: func(gs *mocks.GroupService, os *mocks.OrganizationService, ps *mocks.ProjectService, us *mocks.UserService, rs *mocks.RelationService) { var testGroupList []group.Group for _, u := range testGroupMap { testGroupList = append(testGroupList, u) } - gs.EXPECT().List(mock.AnythingOfType("context.todoCtx"), group.Filter{}).Return(testGroupList, nil) + os.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), "some-id").Return(organization.Organization{}, errors.New("some error")) + + us.EXPECT().FetchCurrentUser(mock.AnythingOfType("context.todoCtx")).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) + + rs.EXPECT().LookupResources(mock.AnythingOfType("context.todoCtx"), schema.ServiceDataKeyNamespace, schema.ViewPermission, schema.UserPrincipal, "083a77a2-ab14-40d2-a06d-f6d9f80c6378").Return([]string{}, nil) + + ps.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), "").Return(project.Project{ + Name: "system", + Slug: "system", + ID: "78849300-1146-4875-9cce-67ba353ed97e", + }, nil) + + gs.EXPECT().List(mock.AnythingOfType("context.todoCtx"), group.Filter{ + ProjectID: "78849300-1146-4875-9cce-67ba353ed97e", + ServicedataKeyResourceIDs: []string{}, + }).Return(testGroupList, nil) }, request: &shieldv1beta1.ListGroupsRequest{}, want: &shieldv1beta1.ListGroupsResponse{ @@ -172,14 +183,35 @@ func TestHandler_ListGroups(t *testing.T) { }, { name: "should return filtered groups if query param org_id exist", - setup: func(gs *mocks.GroupService) { + setup: func(gs *mocks.GroupService, os *mocks.OrganizationService, ps *mocks.ProjectService, us *mocks.UserService, rs *mocks.RelationService) { var testGroupList []group.Group for _, u := range testGroupMap { testGroupList = append(testGroupList, u) } + + os.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), "9f256f86-31a3-11ec-8d3d-0242ac130003").Return(organization.Organization{ + ID: "9f256f86-31a3-11ec-8d3d-0242ac130003", + }, nil) + + us.EXPECT().FetchCurrentUser(mock.AnythingOfType("context.todoCtx")).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) + + rs.EXPECT().LookupResources(mock.AnythingOfType("context.todoCtx"), schema.ServiceDataKeyNamespace, schema.ViewPermission, schema.UserPrincipal, "083a77a2-ab14-40d2-a06d-f6d9f80c6378").Return([]string{}, nil) + + ps.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), "").Return(project.Project{ + Name: "system", + Slug: "system", + ID: "78849300-1146-4875-9cce-67ba353ed97e", + }, nil) + gs.EXPECT().List(mock.AnythingOfType("context.todoCtx"), group.Filter{ - OrganizationID: "9f256f86-31a3-11ec-8d3d-0242ac130003", + OrganizationID: "9f256f86-31a3-11ec-8d3d-0242ac130003", + ProjectID: "78849300-1146-4875-9cce-67ba353ed97e", + ServicedataKeyResourceIDs: []string{}, }).Return(testGroupList, nil) + + gs.EXPECT().List(mock.AnythingOfType("context.todoCtx"), group.Filter{}).Return(testGroupList, nil) }, request: &shieldv1beta1.ListGroupsRequest{ OrgId: "9f256f86-31a3-11ec-8d3d-0242ac130003", @@ -207,11 +239,19 @@ func TestHandler_ListGroups(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mockGroupSvc := new(mocks.GroupService) + mockOrgSvc := new(mocks.OrganizationService) + mockProjectSvc := new(mocks.ProjectService) + mockUserSvc := new(mocks.UserService) + mockRelationSvc := new(mocks.RelationService) if tt.setup != nil { - tt.setup(mockGroupSvc) + tt.setup(mockGroupSvc, mockOrgSvc, mockProjectSvc, mockUserSvc, mockRelationSvc) } h := Handler{ - groupService: mockGroupSvc, + groupService: mockGroupSvc, + orgService: mockOrgSvc, + projectService: mockProjectSvc, + userService: mockUserSvc, + relationService: mockRelationSvc, } got, err := h.ListGroups(context.TODO(), tt.request) assert.EqualValues(t, got, tt.want) @@ -226,20 +266,23 @@ func TestHandler_CreateGroup(t *testing.T) { someGroupID := uuid.NewString() tests := []struct { name string - setup func(ctx context.Context, gs *mocks.GroupService) context.Context + setup func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context request *shieldv1beta1.CreateGroupRequest want *shieldv1beta1.CreateGroupResponse wantErr error }{ { name: "should return unauthenticated error if auth email in context is empty and group service return invalid user email", - setup: func(ctx context.Context, gs *mocks.GroupService) context.Context { + setup: func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context { + us.EXPECT().FetchCurrentUser(mock.AnythingOfType("context.todoCtx")).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) gs.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), group.Group{ Name: "some group", Slug: "some-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, user.ErrInvalidEmail) return ctx }, @@ -253,15 +296,19 @@ func TestHandler_CreateGroup(t *testing.T) { }, { name: "should return internal error if group service return some error", - setup: func(ctx context.Context, gs *mocks.GroupService) context.Context { + setup: func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context { + ctx = user.SetContextWithEmail(ctx, email) + us.EXPECT().FetchCurrentUser(ctx).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) gs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), group.Group{ Name: "some group", Slug: "some-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, errors.New("some error")) - return user.SetContextWithEmail(ctx, email) + return ctx }, request: &shieldv1beta1.CreateGroupRequest{Body: &shieldv1beta1.GroupRequestBody{ Name: "some group", @@ -273,15 +320,19 @@ func TestHandler_CreateGroup(t *testing.T) { }, { name: "should return already exist error if group service return error conflict", - setup: func(ctx context.Context, gs *mocks.GroupService) context.Context { + setup: func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context { + ctx = user.SetContextWithEmail(ctx, email) + us.EXPECT().FetchCurrentUser(ctx).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) gs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), group.Group{ Name: "some group", Slug: "some-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrConflict) - return user.SetContextWithEmail(ctx, email) + return ctx }, request: &shieldv1beta1.CreateGroupRequest{Body: &shieldv1beta1.GroupRequestBody{ Name: "some group", @@ -294,14 +345,18 @@ func TestHandler_CreateGroup(t *testing.T) { }, { name: "should return bad request error if name empty", - setup: func(ctx context.Context, gs *mocks.GroupService) context.Context { + setup: func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context { + ctx = user.SetContextWithEmail(ctx, email) + us.EXPECT().FetchCurrentUser(ctx).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) gs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), group.Group{ Slug: "some-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrInvalidDetail) - return user.SetContextWithEmail(ctx, email) + return ctx }, request: &shieldv1beta1.CreateGroupRequest{Body: &shieldv1beta1.GroupRequestBody{ Slug: "some-group", @@ -313,14 +368,18 @@ func TestHandler_CreateGroup(t *testing.T) { }, { name: "should return bad request error if org id is not uuid", - setup: func(ctx context.Context, gs *mocks.GroupService) context.Context { + setup: func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context { + ctx = user.SetContextWithEmail(ctx, email) + us.EXPECT().FetchCurrentUser(ctx).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) gs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), group.Group{ Name: "some group", Slug: "some-group", OrganizationID: "some-org-id", - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, organization.ErrInvalidUUID) - return user.SetContextWithEmail(ctx, email) + return ctx }, request: &shieldv1beta1.CreateGroupRequest{Body: &shieldv1beta1.GroupRequestBody{ Name: "some group", @@ -333,15 +392,19 @@ func TestHandler_CreateGroup(t *testing.T) { }, { name: "should return bad request error if org id not exist", - setup: func(ctx context.Context, gs *mocks.GroupService) context.Context { + setup: func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context { + ctx = user.SetContextWithEmail(ctx, email) + us.EXPECT().FetchCurrentUser(ctx).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) gs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), group.Group{ Name: "some group", Slug: "some-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, organization.ErrNotExist) - return user.SetContextWithEmail(ctx, email) + return ctx }, request: &shieldv1beta1.CreateGroupRequest{Body: &shieldv1beta1.GroupRequestBody{ Name: "some group", @@ -360,13 +423,17 @@ func TestHandler_CreateGroup(t *testing.T) { }, { name: "should return success if group service return nil", - setup: func(ctx context.Context, gs *mocks.GroupService) context.Context { + setup: func(ctx context.Context, gs *mocks.GroupService, us *mocks.UserService) context.Context { + ctx = user.SetContextWithEmail(ctx, email) + us.EXPECT().FetchCurrentUser(ctx).Return(user.User{ + ID: "083a77a2-ab14-40d2-a06d-f6d9f80c6378", + }, nil) gs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), group.Group{ Name: "some group", Slug: "some-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{ ID: someGroupID, Name: "some group", @@ -375,7 +442,7 @@ func TestHandler_CreateGroup(t *testing.T) { OrganizationID: someOrgID, Metadata: metadata.Metadata{}, }, nil) - return user.SetContextWithEmail(ctx, email) + return ctx }, request: &shieldv1beta1.CreateGroupRequest{Body: &shieldv1beta1.GroupRequestBody{ Name: "some group", @@ -401,12 +468,14 @@ func TestHandler_CreateGroup(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mockGroupSvc := new(mocks.GroupService) + mockUserSvc := new(mocks.UserService) ctx := context.TODO() if tt.setup != nil { - ctx = tt.setup(ctx, mockGroupSvc) + ctx = tt.setup(ctx, mockGroupSvc, mockUserSvc) } h := Handler{ groupService: mockGroupSvc, + userService: mockUserSvc, } got, err := h.CreateGroup(ctx, tt.request) assert.EqualValues(t, got, tt.want) @@ -464,7 +533,7 @@ func TestHandler_GetGroup(t *testing.T) { }), }).Return([]servicedata.ServiceData{{ Key: servicedata.Key{ - Key: "foo", + Name: "foo", }, Value: "bar", }}, nil) @@ -525,7 +594,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "new-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, errors.New("some error")) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -556,7 +625,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "some-id", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrNotExist) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -579,7 +648,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "new-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrNotExist) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -601,7 +670,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "", // consider it by slug and make the slug empty OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrInvalidID) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -623,7 +692,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "new-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrConflict) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -646,7 +715,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "new-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, organization.ErrNotExist) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -669,7 +738,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "new-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, organization.ErrInvalidUUID) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -691,7 +760,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "new-group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrInvalidDetail) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -712,7 +781,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Name: "new group", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{}, group.ErrInvalidDetail) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -733,15 +802,13 @@ func TestHandler_UpdateGroup(t *testing.T) { Name: "new group", Slug: "new-group", OrganizationID: someOrgID, - - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{ ID: someGroupID, Name: "new group", Slug: "new-group", OrganizationID: someOrgID, - - Metadata: metadata.Metadata{}, + Metadata: metadata.Metadata{}, }, nil) }, request: &shieldv1beta1.UpdateGroupRequest{ @@ -775,7 +842,7 @@ func TestHandler_UpdateGroup(t *testing.T) { Slug: "some-slug", OrganizationID: someOrgID, - Metadata: metadata.Metadata{}, + Metadata: nil, }).Return(group.Group{ ID: someGroupID, Name: "new group", diff --git a/internal/api/v1beta1/mocks/relation_service.go b/internal/api/v1beta1/mocks/relation_service.go index ce7bf9235..2c5d61c9a 100644 --- a/internal/api/v1beta1/mocks/relation_service.go +++ b/internal/api/v1beta1/mocks/relation_service.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.2. DO NOT EDIT. +// Code generated by mockery v2.32.0. DO NOT EDIT. package mocks @@ -26,10 +26,6 @@ func (_m *RelationService) EXPECT() *RelationService_Expecter { func (_m *RelationService) Create(ctx context.Context, rel relation.RelationV2) (relation.RelationV2, error) { ret := _m.Called(ctx, rel) - if len(ret) == 0 { - panic("no return value specified for Create") - } - var r0 relation.RelationV2 var r1 error if rf, ok := ret.Get(0).(func(context.Context, relation.RelationV2) (relation.RelationV2, error)); ok { @@ -83,10 +79,6 @@ func (_c *RelationService_Create_Call) RunAndReturn(run func(context.Context, re func (_m *RelationService) DeleteV2(ctx context.Context, rel relation.RelationV2) error { ret := _m.Called(ctx, rel) - if len(ret) == 0 { - panic("no return value specified for DeleteV2") - } - var r0 error if rf, ok := ret.Get(0).(func(context.Context, relation.RelationV2) error); ok { r0 = rf(ctx, rel) @@ -130,10 +122,6 @@ func (_c *RelationService_DeleteV2_Call) RunAndReturn(run func(context.Context, func (_m *RelationService) Get(ctx context.Context, id string) (relation.RelationV2, error) { ret := _m.Called(ctx, id) - if len(ret) == 0 { - panic("no return value specified for Get") - } - var r0 relation.RelationV2 var r1 error if rf, ok := ret.Get(0).(func(context.Context, string) (relation.RelationV2, error)); ok { @@ -187,10 +175,6 @@ func (_c *RelationService_Get_Call) RunAndReturn(run func(context.Context, strin func (_m *RelationService) GetRelationByFields(ctx context.Context, rel relation.RelationV2) (relation.RelationV2, error) { ret := _m.Called(ctx, rel) - if len(ret) == 0 { - panic("no return value specified for GetRelationByFields") - } - var r0 relation.RelationV2 var r1 error if rf, ok := ret.Get(0).(func(context.Context, relation.RelationV2) (relation.RelationV2, error)); ok { @@ -244,10 +228,6 @@ func (_c *RelationService_GetRelationByFields_Call) RunAndReturn(run func(contex func (_m *RelationService) List(ctx context.Context) ([]relation.RelationV2, error) { ret := _m.Called(ctx) - if len(ret) == 0 { - panic("no return value specified for List") - } - var r0 []relation.RelationV2 var r1 error if rf, ok := ret.Get(0).(func(context.Context) ([]relation.RelationV2, error)); ok { @@ -298,6 +278,64 @@ func (_c *RelationService_List_Call) RunAndReturn(run func(context.Context) ([]r return _c } +// LookupResources provides a mock function with given fields: ctx, resourceType, permission, subjectType, subjectID +func (_m *RelationService) LookupResources(ctx context.Context, resourceType string, permission string, subjectType string, subjectID string) ([]string, error) { + ret := _m.Called(ctx, resourceType, permission, subjectType, subjectID) + + var r0 []string + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string) ([]string, error)); ok { + return rf(ctx, resourceType, permission, subjectType, subjectID) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string) []string); ok { + r0 = rf(ctx, resourceType, permission, subjectType, subjectID) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, string, string) error); ok { + r1 = rf(ctx, resourceType, permission, subjectType, subjectID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// RelationService_LookupResources_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'LookupResources' +type RelationService_LookupResources_Call struct { + *mock.Call +} + +// LookupResources is a helper method to define mock.On call +// - ctx context.Context +// - resourceType string +// - permission string +// - subjectType string +// - subjectID string +func (_e *RelationService_Expecter) LookupResources(ctx interface{}, resourceType interface{}, permission interface{}, subjectType interface{}, subjectID interface{}) *RelationService_LookupResources_Call { + return &RelationService_LookupResources_Call{Call: _e.mock.On("LookupResources", ctx, resourceType, permission, subjectType, subjectID)} +} + +func (_c *RelationService_LookupResources_Call) Run(run func(ctx context.Context, resourceType string, permission string, subjectType string, subjectID string)) *RelationService_LookupResources_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(string), args[4].(string)) + }) + return _c +} + +func (_c *RelationService_LookupResources_Call) Return(_a0 []string, _a1 error) *RelationService_LookupResources_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *RelationService_LookupResources_Call) RunAndReturn(run func(context.Context, string, string, string, string) ([]string, error)) *RelationService_LookupResources_Call { + _c.Call.Return(run) + return _c +} + // NewRelationService creates a new instance of RelationService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewRelationService(t interface { diff --git a/internal/api/v1beta1/servicedata_test.go b/internal/api/v1beta1/servicedata_test.go index 4d0d99195..a425331b9 100644 --- a/internal/api/v1beta1/servicedata_test.go +++ b/internal/api/v1beta1/servicedata_test.go @@ -14,6 +14,7 @@ import ( shieldv1beta1 "github.com/goto/shield/proto/v1beta1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "google.golang.org/protobuf/types/known/structpb" ) var ( @@ -27,7 +28,7 @@ var ( ID: testKeyID, URN: "key-urn", ProjectID: testKeyProjectID, - Key: testKeyName, + Name: testKeyName, Description: "test description", ResourceID: testKeyResourceID, } @@ -39,7 +40,7 @@ var ( EntityID: testEntityID, NamespaceID: userNamespaceID, Key: servicedata.Key{ - Key: testKeyName, + Name: testKeyName, ProjectID: testKeyProjectID, }, Value: testValue, @@ -48,7 +49,7 @@ var ( EntityID: testEntityID, NamespaceID: groupNamespaceID, Key: servicedata.Key{ - Key: testKeyName, + Name: testKeyName, ProjectID: testKeyProjectID, }, Value: testValue, @@ -87,7 +88,7 @@ func TestHandler_CreateKey(t *testing.T) { setup: func(ctx context.Context, ss *mocks.ServiceDataService) context.Context { ss.EXPECT().CreateKey(mock.AnythingOfType("*context.valueCtx"), servicedata.Key{ ProjectID: "non-existing-project", - Key: testKey.Key, + Name: testKey.Name, Description: testKey.Description, }).Return(servicedata.Key{}, project.ErrNotExist) return user.SetContextWithEmail(ctx, email) @@ -95,7 +96,7 @@ func TestHandler_CreateKey(t *testing.T) { request: &shieldv1beta1.CreateServiceDataKeyRequest{ Body: &shieldv1beta1.ServiceDataKeyRequestBody{ Project: "non-existing-project", - Key: testKey.Key, + Key: testKey.Name, Description: testKey.Description, }, }, @@ -107,7 +108,7 @@ func TestHandler_CreateKey(t *testing.T) { setup: func(ctx context.Context, ss *mocks.ServiceDataService) context.Context { ss.EXPECT().CreateKey(mock.AnythingOfType("*context.valueCtx"), servicedata.Key{ ProjectID: testKey.ProjectID, - Key: testKey.Key, + Name: testKey.Name, Description: testKey.Description, }).Return(servicedata.Key{}, servicedata.ErrConflict) return user.SetContextWithEmail(ctx, email) @@ -115,7 +116,7 @@ func TestHandler_CreateKey(t *testing.T) { request: &shieldv1beta1.CreateServiceDataKeyRequest{ Body: &shieldv1beta1.ServiceDataKeyRequestBody{ Project: testKey.ProjectID, - Key: testKey.Key, + Key: testKey.Name, Description: testKey.Description, }, }, @@ -127,7 +128,7 @@ func TestHandler_CreateKey(t *testing.T) { setup: func(ctx context.Context, ss *mocks.ServiceDataService) context.Context { ss.EXPECT().CreateKey(mock.AnythingOfType("*context.valueCtx"), servicedata.Key{ ProjectID: testKey.ProjectID, - Key: testKey.Key, + Name: testKey.Name, Description: testKey.Description, }).Return(servicedata.Key{}, relation.ErrInvalidDetail) return user.SetContextWithEmail(ctx, email) @@ -135,7 +136,7 @@ func TestHandler_CreateKey(t *testing.T) { request: &shieldv1beta1.CreateServiceDataKeyRequest{ Body: &shieldv1beta1.ServiceDataKeyRequestBody{ Project: testKey.ProjectID, - Key: testKey.Key, + Key: testKey.Name, Description: testKey.Description, }, }, @@ -147,7 +148,7 @@ func TestHandler_CreateKey(t *testing.T) { setup: func(ctx context.Context, ss *mocks.ServiceDataService) context.Context { ss.EXPECT().CreateKey(mock.AnythingOfType("*context.valueCtx"), servicedata.Key{ ProjectID: testKey.ProjectID, - Key: testKey.Key, + Name: testKey.Name, Description: testKey.Description, }).Return(servicedata.Key{}, servicedata.ErrInvalidDetail) return user.SetContextWithEmail(ctx, email) @@ -155,7 +156,7 @@ func TestHandler_CreateKey(t *testing.T) { request: &shieldv1beta1.CreateServiceDataKeyRequest{ Body: &shieldv1beta1.ServiceDataKeyRequestBody{ Project: testKey.ProjectID, - Key: testKey.Key, + Key: testKey.Name, Description: testKey.Description, }, }, @@ -167,7 +168,7 @@ func TestHandler_CreateKey(t *testing.T) { setup: func(ctx context.Context, ss *mocks.ServiceDataService) context.Context { ss.EXPECT().CreateKey(mock.AnythingOfType("*context.valueCtx"), servicedata.Key{ ProjectID: testKey.ProjectID, - Key: testKey.Key, + Name: testKey.Name, Description: testKey.Description, }).Return(servicedata.Key{ ID: testKey.ID, @@ -178,7 +179,7 @@ func TestHandler_CreateKey(t *testing.T) { request: &shieldv1beta1.CreateServiceDataKeyRequest{ Body: &shieldv1beta1.ServiceDataKeyRequestBody{ Project: testKey.ProjectID, - Key: testKey.Key, + Key: testKey.Name, Description: testKey.Description, }, }, @@ -232,7 +233,7 @@ func TestHandler_UpdateUserServiceData(t *testing.T) { request: &shieldv1beta1.UpsertUserServiceDataRequest{ UserId: "", Body: &shieldv1beta1.UpsertServiceDataRequestBody{ - Data: map[string]string{}, + Data: &structpb.Struct{}, }, }, want: nil, @@ -243,9 +244,11 @@ func TestHandler_UpdateUserServiceData(t *testing.T) { request: &shieldv1beta1.UpsertUserServiceDataRequest{ UserId: "", Body: &shieldv1beta1.UpsertServiceDataRequestBody{ - Data: map[string]string{ - "test-key-1": "test-value-1", - "test-key-2": "test-value-2", + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "test-key-1": structpb.NewStringValue("test-value-1"), + "test-key-2": structpb.NewStringValue("test-value-2"), + }, }, }, }, @@ -257,8 +260,10 @@ func TestHandler_UpdateUserServiceData(t *testing.T) { request: &shieldv1beta1.UpsertUserServiceDataRequest{ UserId: "", Body: &shieldv1beta1.UpsertServiceDataRequestBody{ - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, @@ -275,8 +280,10 @@ func TestHandler_UpdateUserServiceData(t *testing.T) { UserId: testEntityID, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: testKeyProjectID, - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, @@ -294,8 +301,10 @@ func TestHandler_UpdateUserServiceData(t *testing.T) { UserId: testEntityID, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: testKeyProjectID, - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, @@ -313,22 +322,26 @@ func TestHandler_UpdateUserServiceData(t *testing.T) { UserId: testEntityID, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: testKeyProjectID, - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, setup: func(ctx context.Context, ss *mocks.ServiceDataService, us *mocks.UserService) context.Context { us.EXPECT().Get(mock.AnythingOfType("*context.valueCtx"), testEntityID).Return(user.User{ID: testEntityID}, nil) ss.EXPECT().Upsert(mock.AnythingOfType("*context.valueCtx"), testUserServiceDataCreate).Return(servicedata.ServiceData{ - Key: servicedata.Key{Key: testKeyName}, + Key: servicedata.Key{Name: testKeyName}, Value: testValue, }, nil) return user.SetContextWithEmail(ctx, email) }, want: &shieldv1beta1.UpsertUserServiceDataResponse{ - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, wantErr: nil, @@ -379,7 +392,7 @@ func TestHandler_UpdateGroupServiceData(t *testing.T) { request: &shieldv1beta1.UpsertGroupServiceDataRequest{ GroupId: "", Body: &shieldv1beta1.UpsertServiceDataRequestBody{ - Data: map[string]string{}, + Data: &structpb.Struct{}, }, }, want: nil, @@ -390,9 +403,11 @@ func TestHandler_UpdateGroupServiceData(t *testing.T) { request: &shieldv1beta1.UpsertGroupServiceDataRequest{ GroupId: "", Body: &shieldv1beta1.UpsertServiceDataRequestBody{ - Data: map[string]string{ - "test-key-1": "test-value-1", - "test-key-2": "test-value-2", + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "test-key-1": structpb.NewStringValue("test-value-1"), + "test-key-2": structpb.NewStringValue("test-value-2"), + }, }, }, }, @@ -404,8 +419,10 @@ func TestHandler_UpdateGroupServiceData(t *testing.T) { request: &shieldv1beta1.UpsertGroupServiceDataRequest{ GroupId: testEntityID, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, @@ -422,8 +439,10 @@ func TestHandler_UpdateGroupServiceData(t *testing.T) { GroupId: testEntityID, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: testKeyProjectID, - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, @@ -441,8 +460,10 @@ func TestHandler_UpdateGroupServiceData(t *testing.T) { GroupId: testEntityID, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: testKeyProjectID, - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, @@ -460,22 +481,26 @@ func TestHandler_UpdateGroupServiceData(t *testing.T) { GroupId: testEntityID, Body: &shieldv1beta1.UpsertServiceDataRequestBody{ Project: testKeyProjectID, - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, }, setup: func(ctx context.Context, ss *mocks.ServiceDataService, gs *mocks.GroupService) context.Context { gs.EXPECT().Get(mock.AnythingOfType("*context.valueCtx"), testEntityID).Return(group.Group{ID: testEntityID}, nil) ss.EXPECT().Upsert(mock.AnythingOfType("*context.valueCtx"), testGroupServiceDataCreate).Return(servicedata.ServiceData{ - Key: servicedata.Key{Key: testKeyName}, + Key: servicedata.Key{Name: testKeyName}, Value: testValue, }, nil) return user.SetContextWithEmail(ctx, email) }, want: &shieldv1beta1.UpsertGroupServiceDataResponse{ - Data: map[string]string{ - testKeyName: testValue, + Data: &structpb.Struct{ + Fields: map[string]*structpb.Value{ + testKeyName: structpb.NewStringValue(testValue), + }, }, }, wantErr: nil, diff --git a/internal/api/v1beta1/user.go b/internal/api/v1beta1/user.go index 0fa5b5069..a811f3b66 100644 --- a/internal/api/v1beta1/user.go +++ b/internal/api/v1beta1/user.go @@ -174,6 +174,7 @@ func (h Handler) CreateUser(ctx context.Context, request *shieldv1beta1.CreateUs serviceDataMap[serviceDataResp.Key.Name] = serviceDataResp.Value } + //TODO: use serviceDataMap newUser.Metadata = metaDataMap userPB, err := transformUserToPB(newUser)