Skip to content

Commit

Permalink
Simplify validation in forms.
Browse files Browse the repository at this point in the history
  • Loading branch information
dokterbob committed May 21, 2016
1 parent 79b19e3 commit 40ee55d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
16 changes: 4 additions & 12 deletions newsletter/forms.py
Expand Up @@ -41,15 +41,11 @@ class SubscribeRequestForm(NewsletterForm):
subscription.
"""

email_field = forms.EmailField(validators=[validate_email_nouser])

def clean_email_field(self):
data = self.cleaned_data['email_field']

if not data:
raise ValidationError(_("An e-mail address is required."))

# Check whether we should be subscribed to as a user
validate_email_nouser(data)

# Check whether we have already been subscribed to
try:
subscription = Subscription.objects.get(
Expand Down Expand Up @@ -78,6 +74,8 @@ class UpdateRequestForm(NewsletterForm):
email being sent.
"""

email_field = forms.EmailField(validators=[validate_email_nouser])

class Meta(NewsletterForm.Meta):
fields = ('email_field',)

Expand All @@ -92,12 +90,6 @@ def clean(self):
def clean_email_field(self):
data = self.cleaned_data['email_field']

if not data:
raise ValidationError(_("An e-mail address is required."))

# Check whether we should update as a user
validate_email_nouser(data)

# Set our instance on the basis of the email field, or raise
# a validationerror
try:
Expand Down
66 changes: 66 additions & 0 deletions tests/test_web.py
Expand Up @@ -568,6 +568,40 @@ def test_subscribe_request_post(self):

self.assertEmailContains(full_activate_url)

@override_settings(NEWSLETTER_CONFIRM_EMAIL_SUBSCRIBE=True)
def test_subscribe_request_post_emptyemail(self):
""" Post the subscription form without email shoud fail. """

response = self.client.post(
self.subscribe_url, {
'name_field': 'Test Name',
'email_field': ''
}
)

self.assertContains(response, 'This field is required.')

@override_settings(NEWSLETTER_CONFIRM_EMAIL_SUBSCRIBE=True)
def test_subscribe_request_post_existinguser_email(self):
""" Post the subscription form without email shoud fail. """

User = get_user_model()
password = User.objects.make_random_password()
user = User.objects.create_user(
'john', 'lennon@thebeatles.com', password)
user.save()

response = self.client.post(
self.subscribe_url, {
'name_field': 'Test Name',
'email_field': user.email
}
)

self.assertContains(
response, "belongs to a user with an account on this site"
)

@override_settings(NEWSLETTER_CONFIRM_EMAIL_SUBSCRIBE=False)
def test_subscribe_request_post_no_email(self):
"""
Expand Down Expand Up @@ -977,6 +1011,38 @@ def test_update_request_post(self):

self.assertEmailContains(full_activate_url)

@override_settings(NEWSLETTER_CONFIRM_EMAIL_SUBSCRIBE=True)
def test_update_request_post_emptyemail(self):
""" Post the update form without email shoud fail. """

response = self.client.post(
self.update_url, {
'email_field': ''
}
)

self.assertContains(response, 'This field is required.')

@override_settings(NEWSLETTER_CONFIRM_EMAIL_SUBSCRIBE=True)
def test_update_request_post_existinguser_email(self):
""" Post the update form without email shoud fail. """

User = get_user_model()
password = User.objects.make_random_password()
user = User.objects.create_user(
'john', 'lennon@thebeatles.com', password)
user.save()

response = self.client.post(
self.update_url, {
'email_field': user.email
}
)

self.assertContains(
response, "belongs to a user with an account on this site"
)

@override_settings(NEWSLETTER_CONFIRM_EMAIL_UPDATE=False)
def test_update_request_post_no_email(self):
"""
Expand Down

0 comments on commit 40ee55d

Please sign in to comment.