如何在提交注册表单后测试用户是否已登录?
我尝试了以下操作但是True在我将登录逻辑添加到注册视图之前它就会返回.
def test_that_user_gets_logged_in(self):
response = self.client.post(reverse('auth-registration'),
{ 'username':'foo',
'password1':'bar',
'password2':'bar' } )
user = User.objects.get(username='foo')
assert user.is_authenticated()
Run Code Online (Sandbox Code Playgroud)
正在测试的代码:
class RegistrationView(CreateView):
template_name = 'auth/registration.html'
form_class = UserCreationForm
success_url = '/'
def auth_login(self, request, username, password):
'''
Authenticate always needs to be called before login because it
adds which backend did the authentication which is required by login.
'''
user = authenticate(username=username, password=password)
login(request, user)
def form_valid(self, form):
'''
Overwrite form_valid to login.
'''
#save the user
response = …Run Code Online (Sandbox Code Playgroud) 我想为使用python-social-auth的 Django应用程序编写单元测试.运行Django并使用浏览器时,这一切都很棒,感谢python-social-auth!
但是,我似乎无法编写单元测试,因为我无法创建经过身份验证的客户端进行测试.
有没有人这么成功?
你是如何获得经过身份验证的客户端()的?
我试过这个(登录返回false并且不起作用):
self.c = Client()
self.u = User.objects.create(username="testuser", password="password", is_staff=True, is_active=True, is_superuser=True)
self.u.save()
self.auth = UserSocialAuth(user=self.u, provider="Facebook")
self.auth.save()
self.c.login(username=self.u.username, password=self.u.password)
Run Code Online (Sandbox Code Playgroud)