2019-10-14 14:08:24 +00:00
|
|
|
"""policy engine tests"""
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2020-05-16 14:02:42 +00:00
|
|
|
from passbook.core.models import Policy, User
|
2020-05-10 00:14:55 +00:00
|
|
|
from passbook.policies.dummy.models import DummyPolicy
|
2019-10-14 14:08:24 +00:00
|
|
|
from passbook.policies.engine import PolicyEngine
|
|
|
|
|
|
|
|
|
|
|
|
class PolicyTestEngine(TestCase):
|
|
|
|
"""PolicyEngine tests"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
cache.clear()
|
2019-12-31 11:51:16 +00:00
|
|
|
self.user = User.objects.create_user(username="policyuser")
|
2020-05-10 00:14:55 +00:00
|
|
|
self.policy_false = DummyPolicy.objects.create(
|
2019-12-31 11:51:16 +00:00
|
|
|
result=False, wait_min=0, wait_max=1
|
|
|
|
)
|
2020-05-10 00:14:55 +00:00
|
|
|
self.policy_true = DummyPolicy.objects.create(
|
2019-12-31 11:51:16 +00:00
|
|
|
result=True, wait_min=0, wait_max=1
|
|
|
|
)
|
2020-05-10 00:14:55 +00:00
|
|
|
self.policy_negate = DummyPolicy.objects.create(
|
2019-12-31 11:51:16 +00:00
|
|
|
negate=True, result=True, wait_min=0, wait_max=1
|
|
|
|
)
|
|
|
|
self.policy_raises = Policy.objects.create(name="raises")
|
2019-10-14 14:08:24 +00:00
|
|
|
|
|
|
|
def test_engine_empty(self):
|
|
|
|
"""Ensure empty policy list passes"""
|
|
|
|
engine = PolicyEngine([], self.user)
|
|
|
|
self.assertEqual(engine.build().passing, True)
|
|
|
|
|
|
|
|
def test_engine(self):
|
|
|
|
"""Ensure all policies passes (Mix of false and true -> false)"""
|
2019-12-31 11:51:16 +00:00
|
|
|
engine = PolicyEngine(
|
2020-05-10 00:14:55 +00:00
|
|
|
DummyPolicy.objects.filter(negate__exact=False), self.user
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2019-10-14 14:08:24 +00:00
|
|
|
self.assertEqual(engine.build().passing, False)
|
|
|
|
|
|
|
|
def test_engine_negate(self):
|
|
|
|
"""Test negate flag"""
|
2020-05-10 00:14:55 +00:00
|
|
|
engine = PolicyEngine(DummyPolicy.objects.filter(negate__exact=True), self.user)
|
2019-10-14 14:08:24 +00:00
|
|
|
self.assertEqual(engine.build().passing, False)
|
|
|
|
|
|
|
|
def test_engine_policy_error(self):
|
|
|
|
"""Test negate flag"""
|
2019-12-31 11:51:16 +00:00
|
|
|
engine = PolicyEngine(Policy.objects.filter(name="raises"), self.user)
|
2019-10-14 14:08:24 +00:00
|
|
|
self.assertEqual(engine.build().passing, False)
|
|
|
|
|
|
|
|
def test_engine_cache(self):
|
|
|
|
"""Ensure empty policy list passes"""
|
2019-12-31 11:51:16 +00:00
|
|
|
engine = PolicyEngine(
|
2020-05-10 00:14:55 +00:00
|
|
|
DummyPolicy.objects.filter(negate__exact=False), self.user
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(len(cache.keys("policy_*")), 0)
|
2019-10-14 14:08:24 +00:00
|
|
|
self.assertEqual(engine.build().passing, False)
|
2019-12-31 11:51:16 +00:00
|
|
|
self.assertEqual(len(cache.keys("policy_*")), 2)
|
2019-10-14 14:08:24 +00:00
|
|
|
self.assertEqual(engine.build().passing, False)
|
2019-12-31 11:51:16 +00:00
|
|
|
self.assertEqual(len(cache.keys("policy_*")), 2)
|