如何修改Django测试框架中的session

发布于 2024-10-08 10:55:12 字数 570 浏览 0 评论 0 原文

我的网站允许个人在没有登录的情况下通过根据当前 session_key 创建用户来贡献内容

我想为我的视图设置一个测试,但似乎无法修改 request.session:

I想要这样做:

from django.contrib.sessions.models import Session
s = Session()
s.expire_date = '2010-12-05'
s.session_key = 'my_session_key'
s.save()
self.client.session = s
response = self.client.get('/myview/')

但我收到错误:

AttributeError: can't set attribute

在发出获取请求之前如何修改客户端会话的想法? 我已经看到这个,它似乎不起作用

My site allows individuals to contribute content in the absence of being logged in by creating a User based on the current session_key

I would like to setup a test for my view, but it seems that it is not possible to modify the request.session:

I'd like to do this:

from django.contrib.sessions.models import Session
s = Session()
s.expire_date = '2010-12-05'
s.session_key = 'my_session_key'
s.save()
self.client.session = s
response = self.client.get('/myview/')

But I get the error:

AttributeError: can't set attribute

Thoughts on how to modify the client session before making get requests?
I have seen this and it doesn't seem to work

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

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

发布评论

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

评论(5

花想c 2024-10-15 10:55:12

django 测试框架的客户端对象使得接触会话成为可能。看看 http:// docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session 了解详细信息

注意:要修改会话然后保存它,它必须首先存储在变量中(因为每次访问此属性时都会创建一个新的 SessionStore)

我认为下面这样的东西应该可以工作

s = self.client.session
s.update({
    "expire_date": '2010-12-05',
    "session_key": 'my_session_key',
})
s.save()
response = self.client.get('/myview/')

The client object of the django testing framework makes possible to touch the session. Look at http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session for details

Be careful : To modify the session and then save it, it must be stored in a variable first (because a new SessionStore is created every time this property is accessed)

I think something like this below should work

s = self.client.session
s.update({
    "expire_date": '2010-12-05',
    "session_key": 'my_session_key',
})
s.save()
response = self.client.get('/myview/')
葬シ愛 2024-10-15 10:55:12

这就是我的做法(受到 http://blog.mediaonfire.com/?p 中的解决方案的启发=36)。

from django.test import TestCase
from django.conf import settings
from django.utils.importlib import import_module

class SessionTestCase(TestCase):
    def setUp(self):
        # http://code.djangoproject.com/ticket/10899
        settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file'
        engine = import_module(settings.SESSION_ENGINE)
        store = engine.SessionStore()
        store.save()
        self.session = store
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

之后,您可以将测试创建为:

class BlahTestCase(SessionTestCase):

    def test_blah_with_session(self):
        session = self.session
        session['operator'] = 'Jimmy'
        session.save()

等等...

This is how I did it (inspired by a solution in http://blog.mediaonfire.com/?p=36).

from django.test import TestCase
from django.conf import settings
from django.utils.importlib import import_module

class SessionTestCase(TestCase):
    def setUp(self):
        # http://code.djangoproject.com/ticket/10899
        settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file'
        engine = import_module(settings.SESSION_ENGINE)
        store = engine.SessionStore()
        store.save()
        self.session = store
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

After that, you may create your tests as:

class BlahTestCase(SessionTestCase):

    def test_blah_with_session(self):
        session = self.session
        session['operator'] = 'Jimmy'
        session.save()

etc...

北城孤痞 2024-10-15 10:55:12

正如安德鲁·奥斯汀已经提到的,由于这个错误,它不起作用: https://code.djangoproject.com /ticket/11475

不过,您可以做的是:

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User

class SessionTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        User.objects.create_user('john', '[email protected]', 'johnpassword')
        self.client.login(username='john', password='johnpassword')

    def test_something_with_sessions(self):
        session = self.client.session
        session['key'] = 'value'
        session.save()

使用 User.objects.create_user() 和 self.client.login() 创建并登录用户后(如上面的代码所示),会话应该可以工作。

As Andrew Austin already mentioned, it doesn't work because of this bug: https://code.djangoproject.com/ticket/11475

What you can do though is this:

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User

class SessionTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        User.objects.create_user('john', '[email protected]', 'johnpassword')
        self.client.login(username='john', password='johnpassword')

    def test_something_with_sessions(self):
        session = self.client.session
        session['key'] = 'value'
        session.save()

After creating and logging in a user with User.objects.create_user() and self.client.login(), as in the code above, sessions should work.

勿忘初心 2024-10-15 10:55:12

向测试客户端中的会话添加值

def test_something(self):
    session = self.client.session
    session['somekey'] = 'test'
    session.save()

根据文档,您可以通过例如https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.Client.session

这将允许您测试需要数据的视图会话才能正常运行。

那么对于这个问题:

session = self.client.session
   session['expire_date'] = '2010-12-05'
   .....
   session.save()

as per the docs you can add values to the session in the test client via eg

def test_something(self):
    session = self.client.session
    session['somekey'] = 'test'
    session.save()

https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.Client.session

This will let you test views that require data in the session to function correctly.

So for this question:

session = self.client.session
   session['expire_date'] = '2010-12-05'
   .....
   session.save()
埖埖迣鎅 2024-10-15 10:55:12

您可以创建一个插入虚拟数据(例如会话)的自定义视图。

具有相应网址的视图:/dummy/:

def dummy(request):
    # dummy init data
    request.session['expiry_date'] = '2010-12-05'
    return HttpResponse('Dummy data has been set successfully')

比在测试脚本中只需调用 self.client.get('/dummy/')

我还使用此虚拟视图来初始化会话中的虚拟数据手动测试时。

You can create a custom view which inserts dummy data such as the session.

The view with the corresponding url: /dummy/:

def dummy(request):
    # dummy init data
    request.session['expiry_date'] = '2010-12-05'
    return HttpResponse('Dummy data has been set successfully')

Than in test script just call self.client.get('/dummy/')

I also use this dummy view to initilize the dummy data in the session when testing by hand.

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