From 8725d04764e3528cbaefd0925501f8403d22ae3e Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 12 Mar 2020 10:56:38 +0100 Subject: [PATCH] Fixed random auth_tests.test_tokens.TokenGeneratorTest.test_timeout failures. Random failures depended on the current timestamp. Thanks Matthijs Kooijman for the review. --- tests/auth_tests/test_tokens.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/auth_tests/test_tokens.py b/tests/auth_tests/test_tokens.py index 5ac242db29..eaff78bd57 100644 --- a/tests/auth_tests/test_tokens.py +++ b/tests/auth_tests/test_tokens.py @@ -39,23 +39,24 @@ class TokenGeneratorTest(TestCase): # Uses a mocked version of PasswordResetTokenGenerator so we can change # the value of 'now'. user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw') - p0 = PasswordResetTokenGenerator() + now = datetime.now() + p0 = MockedPasswordResetTokenGenerator(now) tk1 = p0.make_token(user) p1 = MockedPasswordResetTokenGenerator( - datetime.now() + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT) + now + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT) ) self.assertIs(p1.check_token(user, tk1), True) p2 = MockedPasswordResetTokenGenerator( - datetime.now() + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1)) + now + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1)) ) self.assertIs(p2.check_token(user, tk1), False) with self.settings(PASSWORD_RESET_TIMEOUT=60 * 60): p3 = MockedPasswordResetTokenGenerator( - datetime.now() + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT) + now + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT) ) self.assertIs(p3.check_token(user, tk1), True) p4 = MockedPasswordResetTokenGenerator( - datetime.now() + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1)) + now + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1)) ) self.assertIs(p4.check_token(user, tk1), False)