Reputation: 1049
When a user signs up using a socialAccount (google in my case) I have a user_signed_up
signal that tests if the user signed up using a socialAccount, and if they did then it associates data from the socialAccount instance with the User model instance.
How would I create a user using the socialAccount method as part of a test, using pytest?
the signal definition is:
import logging
from allauth.account.signals import user_signed_up
from django.contrib.auth import get_user_model
from django.dispatch import receiver
User = get_user_model()
logger = logging.getLogger("email_app.apps.users")
@receiver(user_signed_up, sender=User, dispatch_uid="apps.users.signals")
def associate_social_data_with_user_model(user, **kwargs):
if user.socialaccount_set.filter(provider='google').count() < 1:
logger.info(f"social account single sign on not used. user signed up the old fashioned way. {user = }")
else:
extra_data = user.socialaccount_set.filter(provider='google')[0].extra_data
user.name = extra_data['given_name']
user.save()
Upvotes: 1
Views: 400
Reputation:
Take a look at allauth.tests.MockedResponse
for the basics and specifically, allauth.socialaccount.providers.google.tests.GoogleTests.test_user_signed_up_signal
. In short: the signal mechanism is already tested there and you can use that testcase and extend it with tests specific for your data.
Upvotes: 1