validator.py: Create a validator for login email.

This validator raises JsonableError exception.

Fixes: #2748
This commit is contained in:
Umair Khan 2017-04-10 11:06:10 +05:00 committed by Tim Abbott
parent 6e425814bf
commit ad2114a7a8
2 changed files with 24 additions and 1 deletions

View File

@ -27,8 +27,12 @@ for any particular type of object.
'''
from __future__ import absolute_import
from django.utils.translation import ugettext as _
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
import six
from typing import Any, Callable, Iterable, Optional, Tuple, TypeVar
from typing import Any, Callable, Iterable, Optional, Tuple, TypeVar, Text
from zerver.lib.request import JsonableError
Validator = Callable[[str, Any], Optional[str]]
@ -144,3 +148,10 @@ def equals(expected_val):
'value': val})
return None
return f
def validate_login_email(email):
# type: (Text) -> None
try:
validate_email(email)
except ValidationError as err:
raise JsonableError(str(err.message))

View File

@ -22,6 +22,8 @@ from zerver.lib.actions import (
do_reactivate_user,
do_set_realm_authentication_methods,
)
from zerver.lib.validator import validate_login_email
from zerver.lib.request import JsonableError
from zerver.lib.initial_password import initial_password
from zerver.lib.sessions import get_session_dict_user
from zerver.lib.test_classes import (
@ -1834,3 +1836,13 @@ class TestAdminSetBackends(ZulipTestCase):
self.assertFalse(github_auth_enabled(realm))
self.assertTrue(dev_auth_enabled(realm))
self.assertFalse(password_auth_enabled(realm))
class LoginEmailValidatorTestCase(TestCase):
def test_valid_email(self):
# type: () -> None
validate_login_email(u'hamlet@zulip.com')
def test_invalid_email(self):
# type: () -> None
with self.assertRaises(JsonableError):
validate_login_email(u'hamlet')