Search code examples
djangodjango-testingdjango-socialauthdjango-unittest

Logging in using django-social-auth in a django unittest


I'm writing a test suite for a django project which needs to login via django-social-auth (its facebook backend) in order to access the website.

The method I'm taking now is:

  • for each test:
    • create a test user using facebook API
    • use selenium to login
    • interact with the website once user is logged in

problem is: I need to write a few tests which use the regular django unittest (not selenium). Any idea how I can login using the regular django unittest?


Solution

  • It's not an easy task since the oauth code is very coupled in social-auth. So you really need a oauth service answering.

    What I have done is to patch the function social_auth.utils.urlopen with a mock that returns previously recorded answers to trick django-social-auth into thinking it is talking with a real oauth provider.

    It's a really hacky solution but allows me to test my custom social-auth pipeline in an isolated environment.