From 3b4b36fb1d6c9dad80dce8ec2dbf77d269f3d0e7 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 13 Nov 2019 10:31:41 +0100 Subject: [PATCH] Moved MockedPasswordResetTokenGenerator outside of TokenGeneratorTest.test_timeout(). --- tests/auth_tests/test_tokens.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/auth_tests/test_tokens.py b/tests/auth_tests/test_tokens.py index 6d6caab9b1..5db97c3bf5 100644 --- a/tests/auth_tests/test_tokens.py +++ b/tests/auth_tests/test_tokens.py @@ -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):