0

Good morning, I would like to test that Django properly sends an e-mail for password reset as I have customised the routes. However I get a <HttpResponseForbidden status_code=403, "text/html"> and the e-mail is not implemented when I test it. Here are the main features:

  • url : path('password_reset/', auth_views.PasswordResetView.as_view(template_name="customer/password_reset_form.html", email_template_name="customer/password_reset_e-mail.html", success_url="done/"),
  • test method:
from django.contrib.auth import views as auth_views
from django.test import RequestFactory
from django.contrib.auth.models import AnonymousUser
class PasswordResetTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = User.objects.create_user(
                    username="user",
                    email="user@mail.com",
                    password="pwd")

    def test_send_mail(self):
        request = self.factory.post('/customer/password_reset', {'email': 'user@mail.com'})
        response = auth_views.PasswordResetView.as_view()(request)
        self.assertTemplateUsed('password_reset_e-mail.html')
        print(response)
        self.assertEqual(len(mail.outbox), 1)

Throught the print command, I can see an error 403 and that the outbox is empty. I just cannot find proper documentation to build a test on this very part of Django.

Thank you for your support.

Fabrice Jaouën
  • 180
  • 1
  • 9
  • 1
    Are you sure your authen work? You can change to APITestCase and force authen – Tiana987642 Feb 28 '21 at 09:21
  • Yes, the authentication works for other tests. After additional checks I came to the conclusion that, when one use the django default forms, during the test phase, they get back to their default routes (`/registration/etc`) and the changes one has introduced in the url are skipped. I believe that the solution may be to use mixins. – Fabrice Jaouën Feb 28 '21 at 11:07

0 Answers0