如何在 Django 中对会话超时进行单元测试

发布于 2024-08-02 11:46:16 字数 453 浏览 10 评论 0原文

我有一个这样的要求:

一旦用户注册(并且将处于等待状态,直到他确认他的电子邮件地址),会话变量就会被设置为类似“FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY”(抱歉,如果这个名字听起来令人困惑!)将被设置为一个日期时间对象,在当前时间上加上 8 小时。

这对用户有何影响,用户有 8 小时的时间来实际使用我们网站的所有功能,而无需确认他的注册电子邮件地址。 8 小时后,每个视图/页面都会显示一个大横幅,告诉用户确认。 (所有这些功能都是使用每个视图的单个“ensure_confirmed_user”装饰器来实现的)。

我想使用 django 的 unittest 插件(TestCase 类)测试相同的功能。我该怎么做?

更新:我是否需要手动更新提到的会话变量值(将 8 小时修改为几秒)才能完成?或者有什么更好的办法吗?

更新:这可能听起来很疯狂,但我想模拟来自未来的请求。

I have a requirement something like this:

As soon as the user signsup(and will be in the waiting state untill he confirms his email address), a session variable is set something like "FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY"(sorry if the name sounds confusing!) which will be set to a datetime object adding 8hrs to the current time.

How this should impact the user is, the user gets 8hrs time to actually use all the features of our site, without confirming his signedup email address. After 8hrs, every view/page will show a big banner telling the user to confirm. (All this functionality is achieved using a single "ensure_confirmed_user" decorator for every view).

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

Update: Do I need to manually update the mentioned session variable value(modified 8hrs to a few seconds) so as to get it done? Or any better way is there?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-08-09 11:46:16

一般来说,如果单元测试很困难,因为产品代码依赖于不合作的外部资源,您可以抽象出这些资源并用可以执行您想要的操作的虚拟资源替换它们。

在这种情况下,外部资源就是时间。不要使用 datetime.now(),而是重构代码以接受外部时间函数。它可以默认为 datetime.now。然后在单元测试中,您可以随着测试的进行更改时间。

这比将会话超时更改为几秒钟要好,因为即使这样,您也必须在测试中休眠几秒钟才能获得您想要的效果。单元测试应该尽可能快地运行,以便更频繁地运行。

Generally, if unit testing is difficult because the product code depends on external resources that won't cooperate, you can abstract away those resources and replace them with dummies that do what you want.

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

This is better than changing the session timeout to a few seconds, because even then, you have to sleep for a few seconds in the test to get the effect you want. Unit tests should run as fast as you can get them to, so that they will be run more often.

时间你老了 2024-08-09 11:46:16

我可以想到几种可能性。在测试运行期间,覆盖 FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY 变量并将其设置为较小的时间限制。然后,您可以等到该时间限制结束,然后验证该功能是否按预期工作。

或者替换您自己的 datetime 函数(假设您的功能依赖于 datetime

您可以通过覆盖 setup_teardown_test_environment 来完成这些操作> 方法。

I can think of a couple of possibilities. During your test run, override the FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY variable and set it to a smaller time limit. You can then wait until that time limit is over and verify if the feature is working as expected.

Alternately replace your own datetime functions (assuming your feature relies on datetime)

You can accomplish these by overriding setup_ and teardown_test_environment methods.

一生独一 2024-08-09 11:46:16

我的 settings.py 略有不同,具体取决于 django 是在生产环境中还是在开发环境中运行。我有 2 个设置模块:settings.py 和 settings_dev.py。开发版本如下所示:

from settings import *

DEBUG = True

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

现在您可以通过不同的方式解决您的问题:

  1. 将具有不同值的变量添加到两个设置模块;
  2. 在设置变量的位置,根据 DEBUG 设置的值在两个值之间进行选择。在生产服务器上时,您还可以使用 DEBUG 省略单元测试,因为无论如何测试可能会花费太长时间。

您可以像这样使用活动设置模块:

from django.conf.project_template import settings

if settings.DEBUG:
    ...

My settings.py differs slightly depending on whether django is run in a production environment or in a development environment. I have 2 settings modules: settings.py and settings_dev.py. The development version looks as follows:

from settings import *

DEBUG = True

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

Now you can solve your problem in different ways:

  1. add the variable with different values to both settings modules;
  2. Where you set the variable, choose between two values according to the value of the DEBUG setting. You can also use DEBUG to omit the unit test when on the production server, because the test will probably take too long there anyway.

You can use the active settings module like this:

from django.conf.project_template import settings

if settings.DEBUG:
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文