gpt4 book ai didi

django - 如何创建用于测试目的的 django-allauth 常规和社交帐户?

转载 作者:行者123 更新时间:2023-12-03 16:29:04 25 4
gpt4 key购买 nike

managed to create a regular user使用 Django-allauth 登录时发生的情况.

我一直在尝试为社交帐户(Github)做同样的事情,但我真的很挣扎。我想这里一定有人为了测试目的而必须创建一个社交帐户。谁能展示一下他们是如何做到的?

此外,如果您知道创建普通用户的更好方法,我们将不胜感激。

最佳答案

以下来自 django-allauth tests 的片段展示了如何做到这一点:

from allauth.account import app_settings as account_settings
from allauth.account.models import EmailAddress
from allauth.account.utils import user_email
from allauth.socialaccount.helpers import complete_social_login
from allauth.socialaccount.models import SocialApp, SocialAccount, SocialLogin
from allauth.utils import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.contrib.auth.models import User
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import TestCase
from django.test.client import Client
from django.test.client import RequestFactory
from django.test.utils import override_settings


class SocialAccountTests(TestCase):

@override_settings(
SOCIALACCOUNT_AUTO_SIGNUP=True,
ACCOUNT_SIGNUP_FORM_CLASS=None,
ACCOUNT_EMAIL_VERIFICATION=account_settings.EmailVerificationMethod.NONE # noqa
)
def test_email_address_created(self):
factory = RequestFactory()
request = factory.get('/accounts/login/callback/')
request.user = AnonymousUser()
SessionMiddleware().process_request(request)
MessageMiddleware().process_request(request)

User = get_user_model()
user = User()
setattr(user, account_settings.USER_MODEL_USERNAME_FIELD, 'test')
setattr(user, account_settings.USER_MODEL_EMAIL_FIELD, 'test@test.com')

account = SocialAccount(user=user, provider='openid', uid='123')
sociallogin = SocialLogin(account)
complete_social_login(request, sociallogin)

user = User.objects.get(
**{account_settings.USER_MODEL_USERNAME_FIELD: 'test'}
)
self.assertTrue(
SocialAccount.objects.filter(user=user, uid=account.uid).exists()
)
self.assertTrue(
EmailAddress.objects.filter(user=user,
email=user_email(user)).exists()
)

关于django - 如何创建用于测试目的的 django-allauth 常规和社交帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com