From e0ac593850df8ffde02792343033e8baa6aa92a9 Mon Sep 17 00:00:00 2001 From: Eli Chadwick Date: Tue, 21 Nov 2023 12:36:40 +0000 Subject: [PATCH] add validation for eventbrite urls --- amy/extforms/forms.py | 7 ++++ .../tests/test_training_request_form.py | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/amy/extforms/forms.py b/amy/extforms/forms.py index 15363c3de..62491e6ff 100644 --- a/amy/extforms/forms.py +++ b/amy/extforms/forms.py @@ -1,5 +1,6 @@ from datetime import date from typing import Iterable, cast +from urllib.parse import urlparse from captcha.fields import ReCaptchaField from crispy_forms.layout import HTML, Div, Field, Layout @@ -251,6 +252,12 @@ def validate_member_code( return errors + def clean_eventbrite_url(self): + """Check that entered URL includes 'eventbrite' in the domain.""" + eventbrite_url = self.cleaned_data.get("eventbrite_url", "") + if eventbrite_url and "eventbrite" not in urlparse(eventbrite_url).netloc: + raise ValidationError("Must be an Eventbrite URL.") + def clean(self): super().clean() errors = dict() diff --git a/amy/extforms/tests/test_training_request_form.py b/amy/extforms/tests/test_training_request_form.py index a87ca36dc..1d76d39b8 100644 --- a/amy/extforms/tests/test_training_request_form.py +++ b/amy/extforms/tests/test_training_request_form.py @@ -18,6 +18,7 @@ class TestTrainingRequestForm(TestBase): MEMBER_CODE_OVERRIDE_EMAIL_WARNING = ( "A member of our team will check the code and follow up with you" ) + INVALID_EVENTBRITE_URL_ERROR = "Must be an Eventbrite URL." def setUp(self): self._setUpUsersAndLogin() @@ -470,3 +471,42 @@ def test_member_code_validation__code_invalid_override_full_request(self): self.assertNotIn( settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"], msg.body ) + + def test_eventbrite_url_validation__none(self): + """Should not error if no URL is entered.""" + # Arrange + self.setUpMembership() + data = {"eventbrite_url": ""} + + # Act + rv = self.client.post(reverse("training_request"), data=data) + + # Assert + self.assertEqual(rv.status_code, 200) + self.assertNotContains(rv, self.INVALID_EVENTBRITE_URL_ERROR) + + def test_eventbrite_url_validation__invalid(self): + """Should error if a non-Eventbrite URL is entered.""" + # Arrange + self.setUpMembership() + data = {"eventbrite_url": "https://google.com"} + + # Act + rv = self.client.post(reverse("training_request"), data=data) + + # Assert + self.assertEqual(rv.status_code, 200) + self.assertContains(rv, self.INVALID_EVENTBRITE_URL_ERROR) + + def test_eventbrite_url_validation__valid(self): + """Should not error if an Eventbrite URL is entered.""" + # Arrange + self.setUpMembership() + data = {"eventbrite_url": "https://www.eventbrite.com/e/711576483417"} + + # Act + rv = self.client.post(reverse("training_request"), data=data) + + # Assert + self.assertEqual(rv.status_code, 200) + self.assertNotContains(rv, self.INVALID_EVENTBRITE_URL_ERROR)