Nic*_*las 7 django django-forms django-views django-contrib django-authentication
我正在尝试为密码重置流程实施集成测试,但我被困在“ password_reset_confirm ”视图中。我已经手动测试了流程,它工作正常。不幸的是,Django 单元测试客户端似乎无法正确遵循此视图中所需的重定向。
from django.contrib.auth import views as auth_views
url(r"^accounts/password_change/$",
auth_views.PasswordChangeView.as_view(),
name="password_change"),
url(r"^accounts/password_change/done/$",
auth_views.PasswordChangeDoneView.as_view(),
name="password_change_done"),
url(r"^accounts/password_reset/$",
auth_views.PasswordResetView.as_view(email_template_name="app/email/accounts/password_reset_email.html",
success_url=reverse_lazy("app:password_reset_done"),
subject_template_name="app/email/accounts/password_reset_subject.html"),
name="password_reset"),
url(r"^accounts/password_reset/done/$",
auth_views.PasswordResetDoneView.as_view(),
name="password_reset_done"),
url(r"^accounts/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$",
auth_views.PasswordResetConfirmView.as_view(
success_url=reverse_lazy("app:password_reset_complete"),
form_class=CustomSetPasswordForm),
name="password_reset_confirm"),
url(r"^accounts/reset/complete/$",
auth_views.PasswordResetCompleteView.as_view(),
name="password_reset_complete"),
Run Code Online (Sandbox Code Playgroud)
import re
from django.urls import reverse, NoReverseMatch
from django.test import TestCase, Client
from django.core import mail
from django.test.utils import override_settings
from django.contrib.auth import authenticate
VALID_USER_NAME = "username"
USER_OLD_PSW = "oldpassword"
USER_NEW_PSW = "newpassword"
PASSWORD_RESET_URL = reverse("app:password_reset")
def PASSWORD_RESET_CONFIRM_URL(uidb64, token):
try:
return reverse("app:password_reset_confirm", args=(uidb64, token))
except NoReverseMatch:
return f"/accounts/reset/invaliduidb64/invalid-token/"
def utils_extract_reset_tokens(full_url):
return re.findall(r"/([\w\-]+)",
re.search(r"^http\://.+$", full_url, flags=re.MULTILINE)[0])[3:5]
@override_settings(EMAIL_BACKEND="anymail.backends.test.EmailBackend")
class PasswordResetTestCase(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.myclient = Client()
def test_password_reset_ok(self):
# ask for password reset
response = self.myclient.post(PASSWORD_RESET_URL,
{"email": VALID_USER_NAME},
follow=True)
# extract reset token from email
self.assertEqual(len(mail.outbox), 1)
msg = mail.outbox[0]
uidb64, token = utils_extract_reset_tokens(msg.body)
# change the password
response = self.myclient.post(PASSWORD_RESET_CONFIRM_URL(uidb64, token),
{"new_password1": USER_NEW_PSW,
"new_password2": USER_NEW_PSW},
follow=True)
self.assertIsNone(authenticate(username=VALID_USER_NAME,password=USER_OLD_PSW))
Run Code Online (Sandbox Code Playgroud)
现在,断言失败:用户使用旧密码进行身份验证。从日志中,我能够检测到未执行更改密码。
一些额外的有用信息:
post返回一个成功的HTTP 200;response.redirect_chain是[('/accounts/reset/token_removed/set-password/', 302)],我认为这是不对的,因为它应该有另一个环(在手动情况下,我看到另外一个电话到调度方法);关于如何正确测试这个场景的任何想法?我需要这个来确保正确执行电子邮件和日志记录(并且永远不会被删除)。
非常感谢!
正如公认的解决方案所解释的,这里是测试用例的工作代码:
def test_password_reset_ok(self):
# ask for password reset
response = self.myclient.post(PASSWORD_RESET_URL,
{"email": VALID_USER_NAME},
follow=True)
# extract reset token from email
self.assertEqual(len(mail.outbox), 1)
msg = mail.outbox[0]
uidb64, token = utils_extract_reset_tokens(msg.body)
# change the password
self.myclient.get(PASSWORD_RESET_CONFIRM_URL(uidb64, token), follow=True)
response = self.myclient.post(PASSWORD_RESET_CONFIRM_URL(uidb64, "set-password"),
{"new_password1": USER_NEW_PSW,
"new_password2": USER_NEW_PSW},
follow=True)
self.assertIsNone(authenticate(username=VALID_USER_NAME,password=USER_OLD_PSW))
Run Code Online (Sandbox Code Playgroud)
这很有趣;所以看起来 Django 在密码重置页面中实现了一个安全功能,以防止令牌在HTTP Referrer 标头中泄露。在此处阅读有关 Referrer 标头泄漏的更多信息。
Django 基本上是从 URL 中获取敏感令牌并将其放置在 Session 中并执行内部重定向(同一域)以防止您点击到不同的站点并通过 Referer 标头泄漏令牌。
/accounts/reset/uidb64/token/(您应该在这里执行 GET,但是您在测试用例中执行 POST),Django 从 URL 中提取令牌并将其设置在会话中并将您重定向到/accounts/reset/uidb64/set-password/./accounts/reset/uidb64/set-password/页面,您可以在其中设置密码并执行POSTtokenURL 参数可以处理 token 和 string set-password。set-password而不是令牌访问它,因此它希望从会话中提取您的实际令牌,然后更改密码。这是流程图:
GET
/reset/uidb64/token/--> 在会话中设置令牌 --> 302 重定向到/reset/uidb64/set-token/--> POST 密码 --> 从会话中获取令牌 --> 令牌是否有效?--> 重置密码
这是代码!
INTERNAL_RESET_URL_TOKEN = 'set-password'
INTERNAL_RESET_SESSION_TOKEN = '_password_reset_token'
@method_decorator(sensitive_post_parameters())
@method_decorator(never_cache)
def dispatch(self, *args, **kwargs):
assert 'uidb64' in kwargs and 'token' in kwargs
self.validlink = False
self.user = self.get_user(kwargs['uidb64'])
if self.user is not None:
token = kwargs['token']
if token == INTERNAL_RESET_URL_TOKEN:
session_token = self.request.session.get(INTERNAL_RESET_SESSION_TOKEN)
if self.token_generator.check_token(self.user, session_token):
# If the token is valid, display the password reset form.
self.validlink = True
return super().dispatch(*args, **kwargs)
else:
if self.token_generator.check_token(self.user, token):
# Store the token in the session and redirect to the
# password reset form at a URL without the token. That
# avoids the possibility of leaking the token in the
# HTTP Referer header.
self.request.session[INTERNAL_RESET_SESSION_TOKEN] = token
redirect_url = self.request.path.replace(token, INTERNAL_RESET_URL_TOKEN)
return HttpResponseRedirect(redirect_url)
# Display the "Password reset unsuccessful" page.
return self.render_to_response(self.get_context_data())
Run Code Online (Sandbox Code Playgroud)
注意代码中发生这种魔法的注释:
将令牌存储在会话中并重定向到没有令牌的 URL 处的密码重置表单。这避免了在 HTTP Referer 标头中泄漏令牌的可能性。
我认为这清楚地说明了如何修复单元测试;做一个 GET ,PASSWORD_RESET_URL它会给你重定向 URL,然后你可以 POST 到这个 redirect_url 并执行密码重置!
| 归档时间: |
|
| 查看次数: |
1059 次 |
| 最近记录: |