Monday, July 30, 2018

Re: Django tests and token timeout

Hi Birger,

On 2018-07-30 16:12, Birger Schacht wrote:
> 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?
>

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b35369b8-8f76-074a-ad19-02e8543d0fc8%40stacktrace.dk.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment