testing用户是否已成功login
如何在提交registry单后testing用户login?
我尝试了以下,但它甚至在login逻辑添加到我的注册视图之前返回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()
正在testing的代码:
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
这不是最好的答案 。 请参阅https://stackoverflow.com/a/35871564/307511
Chronial已经给出了一个很好的例子来说明如何做出这个断言。 现在的代码,他的回答比我的要好。
testing用户是否login的最直接的方法是testingClient对象:
self.assertIn('_auth_user_id', self.client.session)
您还可以检查特定用户是否login:
self.assertEqual(int(self.client.session['_auth_user_id']), user.pk)
作为附加信息, response.request
对象不是HttpRequest
对象; 相反,它是一个普通的字典,其中包含有关实际请求的一些信息,所以它不会有user
属性。
另外,testingresponse.context
对象是不安全的,因为你没有上下文。
您可以使用auth
模块的get_user
方法。 它说它想要一个请求作为参数,但它只使用请求的session
属性。 而恰巧我们的Client
有这个属性。
from django.contrib import auth user = auth.get_user(self.client) assert user.is_authenticated()
Django的TestClient有一个login方法 ,如果用户login成功,返回True。
User
模型中的is_authenticated()
方法总是返回True
。 在request.user
是AnonymousUser
的实例的情况下,返回False
为request.user.is_authenticated()
, is_authenticated()
方法始终返回False
。 在testing的时候你可以看一下response.context['request'].user.is_authenticated()
。
您还可以尝试访问另一个需要login的testing页面,并查看response.status
返回200
或302
(从login_required
redirect)。
你在哪里初始化你的self.client
? 你的setUp
方法还有什么? 我有一个类似的testing,你的代码应该工作正常。 以下是我如何做到这一点:
from django.contrib.auth.models import User from django.test import TestCase from django.test.client import Client class UserTestCase(TestCase): def setUp(self): self.client = Client() def testLogin(self): print User.objects.all() # returns [] response = self.client.post(reverse('auth-registration'), { 'username':'foo', 'password1':'bar', 'password2':'bar' } ) print User.objects.all() # returns one user print User.objects.all()[0].is_authenticated() # returns True
编辑
如果我注释掉我的login逻辑,我不会在self.client.post(
。)之后得到任何用户。如果你真的想检查用户是否被authentication,使用self.client
来访问另一个需要用户authentication的url。继续从上面,访问另一个页面:
response = self.client.get(reverse('another-page-which-requires-authentication')) print response.status_code
上面应该返回200来确认用户已经authentication。 除此之外,它将使用302代码redirect到login页面。
还有一个简洁的方法,使用wsgi_request
作为response
:
response = self.client.post('/signup', data) assert response.wsgi_request.user.is_authenticated()
而@Chronial的方式也可用于wsgi_request
:
from django.contrib import auth user = auth.get_user(response.wsgi_request) assert user.is_authenticated()
因为response.wsgi_request
对象具有session
属性。
但是,我认为使用response.wsgi_request.user
更简单。