diff --git a/model/diff/diff.go b/model/diff/diff.go index 0b5445f..82d0b89 100644 --- a/model/diff/diff.go +++ b/model/diff/diff.go @@ -1,8 +1,6 @@ package diff import ( - "context" - "github.com/aserto-dev/go-directory/pkg/derr" "github.com/hashicorp/go-multierror" "github.com/pkg/errors" @@ -21,27 +19,27 @@ type Changes struct { } type Instances interface { - ObjectsExist(ctx context.Context, objectType string) (bool, error) - RelationsExist(ctx context.Context, objectType, relationName string) (bool, error) + ObjectsExist(objectType string) (bool, error) + RelationsExist(objectType, relationName string) (bool, error) } -func (d *Diff) Validate(ctx context.Context, dv Instances) error { +func (d *Diff) Validate(dv Instances) error { var errs error - if err := d.validateObjectTypes(ctx, dv); err != nil { + if err := d.validateObjectTypes(dv); err != nil { errs = multierror.Append(errs, err) } - if err := d.validateRelationsTypes(ctx, dv); err != nil { + if err := d.validateRelationsTypes(dv); err != nil { errs = multierror.Append(errs, err) } return errs } -func (d *Diff) validateObjectTypes(ctx context.Context, dv Instances) error { +func (d *Diff) validateObjectTypes(dv Instances) error { var errs error for _, objType := range d.Removed.Objects { - hasInstance, err := dv.ObjectsExist(ctx, objType) + hasInstance, err := dv.ObjectsExist(objType) if err != nil { errs = multierror.Append(errs, err) continue @@ -53,11 +51,11 @@ func (d *Diff) validateObjectTypes(ctx context.Context, dv Instances) error { return errs } -func (d *Diff) validateRelationsTypes(ctx context.Context, dv Instances) error { +func (d *Diff) validateRelationsTypes(dv Instances) error { var errs error for objType, rels := range d.Removed.Relations { for _, rel := range rels { - hasInstance, err := dv.RelationsExist(ctx, objType, rel) + hasInstance, err := dv.RelationsExist(objType, rel) if err != nil { errs = multierror.Append(errs, err) continue diff --git a/model/diff/diff_test.go b/model/diff/diff_test.go index 849164a..d75d61b 100644 --- a/model/diff/diff_test.go +++ b/model/diff/diff_test.go @@ -1,7 +1,6 @@ package diff_test import ( - "context" "errors" "testing" @@ -11,41 +10,41 @@ import ( "github.com/stretchr/testify/require" ) +var ErrBoom = errors.New("Boom") + func TestValidateDiffNoDeletion(t *testing.T) { ctrl := gomock.NewController(t) - mockDirectoryValidator := diff.NewMockInstances(ctrl) + mockInstances := diff.NewMockInstances(ctrl) dif := diff.Diff{Removed: diff.Changes{}, Added: diff.Changes{}} - err := dif.Validate(context.Background(), mockDirectoryValidator) + err := dif.Validate(mockInstances) require.NoError(t, err) } func TestValidateDiffWithObjectTypeDeletion(t *testing.T) { ctrl := gomock.NewController(t) - mockDirectoryValidator := diff.NewMockInstances(ctrl) + mockInstances := diff.NewMockInstances(ctrl) objType := "user" - bCtx := context.Background() dif := diff.Diff{Removed: diff.Changes{Objects: []string{objType}}, Added: diff.Changes{}} - mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objType).Return(false, nil) - err := dif.Validate(bCtx, mockDirectoryValidator) + mockInstances.EXPECT().ObjectsExist(objType).Return(false, nil) + err := dif.Validate(mockInstances) require.NoError(t, err) } func TestValidateDiffWith2ObjectTypeDeletion(t *testing.T) { ctrl := gomock.NewController(t) - mockDirectoryValidator := diff.NewMockInstances(ctrl) + mockInstances := diff.NewMockInstances(ctrl) objTypes := []string{"user", "member"} - bCtx := context.Background() dif := diff.Diff{Removed: diff.Changes{Objects: objTypes}, Added: diff.Changes{}} - mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[0]).Return(false, nil) - mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[1]).Return(true, nil) - err := dif.Validate(bCtx, mockDirectoryValidator) + mockInstances.EXPECT().ObjectsExist(objTypes[0]).Return(false, nil) + mockInstances.EXPECT().ObjectsExist(objTypes[1]).Return(true, nil) + err := dif.Validate(mockInstances) require.Error(t, err) require.Contains(t, err.Error(), derr.ErrObjectTypeInUse.Message) @@ -53,17 +52,16 @@ func TestValidateDiffWith2ObjectTypeDeletion(t *testing.T) { func TestValidateDiffWithRelationTypeDeletion(t *testing.T) { ctrl := gomock.NewController(t) - mockDirectoryValidator := diff.NewMockInstances(ctrl) + mockInstances := diff.NewMockInstances(ctrl) objTypes := []string{"user", "member"} relationTypes := map[string][]string{"folder": {"parent_folder"}} - bCtx := context.Background() dif := diff.Diff{Removed: diff.Changes{Objects: objTypes, Relations: relationTypes}, Added: diff.Changes{}} - mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[0]).Return(false, nil) - mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[1]).Return(false, nil) - mockDirectoryValidator.EXPECT().RelationsExist(bCtx, "folder", relationTypes["folder"][0]).Return(true, nil) - err := dif.Validate(bCtx, mockDirectoryValidator) + mockInstances.EXPECT().ObjectsExist(objTypes[0]).Return(false, nil) + mockInstances.EXPECT().ObjectsExist(objTypes[1]).Return(false, nil) + mockInstances.EXPECT().RelationsExist("folder", relationTypes["folder"][0]).Return(true, nil) + err := dif.Validate(mockInstances) require.Error(t, err) require.Contains(t, err.Error(), derr.ErrRelationTypeInUse.Message) @@ -71,20 +69,19 @@ func TestValidateDiffWithRelationTypeDeletion(t *testing.T) { func TestValidateDiffWithObjectInstances(t *testing.T) { ctrl := gomock.NewController(t) - mockDirectoryValidator := diff.NewMockInstances(ctrl) + mockInstances := diff.NewMockInstances(ctrl) objTypes := []string{"user", "member"} relationTypes := map[string][]string{"folder": {"parent_folder"}} - bCtx := context.Background() dif := diff.Diff{Removed: diff.Changes{Objects: objTypes, Relations: relationTypes}, Added: diff.Changes{}} - mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[0]).Return(false, errors.New("Boom!")) - mockDirectoryValidator.EXPECT().ObjectsExist(bCtx, objTypes[1]).Return(true, nil) - mockDirectoryValidator.EXPECT().RelationsExist(bCtx, "folder", relationTypes["folder"][0]).Return(true, nil) - err := dif.Validate(bCtx, mockDirectoryValidator) + mockInstances.EXPECT().ObjectsExist(objTypes[0]).Return(false, ErrBoom) + mockInstances.EXPECT().ObjectsExist(objTypes[1]).Return(true, nil) + mockInstances.EXPECT().RelationsExist("folder", relationTypes["folder"][0]).Return(true, nil) + err := dif.Validate(mockInstances) require.Error(t, err) require.Contains(t, err.Error(), derr.ErrRelationTypeInUse.Message) require.Contains(t, err.Error(), derr.ErrObjectTypeInUse.Message) - require.Contains(t, err.Error(), "Boom!") + require.Contains(t, err.Error(), ErrBoom.Error()) } diff --git a/model/diff/mock_instances.go b/model/diff/mock_instances.go index 29c4abf..ffedf25 100644 --- a/model/diff/mock_instances.go +++ b/model/diff/mock_instances.go @@ -5,7 +5,6 @@ package diff import ( - context "context" gomock "github.com/golang/mock/gomock" reflect "reflect" ) @@ -34,31 +33,31 @@ func (m *MockInstances) EXPECT() *MockInstancesMockRecorder { } // ObjectsExist mocks base method -func (m *MockInstances) ObjectsExist(arg0 context.Context, arg1 string) (bool, error) { +func (m *MockInstances) ObjectsExist(arg0 string) (bool, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ObjectsExist", arg0, arg1) + ret := m.ctrl.Call(m, "ObjectsExist", arg0) ret0, _ := ret[0].(bool) ret1, _ := ret[1].(error) return ret0, ret1 } // ObjectsExist indicates an expected call of ObjectsExist -func (mr *MockInstancesMockRecorder) ObjectsExist(arg0, arg1 interface{}) *gomock.Call { +func (mr *MockInstancesMockRecorder) ObjectsExist(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ObjectsExist", reflect.TypeOf((*MockInstances)(nil).ObjectsExist), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ObjectsExist", reflect.TypeOf((*MockInstances)(nil).ObjectsExist), arg0) } // RelationsExist mocks base method -func (m *MockInstances) RelationsExist(arg0 context.Context, arg1, arg2 string) (bool, error) { +func (m *MockInstances) RelationsExist(arg0, arg1 string) (bool, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "RelationsExist", arg0, arg1, arg2) + ret := m.ctrl.Call(m, "RelationsExist", arg0, arg1) ret0, _ := ret[0].(bool) ret1, _ := ret[1].(error) return ret0, ret1 } // RelationsExist indicates an expected call of RelationsExist -func (mr *MockInstancesMockRecorder) RelationsExist(arg0, arg1, arg2 interface{}) *gomock.Call { +func (mr *MockInstancesMockRecorder) RelationsExist(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RelationsExist", reflect.TypeOf((*MockInstances)(nil).RelationsExist), arg0, arg1, arg2) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RelationsExist", reflect.TypeOf((*MockInstances)(nil).RelationsExist), arg0, arg1) }