[英]django-webtest, user authentication and view decorators
I'm using django-webtest (v1.5.6) to test a decorator is limiting access to a view to authenticated users. 我正在使用django- webtest(v1.5.6)来测试装饰器,从而限制了对经过身份验证的用户的视图访问。
My view is simply: 我的看法很简单:
@active_account_required
def homepage(request):
return render(request, 'accounts/account_homepage.html', {
'user': request.user,
})
The active_account_required
decorator is: active_account_required
装饰器为:
def active_account_required(function = None):
"""
Check the user is both logged in and has a valid, activated user account.
If a user tries to access a view protected by this decorator, they will be
redirected accordingly.
See http://passingcuriosity.com/2009/writing-view-decorators-for-django/
"""
def _dec(view_func):
def _view(request, *args, **kwargs):
if request.user.is_anonymous():
return HttpResponseRedirect(reverse_lazy('auth_login'))
if not request.user.get_profile().is_activated():
return HttpResponseRedirect(reverse_lazy('registration_activation_incomplete'))
return view_func(request, *args, **kwargs)
_view.__name__ = view_func.__name__
_view.__dict__ = view_func.__dict__
_view.__doc__ = view_func.__doc__
return _view
if function is None:
return _dec
else:
return _dec(function)
My test method is 我的测试方法是
class AccountViewsTests(WebTest):
def test_activated_user_can_access_account_homepage(self):
"""
Test an activated user can access the account homepage
"""
user = G(User)
user.get_profile().datetime_activated = timezone.now()
res = self.app.get(reverse('account_homepage'), user = user)
pdb.set_trace()
self.assertTemplateUsed(res, 'accounts/account_homepage.html',
'Activated account did not access account homepage correctly')
(The user object is created using the G
function from django-dynamic-fixture ) (用户对象是使用django-dynamic-fixture中的G
函数创建的)
When running the test, the decorator is preventing access to the homepage
view. 运行测试时,装饰器阻止访问homepage
视图。
You can see I'm using pdb
to inspect the objects. 您可以看到我正在使用pdb
检查对象。 User is a valid user object that should pass all the tests in the active_account_required
decorator: User是一个有效的用户对象,应通过active_account_required
装饰器中的所有测试:
(Pdb) user
<User: 2>
(Pdb) user.is_anonymous()
False
(Pdb) user.get_profile().is_activated()
True
Despite the user being correct, the response from self.app.get(reverse('account_homepage'), user = user)
is a 302 redirect to the registration_activation_incomplete
URL as per the decorator code: 尽管用户正确,但根据装饰器代码,来自self.app.get(reverse('account_homepage'), user = user)
的响应self.app.get(reverse('account_homepage'), user = user)
是302重定向到registration_activation_incomplete
URL:
(Pdb) res
<302 FOUND text/html location: http://localhost:80/accounts/registration/activate/incomplete/ no body>
It appears the user object is not being sent correctly in the WebTest request, but this matches the django-webtest documentation . 似乎未在WebTest请求中正确发送用户对象,但这与django-webtest文档匹配。 I've also tried passing the user in by username as user='2'
but get the same result. 我也尝试通过用户名将用户作为user='2'
传递给用户,但得到的结果相同。
Any ideas? 有任何想法吗?
Oops - the problem is that I simply forgot to save my user profile after setting the activation timestamp! 糟糕-问题是我只是在设置激活时间戳后忘记保存用户个人资料!
Changing the test code to: 将测试代码更改为:
user = G(User)
user.get_profile().datetime_activated = timezone.now()
user.get_profile().save()
res = self.app.get(reverse('account_homepage'), user = user)
ie adding user.get_profile().save()
got it working :) 即添加user.get_profile().save()
使其工作:)
Sorry for the noise. 对不起,噪音。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.