-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
oanatmaria
committed
Oct 19, 2023
1 parent
12aee9c
commit ab498dc
Showing
10 changed files
with
261 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package diff | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aserto-dev/go-directory/pkg/derr" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
//go:generate go run github.com/golang/mock/mockgen -destination=mock_directory_validator.go -package=diff github.com/aserto-dev/azm/model/diff DirectoryValidator | ||
|
||
type Diff struct { | ||
Added Changes | ||
Removed Changes | ||
} | ||
|
||
type Changes struct { | ||
Objects []string | ||
Relations map[string][]string | ||
} | ||
|
||
type DirectoryValidator interface { | ||
HasObjectInstances(ctx context.Context, objectType string) (bool, error) | ||
HasRelationInstances(ctx context.Context, objectType, relationName string) (bool, error) | ||
} | ||
|
||
func (d *Diff) Validate(ctx context.Context, dv DirectoryValidator) error { | ||
if err := d.validateObjectTypes(ctx, dv); err != nil { | ||
return err | ||
} | ||
|
||
if err := d.validateRelationsTypes(ctx, dv); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *Diff) validateObjectTypes(ctx context.Context, dv DirectoryValidator) error { | ||
for _, objType := range d.Removed.Objects { | ||
hasInstance, err := dv.HasObjectInstances(ctx, objType) | ||
if err != nil { | ||
return err | ||
} | ||
if hasInstance { | ||
return errors.Wrapf(derr.ErrObjectTypeInUse, "object type: %s", objType) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (d *Diff) validateRelationsTypes(ctx context.Context, dv DirectoryValidator) error { | ||
for objType, rels := range d.Removed.Relations { | ||
for _, rel := range rels { | ||
hasInstance, err := dv.HasRelationInstances(ctx, objType, rel) | ||
if err != nil { | ||
return err | ||
} | ||
if hasInstance { | ||
return errors.Wrapf(derr.ErrRelationTypeInUse, "relation type: %s; object type: %s", rel, objType) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package diff_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aserto-dev/azm/model/diff" | ||
"github.com/aserto-dev/go-directory/pkg/derr" | ||
gomock "github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateDiffNoDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockDirectoryValidator := diff.NewMockDirectoryValidator(ctrl) | ||
|
||
dif := diff.Diff{Removed: diff.Changes{}, Added: diff.Changes{}} | ||
err := dif.Validate(context.Background(), mockDirectoryValidator) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestValidateDiffWithObjectTypeDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockDirectoryValidator := diff.NewMockDirectoryValidator(ctrl) | ||
objType := "user" | ||
bCtx := context.Background() | ||
|
||
dif := diff.Diff{Removed: diff.Changes{Objects: []string{objType}}, Added: diff.Changes{}} | ||
|
||
mockDirectoryValidator.EXPECT().HasObjectInstances(bCtx, objType).Return(false, nil) | ||
err := dif.Validate(bCtx, mockDirectoryValidator) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestValidateDiffWith2ObjectTypeDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockDirectoryValidator := diff.NewMockDirectoryValidator(ctrl) | ||
objTypes := []string{"user", "member"} | ||
bCtx := context.Background() | ||
|
||
dif := diff.Diff{Removed: diff.Changes{Objects: objTypes}, Added: diff.Changes{}} | ||
|
||
mockDirectoryValidator.EXPECT().HasObjectInstances(bCtx, objTypes[0]).Return(false, nil) | ||
mockDirectoryValidator.EXPECT().HasObjectInstances(bCtx, objTypes[1]).Return(true, nil) | ||
err := dif.Validate(bCtx, mockDirectoryValidator) | ||
|
||
require.Error(t, err) | ||
require.Contains(t, err.Error(), derr.ErrObjectTypeInUse.Message) | ||
} | ||
|
||
func TestValidateDiffWithRelationTypeDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockDirectoryValidator := diff.NewMockDirectoryValidator(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().HasObjectInstances(bCtx, objTypes[0]).Return(false, nil) | ||
mockDirectoryValidator.EXPECT().HasObjectInstances(bCtx, objTypes[1]).Return(false, nil) | ||
mockDirectoryValidator.EXPECT().HasRelationInstances(bCtx, "folder", relationTypes["folder"][0]).Return(true, nil) | ||
err := dif.Validate(bCtx, mockDirectoryValidator) | ||
|
||
require.Error(t, err) | ||
require.Contains(t, err.Error(), derr.ErrRelationTypeInUse.Message) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.