Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Converted contrib/auth/tokens doctests to unittests. We've always sai…

…d "no more" to doctests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14100 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f3429da6a06c43dd86fbda45c120e96986cb6daa 1 parent 41551a0
Luke Plant authored October 10, 2010
6  django/contrib/auth/tests/__init__.py
@@ -5,12 +5,8 @@
5 5
 from django.contrib.auth.tests.remote_user \
6 6
         import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
7 7
 from django.contrib.auth.tests.models import ProfileTestCase
8  
-from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS
  8
+from django.contrib.auth.tests.tokens import TokenGeneratorTest
9 9
 from django.contrib.auth.tests.views \
10 10
         import PasswordResetTest, ChangePasswordTest, LoginTest, LogoutTest
11 11
 
12 12
 # The password for the fixture data users is 'password'
13  
-
14  
-__test__ = {
15  
-    'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS,
16  
-}
89  django/contrib/auth/tests/tokens.py
... ...
@@ -1,37 +1,52 @@
1  
-TOKEN_GENERATOR_TESTS = """
2  
->>> from django.contrib.auth.models import User, AnonymousUser
3  
->>> from django.contrib.auth.tokens import PasswordResetTokenGenerator
4  
->>> from django.conf import settings
5  
->>> u = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
6  
->>> p0 = PasswordResetTokenGenerator()
7  
->>> tk1 = p0.make_token(u)
8  
->>> p0.check_token(u, tk1)
9  
-True
10  
-
11  
->>> u = User.objects.create_user('comebackkid', 'test3@example.com', 'testpw')
12  
->>> p0 = PasswordResetTokenGenerator()
13  
->>> tk1 = p0.make_token(u)
14  
->>> reload = User.objects.get(username='comebackkid')
15  
->>> tk2 = p0.make_token(reload)
16  
->>> tk1 == tk2
17  
-True
18  
-
19  
-Tests to ensure we can use the token after n days, but no greater.
20  
-Use a mocked version of PasswordResetTokenGenerator so we can change
21  
-the value of 'today'
22  
-
23  
->>> class Mocked(PasswordResetTokenGenerator):
24  
-...     def __init__(self, today):
25  
-...         self._today_val = today
26  
-...     def _today(self):
27  
-...         return self._today_val
28  
-
29  
->>> from datetime import date, timedelta
30  
->>> p1 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS))
31  
->>> p1.check_token(u, tk1)
32  
-True
33  
->>> p2 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS + 1))
34  
->>> p2.check_token(u, tk1)
35  
-False
36  
-
37  
-"""
  1
+from datetime import date, timedelta
  2
+
  3
+from django.conf import settings
  4
+from django.contrib.auth.models import User, AnonymousUser
  5
+from django.contrib.auth.tokens import PasswordResetTokenGenerator
  6
+from django.test import TestCase
  7
+
  8
+
  9
+class TokenGeneratorTest(TestCase):
  10
+
  11
+    def test_make_token(self):
  12
+        """
  13
+        Ensure that we can make a token and that it is valid
  14
+        """
  15
+        user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
  16
+        p0 = PasswordResetTokenGenerator()
  17
+        tk1 = p0.make_token(user)
  18
+        self.assertTrue(p0.check_token(user, tk1))
  19
+
  20
+    def test_10265(self):
  21
+        """
  22
+        Ensure that the token generated for a user created in the same request
  23
+        will work correctly.
  24
+        """
  25
+        # See ticket #10265
  26
+        user = User.objects.create_user('comebackkid', 'test3@example.com', 'testpw')
  27
+        p0 = PasswordResetTokenGenerator()
  28
+        tk1 = p0.make_token(user)
  29
+        reload = User.objects.get(username='comebackkid')
  30
+        tk2 = p0.make_token(reload)
  31
+        self.assertEqual(tk1, tk2)
  32
+
  33
+    def test_timeout(self):
  34
+        """
  35
+        Ensure we can use the token after n days, but no greater.
  36
+        """
  37
+        # Uses a mocked version of PasswordResetTokenGenerator so we can change
  38
+        # the value of 'today'
  39
+        class Mocked(PasswordResetTokenGenerator):
  40
+            def __init__(self, today):
  41
+                self._today_val = today
  42
+            def _today(self):
  43
+                return self._today_val
  44
+
  45
+        user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
  46
+        p0 = PasswordResetTokenGenerator()
  47
+        tk1 = p0.make_token(user)
  48
+        p1 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS))
  49
+        self.assertTrue(p1.check_token(user, tk1))
  50
+
  51
+        p2 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS + 1))
  52
+        self.assertFalse(p2.check_token(user, tk1))

0 notes on commit f3429da

Please sign in to comment.
Something went wrong with that request. Please try again.