Merge branch 'validation' of https://github.com/strikaco/evennia-dev into strikaco-validation
This commit is contained in:
commit
33f04312f2
9 changed files with 217 additions and 11 deletions
|
|
@ -23,6 +23,9 @@ try:
|
|||
from django.utils import unittest
|
||||
except ImportError:
|
||||
import unittest
|
||||
|
||||
from evennia.server.validators import EvenniaPasswordValidator
|
||||
from evennia.utils.test_resources import EvenniaTest
|
||||
|
||||
from django.test.runner import DiscoverRunner
|
||||
|
||||
|
|
@ -77,3 +80,16 @@ class TestDeprecations(TestCase):
|
|||
self.assertRaises(DeprecationWarning, check_errors, MockSettings(setting))
|
||||
# test check for WEBSERVER_PORTS having correct value
|
||||
self.assertRaises(DeprecationWarning, check_errors, MockSettings("WEBSERVER_PORTS", value=["not a tuple"]))
|
||||
|
||||
class ValidatorTest(EvenniaTest):
|
||||
|
||||
def test_validator(self):
|
||||
# Validator returns None on success and ValidationError on failure.
|
||||
validator = EvenniaPasswordValidator()
|
||||
|
||||
# This password should meet Evennia standards.
|
||||
self.assertFalse(validator.validate('testpassword', user=self.account))
|
||||
|
||||
# This password contains illegal characters and should raise an Exception.
|
||||
from django.core.exceptions import ValidationError
|
||||
self.assertRaises(ValidationError, validator.validate, '(#)[#]<>', user=self.account)
|
||||
51
evennia/server/validators.py
Normal file
51
evennia/server/validators.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import gettext as _
|
||||
import re
|
||||
|
||||
class EvenniaPasswordValidator:
|
||||
|
||||
def __init__(self, regex=r"^[\w. @+\-',]+$", policy="Password should contain a mix of letters, spaces, digits and @/./+/-/_/'/, only."):
|
||||
"""
|
||||
Constructs a standard Django password validator.
|
||||
|
||||
Args:
|
||||
regex (str): Regex pattern of valid characters to allow.
|
||||
policy (str): Brief explanation of what the defined regex permits.
|
||||
|
||||
"""
|
||||
self.regex = regex
|
||||
self.policy = policy
|
||||
|
||||
def validate(self, password, user=None):
|
||||
"""
|
||||
Validates a password string to make sure it meets predefined Evennia
|
||||
acceptable character policy.
|
||||
|
||||
Args:
|
||||
password (str): Password to validate
|
||||
user (None): Unused argument but required by Django
|
||||
|
||||
Returns:
|
||||
None (None): None if password successfully validated,
|
||||
raises ValidationError otherwise.
|
||||
|
||||
"""
|
||||
# Check complexity
|
||||
if not re.findall(self.regex, password):
|
||||
raise ValidationError(
|
||||
_(self.policy),
|
||||
code='evennia_password_policy',
|
||||
)
|
||||
|
||||
def get_help_text(self):
|
||||
"""
|
||||
Returns a user-facing explanation of the password policy defined
|
||||
by this validator.
|
||||
|
||||
Returns:
|
||||
text (str): Explanation of password policy.
|
||||
|
||||
"""
|
||||
return _(
|
||||
"%s From a terminal client, you can also use a phrase of multiple words if you enclose the password in double quotes." % self.policy
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue