gpt4 book ai didi

django - 如何将 OpenId session 数据添加到 Django 测试客户端 POST?

转载 作者:行者123 更新时间:2023-12-04 21:26:40 24 4
gpt4 key购买 nike

我正在尝试测试在 django_authopenid 中注册新用户时是否创建了 UserProfile 模型。
我不明白如何将 Openid session 数据添加到 POST。

class UserTestCAse(TestCase):  
def test_register_should_create_UserProfile(self):
from django.test.client import Client
c = Client()
response = c.post('/account/register/', {u'username': [u'john'], u'email': [u'john@beatles.com'], u'bnewaccount': [u'Signup']},)

self.assertEqual(response.status_code, 302)

user = User.objects.get( username ='john')
self.assertTrue(user.get_profile())

最佳答案

显然,在不使用 Client.login() 的情况下将 session 数据添加到 django.test.client.Client 并不是那么容易。

最简单的解决方案是创建一个新的 RequestFactory 类,以便我可以使用 OpenID session 对象构建一个请求。

    class RequestFactory(Client):  
"""
Class that lets you create mock Request objects for use in testing.

Usage:

rf = RequestFactory()
get_request = rf.get('/hello/')
post_request = rf.post('/submit/', {'foo': 'bar'})

This class re-uses the django.test.client.Client interface, docs here:
http://www.djangoproject.com/documentation/testing/#the-test-client

Once you have a request object you can pass it to any view function,
just as if that view had been hooked up using a URLconf.

"""
def request(self, **request):
"""
Similar to parent class, but returns the request object as soon as it
has created it.
"""
environ = {
'HTTP_COOKIE': self.cookies,
'PATH_INFO': '/',
'QUERY_STRING': '',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': '',
'SERVER_NAME': 'testserver',
'SERVER_PORT': 80,
'SERVER_PROTOCOL': 'HTTP/1.1',
}
environ.update(self.defaults)
environ.update(request)
request = WSGIRequest(environ)
handler = BaseHandler()
handler.load_middleware()
for middleware_method in handler._request_middleware:
if middleware_method(request):
raise Exception("Couldn't create request mock object - "
"request middleware returned a response")
return request

我使用了这样的 RequestFactory 方法:
class UserProfileCreation(TestCase):  
def test_register_should_create_UserProfile(self):
count = User.objects.count()
print 'Original Users' + str(count)

from forum.tests.request_factory import RequestFactory
rf = RequestFactory()
post_request = rf.post('/register/', {u'username': [u'john'], u'email': [u'john@beatles.com'], u'bnewaccount': [u'Signup'] } )

from django_authopenid.util import OpenID
openid_instance = OpenID('https://www.google.com/accounts/o8/id?id=AItOxxxxxxxxxxxxxxxxxA',
1267727884,
[u'openid.op_endpoint', u'openid.claimed_id', u'openid.identity', u'openid.return_to', u'openid.response_nonce', u'openid.assoc_handle'],
)

post_request.session['openid'] = openid_instance

from django_authopenid.views import register
response = register(post_request)

print "after POST User count: " + str(User.objects.count())
self.assertEqual(response.status_code, 302)
user = User.objects.get( username ='john')
self.assertTrue(user.get_profile())

关于django - 如何将 OpenId session 数据添加到 Django 测试客户端 POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2327893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com