users_test.py 2.04 KB
Newer Older
Romain Bignon's avatar
Romain Bignon committed
1 2
from assnet.storage import Storage
from assnet.users import User
3 4 5 6
from unittest import TestCase
from tempfile import mkdtemp
import shutil

Laurent Bachelier's avatar
Laurent Bachelier committed
7

Laurent Bachelier's avatar
Laurent Bachelier committed
8
class UsersTest(TestCase):
9
    def setUp(self):
Romain Bignon's avatar
Romain Bignon committed
10
        self.root = mkdtemp(prefix='assnet_test_root')
11
        self.storage = Storage.create(self.root)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    def tearDown(self):
        if self.root:
            shutil.rmtree(self.root)

    def test_password(self):
        u = User(self.storage, 'penguin')
        assert not u.is_valid_password('hello')
        self.assertRaises(AssertionError, u.is_valid_password, None)
        self.assertRaises(AssertionError, u.is_valid_password, False)
        assert not u.is_valid_password('')

        u.password = 'hello'
        assert u.is_valid_password('hello')
        assert not u.is_valid_password('HELLO')
        assert not u.is_valid_password('')

        u.save()
        assert u.password is True
        self.assertRaises(AssertionError, u.is_valid_password, True)
        assert u.is_valid_password('hello')
        assert not u.is_valid_password('HELLO')
        assert not u.is_valid_password('')

        u = User(self.storage, 'penguin')
        u.read()
        assert u.password is True
        assert u.is_valid_password('hello')
        assert not u.is_valid_password('HELLO')
        assert not u.is_valid_password('')

        # Set the password to the same value.
        # If the salts and hashed passwords are the same, either our hashing
        # algorithm is weak or it's the end of the word.
        password1 = u.data['auth']['password']
        salt1 = u.data['auth']['salt']
        u.password = 'hello'
        u.save()
        password2 = u.data['auth']['password']
        salt2 = u.data['auth']['salt']
        assert password1 != password2
        assert salt1 != salt2
54 55 56 57 58 59 60 61 62 63 64 65 66

    def test_key(self):
        u1 = User(self.storage, 'penguin')
        u1.read()
        assert u1.key is None
        u1.gen_key()
        u1.save()

        u2 = User(self.storage, 'penguin')
        u2.read()
        assert len(u2.key)
        u2.gen_key()
        assert u2.key != u1.key