Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina committed Jul 19, 2024
1 parent bafc8f7 commit 435125a
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions core/relation/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
}
testUserID = uuid.NewString()
auditKeyRelationCreate = "relation.create"
testResourceID = uuid.NewString()
)

func TestService_Get(t *testing.T) {
Expand Down Expand Up @@ -266,6 +267,67 @@ func TestService_List(t *testing.T) {
}
}

func TestService_GetRelationByField(t *testing.T) {
t.Parallel()

tests := []struct {
name string
rel relation.RelationV2
setup func(t *testing.T) *relation.Service
want relation.RelationV2
wantErr error
}{
{
name: "GetRelationByFieldSuccess",
rel: testRelationV2,
setup: func(t *testing.T) *relation.Service {
t.Helper()
repository := &mocks.Repository{}
authzRepository := &mocks.AuthzRepository{}
userService := &mocks.UserService{}
activityService := &mocks.ActivityService{}
repository.EXPECT().GetByFields(mock.Anything, testRelationV2).Return(testRelationV2, nil)
return relation.NewService(testLogger, repository, authzRepository, userService, activityService)
},
want: testRelationV2,
wantErr: nil,
},
{
name: "GetRelationByFieldErr",
rel: testRelationV2,
setup: func(t *testing.T) *relation.Service {
t.Helper()
repository := &mocks.Repository{}
authzRepository := &mocks.AuthzRepository{}
userService := &mocks.UserService{}
activityService := &mocks.ActivityService{}
repository.EXPECT().GetByFields(mock.Anything, testRelationV2).Return(relation.RelationV2{}, relation.ErrInvalidDetail)
return relation.NewService(testLogger, repository, authzRepository, userService, activityService)
},
wantErr: relation.ErrInvalidDetail,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

assert.NotNil(t, svc)

got, err := svc.GetRelationByFields(context.TODO(), tt.rel)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}

func TestService_CheckPermission(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -413,3 +475,76 @@ func TestService_BulkCheckPermission(t *testing.T) {
})
}
}

func TestService_LookupResources(t *testing.T) {
t.Parallel()

tests := []struct {
name string
resourceType string
permission string
subjectType string
subjectID string
setup func(t *testing.T) *relation.Service
want []string
wantErr error
}{
{
name: "LookupResourceSuccess",
resourceType: testRelation.ObjectNamespace.ID,
permission: testAction.ID,
subjectType: testRelation.SubjectNamespace.ID,
subjectID: testRelation.SubjectID,
setup: func(t *testing.T) *relation.Service {
t.Helper()
repository := &mocks.Repository{}
authzRepository := &mocks.AuthzRepository{}
userService := &mocks.UserService{}
activityService := &mocks.ActivityService{}
authzRepository.EXPECT().LookupResources(mock.Anything, testRelation.ObjectNamespace.ID, testAction.ID, testRelation.SubjectNamespace.ID, testRelation.SubjectID).
Return([]string{testResourceID}, nil)
return relation.NewService(testLogger, repository, authzRepository, userService, activityService)
},
want: []string{testResourceID},
wantErr: nil,
},
{
name: "LookupResourceErr",
resourceType: testRelation.ObjectNamespace.ID,
permission: testAction.ID,
subjectType: testRelation.SubjectNamespace.ID,
subjectID: testRelation.SubjectID,
setup: func(t *testing.T) *relation.Service {
t.Helper()
repository := &mocks.Repository{}
authzRepository := &mocks.AuthzRepository{}
userService := &mocks.UserService{}
activityService := &mocks.ActivityService{}
authzRepository.EXPECT().LookupResources(mock.Anything, testRelation.ObjectNamespace.ID, testAction.ID, testRelation.SubjectNamespace.ID, testRelation.SubjectID).
Return([]string{}, relation.ErrInvalidDetail)
return relation.NewService(testLogger, repository, authzRepository, userService, activityService)
},
want: []string{},
wantErr: relation.ErrInvalidDetail,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

assert.NotNil(t, svc)

got, err := svc.LookupResources(context.TODO(), tt.resourceType, tt.permission, tt.subjectType, tt.subjectID)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 435125a

Please sign in to comment.