Skip to content

Commit

Permalink
Fix email regex (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cooke authored Feb 10, 2022
1 parent 35b9f20 commit fdaaabe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/authentication/providers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import re

from django.contrib.auth import get_user_model
from django.core.validators import EmailValidator
Expand All @@ -20,14 +21,15 @@ def register_user(self, **kwargs):
pass

def validate_email(self, email):
allow_domain = config.get("email_allow")
if allow_domain:
email_validator = EmailValidator(allow_domain)
else:
email_validator = EmailValidator()
if email_validator(email):
if config.get("email_regex") and not re.compile(config.get("email_regex")).match(email):
raise ValidationError("invalid_email")

if config.get("email_domain") and not email.endswith(config.get("email_domain")):
raise ValidationError("invalid_email")

email_validator = EmailValidator()
email_validator(email)

def check_email_or_username_in_use(self, email=None, username=None):
if get_user_model().objects.filter(username=username) or get_user_model().objects.filter(email=email):
raise ValidationError("email_or_username_in_use")
Expand Down
60 changes: 60 additions & 0 deletions src/authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,66 @@ def test_register_teams_disabled(self):
self.assertEqual(response.status_code, HTTP_201_CREATED)
self.assertEqual(get_user_model().objects.get(username="user10").team.name, "user10")

def test_register_with_mail_passing_regex(self):
with self.settings(
MAIL={"SEND_ADDRESS": "no-reply@ractf.co.uk", "SEND_NAME": "RACTF", "SEND_MODE": "SES"},
EMAIL_ENABLED=True
):
config.set("email_regex", ".*@(live\\.)?example\\.ac\\.uk$")
data = {
"username": "user1",
"password": "uO7*$E@0ngqL",
"email": "user@example.ac.uk",
}
response = self.client.post(reverse("register"), data)
config.set("email_regex", None)
self.assertEqual(response.status_code, HTTP_201_CREATED)

def test_register_with_mail_failing_regex(self):
with self.settings(
MAIL={"SEND_ADDRESS": "no-reply@ractf.co.uk", "SEND_NAME": "RACTF", "SEND_MODE": "SES"},
EMAIL_ENABLED=True
):
config.set("email_regex", ".*@(live\\.)?example\\.org$")
data = {
"username": "user1",
"password": "uO7*$E@0ngqL",
"email": "user@example.ac.uk",
}
response = self.client.post(reverse("register"), data)
config.set("email_regex", None)
self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)

def test_register_with_mail_passing_domain(self):
with self.settings(
MAIL={"SEND_ADDRESS": "no-reply@ractf.co.uk", "SEND_NAME": "RACTF", "SEND_MODE": "SES"},
EMAIL_ENABLED=True
):
config.set("email_domain", "example.ac.uk")
data = {
"username": "user1",
"password": "uO7*$E@0ngqL",
"email": "user@example.ac.uk",
}
response = self.client.post(reverse("register"), data)
config.set("email_domain", None)
self.assertEqual(response.status_code, HTTP_201_CREATED)

def test_register_with_mail_failing_domain(self):
with self.settings(
MAIL={"SEND_ADDRESS": "no-reply@ractf.co.uk", "SEND_NAME": "RACTF", "SEND_MODE": "SES"},
EMAIL_ENABLED=True
):
config.set("email_domain", "example.org")
data = {
"username": "user1",
"password": "uO7*$E@0ngqL",
"email": "user@example.ac.uk",
}
response = self.client.post(reverse("register"), data)
config.set("email_domain", None)
self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)


class EmailResendTestCase(APITestCase):
def test_email_resend(self):
Expand Down

0 comments on commit fdaaabe

Please sign in to comment.