3
\$\begingroup\$

I am writing tests for the password reset flow. It involves emails, links to reset the password etc so I made a really long test. It is testing quite a lot of things. If I break up the tests then the full flow is not tested, so this is more like a functional test. How should I break this up especially with regards to getting the activation/reset link?

def test_reset_password_email_send_existing(self):
    '''Test that an email to reset the password is sent
    and redirected to reset complete
    '''
    self.assertEqual(len(mail.outbox), 0)
    response = self.app.get(reverse('auth_password_reset'))
    form = response.form
    form['email'] = self.user.email
    response = form.submit()
    self.assertEqual(len(mail.outbox), 1)
    site = Site.objects.get(id=1)
    self.assertEqual(
        mail.outbox[0].subject,
        f'Password reset on {site.name}'
    )
    self.assertRedirects(
        response,
        reverse('auth_password_reset_done')
    )
    items = re.search(
        r'http[s]?:[\w/-]+.[\w]+\/(?P<rest>[\w/-]+)',
        mail.outbox[0].body
    )
    activate_url = items.groupdict()['rest']
    # get the reset link
    self.app.reset()
    response = self.app.get(
        f'/{ activate_url }'
    )
    self.assertEqual(response.status_code, 302)
    response = response.follow()
    form = response.form
    form['new_password1'] = 'mynewpass'
    form['new_password2'] = 'mynewpass'
    response = form.submit()

    self.assertRedirects(
        response,
        reverse('auth_password_reset_complete')
    )

    response = response.follow()
    self.assertContains(
        response,
        'Password reset successfully'
    )
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Yes, what you're trying to do is functional testing. Unit testing tests, as the name says, a unit of your application, not a whole workflow. \$\endgroup\$
    – cezar
    Oct 12, 2017 at 10:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.