开发者

How do you add OpenId session data to a Django test client POST?

开发者 https://www.devze.com 2022-12-20 23:14 出处:网络
I\'m trying to test that a UserProfile model is created as a new User is registered in django_authopenid.

I'm trying to test that a UserProfile model is created as a new User is registered in django_authopenid. I don't understand how to add the Openid session data to the POST.

class UserTestCAse(TestCase):  
  def test_register_should_create_UserProfile(self):  
    from django.test.client import Client
    c = Client()  
    response =开发者_JS百科 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())  


Apparently, adding session data to the django.test.client.Client without using Client.login() is not so easy.

The easiest solution was to create a new RequestFactory class so I could build a Request with the OpenID session object.

    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  

I used the RequestFactory method like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消