Skip to content

Commit

Permalink
Add support for custom email validators on the registration page
Browse files Browse the repository at this point in the history
new setting: EMAIL_VALIDATORS - tuple or list
  • Loading branch information
atodorov committed Feb 12, 2024
1 parent e5fc782 commit 259cc5b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tcms/kiwi_auth/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ def validate_email_already_in_use(email):
raise forms.ValidationError(_("A user with that email already exists."))


def custom_email_validators(email):
for validator in getattr(settings, "EMAIL_VALIDATORS", ()):
validator(email)


class RegistrationForm(UserCreationForm): # pylint: disable=too-many-ancestors
email = forms.EmailField(
validators=[validate_email_already_in_use],
validators=[validate_email_already_in_use, custom_email_validators],
)
captcha = (
fields.CaptchaField(
Expand Down
25 changes: 25 additions & 0 deletions tcms/kiwi_auth/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.forms import ValidationError
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import timezone
Expand Down Expand Up @@ -94,6 +95,11 @@ def test_logout_then_goto_next(self):
self.assertRedirects(response, next_url)


def deny_certain_email_addresses(email):
if email.lower().find("yahoo") > -1:
raise ValidationError("@yahoo email address has been denied")


class TestRegistration(TestCase):
def setUp(self):
self.register_url = reverse("tcms-register")
Expand Down Expand Up @@ -285,6 +291,25 @@ def test_register_user_already_registered(self):
user = User.objects.filter(username="test_user")
self.assertEqual(user.count(), 0)

@override_settings(
EMAIL_VALIDATORS=(deny_certain_email_addresses,),
)
def test_custom_email_validators(self):
response = self.client.post(
self.register_url,
{
"username": "to_be_denied_user",
"password1": __FOR_TESTING__,
"password2": __FOR_TESTING__,
"email": "will-be-denied@yahoo.co.in",
},
follow=False,
)
self.assertContains(response, _("@yahoo email address has been denied"))

user = User.objects.filter(email__icontains="yahoo")
self.assertEqual(user.count(), 0)

def test_first_user_is_superuser(self):
_response, user = self.assert_user_registration("tester_1")

Expand Down
4 changes: 4 additions & 0 deletions tcms/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
SERVER_EMAIL = DEFAULT_FROM_EMAIL = "kiwi@example.com"
EMAIL_SUBJECT_PREFIX = "[Kiwi-TCMS] "

# functions which can perform custom email address validation on the registration page
# see tcms/kiwi_auth/tests/test_views.py::TestRegistration.test_custom_email_validators
EMAIL_VALIDATORS = ()

# SMTP specific settings
# EMAIL_HOST = 'smtp.example.com'
# EMAIL_PORT = 25
Expand Down

0 comments on commit 259cc5b

Please sign in to comment.