"""
open/DurusWorks/qp/pub/test/utest_user.py

"""
from qp.pub.common import clear_publisher
from qp.pub.publish import Publisher
from qp.pub.user import User, Permissions
from qp.pub.user import get_valid_permissions
from qp.pub.user import get_users_with_permissions_for_granter
from sancho.utest import UTest
from qp.lib.qtest import QTest
from durus.persistent import PersistentObject
from qp.pub.common import get_publisher
import random

class TmpUser(User):

    global_permissions = {
        "perm1" : "Global"
        }

class TmpGranter(PersistentObject):

    valid_permissions = {
        "perm2" : "Granter",
        }

class TestPermission(QTest):

    def _pre(self):
        QTest._pre(self)
        self.user1 = TmpUser(str(random.random()))
        self.user1.get_permissions().grant("perm1", True)
        self.granter = TmpGranter()
        self.user2 = TmpUser(str(random.random()))
        self.user2.get_permissions().grant("perm2", self.granter)
        get_publisher().add_user(self.user1)
        get_publisher().add_user(self.user2)

    def a(self):
        assert get_valid_permissions(self.user1, True) == dict(perm1="Global")
        assert get_valid_permissions(self.user1, self.granter) == dict(perm2="Granter")

    def b(self):
        assert get_users_with_permissions_for_granter(self.granter, get_valid_permissions(self.user2, self.granter)) == {self.user2:['perm2']}, (
            get_users_with_permissions_for_granter(self.granter, get_valid_permissions(self.user2, self.granter)))

class TestUser(UTest):

    def _pre(self):
        clear_publisher()

    def _post(self):
        clear_publisher()

    def check_a(self):
        publisher = Publisher()
        user = User('a')
        assert user
        assert not User('')
        assert user.get_id() == 'a'
        assert user.get_digester().get_realms() == []
        user.set_password('foo')
        assert user.get_digester().get_realms()
        assert user.has_password('foo')
        assert not user.has_password('bah')
        user.set_password('')
        assert user.get_digester().get_realms() == []
        assert isinstance(user.get_permissions(), Permissions)
        assert not user.is_granted('power')
        assert not user.is_admin()
        try:
            user.get_permissions().grant('administrator', None)
            assert 0
        except TypeError:
            pass
        user.get_permissions().grant('administrator', user)
        assert not user.is_admin()
        user.get_permissions().ungrant('administrator', user)
        user.get_permissions().grant('administrator', user)
        user.get_permissions().grant('administrator', True)
        assert user.is_admin()
        user.get_permissions().ungrant('administrator', True)
        assert not user.is_admin()
        user.get_permissions().grant('power', user)
        assert not user.is_granted('power')
        assert user.is_granted('power', user)
        token = user.get_tokens().new_token()
        assert token in user.get_tokens()
        user.get_tokens().discard(token)
        assert token not in user.get_tokens()
        user.get_tokens().discard(token)
        user.get_tokens().clear()

if __name__ == '__main__':
    TestUser()
    TestPermission()
