From 435125a81c8f303db64e83a39b33dddba3403262 Mon Sep 17 00:00:00 2001 From: Femi Novia Lina Date: Fri, 19 Jul 2024 15:45:22 +0700 Subject: [PATCH] test: add test --- core/relation/service_test.go | 135 ++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/core/relation/service_test.go b/core/relation/service_test.go index ef0b13ebf..5a3ef31bc 100644 --- a/core/relation/service_test.go +++ b/core/relation/service_test.go @@ -43,6 +43,7 @@ var ( } testUserID = uuid.NewString() auditKeyRelationCreate = "relation.create" + testResourceID = uuid.NewString() ) func TestService_Get(t *testing.T) { @@ -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() @@ -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) + }) + } +}