authentication - Testing Django 1-5 Reset Password Form - how to generate the token for the test? -
with following test, token not recognised valid. in manual test, it's working i'm missing in way password generated guess.
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 # loads page error message mentioning token used # cannot carry on: form = response.form form['new_password1'] = new_password form['new_password2'] = new_password response = form.submit()
in django source code, in passwordresetform, i've found code; can't see difference is:
def save(self, ..., token_generator=default_token_generator, ...): """ generates one-use link resetting password , sends user. """ ... user in self.users_cache: ... c = { ... 'token': token_generator.make_token(user), ... } ... send_mail(subject, email, from_email, [user.email])
ok, searching info on how , question prompted me figure out myself. i'm not sure if you're still working on this, here's how got work:
from django.core import mail # first initial password reset form. # not strictly necessary, included completeness response = self.c.get(reverse('password_reset')) self.assertequal(response.status_code, 200) self.assertequal(response.template_name, 'authentication/password_reset_form.html') # post response our "email address" response = self.c.post(reverse('password_reset'),{'email':'fred@home.com'}) self.assertequal(response.status_code, 302) # @ point system "send" email. can "check" thusly: self.assertequal(len(mail.outbox), 1) self.assertequal(mail.outbox[0].subject, 'password reset on example.com') # now, here's kicker: token , userid response token = response.context[0]['token'] uid = response.context[0]['uid'] # can use token password change form response = self.c.get(reverse('password_reset_confirm', kwargs={'token':token,'uidb64':uid})) self.assertequal(response.status_code, 200) self.assertequal(response.template_name, 'authentication/password_reset_confirm.html') # post same url our new password: response = self.c.post(reverse('password_reset_confirm', kwargs={'token':token,'uidb36':uid}), {'new_password1':'pass','new_password2':'pass'}) self.assertequal(response.status_code, 302)
and that's it! not hard after all.
Comments
Post a Comment