Skip to content

Commit

Permalink
add validation for eventbrite urls
Browse files Browse the repository at this point in the history
  • Loading branch information
elichad committed Nov 21, 2023
1 parent 4240ba0 commit e0ac593
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions amy/extforms/forms.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down
40 changes: 40 additions & 0 deletions amy/extforms/tests/test_training_request_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

0 comments on commit e0ac593

Please sign in to comment.