是否有一种简单的方法以编程方式使用 AllAuth 登录?我不太关心 cookie 或 session ,我只想获得一个与 AllAuth 用户帐户相对应的 Client
。
例子:
from django.test import Client
from django.contrib.auth.models import User
user = User.objects.first() # assuming user registered with AllAuth
client.login(username=user.username) # password needed!
AllAuth 用户帐户不保证有密码,因此上面的代码将不起作用。那么如何让客户端免密码登录,让测试更简单呢?
最佳答案
你可以使用APITestCase的
方法force_authenticate
class Test(APITestCase):
def setUp(self):
self.user = ... #get the user
self.client.force_authenticate(user=self.user)
这是 docs
关于django - 使用 Django-AllAuth 以编程方式登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43203038/