From e574be4405a0bf7a946839e8dc294e1f5728e457 Mon Sep 17 00:00:00 2001 From: Mac Chaffee Date: Sun, 14 May 2017 18:09:49 -0400 Subject: [PATCH] Add unit tests for SignupForm --- pgweb/account/tests/__init__.py | 0 pgweb/account/tests/test_forms.py | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 pgweb/account/tests/__init__.py create mode 100644 pgweb/account/tests/test_forms.py diff --git a/pgweb/account/tests/__init__.py b/pgweb/account/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pgweb/account/tests/test_forms.py b/pgweb/account/tests/test_forms.py new file mode 100644 index 0000000..7510964 --- /dev/null +++ b/pgweb/account/tests/test_forms.py @@ -0,0 +1,55 @@ +# -*- coding: UTF-8 -*- +from django.test import TestCase +from django.contrib.auth.models import User +from pgweb.account.forms import SignupForm + +class TestSignupForm(TestCase): + def setUp(self): + """Create a user to test username and email uniqueness""" + User.objects.create_user( + username='test-user', + password='test-pass', + email='test@example.com' + ) + + def test_clean_username(self): + """Usernames should be unique and only contain letters, numbers, ., and -""" + invalid_usernames = [ + '1234567890123456789012345678901', # More than 30 characters + 'test-user#5', # Invalid ascii character + u'слон' # Non-ascii characters + 'test-user', # Username already exists from setUp + '' # Self-explanatory + ] + for username in invalid_usernames: + form = SignupForm(remoteip='127.0.0.1', data={'username': username}) + # Accessing form.errors triggers a full clean automatically + self.assertIn('username', form.errors, msg="There should be validation errors since the username is %s" % username) + + def test_clean_emails(self): + """Emails should be unique and the email field must equal email2""" + existing_email_data = { + 'email': 'test@example.com' + } + form = SignupForm(remoteip='127.0.0.1', data=existing_email_data) + self.assertIn('email', form.errors, msg="The email should be invalid since it's already in use") + + mismatched_email_data = { + 'email': 'new@example.com', + 'email2': 'new2@example.com' + } + form = SignupForm(remoteip='127.0.0.1', data=mismatched_email_data) + self.assertIn('email2', form.errors, msg="The 'email2' field should be invalid since it didn't match 'email'") + + def test_valid_data(self): + """Forms with valid data should be... valid""" + valid_data = { + 'username': '-slonik.1-', + 'first_name': 'Slonik', + 'last_name': 'Elephant', + 'password': 'test-pass', + 'email': 'slonik@postgresql.org', + 'email2': 'slonik@postgresql.org', + } + form = SignupForm(remoteip='127.0.0.1', data=valid_data) + self.assertTrue(form.is_valid(), msg="This form should be valid. Errors are: %s" % form.errors) -- 2.11.0