Source

hashish / tests.py

from nose import tools as t

import hashish

class _TestObject(hashish.HashishMixin):
    __PASSWORD_FIELD__ = "password_hash"
    __SALT_COUNT__ = 4

    def __init__(self):
        self.password_hash = None

    @property
    def password_field(self):
        return self.__PASSWORD_FIELD__

    @property
    def salt_count(self):
        return self.__SALT_COUNT__

def test_functional():
    obj = _TestObject()

    t.eq_(obj.password_hash, None, "Password Hash not initialized")
    t.eq_(obj.password_field, "password_hash")
    t.eq_(obj.salt_count, 4)
    obj.password = "1234"
    t.assert_is_not_none(obj.password_hash,
        "Password Hash was not generated when password was set")
    t.assert_not_equal(obj.password_hash, "1234",
        "Password did not hash properly")
    t.eq_(obj.password_hash[:5], "{0};".format(obj.salt))

def test_salt_value():
    obj = _TestObject()

    t.eq_(len(obj.salt), obj.salt_count)

@t.raises(AttributeError)
def test_cannot_read_password():
    obj = _TestObject()
    print obj.password

@t.raises(ValueError)
def test_salt_too_long():
    obj = _TestObject()

    obj.salt="12345"

@t.raises(ValueError)
def test_salt_too_short():
    obj = _TestObject()
    obj.salt="123"

def test_salt_regeneration():
    obj = _TestObject()
    salt1 = obj.salt

    obj.salt = None
    salt2 = obj.salt

    t.assert_not_equal(salt1, salt2)

def test_setting_salt():
    obj = _TestObject()

    obj.salt = "1234"
    t.eq_(obj.salt, "1234")

def test_validate_valid():
    obj = _TestObject()

    obj.password = "1234"
    t.ok_(obj.validate("1234"))

def test_validate_invalid_no_exception():
    obj = _TestObject()

    obj.password = "1234"
    t.ok_(not obj.validate("12345"))

@t.raises(hashish.InvalidPasswordError)
def test_validate_invalid_exception():
    obj = _TestObject()

    obj.password = "1234"
    obj.validate("12345", throw=True)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.