-
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.
Merge pull request #8 from aserto-dev/add_diff_validate
Add validation for Diff struct
- Loading branch information
Showing
10 changed files
with
297 additions
and
36 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,100 @@ | ||
package diff | ||
|
||
import ( | ||
"github.com/aserto-dev/go-directory/pkg/derr" | ||
"github.com/hashicorp/go-multierror" | ||
"github.com/pkg/errors" | ||
"github.com/samber/lo" | ||
) | ||
|
||
//go:generate go run github.com/golang/mock/mockgen -destination=mock_instances.go -package=diff github.com/aserto-dev/azm/model/diff Instances | ||
|
||
type Diff struct { | ||
Added Changes | ||
Removed Changes | ||
} | ||
|
||
type Changes struct { | ||
Objects []string | ||
Relations map[string][]string | ||
} | ||
|
||
// Only the types of the relation instances are needed. | ||
type RelationKind struct { | ||
Object string | ||
Relation string | ||
Subject string | ||
SubjectRelation string | ||
} | ||
|
||
type Instances interface { | ||
ObjectTypes() ([]string, error) | ||
RelationTypes() ([]*RelationKind, error) | ||
} | ||
|
||
func (d *Diff) Validate(dv Instances) error { | ||
var errs error | ||
var rels []*RelationKind | ||
if len(d.Removed.Objects) > 0 { | ||
objs, err := dv.ObjectTypes() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rels, err = dv.RelationTypes() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = d.validateObjectTypes(objs, rels) | ||
errs = multierror.Append(errs, err) | ||
} | ||
|
||
if len(d.Removed.Relations) > 0 { | ||
var err error | ||
if len(rels) == 0 { | ||
rels, err = dv.RelationTypes() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
} | ||
err = d.validateRelationsTypes(rels) | ||
errs = multierror.Append(errs, err) | ||
} | ||
|
||
if merr, ok := errs.(*multierror.Error); ok && len(merr.Errors) > 0 { | ||
return errs | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *Diff) validateObjectTypes(objs []string, rels []*RelationKind) error { | ||
var errs error | ||
for _, objType := range d.Removed.Objects { | ||
if lo.Contains(objs, objType) { | ||
errs = multierror.Append(errs, errors.Wrapf(derr.ErrObjectTypeInUse, "object type [%s]", objType)) | ||
} | ||
rel, found := lo.Find(rels, func(rel *RelationKind) bool { return rel.Object == objType || rel.Subject == objType }) | ||
if found { | ||
errs = multierror.Append(errs, errors.Wrapf(derr.ErrRelationTypeInUse, "object type [%s], relation type [%s]", objType, rel.Relation)) | ||
} | ||
} | ||
return errs | ||
} | ||
|
||
func (d *Diff) validateRelationsTypes(relations []*RelationKind) error { | ||
var errs error | ||
for objType, rels := range d.Removed.Relations { | ||
for _, rel := range rels { | ||
_, found := lo.Find(relations, func(rl *RelationKind) bool { | ||
return (rl.Object == objType && rl.Relation == rel) || (rl.Subject == objType && rl.SubjectRelation == rel) | ||
}) | ||
if found { | ||
errs = multierror.Append(errs, errors.Wrapf(derr.ErrRelationTypeInUse, "object type [%s], relation type [%s]", objType, rel)) | ||
} | ||
} | ||
} | ||
return errs | ||
} |
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,83 @@ | ||
package diff_test | ||
|
||
import ( | ||
"errors" | ||
"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" | ||
) | ||
|
||
var ErrBoom = errors.New("Boom") | ||
|
||
func TestValidateDiffNoDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockInstances := diff.NewMockInstances(ctrl) | ||
|
||
dif := diff.Diff{Removed: diff.Changes{}, Added: diff.Changes{}} | ||
err := dif.Validate(mockInstances) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestValidateDiffWithObjectTypeDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockInstances := diff.NewMockInstances(ctrl) | ||
objType := "user" | ||
|
||
dif := diff.Diff{Removed: diff.Changes{Objects: []string{objType}}, Added: diff.Changes{}} | ||
|
||
mockInstances.EXPECT().ObjectTypes().Return([]string{}, nil) | ||
mockInstances.EXPECT().RelationTypes().Return([]*diff.RelationKind{}, nil) | ||
err := dif.Validate(mockInstances) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func TestValidateDiffWith2ObjectTypeDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockInstances := diff.NewMockInstances(ctrl) | ||
objTypes := []string{"user", "member"} | ||
|
||
dif := diff.Diff{Removed: diff.Changes{Objects: objTypes}, Added: diff.Changes{}} | ||
|
||
mockInstances.EXPECT().ObjectTypes().Return([]string{"user"}, nil) | ||
mockInstances.EXPECT().RelationTypes().Return([]*diff.RelationKind{}, nil) | ||
err := dif.Validate(mockInstances) | ||
|
||
require.Error(t, err) | ||
require.Contains(t, err.Error(), derr.ErrObjectTypeInUse.Message) | ||
} | ||
|
||
func TestValidateDiffWithRelationTypeDeletion(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockInstances := diff.NewMockInstances(ctrl) | ||
objTypes := []string{"user", "member"} | ||
relationTypes := map[string][]string{"folder": {"parent_folder"}} | ||
|
||
dif := diff.Diff{Removed: diff.Changes{Objects: objTypes, Relations: relationTypes}, Added: diff.Changes{}} | ||
|
||
mockInstances.EXPECT().ObjectTypes().Return([]string{}, nil) | ||
mockInstances.EXPECT().RelationTypes().Return([]*diff.RelationKind{{Object: "folder", Relation: "parent_folder"}}, nil) | ||
err := dif.Validate(mockInstances) | ||
|
||
require.Error(t, err) | ||
require.Contains(t, err.Error(), derr.ErrRelationTypeInUse.Message) | ||
} | ||
|
||
func TestValidateDiffWithObjectInstances(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockInstances := diff.NewMockInstances(ctrl) | ||
objTypes := []string{"user", "member"} | ||
relationTypes := map[string][]string{"folder": {"parent_folder"}} | ||
|
||
dif := diff.Diff{Removed: diff.Changes{Objects: objTypes, Relations: relationTypes}, Added: diff.Changes{}} | ||
|
||
mockInstances.EXPECT().ObjectTypes().Return([]string{}, ErrBoom) | ||
err := dif.Validate(mockInstances) | ||
|
||
require.Error(t, err) | ||
require.Contains(t, err.Error(), ErrBoom.Error()) | ||
} |
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.