annotate accounts/tests/view_tests.py @ 621:08d83015e15d

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