#!/www/python/bin/python

"""$URL: svn+ssh://svn/repos/trunk/dulcinea/lib/test/utest_permission.py $
$Id: utest_permission.py 25054 2004-09-09 18:04:30Z dbinger $

Test for dulcinea.permission.
"""

from sets import Set
from sancho.utest import UTest
from dulcinea.permission import PermissionManager


class WaitPass (PermissionManager):

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


class PermissionManagerTest (UTest):

    def check_recursion_control(self):
        self.s=s=WaitPass()
        self.r=r=WaitPass()
        self.q=q=WaitPass()
        s.set_permission('pass', r, prerequisite='pass')
        r.set_permission('pass', q, prerequisite='pass')
        q.set_permission('pass', s, prerequisite='pass')
        assert not s.grants('pass', None)
        assert not r.grants('pass', None)
        assert not q.grants('pass', None)
        s.set_permission('pass', s, prerequisite='pass')
        assert not s.grants('pass', None)
        assert not r.grants('pass', None)
        assert not q.grants('pass', None)

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

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

        # Check get_direct_grantees
        assert r.get_direct_grantees('wait') == []
        assert s.get_direct_grantees('pass') == [None]

        # r does not grant 'pass'
        assert not r.grants('pass', None)
        # r grants 'wait' to None because s grants 'pass' to None.
        assert r.grants('wait', None)
        # Set that q grants 'pass' if r grants 'wait'.
        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.
        assert q.grants('pass', None)

        # Check accessor
        assert Set(q.get_valid_permissions().keys()) == Set(['pass', 'wait'])
        assert r.get_permissions(None) == ['wait']
        assert s.get_permissions(None) == ['pass']
        assert s.get_permission_items('pass') == [(None, None)]
        assert s.get_permission_items('wait') == []

        assert not s.grants('nonsense', None)
        WaitPass().set_permission_items('wait', [])
        WaitPass().get_direct_grantees('wait')




if __name__ == "__main__":
    PermissionManagerTest()

