Moved MockedPasswordResetTokenGenerator outside of TokenGeneratorTest.test_timeout().

This commit is contained in:
Mariusz Felisiak 2019-11-13 10:31:41 +01:00
parent 8be79984dc
commit 3b4b36fb1d
1 changed files with 20 additions and 11 deletions

View File

@ -6,6 +6,14 @@ from django.contrib.auth.tokens import PasswordResetTokenGenerator
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):
def test_make_token(self):
@ -31,24 +39,25 @@ class TokenGeneratorTest(TestCase):
"""The token is valid after n seconds, but no greater."""
# Uses a mocked version of PasswordResetTokenGenerator so we can change
# 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')
p0 = PasswordResetTokenGenerator()
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))
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))
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))
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))
def test_check_token_with_nonexistent_token_and_user(self):