jeudi 5 novembre 2020

Test django allauth socialAccount creation

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()

Aucun commentaire:

Enregistrer un commentaire