-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fixed the
subjectPriority
sorting algorithm and support for c…
…hecking the subject role link loop (#322) * fix: Fixed the `subjectPriority` sorting algorithm and support for checking the subject role link loop. * fix: Run black
- Loading branch information
Showing
2 changed files
with
79 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from unittest import TestCase | ||
|
||
from casbin import Model | ||
from casbin.model.model import DEFAULT_DOMAIN | ||
|
||
|
||
class TestModel(TestCase): | ||
m = Model() | ||
|
||
def check_hierarchy(self, policies: list, subject_hierarchy_map: dict): | ||
"""check_hierarchy checks the hierarchy of the subject hierarchy map""" | ||
for policy in policies: | ||
if len(policy) < 2: | ||
raise RuntimeError("policy g expect 2 more params") | ||
domain = DEFAULT_DOMAIN | ||
if len(policy) != 2: | ||
domain = policy[2] | ||
child = self.m.get_name_with_domain(domain, policy[0]) | ||
parent = self.m.get_name_with_domain(domain, policy[1]) | ||
assert subject_hierarchy_map[child] < subject_hierarchy_map[parent] | ||
|
||
def test_get_subject_hierarchy_map(self): | ||
# test 1 | ||
policies = [ | ||
["A1", "B1"], | ||
["A1", "B2"], | ||
["A2", "B3"], | ||
] | ||
res = self.m.get_subject_hierarchy_map(policies) | ||
self.check_hierarchy(policies, res) | ||
# test 2 | ||
policies = [ | ||
["A1", "B1"], | ||
["B1", "B2"], | ||
["B2", "B3"], | ||
["B1", "B4"], | ||
["A1", "B2"], | ||
] | ||
res = self.m.get_subject_hierarchy_map(policies) | ||
self.check_hierarchy(policies, res) | ||
# test 3 | ||
policies = [ | ||
["B1", "B2"], | ||
["B2", "B3"], | ||
["B3", "B1"], | ||
] | ||
self.assertRaises(RuntimeError, self.m.get_subject_hierarchy_map, policies) |