【问题标题】:How do you add OpenId session data to a Django test client POST?如何将 OpenId 会话数据添加到 Django 测试客户端 POST?
【发布时间】:2010-02-24 17:07:24
【问题描述】:

我正在尝试测试在 django_authopenid 中注册新用户时是否创建了 UserProfile 模型。 我不明白如何将 Openid 会话数据添加到 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())  

【问题讨论】:

    标签: django django-testing


    【解决方案1】:

    显然,不使用 Client.login() 将会话数据添加到 django.test.client.Client 并不是那么容易。

    最简单的解决方案是创建一个新的 RequestFactory 类,这样我就可以使用 OpenID 会话对象构建一个 Request。

        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())  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 2019-05-12
      • 2018-06-28
      • 2014-01-28
      相关资源
      最近更新 更多