Django tests and token timeout

37 views
Skip to first unread message

Birger Schacht

unread,
Jul 30, 2018, 10:14:29 AM7/30/18
to django...@googlegroups.com
hi,

i'm using my own PasswordResetTokenGenerator which has a timeout that is
not given in days but hours, to make the password token timeout more
fine grained. Is there a way to test such a timeout in Django tests,
i.e. something that lets the unit test use a fake time in the future?

cheers,
Birger

Adam Stein

unread,
Jul 30, 2018, 10:38:18 AM7/30/18
to django...@googlegroups.com
Whenever I need to set the date or datetime to something specific for unit tests, I use Forbidden Fruit (https://github.com/clarete/forbiddenfruit).

Kasper Laudrup

unread,
Jul 30, 2018, 11:05:18 AM7/30/18
to django...@googlegroups.com
Hi Birger,
You can mock out pretty much everything using the mock module in the
standard Python unittest library that Django already uses.

Depending on your code of course, but something like:

@mock.path('os.time.now')
def test_my_token(self, mock_now):
mock_now.return_value = datetime(3000, 12, 31)
# The rest of your test code goes here

Now your code should see the current time as being new years 3000.

I haven't tested this and there might be better ways to do that, but
that's how I would handle it. Hope that helps.

Kind regards,

Kasper Laudrup

Birger Schacht

unread,
Jul 30, 2018, 3:45:21 PM7/30/18
to django...@googlegroups.com
Hi!
thanks, that did the trick!

cheers,
Birger
Reply all
Reply to author
Forward
0 new messages