|
3 | 3 |
|
4 | 4 | """
|
5 | 5 |
|
| 6 | +import logging |
| 7 | +import sys |
| 8 | + |
6 | 9 | from django.contrib.auth import get_user_model
|
7 |
| -from django.core import signing |
| 10 | +from django.contrib.auth.models import AnonymousUser |
| 11 | +from django.core import mail, signing |
8 | 12 | from django.core.exceptions import ImproperlyConfigured
|
9 |
| -from django.test import override_settings |
| 13 | +from django.test import RequestFactory, override_settings |
10 | 14 | from django.urls import reverse
|
11 | 15 |
|
12 | 16 | from django_registration import forms
|
@@ -86,3 +90,69 @@ def test_user_mismatch_breaks_view(self):
|
86 | 90 | )
|
87 | 91 | with self.assertRaisesMessage(ImproperlyConfigured, message):
|
88 | 92 | view.get_form()
|
| 93 | + |
| 94 | + |
| 95 | +class RegistrationError(Exception): |
| 96 | + """ |
| 97 | + Distinct exception class to simulate an unhandled error in the below |
| 98 | + tests. |
| 99 | +
|
| 100 | + """ |
| 101 | + |
| 102 | + |
| 103 | +class BuggyRegistrationView(base_views.RegistrationView): |
| 104 | + """ |
| 105 | + Registration view that simulates an unhandled exception. |
| 106 | +
|
| 107 | + """ |
| 108 | + |
| 109 | + def registration_allowed(self): |
| 110 | + raise RegistrationError("catch me if you can") |
| 111 | + |
| 112 | + |
| 113 | +buggy_view = BuggyRegistrationView.as_view() |
| 114 | + |
| 115 | + |
| 116 | +@override_settings(ADMINS=[("Admin", "admin@localhost")]) |
| 117 | +class SensitiveParameterFilterTests(RegistrationTestCase): |
| 118 | + """ |
| 119 | + Test filtering of sensitive POST parameters in error reports for the |
| 120 | + registration view. |
| 121 | +
|
| 122 | + """ |
| 123 | + |
| 124 | + logger = logging.getLogger("django") |
| 125 | + factory = RequestFactory() |
| 126 | + |
| 127 | + def test_sensitive_post_parameters_are_filtered(self): |
| 128 | + """ |
| 129 | + When an unexpected exception occurs during a POST request to the |
| 130 | + registration view, the default email report to ADMINS must not |
| 131 | + contain the submitted passwords. |
| 132 | +
|
| 133 | + """ |
| 134 | + request = self.factory.post("/raise/", data=self.valid_data) |
| 135 | + request.user = AnonymousUser() |
| 136 | + # we cannot use self.assertRaises(...) here because of sys.exc_info() |
| 137 | + try: |
| 138 | + buggy_view(request) |
| 139 | + self.fail("expected exception not thrown") |
| 140 | + except RegistrationError as error: |
| 141 | + self.assertEqual(str(error), "catch me if you can") |
| 142 | + # based on code in Django (tests/view_tests/views.py) |
| 143 | + self.logger.error( |
| 144 | + "Internal Server Error: %s" % request.path, |
| 145 | + exc_info=sys.exc_info(), |
| 146 | + extra={"status_code": 500, "request": request}, |
| 147 | + ) |
| 148 | + self.assertEqual(len(mail.outbox), 1) |
| 149 | + email = mail.outbox[0] |
| 150 | + self.assertIn("RegistrationError at /raise/", email.body) |
| 151 | + self.assertIn("catch me if you can", email.body) |
| 152 | + self.assertIn("No GET data", email.body) |
| 153 | + self.assertNotIn("No POST data", email.body) |
| 154 | + self.assertIn("password1", email.body) |
| 155 | + self.assertIn("password2", email.body) |
| 156 | + self.assertNotIn(self.valid_data["password1"], email.body) |
| 157 | + self.assertNotIn(self.valid_data["password2"], email.body) |
| 158 | + self.assertNotIn(self.valid_data["email"], email.body) |
0 commit comments