Fix the LazyCryptContext stays lazy after init

This commit is contained in:
Oleg Pidsadnyi 2016-04-20 14:11:35 +02:00
parent d314076fd1
commit 254e5ce71e
3 changed files with 26 additions and 4 deletions

View File

@ -29,6 +29,7 @@ extras_require = {
'Jinja2>=2.3',
'docutils>=0.10',
'flexmock>=0.9.7',
'mock==2.0.0',
'psycopg2>=2.5.1',
'pytz>=2014.2',
'python-dateutil>=2.2',

View File

@ -130,11 +130,15 @@ class PasswordType(types.TypeDecorator, ScalarCoercible):
# Construct the passlib crypt context.
self.context = LazyCryptContext(**kwargs)
if max_length is None:
max_length = self.calculate_max_length()
self._max_length = max_length
# Set the length to the now-calculated max length.
self.length = max_length
@property
def length(self):
"""Get column length."""
if self._max_length is None:
self._max_length = self.calculate_max_length()
return self._max_length
def calculate_max_length(self):
# Calculate the largest possible encoded password.

View File

@ -1,3 +1,4 @@
import mock
import pytest
import sqlalchemy as sa
from sqlalchemy import inspect
@ -217,3 +218,19 @@ class TestPasswordType(object):
obj = User()
obj.password = b'b'
assert obj.password.hash.decode('utf8').startswith('$pbkdf2-sha256$')
@pytest.mark.parametrize('max_length', [1, 103])
def test_constant_length(self, max_length):
"""
Test that constant max_length is applied.
"""
typ = PasswordType(max_length=max_length)
assert typ.length == max_length
def test_context_is_lazy(self):
"""
Make sure the init doesn't evaluate the lazy context.
"""
onload = mock.Mock(return_value={})
PasswordType(onload=onload)
assert not onload.called