social_django.models.UserSocialAuth.DoesNotExist in unit test

41 views
Skip to first unread message

Roy Smith

unread,
Aug 5, 2020, 2:09:34 AM8/5/20
to django...@googlegroups.com
I have both model and social backends configured in settings.py:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'social_core.backends.mediawiki.MediaWiki',
)

When I run this test:

from django.test import TestCase, Client
from django.contrib.auth.models import User


class UserActivitiesViewTest(TestCase):
    def test_mainspace_title_contains_colon(self):
        user_fred = User.objects.create_user('Fred', 'fr...@example.com', 'password')
        client = Client()
        client.force_login(user_fred, backend='django.contrib.auth.backends.ModelBackend')
        response = client.get('/spi/spi-user-activities/Foo', {'count': 10, 'main': 1}, follow=True)


The view that's being tested is:

class UserActivitiesView(LoginRequiredMixin, View):
    def get(self, request, user_name):
      .....

I get a social_django.models.UserSocialAuth.DoesNotExist exception in the client.get() call.  Why is it doing any kind of query on UserSocialAuth if I'm telling force_login() to use ModelBackend?

I'm running django 2.2, python 3.7

Roy Smith

unread,
Aug 5, 2020, 5:22:09 PM8/5/20
to django...@googlegroups.com
I got this figured out.  It turns out I had a dependency on social_auth in an entirely different part of my code where I execute

reqeust.user.social_auth.get(provider='mediawiki').extra_data['access_token']

regardless of what auth backend was actually used.
Reply all
Reply to author
Forward
0 new messages