changeset 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 1ddd72f48d73
children 20a3bf7a6370
files accounts/tests/test_views.py
diffstat 1 files changed, 59 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/accounts/tests/test_views.py	Sat Jan 11 19:43:37 2014 -0600
+++ b/accounts/tests/test_views.py	Sun Jan 19 00:19:44 2014 -0600
@@ -3,6 +3,7 @@
 
 """
 import datetime
+import re
 
 from django.test import TestCase
 from django.core.urlresolvers import reverse
@@ -290,3 +291,61 @@
         self.assertEqual(len(mail.outbox), 1)
         if len(mail.outbox):
             self.assertTrue(mail.outbox[0].subject.startswith('Forgotten username'))
+
+
+class ForgotEmailTest(TestCase):
+    """Because we use a custom URL its important to test this. This got broken
+    in Django 1.6 when the URL pattern changed.
+
+    """
+
+    def setUp(self):
+        u = User.objects.create_user('user1', 'user1@example.com', 'pw')
+        u.save()
+
+    def test_nominal_case(self):
+        """Test a full forgot password scenario."""
+
+        # GET password reset page
+        response = self.client.get(reverse('accounts-password_reset'))
+        self.assertEqual(response.status_code, 200)
+
+        # POST email address
+        args = {'email': 'user1@example.com'}
+        response = self.client.post(reverse('accounts-password_reset'), args,
+                follow=True)
+        self.assertRedirects(response, reverse('accounts-password_reset_sent'))
+
+        # Ensure the email was sent
+        self.assertEqual(len(mail.outbox), 1)
+        if (len(mail.outbox)):
+            msg = mail.outbox[0]
+            self.assertTrue(msg.subject.startswith('Password reset'))
+            self.assertTrue(len(msg.to) == 1 and msg.to[0] == 'user1@example.com')
+            msg_text = msg.message().as_string()
+            m = re.search(r'http://example.com/accounts/password/reset/confirm/'
+                r'(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9a-z]+-\w+)/',
+                msg_text)
+            self.assertTrue(m is not None)
+            if m:
+                uidb64, token = m.group('uidb64'), m.group('token')
+
+            # visit the password reset page
+            response = self.client.get(
+                    reverse('accounts-password_reset_confirm',
+                            kwargs={'uidb64': uidb64, 'token': token}))
+            self.assertEqual(response.status_code, 200)
+
+            # POST new password
+            args = {'new_password1': 'pw2', 'new_password2': 'pw2'}
+            response = self.client.post(
+                    reverse('accounts-password_reset_confirm',
+                            kwargs={'uidb64': uidb64, 'token': token}),
+                    args, follow=True)
+            self.assertRedirects(response,
+                    reverse('accounts-password_reset_success'))
+            self.assertEqual(response.status_code, 200)
+
+            # Check new password
+            u = User.objects.get(username='user1')
+            self.assertTrue(check_password('pw2', u.password))