pythondjangounit-testingdjango-authenticationpython-social-auth

Django unit test; Login using python-social-auth


I would like to write unit tests for my Django app that is using python-social-auth. It all works great when running Django and using a browser, thanks python-social-auth!

However, I can't seem to write unit tests because I can't create an authenticated client to test with.

Has anyone done so successfully?
How did you get an authenticated client()?

I have tried this (the login returns false and does not work):

self.c = Client()
self.u = User.objects.create(username="testuser", password="password", is_staff=True, is_active=True, is_superuser=True)
self.u.save()
self.auth = UserSocialAuth(user=self.u, provider="Facebook")
self.auth.save()
self.c.login(username=self.u.username, password=self.u.password) 

Solution

  • Got it:

    My mistake was thinking that it mattered how that Client got authenticated, for unit testing the views/endpoints oauth really doesn't need to come into play at all.

    this worked for me:

        self.user = User.objects.create(username='testuser', password='12345', is_active=True, is_staff=True, is_superuser=True) 
        self.user.set_password('hello') 
        self.user.save() 
        self.user = authenticate(username='testuser', password='hello') 
        login = self.c.login(username='testuser', password='hello') 
        self.assertTrue(login)