How to disable session timeout in Django

I have something like this:

As soon as the user subscribes (and will be in a waiting state until he confirms his email address), the session variable will be set like this: "FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY" (sorry if this name seems confusing!), which will be set to datetime, adding 8 hours to the current time.

How this should affect the user, the user receives 8 hours of time to actually use all the functions of our site without confirming his email address to enter the system. After 8 hours, each view / page will display a large banner that will show the user a confirmation. (All this functionality is achieved with a single "provide_confirmed_user" decorator for each view).

I want to test the same functions using django unittest addon (class TestCase). How to do it?

Update. Do I need to manually update the specified value of the session variable (changed by 8 hours to several seconds) to do this? Or is there a better way there?

Update: this may seem insane, but I want to simulate a request from the future.

+3
source share
3 answers

Typically, if unit testing is difficult because the product code depends on external resources that will not interact, you can abstract those resources and replace them with dummies that do what you want.

In this case, the external resource is time. Instead of using datetime.now (), reorganize the code to accept an external time function. It can use datetime.now by default. Then in your unit tests you can change the time as you pass the test.

, - , , . , , .

+3

. FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY . , , .

datetime ( datetime)

, setup_ teardown_test_environment.

0

settings.py , django . 2 : settings.py settings_dev.py. :

from settings import *

DEBUG = True

INSTALLED_APPS = tuple(list(INSTALLED_APPS) + [
            'dev_app',
            ])

-:

  • add a variable with different values ​​to both settings modules;
  • If you set the variable, choose between the two values ​​according to the value of the DEBUG parameter. You can also use DEBUG to omit the unit test when on the production server, because the test is likely to take too much time anyway.

You can use the active settings module as follows:

from django.conf.project_template import settings

if settings.DEBUG:
    ...
0
source

Source: https://habr.com/ru/post/1715868/


All Articles