Skip to content

Commit

Permalink
fix: relocate unittest and fix file path
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Mar 18, 2021
1 parent 243fc96 commit 5ed07b2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
3 changes: 3 additions & 0 deletions casbin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from .enforcer import *
from .synced_enforcer import SyncedEnforcer
from . import util
from .persist import *
from .effect import *
from .model import *
3 changes: 2 additions & 1 deletion casbin/persist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .adapter import *
from .adapter_filtered import *
from .batch_adapter import *
from .batch_adapter import *
from .adapters import *
1 change: 0 additions & 1 deletion examples/rbac_with_domains_policy.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ p, admin, domain1, data1, read
p, admin, domain1, data1, write
p, admin, domain2, data2, read
p, admin, domain2, data2, write

g, alice, admin, domain1
g, bob, admin, domain2
68 changes: 34 additions & 34 deletions test_filter.py → tests/test_filter.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import casbin
import os
from unittest import TestCase
import unittest
from tests.test_enforcer import get_examples


class Filter:
#P,G are strings
# P,G are strings
P = []
G = []


class TestFilteredAdapter(TestCase):
def test_init_filtered_adapter(self):
adapter = casbin.persist.adapters.FilteredAdapter("examples/rbac_with_domains_policy.csv")
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", adapter)
self.assertFalse(e.has_policy(['admin', 'domain1', 'data1','read']))
adapter = casbin.persist.adapters.FilteredAdapter(get_examples("rbac_with_domains_policy.csv"))
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)
self.assertFalse(e.has_policy(['admin', 'domain1', 'data1', 'read']))

def test_load_filtered_policy(self):
adapter = casbin.persist.adapters.FilteredAdapter("examples/rbac_with_domains_policy.csv")
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", adapter)
adapter = casbin.persist.adapters.FilteredAdapter(get_examples("rbac_with_domains_policy.csv"))
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)
filter = Filter()
filter.P = ["", "domain1"]
filter.G = ["", "", "domain1"]
try:
e.load_policy()
except:
raise RuntimeError("nexpected error in LoadFilteredPolicy")
self.assertTrue(e.has_policy(['admin', 'domain1', 'data1','read']))
self.assertTrue(e.has_policy(['admin', 'domain2', 'data2','read']))
raise RuntimeError("unexpected error in LoadFilteredPolicy")
self.assertTrue(e.has_policy(['admin', 'domain1', 'data1', 'read']))
self.assertTrue(e.has_policy(['admin', 'domain2', 'data2', 'read']))
try:
e.load_filtered_policy(filter)
except:
Expand All @@ -32,8 +35,8 @@ def test_load_filtered_policy(self):
if not e.is_filtered:
raise RuntimeError("adapter did not set the filtered flag correctly")

self.assertTrue(e.has_policy(['admin', 'domain1', 'data1','read']))
self.assertFalse(e.has_policy(['admin', 'domain2', 'data2','read']))
self.assertTrue(e.has_policy(['admin', 'domain1', 'data1', 'read']))
self.assertFalse(e.has_policy(['admin', 'domain2', 'data2', 'read']))

with self.assertRaises(RuntimeError):
e.save_policy()
Expand All @@ -42,17 +45,17 @@ def test_load_filtered_policy(self):
e.get_adapter().save_policy(e.get_model())

def test_append_filtered_policy(self):
adapter = casbin.persist.adapters.FilteredAdapter("examples/rbac_with_domains_policy.csv")
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", adapter)
adapter = casbin.persist.adapters.FilteredAdapter(get_examples("rbac_with_domains_policy.csv"))
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)
filter = Filter()
filter.P = ["", "domain1"]
filter.G = ["", "", "domain1"]
try:
e.load_policy()
except:
raise RuntimeError("nexpected error in LoadFilteredPolicy")
self.assertTrue(e.has_policy(['admin', 'domain1', 'data1','read']))
self.assertTrue(e.has_policy(['admin', 'domain2', 'data2','read']))
raise RuntimeError("unexpected error in LoadFilteredPolicy")
self.assertTrue(e.has_policy(['admin', 'domain1', 'data1', 'read']))
self.assertTrue(e.has_policy(['admin', 'domain2', 'data2', 'read']))
try:
e.load_filtered_policy(filter)
except:
Expand All @@ -61,8 +64,8 @@ def test_append_filtered_policy(self):
if not e.is_filtered:
raise RuntimeError("adapter did not set the filtered flag correctly")

self.assertTrue(e.has_policy(['admin', 'domain1', 'data1','read']))
self.assertFalse(e.has_policy(['admin', 'domain2', 'data2','read']))
self.assertTrue(e.has_policy(['admin', 'domain1', 'data1', 'read']))
self.assertFalse(e.has_policy(['admin', 'domain2', 'data2', 'read']))

filter.P = ["", "domain2"]
filter.G = ["", "", "domain2"]
Expand All @@ -71,22 +74,20 @@ def test_append_filtered_policy(self):
except:
raise RuntimeError("unexpected error in LoadFilteredPolicy")

self.assertTrue(e.has_policy(['admin', 'domain1', 'data1','read']))
self.assertTrue(e.has_policy(['admin', 'domain2', 'data2','read']))

self.assertTrue(e.has_policy(['admin', 'domain1', 'data1', 'read']))
self.assertTrue(e.has_policy(['admin', 'domain2', 'data2', 'read']))

def test_filtered_policy_invalid_filter(self):
adapter = casbin.persist.adapters.FilteredAdapter("examples/rbac_with_domains_policy.csv")
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", adapter)
filter = ["","domain1"]
adapter = casbin.persist.adapters.FilteredAdapter(get_examples("rbac_with_domains_policy.csv"))
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)
filter = ["", "domain1"]

with self.assertRaises(RuntimeError):
e.load_filtered_policy(filter)


def test_filtered_policy_empty_filter(self):
adapter = casbin.persist.adapters.FilteredAdapter("examples/rbac_with_domains_policy.csv")
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", adapter)
adapter = casbin.persist.adapters.FilteredAdapter(get_examples("rbac_with_domains_policy.csv"))
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)

try:
e.load_filtered_policy(None)
Expand All @@ -101,9 +102,8 @@ def test_filtered_policy_empty_filter(self):
except:
raise RuntimeError("unexpected error in SavePolicy")


def test_unsupported_filtered_policy(self):
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv")
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), get_examples("rbac_with_domains_policy.csv"))
filter = Filter()
filter.P = ["", "domain1"]
filter.G = ["", "", "domain1"]
Expand All @@ -112,14 +112,14 @@ def test_unsupported_filtered_policy(self):

def test_filtered_adapter_empty_filepath(self):
adapter = casbin.persist.adapters.FilteredAdapter("")
e = casbin.Enforcer("examples/rbac_with_domains_model.conf",adapter)
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)

with self.assertRaises(RuntimeError):
e.load_filtered_policy(None)

def test_filtered_adapter_invalid_filepath(self):
adapter = casbin.persist.adapters.FilteredAdapter("examples/does_not_exist_policy.csv")
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", adapter)
adapter = casbin.persist.adapters.FilteredAdapter(get_examples("does_not_exist_policy.csv"))
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)

with self.assertRaises(RuntimeError):
e.load_filtered_policy(None)

0 comments on commit 5ed07b2

Please sign in to comment.