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 = c.post('/account/register/', {u'username': [u'john'], u'email': [u'[email protected]'], 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'[email protected]'], 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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With