2020-02-23 14:54:26 +00:00
|
|
|
"""evaluator tests"""
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.test import TestCase
|
|
|
|
from guardian.shortcuts import get_anonymous_user
|
|
|
|
|
2020-06-05 10:00:27 +00:00
|
|
|
from passbook.policies.expression.evaluator import PolicyEvaluator
|
2020-02-23 14:54:26 +00:00
|
|
|
from passbook.policies.types import PolicyRequest
|
|
|
|
|
|
|
|
|
|
|
|
class TestEvaluator(TestCase):
|
|
|
|
"""Evaluator tests"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.request = PolicyRequest(user=get_anonymous_user())
|
|
|
|
|
|
|
|
def test_valid(self):
|
|
|
|
"""test simple value expression"""
|
2020-06-05 10:00:27 +00:00
|
|
|
template = "return True"
|
|
|
|
evaluator = PolicyEvaluator("test")
|
2020-06-01 13:25:38 +00:00
|
|
|
evaluator.set_policy_request(self.request)
|
|
|
|
self.assertEqual(evaluator.evaluate(template).passing, True)
|
2020-02-23 14:54:26 +00:00
|
|
|
|
|
|
|
def test_messages(self):
|
|
|
|
"""test expression with message return"""
|
2020-06-05 10:00:27 +00:00
|
|
|
template = 'pb_message("some message");return False'
|
|
|
|
evaluator = PolicyEvaluator("test")
|
2020-06-01 13:25:38 +00:00
|
|
|
evaluator.set_policy_request(self.request)
|
|
|
|
result = evaluator.evaluate(template)
|
2020-02-23 14:54:26 +00:00
|
|
|
self.assertEqual(result.passing, False)
|
|
|
|
self.assertEqual(result.messages, ("some message",))
|
|
|
|
|
|
|
|
def test_invalid_syntax(self):
|
|
|
|
"""test invalid syntax"""
|
2020-06-05 10:00:27 +00:00
|
|
|
template = ";"
|
|
|
|
evaluator = PolicyEvaluator("test")
|
2020-06-01 13:25:38 +00:00
|
|
|
evaluator.set_policy_request(self.request)
|
|
|
|
result = evaluator.evaluate(template)
|
2020-02-23 14:54:26 +00:00
|
|
|
self.assertEqual(result.passing, False)
|
2020-06-05 10:00:27 +00:00
|
|
|
self.assertEqual(result.messages, ("invalid syntax (test, line 2)",))
|
2020-02-23 14:54:26 +00:00
|
|
|
|
|
|
|
def test_undefined(self):
|
|
|
|
"""test undefined result"""
|
|
|
|
template = "{{ foo.bar }}"
|
2020-06-05 10:00:27 +00:00
|
|
|
evaluator = PolicyEvaluator("test")
|
2020-06-01 13:25:38 +00:00
|
|
|
evaluator.set_policy_request(self.request)
|
|
|
|
result = evaluator.evaluate(template)
|
2020-02-23 14:54:26 +00:00
|
|
|
self.assertEqual(result.passing, False)
|
2020-06-05 10:00:27 +00:00
|
|
|
self.assertEqual(result.messages, ("name 'foo' is not defined",))
|
2020-02-23 14:54:26 +00:00
|
|
|
|
|
|
|
def test_validate(self):
|
|
|
|
"""test validate"""
|
|
|
|
template = "True"
|
2020-06-05 10:00:27 +00:00
|
|
|
evaluator = PolicyEvaluator("test")
|
2020-02-23 14:54:26 +00:00
|
|
|
result = evaluator.validate(template)
|
|
|
|
self.assertEqual(result, True)
|
|
|
|
|
|
|
|
def test_validate_invalid(self):
|
|
|
|
"""test validate"""
|
2020-06-05 10:00:27 +00:00
|
|
|
template = ";"
|
|
|
|
evaluator = PolicyEvaluator("test")
|
2020-02-23 14:54:26 +00:00
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
evaluator.validate(template)
|