Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: rewrite and traversal #1531

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/check/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func BenchmarkCheckEngine(b *testing.B) {
//go:embed testfixtures/project_opl.ts
var ProjectOPLConfig string

//go:embed testfixtures/rewrite_and_traversal_opl.ts
var RewriteAndTraversalConfig string

func BenchmarkComputedUsersets(b *testing.B) {
ctx := context.Background()

Expand Down
41 changes: 41 additions & 0 deletions internal/check/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,47 @@ func TestEngine(t *testing.T) {
}
})

t.Run("case=rewrite_and_traversal with depth", func(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
opl string
}{
{"rewrite and traversal", RewriteAndTraversalConfig},
} {
tc := tc
t.Run("opl="+tc.name, func(t *testing.T) {
t.Parallel()

reg := driver.NewSqliteTestRegistry(t, false,
driver.WithConfig(config.KeyLimitMaxReadDepth, 10),
driver.WithOPL(RewriteAndTraversalConfig))

insertFixtures(t, reg.RelationTupleManager(), []string{
"Group:g#supers@SuperUsers:super",
"Role:admin#members@User:u",
"SuperUsers:super#admins@Role:admin#members",
"Comment:c#parents@Group:g",
})

e := check.NewEngine(reg)

t.Run("case=enough depth", func(t *testing.T) {
res, err := e.CheckIsMember(ctx, tupleFromString(t, "Comment:c#update@User:u"), 6)
require.NoError(t, err)
assert.True(t, res)
})

t.Run("case=not enough depth", func(t *testing.T) {
res, err := e.CheckIsMember(ctx, tupleFromString(t, "Comment:c#update@User:u"), 5)
require.NoError(t, err)
assert.False(t, res)
})
})
}
})

t.Run("case=batch check", func(t *testing.T) {
reg := newDepsProvider(t, []*namespace.Namespace{
{Name: "test"},
Expand Down
37 changes: 37 additions & 0 deletions internal/check/testfixtures/rewrite_and_traversal_opl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Namespace, Context, SubjectSet } from "@ory/keto-namespace-types"

class User implements Namespace {}

class Role implements Namespace {
related: {
members: (User | Role)[]
}
}

class SuperUsers implements Namespace {
related: {
admins: SubjectSet<Role, "members">[] // annotier admins
}
}

class Comment implements Namespace {
related: {
parents: Group[]
}

permits = {
delete: (ctx: Context): boolean => this.related.parents.traverse((group) => group.permits.update(ctx)),
update: (ctx: Context): boolean => this.permits.delete(ctx),
}
}

class Group implements Namespace {
related: {
supers: SuperUsers[]
}

permits = {
delete: (ctx: Context): boolean => this.related.supers.traverse((supe) => supe.related.admins.includes(ctx.subject)), // delete a group
update: (ctx: Context): boolean => this.permits.delete(ctx), // if can delete then can update
}
}
92 changes: 17 additions & 75 deletions proto/ory/keto/opl/v1alpha1/syntax_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions proto/ory/keto/opl/v1alpha1/syntax_service_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading