"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/utest_user.py $
$Id: utest_user.py 29435 2007-02-20 20:41:24Z dbinger $
"""
from dulcinea.common import CommonTest
from dulcinea.user import get_realm, DulcineaUser as User, add_user
from dulcinea.user import get_matching_user
from sancho.utest import raises
from qp.pub.common import get_users

class DulcineaUserTest (CommonTest):

    def check_init (self):
        raises(TypeError, User, '!@#$%^')
        User('amk')
        u = User(None)
        u.set_id('foo')
        raises(AssertionError, User('foo').set_id, 'foo')
        raises(TypeError, User(None).set_id, '!foo')
        u = User(None)
        assert u.id == None
        u.set_id('foo')
        assert u.id == "foo"
        raises(AssertionError, u.set_id, 'bar')

    def check_password (self):
        user = User('foo')
        user.set_password("biteme")
        assert user.has_password("biteme")
        assert not user.has_password("dontbiteme")
        assert not user.has_password("biteme", realm='bogus')
        assert user.has_password("biteme", realm=get_realm())
        password1 = user.generate_password()
        assert user.has_password(password1)
        password2 = user.generate_password()
        assert user.has_password(password2)
        assert password1 != password2

    def check_add(self):
        user1 = User('1.1')
        user1.set_password('foo')
        user2 = User('2')
        add_user(user1)
        add_user(user2)
        user2.set_password('junk')
        assert get_users().get('1.1') == user1
        assert get_users()['1.1'] == user1
        raises(AssertionError, add_user, user1)

    def check_match(self):
        user1 = User('1.5')
        user1.set_email('foo@bar.com')
        add_user(user1)
        assert get_matching_user('notfound@bar.com') == None
        assert get_matching_user('foo@bar.com') == user1

if __name__ == "__main__":
    DulcineaUserTest()
