以下的测试中,令牌未被识别为有效。在我的手动测试中,它是有效的,所以我想我在密码生成方面漏掉了什么。
def test_actual_reset_password(self):
new_password = "myNewPassword012*"
token_generator = PasswordResetTokenGenerator()
user = UserFactory.create()
token = token_generator.make_token(user=user)
response = self.assert_page_loading(path="/forgot-password/reset/{0}/".format(token))
print response
# That loads the page with the error message mentioning that the token was already used
# So I cannot carry on:
form = response.form
form['new_password1'] = new_password
form['new_password2'] = new_password
response = form.submit()
在django的源代码中,PasswordResetForm类里有这段代码;但我看不出它们之间的区别:
def save(self, ..., token_generator=default_token_generator, ...):
"""
Generates a one-use only link for resetting password and sends to the
user.
"""
...
for user in self.users_cache:
...
c = {
...
'token': token_generator.make_token(user),
...
}
...
send_mail(subject, email, from_email, [user.email])
self.assertEqual(len(mail.outbox), 1)
中,mail
模块来自哪里? - Luis Crespodjango.core.mail
。 - Luis CrespoTestCase
,您可以使用self.assertTemplateUsed
来缩短这些行。 - Addison Klinke'token': 'set-password'
。有关详细信息,请参见此线程。 - Addison Klinke