From e07254bb37e28091fb8114fb77de013db0d7a0cf Mon Sep 17 00:00:00 2001
From: dzhuang <dzhuang.scut@gmail.com>
Date: Thu, 1 Mar 2018 18:22:53 +0800
Subject: [PATCH] Test reset password by inst_id when user have no email.

---
 course/auth.py     |  3 ---
 tests/test_auth.py | 14 ++++++++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/course/auth.py b/course/auth.py
index ad0b33f4..93f2d84f 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 174fc529..1563a8d0 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
-- 
GitLab