python - 如何在 Django 单元测试中测试 Django session ?

标签 python django unit-testing session django-1.8

我已经创建了 View :

def forgot_password(request):
"""
Actions when user forgot password.
:param request: object
:return: redirect to views.home() or 'loginsys/forgot.html' with form, message
"""
message = None
if request.method == 'POST':
    form = ForgotPasswordForm(request.POST)
    if form.is_valid():
        email = form.cleaned_data['email']
        user = User.objects.filter(email=email)
        salt = random_salt(len(user[0].username))
        code = signing.dumps([user[0].id, user[0].email, user[0].username],
                             key=settings.SECRET_KEY, salt=salt)
        url = settings.SITE_URL + reverse('loginsys:reset', args=[code])
        send_email.apply_async(('Welcome', '<p>Hello</p><p><a href="{0}">Go to this link</a></p>'.
                                format(url), [email]))
        store = ForgotPasswordLink()
        store.random_salt = salt
        store.user_link_id = user[0].id
        store.code_value = code
        store.save()
        forgot_password_salt_life.apply_async((user[0].id, salt), countdown=180)
        request.session["message"] = 'Instruction have sent on your mail - {0}'.format(email)
        return redirect(reverse('home'))
else:
    form = ForgotPasswordForm()
return render(request, 'loginsys/forgot.html', {'form': form, 'message': message})

我在 session 中写了消息:

request.session["message"] = 'Instruction have sent on your mail - {0}'.format(email)

然后我测试了 View :

def test_forgot_password(self):
    response = self.client.post(reverse('loginsys:forgot'), {'email': 'email@project.com'})
    self.assertRedirects(response, reverse('home'), 302, 200)
    session = self.client.session
    self.assertEqual(session['message'], 'Instruction have sent on your mail - email@project.com')

我有错误:

Error
Traceback (most recent call last):
  File "/home/maxim/PycharmProjects/SSHKeyStore/loginsys/tests.py", line 105, in test_forgot_password
    self.assertEqual(session['message'], 'Instruction have sent on your mail - email@project.com')
  File "/home/maxim/SSHKeyStoreVE/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 48, in __getitem__
    return self._session[key]
KeyError: 'message'

所以我不知道是什么问题。我找到了一些例子并在那里制作,但它没有帮助: Django testing stored session data in tests , How do I modify the session in the Django test framework , https://stackoverflow.com/questions/25132621/setting-a-session-variable-in-django-tests 错误:

Error
Traceback (most recent call last):
  File "/home/maxim/PycharmProjects/SSHKeyStore/loginsys/tests.py", line 22, in setUp
    self.client.session = self.session
AttributeError: can't set attribute

我有消息:

RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9.
  return f(*args, **kwds)

这很糟糕。

请帮助我解决问题并理解问题( 可能是这里的解决方案 Using session object in Django unit test但我不明白)。

谢谢。

最佳答案

像这样为此目的定义一个mixin

from django.test import Client
from importlib import import_module

class ModifySessionMixin(object):
    client = Client()

    def create_session(self):   
        session_engine = import_module(settings.SESSION_ENGINE)        
        store = session_engine.SessionStore()                          
        store.save()
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

现在从此 mixin 扩展您的测试类,并在您的 setUp 方法中调用 create_session 来初始化 session 。

class TestMobileResumeUpload(ModifySessionMixin, TestCase):
    def setUp(self):
        ....
        self.create_session()
        ....

您现在可以为 self.client.session 设置任何参数,这些参数可以在您的 View 中作为 session 变量进行访问。此外,在 View 中完成的 session 更改将在 self.client.seesion 中可用以进行测试。

关于python - 如何在 Django 单元测试中测试 Django session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31764970/

相关文章:

android - JaCoCo Gradle - 排除类并覆盖包含

unit-testing - Karma 日志级别值

Django ORM : Conditional filtering

Python Django 1.7 Ubuntu 14.04 如何在 azure 上部署 django 站点?

python - 隐藏 Jinja2 模板中的元素?有什么办法可以隐藏按钮吗?

python - 只旋转图像python的一部分

python - 类型错误 : Field 'id' expected a number but got ['database' ]; ManyToManyField

angular - 具有名称的表单控件没有值访问器...用于垫选择控件

python - 我想从 Trustpilot 评论中抓取用户和位置信息

python - 使用 pip 我安装包当我尝试导入包时它显示一些错误 ImportError no module names "package name"