测试用户登录成功 [英] Test that user was logged in successfully
问题描述
我尝试过以下操作,但是返回 True
甚至在我将登录逻辑添加到我的注册视图之前。
def test_that_user_gets_logged_in(self):
/ pre>
response = self.client.post(reverse('auth-registration'),
{'username':'foo',
'password1':'bar',
'password2' :'bar'})
user = User.objects.get(username ='foo')
assert user.is_authenticated()
正在测试的代码:
class RegistrationView CreateView):
template_name ='auth / registration.html'
form_class = UserCreationForm
success_url ='/'
def auth_login(self,request,username,password ):
'''
验证总是需要在登录之前调用,因为
添加哪个后端执行登录所需的身份验证。
'''
user = authenticate(username = username,password = password)
登录(请求,用户)
def form_valid(self,表单):
'''
覆盖form_valid登录。
'''
#save用户
response = super(RegistrationView,self).form_valid(form)
#获取用户信用
username = form.cleaned_data ['username']
password = form.cleaned_data ['password1']
#authenticate和登录
self.auth_login(self.request ,用户名,密码)
返回响应
解决方案
这不是最好的答案。请参阅 https://stackoverflow.com/a/35871564/307511
Chronial 已经给了
一个很好的例子,说明如何使这个断言如下。他的答案比我现在的代码
。
最直截了当测试用户登录的方法是通过测试Client对象:
self.assertIn('_ auth_user_id',self。 client.session)
您还可以检查特定用户是否登录:
self.assertEqual(int(self.client.session ['_ auth_user_id']),user.pk)
作为附加信息,
response.request
对象不是一个HttpRequest
object;相反,它是一个普通的dict,有一些关于实际请求的信息,所以它不会有用户
属性。
另外,测试
response.context
对象是不安全的,因为你不会有上下文。How can I test that a user is logged in after submitting the registration form?
I tried the following but it returns
True
even before I added the login logic to my registration view.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()
The code that's being tested:
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 = super(RegistrationView, self).form_valid(form) #Get the user creditials username = form.cleaned_data['username'] password = form.cleaned_data['password1'] #authenticate and login self.auth_login(self.request, username, password) return response
解决方案This is not the best answer. See https://stackoverflow.com/a/35871564/307511
Chronial has given an excellent example on how to make this assertion below. His answer better than mine for nowadays code.
The most straightforward method to test if a user is logged in is by testing the Client object:
self.assertIn('_auth_user_id', self.client.session)
You could also check if a specific user is logged in:
self.assertEqual(int(self.client.session['_auth_user_id']), user.pk)
As an additional info, the
response.request
object is not aHttpRequest
object; instead, it's an ordinary dict with some info about the actual request, so it won't have theuser
attribute anyway.Also, testing the
response.context
object is not safe because you don't aways have a context.这篇关于测试用户登录成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!