Merge pull request #211 from olegpidsadnyi/fixlazyinit
Fix the LazyCryptContext stays lazy after init
This commit is contained in:
1
setup.py
1
setup.py
@@ -29,6 +29,7 @@ extras_require = {
|
|||||||
'Jinja2>=2.3',
|
'Jinja2>=2.3',
|
||||||
'docutils>=0.10',
|
'docutils>=0.10',
|
||||||
'flexmock>=0.9.7',
|
'flexmock>=0.9.7',
|
||||||
|
'mock==2.0.0',
|
||||||
'psycopg2>=2.5.1',
|
'psycopg2>=2.5.1',
|
||||||
'pytz>=2014.2',
|
'pytz>=2014.2',
|
||||||
'python-dateutil>=2.2',
|
'python-dateutil>=2.2',
|
||||||
|
@@ -155,11 +155,15 @@ class PasswordType(types.TypeDecorator, ScalarCoercible):
|
|||||||
# Construct the passlib crypt context.
|
# Construct the passlib crypt context.
|
||||||
self.context = LazyCryptContext(**kwargs)
|
self.context = LazyCryptContext(**kwargs)
|
||||||
|
|
||||||
if max_length is None:
|
self._max_length = max_length
|
||||||
max_length = self.calculate_max_length()
|
|
||||||
|
|
||||||
# Set the length to the now-calculated max length.
|
@property
|
||||||
self.length = max_length
|
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):
|
def calculate_max_length(self):
|
||||||
# Calculate the largest possible encoded password.
|
# Calculate the largest possible encoded password.
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import inspect
|
from sqlalchemy import inspect
|
||||||
@@ -217,3 +218,19 @@ class TestPasswordType(object):
|
|||||||
obj = User()
|
obj = User()
|
||||||
obj.password = b'b'
|
obj.password = b'b'
|
||||||
assert obj.password.hash.decode('utf8').startswith('$pbkdf2-sha256$')
|
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
|
||||||
|
Reference in New Issue
Block a user