view src/ltpdarepo/tests/test_users.py @ 0:c812c3020b63

Initial import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Thu, 09 Jun 2011 13:16:24 +0200
parents
children eaa352730e44
line wrap: on
line source

import unittest
import MySQLdb as mysql

from ltpdarepo.user import User, _generate_password


class TestCase(unittest.TestCase):

    def test_generate_password(self):
        p1 = _generate_password()
        self.assertEqual(len(p1), 8)
        p2 = _generate_password()
        self.assertEqual(len(p2), 8)
        self.assertNotEqual(p1, p2)

    def test_users(self):
        u = User()
        self.assertEqual(u.username, '')
        self.assertEqual(u.password, '')
        self.assertEqual(u.name, '')
        self.assertEqual(u['username'], '')
        self.assertEqual(u['password'], '')
        self.assertEqual(u['name'], '')

        u = User(username='foo', name='Foo')
        self.assertEqual(u.username, 'foo')
        self.assertEqual(u.password, '')
        self.assertEqual(u.name, 'Foo')
        self.assertEqual(u['username'], 'foo')
        self.assertEqual(u['password'], '')
        self.assertEqual(u['name'], 'Foo')


class DatabaseTestCase(unittest.TestCase):

    def setUp(self):
        from ltpdarepo.admin import wipe, install, upgrade, adduser
        wipe()
        install()
        upgrade()
        adduser('u1', 'u1')

        from ltpdarepo import app
        self.app = app
        self.app.config.update(HOSTNAME='localhost')
        self.ctx = self.app.test_request_context()
        self.ctx.push()

    def tearDown(self):
        self.ctx.pop()
        from ltpdarepo.admin import wipe
        wipe()

    def test_user_load(self):
        u1 = User().load('u1')
        self.assertEqual(u1.username, 'u1')

    def test_user_create(self):
        u2 = User(username='u2', password='u2')
        u2.create()
        u3 = User().load('u2')
        self.assertEqual(u2.username, u3.username)

    def test_user_create_password(self):
        u2 = User(username='u2')
        u2.create()
        self.assertEqual(len(u2.password), 8)

    def test_user_login(self):
        u2 = User(username='u2', password='u2')
        u2.create()
        # test that the user can connect to the database
        conn = mysql.connect(host=self.app.config['HOSTNAME'],
                             user=u2.username, passwd=u2.password)        

    def test_user_login_generated_password(self):
        u2 = User(username='u2')
        u2.create()
        # test that the user can connect to the database
        conn = mysql.connect(host=self.app.config['HOSTNAME'],
                             user=u2.username, passwd=u2.password)        


if __name__ == '__main__':
    unittest.main()