#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_permission.py $
$Id: test_permission.py 23445 2004-02-06 16:14:55Z dbinger $

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_val("s.grants('pass', None)", None)
        self.test_val("r.grants('pass', None)", None)
        self.test_val("q.grants('pass', None)", None)
        self.test_stmt("s.set_permission('pass', s, prerequisite='pass')")
        self.test_val("s.grants('pass', None)", None)
        self.test_val("r.grants('pass', None)", None)
        self.test_val("q.grants('pass', None)", None)

    def check_all(self):
        self.test_stmt("self.s=s=WaitPass()")
        self.test_stmt("self.r=r=WaitPass()")
        self.test_stmt("self.q=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_val("s.grants('pass', None)", None)
        self.test_val("s.directly_grants('pass', None)", False)
        self.test_stmt("s.set_permission('pass', None)")
        self.test_stmt("s.set_permission_items('pass', [(None,None)])")

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

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

        # r does not grant 'pass'
        self.test_val("r.grants('pass', None)", None)
        # r grants 'wait' to None because self.s grants 'pass' to None.
        self.test_val("r.grants('wait', None)", [('pass', self.s)])
        # Set that q grants 'pass' if r grants 'wait'.
        self.test_stmt("self.q.set_permission('pass', r, prerequisite='wait')")
        # q grants 'pass' to None because self.r grants 'wait' to None,
        # in turn because self.s grants 'pass' to None.
        self.test_val("q.grants('pass', None)",
                      [ ('wait', self.r), ('pass', self.s) ])
        # Set that s does not grant 'pass' to None.
        self.test_stmt("self.s.remove_permission('pass', None)")
        # Now the chain is broken, and q no longer grants 'pass' to None.
        self.test_val("q.grants('pass', None)", None)
        # Set again that s grants 'pass' to None.
        self.test_stmt("self.s.set_permission('pass', None)")
        # The chain is restored.
        self.test_val("q.grants('pass', None)",
                      [ ('wait', self.r), ('pass', self.s) ])
        # Take away r's indirect grant of 'wait'.
        self.test_stmt("self.r.remove_permission('wait', self.s)")
        # The chain is broken again.
        self.test_val("q.grants('pass', None)", None)
        # Check accessor
        self.test_seq("q.get_valid_permissions().keys()", ['pass', 'wait'],
                      match_order=False)
        # Set that r grants 'wait' to 1.
        self.test_stmt("self.r.set_permission('wait', 1)")
        # Check that q grants 'pass' which forces check of r grants 'wait'
        self.test_val("q.grants('pass', None)", None)
        # check get_grants()
        self.test_val("self.r.get_permissions(None)", [])
        self.test_val("self.s.get_permissions(None)", ['pass'])
        self.test_val("self.s.grants('nonsense', None)", None)


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

