annotate gpp/accounts/tests/view_tests.py @ 566:4b9970ad0edb

For bitbucket issue #6, try to improve quoting messages.
author Brian Neal <bgneal@gmail.com>
date Sun, 04 Mar 2012 14:52:24 -0600
parents 6a265b5768ca
children 6301dd73683f
rev   line source
bgneal@565 1 """
bgneal@565 2 View tests for the accounts application.
bgneal@565 3
bgneal@565 4 """
bgneal@565 5 import datetime
bgneal@565 6
bgneal@565 7 from django.test import TestCase
bgneal@565 8 from django.core.urlresolvers import reverse
bgneal@565 9 from django.contrib.auth.models import User, check_password
bgneal@565 10
bgneal@565 11 from antispam.rate_limit import unblock_ip
bgneal@565 12 from accounts.models import PendingUser
bgneal@565 13 from accounts.models import IllegalUsername
bgneal@565 14 from accounts.models import IllegalEmail
bgneal@565 15
bgneal@565 16
bgneal@565 17 class RegistrationTest(TestCase):
bgneal@565 18
bgneal@565 19 def setUp(self):
bgneal@565 20 u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw')
bgneal@565 21 u.save()
bgneal@565 22
bgneal@565 23 # a 2nd user has the same email as another
bgneal@565 24 u = User.objects.create_user('existing_user2', 'existing_user@example.com', 'pw')
bgneal@565 25 u.save()
bgneal@565 26
bgneal@565 27 PendingUser.objects.create(username='pending_user',
bgneal@565 28 email='pending_user@example.com',
bgneal@565 29 password='pw',
bgneal@565 30 date_joined=datetime.datetime.now(),
bgneal@565 31 key='key')
bgneal@565 32
bgneal@565 33 IllegalUsername.objects.create(username='illegalusername')
bgneal@565 34 IllegalEmail.objects.create(email='illegal@example.com')
bgneal@565 35
bgneal@565 36 def tearDown(self):
bgneal@565 37 unblock_ip('127.0.0.1')
bgneal@565 38
bgneal@565 39 def test_get_view(self):
bgneal@565 40 """
bgneal@565 41 Test a simple get of the registration view
bgneal@565 42
bgneal@565 43 """
bgneal@565 44 response = self.client.get(reverse('accounts-register'))
bgneal@565 45 self.assertEqual(response.status_code, 200)
bgneal@565 46
bgneal@565 47 def test_existing_user(self):
bgneal@565 48 """
bgneal@565 49 Ensure we can't register with an existing username.
bgneal@565 50
bgneal@565 51 """
bgneal@565 52 response = self.client.post(reverse('accounts-register'), {
bgneal@565 53 'username': 'existing_user',
bgneal@565 54 'email': 'test@example.com',
bgneal@565 55 'password1': 'my_password',
bgneal@565 56 'password2': 'my_password',
bgneal@565 57 'agree_age': 'on',
bgneal@565 58 'agree_tos': 'on',
bgneal@565 59 'agree_privacy': 'on',
bgneal@565 60 'question1': '101',
bgneal@565 61 'question2': '',
bgneal@565 62 })
bgneal@565 63
bgneal@565 64 self.assertEqual(response.status_code, 200)
bgneal@565 65 self.assertContains(response, 'A user with that username already exists')
bgneal@565 66
bgneal@565 67 def test_pending_user(self):
bgneal@565 68 """
bgneal@565 69 Ensure we can't register with a pending username.
bgneal@565 70
bgneal@565 71 """
bgneal@565 72 response = self.client.post(reverse('accounts-register'), {
bgneal@565 73 'username': 'pending_user',
bgneal@565 74 'email': 'test@example.com',
bgneal@565 75 'password1': 'my_password',
bgneal@565 76 'password2': 'my_password',
bgneal@565 77 'agree_age': 'on',
bgneal@565 78 'agree_tos': 'on',
bgneal@565 79 'agree_privacy': 'on',
bgneal@565 80 'question1': '101',
bgneal@565 81 'question2': '',
bgneal@565 82 })
bgneal@565 83
bgneal@565 84 self.assertEqual(response.status_code, 200)
bgneal@565 85 self.assertContains(response, 'A pending user with that username already exists')
bgneal@565 86
bgneal@565 87 def test_illegal_username(self):
bgneal@565 88 """
bgneal@565 89 Ensure we can't register with a banned username.
bgneal@565 90
bgneal@565 91 """
bgneal@565 92 response = self.client.post(reverse('accounts-register'), {
bgneal@565 93 'username': 'illegalusername',
bgneal@565 94 'email': 'test@example.com',
bgneal@565 95 'password1': 'my_password',
bgneal@565 96 'password2': 'my_password',
bgneal@565 97 'agree_age': 'on',
bgneal@565 98 'agree_tos': 'on',
bgneal@565 99 'agree_privacy': 'on',
bgneal@565 100 'question1': '101',
bgneal@565 101 'question2': '',
bgneal@565 102 })
bgneal@565 103
bgneal@565 104 self.assertEqual(response.status_code, 200)
bgneal@565 105 self.assertContains(response, 'That username is not allowed')
bgneal@565 106
bgneal@565 107 def test_duplicate_existing_email(self):
bgneal@565 108 """
bgneal@565 109 Ensure we can't register with a duplicate email address.
bgneal@565 110
bgneal@565 111 """
bgneal@565 112 response = self.client.post(reverse('accounts-register'), {
bgneal@565 113 'username': 'a_new_user',
bgneal@565 114 'email': 'existing_user@example.com',
bgneal@565 115 'password1': 'my_password',
bgneal@565 116 'password2': 'my_password',
bgneal@565 117 'agree_age': 'on',
bgneal@565 118 'agree_tos': 'on',
bgneal@565 119 'agree_privacy': 'on',
bgneal@565 120 'question1': '101',
bgneal@565 121 'question2': '',
bgneal@565 122 })
bgneal@565 123
bgneal@565 124 self.assertEqual(response.status_code, 200)
bgneal@565 125 self.assertContains(response, 'A user with that email address already exists')
bgneal@565 126
bgneal@565 127 def test_duplicate_pending_email(self):
bgneal@565 128 """
bgneal@565 129 Ensure we can't register with a duplicate email address.
bgneal@565 130
bgneal@565 131 """
bgneal@565 132 response = self.client.post(reverse('accounts-register'), {
bgneal@565 133 'username': 'a_new_user',
bgneal@565 134 'email': 'pending_user@example.com',
bgneal@565 135 'password1': 'my_password',
bgneal@565 136 'password2': 'my_password',
bgneal@565 137 'agree_age': 'on',
bgneal@565 138 'agree_tos': 'on',
bgneal@565 139 'agree_privacy': 'on',
bgneal@565 140 'question1': '101',
bgneal@565 141 'question2': '',
bgneal@565 142 })
bgneal@565 143
bgneal@565 144 self.assertEqual(response.status_code, 200)
bgneal@565 145 self.assertContains(response, 'A pending user with that email address already exists')
bgneal@565 146
bgneal@565 147 def test_illegal_email(self):
bgneal@565 148 """
bgneal@565 149 Ensure we can't register with a banned email address.
bgneal@565 150
bgneal@565 151 """
bgneal@565 152 response = self.client.post(reverse('accounts-register'), {
bgneal@565 153 'username': 'a_new_user',
bgneal@565 154 'email': 'illegal@example.com',
bgneal@565 155 'password1': 'my_password',
bgneal@565 156 'password2': 'my_password',
bgneal@565 157 'agree_age': 'on',
bgneal@565 158 'agree_tos': 'on',
bgneal@565 159 'agree_privacy': 'on',
bgneal@565 160 'question1': '101',
bgneal@565 161 'question2': '',
bgneal@565 162 })
bgneal@565 163
bgneal@565 164 self.assertEqual(response.status_code, 200)
bgneal@565 165 self.assertContains(response, 'That email address is not allowed')
bgneal@565 166
bgneal@565 167 def test_password_match(self):
bgneal@565 168 """
bgneal@565 169 Ensure the passwords match.
bgneal@565 170
bgneal@565 171 """
bgneal@565 172 response = self.client.post(reverse('accounts-register'), {
bgneal@565 173 'username': 'a_new_user',
bgneal@565 174 'email': 'test@example.com',
bgneal@565 175 'password1': 'my_password',
bgneal@565 176 'password2': 'my_password_doesnt match',
bgneal@565 177 'agree_age': 'on',
bgneal@565 178 'agree_tos': 'on',
bgneal@565 179 'agree_privacy': 'on',
bgneal@565 180 'question1': '101',
bgneal@565 181 'question2': '',
bgneal@565 182 })
bgneal@565 183
bgneal@565 184 self.assertEqual(response.status_code, 200)
bgneal@565 185 self.assertContains(response, "The two password fields didn&#39;t match")
bgneal@565 186
bgneal@565 187 def test_question1(self):
bgneal@565 188 """
bgneal@565 189 Ensure our anti-spam question is answered.
bgneal@565 190
bgneal@565 191 """
bgneal@565 192 response = self.client.post(reverse('accounts-register'), {
bgneal@565 193 'username': 'a_new_user',
bgneal@565 194 'email': 'test@example.com',
bgneal@565 195 'password1': 'my_password',
bgneal@565 196 'password2': 'my_password_doesnt match',
bgneal@565 197 'agree_age': 'on',
bgneal@565 198 'agree_tos': 'on',
bgneal@565 199 'agree_privacy': 'on',
bgneal@565 200 'question1': 'huh',
bgneal@565 201 'question2': '',
bgneal@565 202 })
bgneal@565 203
bgneal@565 204 self.assertEqual(response.status_code, 200)
bgneal@565 205 self.assertContains(response, "Incorrect answer to our anti-spam question")
bgneal@565 206
bgneal@565 207 def test_question2(self):
bgneal@565 208 """
bgneal@565 209 Ensure our honeypot question check works.
bgneal@565 210
bgneal@565 211 """
bgneal@565 212 response = self.client.post(reverse('accounts-register'), {
bgneal@565 213 'username': 'a_new_user',
bgneal@565 214 'email': 'test@example.com',
bgneal@565 215 'password1': 'my_password',
bgneal@565 216 'password2': 'my_password_doesnt match',
bgneal@565 217 'agree_age': 'on',
bgneal@565 218 'agree_tos': 'on',
bgneal@565 219 'agree_privacy': 'on',
bgneal@565 220 'question1': '101',
bgneal@565 221 'question2': 'non blank',
bgneal@565 222 })
bgneal@565 223
bgneal@565 224 self.assertEqual(response.status_code, 403)
bgneal@565 225
bgneal@565 226 def test_success(self):
bgneal@565 227 """
bgneal@565 228 Ensure we can successfully register.
bgneal@565 229
bgneal@565 230 """
bgneal@565 231 response = self.client.post(reverse('accounts-register'), {
bgneal@565 232 'username': 'a_new_user',
bgneal@565 233 'email': 'test@example.com',
bgneal@565 234 'password1': 'my_password',
bgneal@565 235 'password2': 'my_password',
bgneal@565 236 'agree_age': 'on',
bgneal@565 237 'agree_tos': 'on',
bgneal@565 238 'agree_privacy': 'on',
bgneal@565 239 'question1': '101',
bgneal@565 240 'question2': '',
bgneal@565 241 })
bgneal@565 242
bgneal@565 243 self.assertEqual(response.status_code, 302)
bgneal@565 244
bgneal@565 245 try:
bgneal@565 246 pending = PendingUser.objects.get(username='a_new_user')
bgneal@565 247 except PendingUser.DoesNotExist:
bgneal@565 248 self.fail("PendingUser was not created")
bgneal@565 249
bgneal@565 250 self.assertEqual(pending.email, 'test@example.com')
bgneal@565 251 self.assertTrue(datetime.datetime.now() - pending.date_joined <
bgneal@565 252 datetime.timedelta(minutes=1))
bgneal@565 253 self.assertTrue(check_password('my_password', pending.password))