Moved MockedPasswordResetTokenGenerator outside of TokenGeneratorTest.test_timeout().
This commit is contained in:
parent
8be79984dc
commit
3b4b36fb1d
|
@ -6,6 +6,14 @@ from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
class MockedPasswordResetTokenGenerator(PasswordResetTokenGenerator):
|
||||||
|
def __init__(self, now):
|
||||||
|
self._now_val = now
|
||||||
|
|
||||||
|
def _now(self):
|
||||||
|
return self._now_val
|
||||||
|
|
||||||
|
|
||||||
class TokenGeneratorTest(TestCase):
|
class TokenGeneratorTest(TestCase):
|
||||||
|
|
||||||
def test_make_token(self):
|
def test_make_token(self):
|
||||||
|
@ -31,24 +39,25 @@ class TokenGeneratorTest(TestCase):
|
||||||
"""The token is valid after n seconds, but no greater."""
|
"""The token is valid after n seconds, but no greater."""
|
||||||
# Uses a mocked version of PasswordResetTokenGenerator so we can change
|
# Uses a mocked version of PasswordResetTokenGenerator so we can change
|
||||||
# the value of 'now'.
|
# the value of 'now'.
|
||||||
class Mocked(PasswordResetTokenGenerator):
|
|
||||||
def __init__(self, now):
|
|
||||||
self._now_val = now
|
|
||||||
|
|
||||||
def _now(self):
|
|
||||||
return self._now_val
|
|
||||||
|
|
||||||
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
||||||
p0 = PasswordResetTokenGenerator()
|
p0 = PasswordResetTokenGenerator()
|
||||||
tk1 = p0.make_token(user)
|
tk1 = p0.make_token(user)
|
||||||
p1 = Mocked(datetime.now() + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT))
|
p1 = MockedPasswordResetTokenGenerator(
|
||||||
|
datetime.now() + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT)
|
||||||
|
)
|
||||||
self.assertTrue(p1.check_token(user, tk1))
|
self.assertTrue(p1.check_token(user, tk1))
|
||||||
p2 = Mocked(datetime.now() + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1)))
|
p2 = MockedPasswordResetTokenGenerator(
|
||||||
|
datetime.now() + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1))
|
||||||
|
)
|
||||||
self.assertFalse(p2.check_token(user, tk1))
|
self.assertFalse(p2.check_token(user, tk1))
|
||||||
with self.settings(PASSWORD_RESET_TIMEOUT=60 * 60):
|
with self.settings(PASSWORD_RESET_TIMEOUT=60 * 60):
|
||||||
p3 = Mocked(datetime.now() + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT))
|
p3 = MockedPasswordResetTokenGenerator(
|
||||||
|
datetime.now() + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT)
|
||||||
|
)
|
||||||
self.assertTrue(p3.check_token(user, tk1))
|
self.assertTrue(p3.check_token(user, tk1))
|
||||||
p4 = Mocked(datetime.now() + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1)))
|
p4 = MockedPasswordResetTokenGenerator(
|
||||||
|
datetime.now() + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1))
|
||||||
|
)
|
||||||
self.assertFalse(p4.check_token(user, tk1))
|
self.assertFalse(p4.check_token(user, tk1))
|
||||||
|
|
||||||
def test_check_token_with_nonexistent_token_and_user(self):
|
def test_check_token_with_nonexistent_token_and_user(self):
|
||||||
|
|
Loading…
Reference in New Issue