[Answered ]-Unit test through an oauth flow in Django

1👍

Kudos to Mark!

To help anyone on their way.
Mocking worked out. In my case (django 1.4) you need to add your tokens to the session. A lot of different advices can be found on the net, but I like simple things and this works in Django at least with the test suite:

session = self.client.session
session['request_token'] = {...}
session['access_token'] = {...}
session.save()

1👍

I faced the same problem and found the following solved my dilemma:

user = User.objects.get(username='lauren')
client = APIClient()
client.force_authenticate(user=user)

This was taken directly from the Django documentation:

http://www.django-rest-framework.org/api-guide/testing/#force_authenticateusernone-tokennone

Unfortunately it took a number of searches before getting to this point. I hope this saves someone else some time.

Leave a comment