annotate accounts/tests/view_tests.py @ 683:c83d330cb65f

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