diff --git a/ynr/forms.py b/ynr/forms.py index cc9b423e38..29f70c2da1 100644 --- a/ynr/forms.py +++ b/ynr/forms.py @@ -1,4 +1,5 @@ from allauth.account.forms import LoginForm, SignupForm +from django.forms import ValidationError class CustomLoginForm(LoginForm): @@ -15,3 +16,13 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for field in ("username", "email", "password1", "password2"): del self.fields[field].widget.attrs["placeholder"] + + def clean_email(self): + email = self.cleaned_data["email"] + prohibited_domains = [".gov", ".ac.uk"] + for domain in prohibited_domains: + if email.endswith(domain): + raise ValidationError( + "Please use a non-government or non-academic email address." + ) + return email diff --git a/ynr/tests/test_signup.py b/ynr/tests/test_signup.py new file mode 100644 index 0000000000..2a34754c2d --- /dev/null +++ b/ynr/tests/test_signup.py @@ -0,0 +1,20 @@ +from django_webtest import WebTest + + +class TestSignUpPage(WebTest): + def test_invalid_email(self): + response = self.app.get("/accounts/signup/") + + self.assertEqual(response.status_code, 200) + # submit the form with an prohibited email domain + form = response.forms["signup_form"] + form["email"] = "john@me.gov" + form["password1"] = "password" + form["password2"] = "password" + response = form.submit() + self.assertFormError( + response, + "form", + "email", + "Please use a non-government or non-academic email address.", + ) diff --git a/ynr/urls.py b/ynr/urls.py index b0d4e6316f..612d1f2d76 100644 --- a/ynr/urls.py +++ b/ynr/urls.py @@ -5,7 +5,6 @@ from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.views.generic import TemplateView - from allauth.account.views import SignupView