Use Levenshtein not nltk
This commit is contained in:
committed by
Michael Terry
parent
2335b57afc
commit
d40ce8c8d0
@@ -10,10 +10,11 @@ from __future__ import division
|
||||
import string
|
||||
import unicodedata
|
||||
|
||||
from nltk.metrics.distance import edit_distance
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from Levenshtein import distance
|
||||
from six import text_type
|
||||
|
||||
|
||||
def validate_password_strength(value):
|
||||
@@ -118,6 +119,6 @@ def validate_password_dictionary(value):
|
||||
|
||||
if password_max_edit_distance and password_dictionary:
|
||||
for word in password_dictionary:
|
||||
distance = edit_distance(value, word)
|
||||
if distance <= password_max_edit_distance:
|
||||
edit_distance = distance(text_type(value), text_type(word))
|
||||
if edit_distance <= password_max_edit_distance:
|
||||
raise ValidationError(_("Too similar to a restricted dictionary word."), code="dictionary_word")
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Tests for util.password_policy_validators module."""
|
||||
|
||||
import unittest
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from util.password_policy_validators import validate_password_dictionary
|
||||
|
||||
|
||||
class PasswordPolicyValidatorsTestCase(unittest.TestCase):
|
||||
""" Tests for password validator utility functions """
|
||||
|
||||
@override_settings(PASSWORD_DICTIONARY_EDIT_DISTANCE_THRESHOLD=2)
|
||||
@override_settings(PASSWORD_DICTIONARY=['testme'])
|
||||
def test_validate_password_dictionary(self):
|
||||
""" Tests dictionary checks """
|
||||
# Direct match
|
||||
with self.assertRaises(ValidationError):
|
||||
validate_password_dictionary('testme')
|
||||
|
||||
# Off by one
|
||||
with self.assertRaises(ValidationError):
|
||||
validate_password_dictionary('estme')
|
||||
|
||||
# Off by two
|
||||
with self.assertRaises(ValidationError):
|
||||
validate_password_dictionary('bestmet')
|
||||
|
||||
# Off by three (should pass)
|
||||
validate_password_dictionary('bestem')
|
||||
Reference in New Issue
Block a user