This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/policies/expression/tests.py

63 lines
2.2 KiB
Python
Raw Normal View History

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
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"""
template = "return True"
evaluator = PolicyEvaluator("test")
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"""
template = 'pb_message("some message");return False'
evaluator = PolicyEvaluator("test")
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"""
template = ";"
evaluator = PolicyEvaluator("test")
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, ("invalid syntax (test, line 3)",))
2020-02-23 14:54:26 +00:00
def test_undefined(self):
"""test undefined result"""
template = "{{ foo.bar }}"
evaluator = PolicyEvaluator("test")
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, ("name 'foo' is not defined",))
2020-02-23 14:54:26 +00:00
def test_validate(self):
"""test validate"""
template = "True"
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"""
template = ";"
evaluator = PolicyEvaluator("test")
2020-02-23 14:54:26 +00:00
with self.assertRaises(ValidationError):
evaluator.validate(template)