#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_permission.py $
$Id: test_permission.py 24493 2004-06-16 22:07:25Z nascheme $

Test for dulcinea.permission.
"""

from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.permission import PermissionManager

tested_modules = [ "dulcinea.permission" ]

class WaitPass (PermissionManager):

    valid_permissions = {'pass' : 'just pass',
                         'wait' : 'just wait'}


class PermissionManagerTest (TestScenario):

    def check_recursion_control(self):
        self.test_stmt("self.s=s=WaitPass()")
        self.test_stmt("self.r=r=WaitPass()")
        self.test_stmt("self.q=q=WaitPass()")
        self.test_stmt("s.set_permission('pass', r, prerequisite='pass')")
        self.test_stmt("r.set_permission('pass', q, prerequisite='pass')")
        self.test_stmt("q.set_permission('pass', s, prerequisite='pass')")
        self.test_false("s.grants('pass', None)")
        self.test_false("r.grants('pass', None)")
        self.test_false("q.grants('pass', None)")
        self.test_stmt("s.set_permission('pass', s, prerequisite='pass')")
        self.test_false("s.grants('pass', None)")
        self.test_false("r.grants('pass', None)")
        self.test_false("q.grants('pass', None)")

    def check_all(self):
        self.test_stmt("s = WaitPass()")
        self.test_stmt("r = WaitPass()")
        self.test_stmt("q = WaitPass()")
        # None is used in this test as the object for which
        # permissions are applied.  Permissions can be granted to
        # any hashable object.
        self.test_false("s.grants('pass', None)")
        self.test_false("s.directly_grants('pass', None)")
        self.test_stmt("s.set_permission('pass', None)")
        self.test_stmt("s.set_permission_items('pass', [(None,None)])")
        self.test_true("s.directly_grants('pass', None)")

        # None shall pass.  s grants 'pass' to None.
        self.test_true("s.grants('pass', None)")
        # Set that r grants 'wait' if s grants 'pass'
        self.test_stmt("r.set_permission('wait', s, prerequisite='pass')")

        # Check get_direct_grantees
        self.test_val("r.get_direct_grantees('wait')", [])
        self.test_val("s.get_direct_grantees('pass')", [None])

        # r does not grant 'pass'
        self.test_false("r.grants('pass', None)")
        # r grants 'wait' to None because s grants 'pass' to None.
        self.test_true("r.grants('wait', None)")
        # Set that q grants 'pass' if r grants 'wait'.
        self.test_stmt("q.set_permission('pass', r, prerequisite='wait')")
        # q grants 'pass' to None because r grants 'wait' to None,
        # in turn because s grants 'pass' to None.
        self.test_true("q.grants('pass', None)")

        # Check accessor
        self.test_set("q.get_valid_permissions().keys()", ['pass', 'wait'])
        self.test_val("r.get_permissions(None)", ['wait'])
        self.test_val("s.get_permissions(None)", ['pass'])
        self.test_val("s.get_permission_items('pass')", [(None, None)])
        self.test_val("s.get_permission_items('wait')", [])

        self.test_false("s.grants('nonsense', None)")
        self.test_stmt("WaitPass().set_permission_items('wait', [])")
        self.test_stmt("WaitPass().get_direct_grantees('wait')")




if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios (scenarios, options)

