comparison accounts/tests/test_views.py @ 762:840f2579ef1c

Added a test for the forgotten password cycle. This was broken when I upgraded to Django 1.6. Ensure we catch it next time.
author Brian Neal <bgneal@gmail.com>
date Sun, 19 Jan 2014 00:19:44 -0600
parents 8789299c75b1
children 9133b4626a4b
comparison
equal deleted inserted replaced
761:1ddd72f48d73 762:840f2579ef1c
1 """ 1 """
2 View tests for the accounts application. 2 View tests for the accounts application.
3 3
4 """ 4 """
5 import datetime 5 import datetime
6 import re
6 7
7 from django.test import TestCase 8 from django.test import TestCase
8 from django.core.urlresolvers import reverse 9 from django.core.urlresolvers import reverse
9 from django.core import mail 10 from django.core import mail
10 from django.contrib.auth.models import User 11 from django.contrib.auth.models import User
288 self.assertRedirects(response, reverse('accounts-username_sent')) 289 self.assertRedirects(response, reverse('accounts-username_sent'))
289 290
290 self.assertEqual(len(mail.outbox), 1) 291 self.assertEqual(len(mail.outbox), 1)
291 if len(mail.outbox): 292 if len(mail.outbox):
292 self.assertTrue(mail.outbox[0].subject.startswith('Forgotten username')) 293 self.assertTrue(mail.outbox[0].subject.startswith('Forgotten username'))
294
295
296 class ForgotEmailTest(TestCase):
297 """Because we use a custom URL its important to test this. This got broken
298 in Django 1.6 when the URL pattern changed.
299
300 """
301
302 def setUp(self):
303 u = User.objects.create_user('user1', 'user1@example.com', 'pw')
304 u.save()
305
306 def test_nominal_case(self):
307 """Test a full forgot password scenario."""
308
309 # GET password reset page
310 response = self.client.get(reverse('accounts-password_reset'))
311 self.assertEqual(response.status_code, 200)
312
313 # POST email address
314 args = {'email': 'user1@example.com'}
315 response = self.client.post(reverse('accounts-password_reset'), args,
316 follow=True)
317 self.assertRedirects(response, reverse('accounts-password_reset_sent'))
318
319 # Ensure the email was sent
320 self.assertEqual(len(mail.outbox), 1)
321 if (len(mail.outbox)):
322 msg = mail.outbox[0]
323 self.assertTrue(msg.subject.startswith('Password reset'))
324 self.assertTrue(len(msg.to) == 1 and msg.to[0] == 'user1@example.com')
325 msg_text = msg.message().as_string()
326 m = re.search(r'http://example.com/accounts/password/reset/confirm/'
327 r'(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9a-z]+-\w+)/',
328 msg_text)
329 self.assertTrue(m is not None)
330 if m:
331 uidb64, token = m.group('uidb64'), m.group('token')
332
333 # visit the password reset page
334 response = self.client.get(
335 reverse('accounts-password_reset_confirm',
336 kwargs={'uidb64': uidb64, 'token': token}))
337 self.assertEqual(response.status_code, 200)
338
339 # POST new password
340 args = {'new_password1': 'pw2', 'new_password2': 'pw2'}
341 response = self.client.post(
342 reverse('accounts-password_reset_confirm',
343 kwargs={'uidb64': uidb64, 'token': token}),
344 args, follow=True)
345 self.assertRedirects(response,
346 reverse('accounts-password_reset_success'))
347 self.assertEqual(response.status_code, 200)
348
349 # Check new password
350 u = User.objects.get(username='user1')
351 self.assertTrue(check_password('pw2', u.password))