diff --git a/course/auth.py b/course/auth.py index ad0b33f453c3d2a244677660a70f471ad5ffa03b..93f2d84f96a66b31fd0962d579ff24b60b08dba4 100644 --- a/course/auth.py +++ b/course/auth.py @@ -620,9 +620,6 @@ def reset_password(request, field="email"): % {"field": FIELD_DICT[field]}) else: if not user.email: - # happens when a user have an inst_id but have no email. - # This is almost impossible, because the email field of - # User should meet NOT NULL constraint. messages.add_message(request, messages.ERROR, _("The account with that institution ID " "doesn't have an associated email.")) diff --git a/tests/test_auth.py b/tests/test_auth.py index 174fc52949542531b38ae04800bb65f88e39e5e6..1563a8d0730beffab54c8cf85d1a242894a6da05 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1667,6 +1667,20 @@ class ResetPasswordStageOneTest(CoursesTestMixinBase, LocmemBackendTestsMixin, self.assertEqual(len(mail.outbox), 0) self.assertEqual(resp.status_code, 200) + def test_reset_user_has_no_email(self): + with mock.patch(ADD_MESSAGES_FUNC_PATH) as mock_add_msg: + self.user.email = "" + self.user.save() + expected_msg = ( + "The account with that institution ID " + "doesn't have an associated email.") + resp = self.post_reset_password(data={"instid": self.user_inst_id}, + use_instid=True) + self.assertTrue(resp.status_code, 200) + self.assertIn(expected_msg, mock_add_msg.call_args[0]) + self.assertEqual(len(mail.outbox), 0) + self.assertEqual(resp.status_code, 200) + def test_reset_by_email_have_multiple_user_with_same_email(self): with mock.patch("accounts.models.User.objects.get") as mock_get_user: from django.core.exceptions import MultipleObjectsReturned