Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #95 from postatum/99278694_user_indexed_twice
Browse files Browse the repository at this point in the history
Move encrypt_password processor to before_validation
  • Loading branch information
jstoiko committed Aug 25, 2015
2 parents 3691eca + 7e75bff commit e3586a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion nefertari/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def random_uuid(**kwargs):
def encrypt_password(**kwargs):
""" Crypt :new_value: if it's not crypted yet. """
new_value = kwargs['new_value']
field = kwargs['field']
min_length = field.params['min_length']
if len(new_value) < min_length:
raise ValueError(
'`{}`: Value length must be more than {}'.format(
field.name, field.params['min_length']))

if new_value and not crypt.match(new_value):
new_value = str(crypt.encode(new_value))
return new_value
Expand All @@ -179,7 +186,7 @@ class AuthUserMixin(AuthModelMethodsMixin):
before_validation=[lower_strip])
password = engine.StringField(
min_length=3, required=True,
after_validation=[encrypt_password])
before_validation=[encrypt_password])
groups = engine.ListField(
item_type=engine.StringField,
choices=['admin', 'user'], default=['user'])
Expand Down
16 changes: 14 additions & 2 deletions tests/test_authentication/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ def test_lower_strip(self, engine_mock):

def test_encrypt_password(self, engine_mock):
from nefertari.authentication import models
field = Mock(params={'min_length': 1})
encrypted = models.encrypt_password(
instance=None, new_value='foo')
instance=None, new_value='foo',
field=field)
assert models.crypt.match(encrypted)
assert encrypted != 'foo'
assert encrypted == models.encrypt_password(
instance=None, new_value=encrypted)
instance=None, new_value=encrypted, field=field)

def test_encrypt_password_failed(self, engine_mock):
from nefertari.authentication import models
field = Mock(params={'min_length': 10})
field.name = 'q'
with pytest.raises(ValueError) as ex:
models.encrypt_password(
instance=None, new_value='foo',
field=field)
assert str(ex.value) == '`q`: Value length must be more than 10'

@patch('nefertari.authentication.models.uuid.uuid4')
def test_create_apikey_token(self, mock_uuid, engine_mock):
Expand Down

0 comments on commit e3586a4

Please sign in to comment.