Skip to content

Commit

Permalink
Merge pull request #1609 from Permify/feature/add-linked-schema-test-…
Browse files Browse the repository at this point in the history
…cases

test: add test cases for linked schema
  • Loading branch information
tolgaOzen authored Sep 19, 2024
2 parents e1442dc + 9e8be60 commit 44c4697
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions internal/schema/linkedSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,5 +1021,165 @@ var _ = Describe("linked schema", func() {
},
}))
})

It("Case 21", func() {
sch, err := parser.NewParser(`
entity user {}
entity organization {
relation owner @user
attribute balance integer
permission withdraw = check_balance(balance) or owner
}
entity account {
relation parent @organization
relation owner @user
attribute balance integer
permission withdraw = check_balance(balance) and owner or parent.withdraw
}
rule check_balance(balance integer) {
(balance >= 5000)
}
`).Parse()

Expect(err).ShouldNot(HaveOccurred())

c := compiler.NewCompiler(true, sch)
a, _, _ := c.Compile()

g := NewLinkedGraph(NewSchemaFromEntityAndRuleDefinitions(a, nil))

ent, err := g.LinkedEntrances(&base.Entrance{
Type: "account",
Value: "withdraw",
}, &base.Entrance{
Type: "user",
Value: "",
})

Expect(err).ShouldNot(HaveOccurred())
Expect(ent).Should(Equal([]*LinkedEntrance{
{
Kind: AttributeLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "account",
Value: "balance",
},
TupleSetRelation: "",
},
{
Kind: RelationLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "account",
Value: "owner",
},
TupleSetRelation: "",
},
{
Kind: AttributeLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "organization",
Value: "balance",
},
TupleSetRelation: "",
},
{
Kind: RelationLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "organization",
Value: "owner",
},
TupleSetRelation: "",
},
}))
})

It("Case 22", func() {
sch, err := parser.NewParser(`
entity user {}
entity organization {
relation owner @user
attribute balance integer
permission withdraw = check_balance(balance) or owner
}
entity account {
relation parent @organization
relation owner @user @account#owner
attribute balance integer
permission withdraw = check_balance(balance) and owner or parent.withdraw
}
rule check_balance(balance integer) {
(balance >= 5000)
}
`).Parse()

Expect(err).ShouldNot(HaveOccurred())

c := compiler.NewCompiler(true, sch)
a, _, _ := c.Compile()

g := NewLinkedGraph(NewSchemaFromEntityAndRuleDefinitions(a, nil))

ent, err := g.LinkedEntrances(&base.Entrance{
Type: "account",
Value: "withdraw",
}, &base.Entrance{
Type: "account",
Value: "owner",
})

Expect(err).ShouldNot(HaveOccurred())
Expect(ent).Should(Equal([]*LinkedEntrance{
{
Kind: AttributeLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "account",
Value: "balance",
},
TupleSetRelation: "",
},
{
Kind: ComputedUserSetLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "account",
Value: "withdraw",
},
TupleSetRelation: "",
},
{
Kind: RelationLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "account",
Value: "owner",
},
TupleSetRelation: "",
},
{
Kind: AttributeLinkedEntrance,
TargetEntrance: &base.Entrance{
Type: "organization",
Value: "balance",
},
TupleSetRelation: "",
},
}))
})
})
})

0 comments on commit 44c4697

Please sign in to comment.