Repository: inducer/relate

Issue:
Students are able to enroll without approval
https://github.com/inducer/relate/blob/b0e64c93799cb8b603880f13d5d2c07f4117d06a/course/enrollment.py#L251-L267

Looking at the code in the enrollment view, it looks like students are able to enroll without approval as long as their email suffix matches. This is causing some operational disruption on our end.

@dzhuang Since you were the last one to touch the code, could you explain?

cc @eecarrier


Discussion:

Resolving patch:
diff --git a/course/enrollment.py b/course/enrollment.py
--- a/course/enrollment.py
+++ b/course/enrollment.py
@@ -240,6 +240,16 @@ def email_suffix_matches(email, suffix):
         else:
             return email.endswith("@%s" % suffix) or email.endswith(".%s" % suffix)
 
+    if (preapproval is None
+        and course.enrollment_required_email_suffix
+        and not email_suffix_matches(
+            user.email, course.enrollment_required_email_suffix)):
+
+        messages.add_message(request, messages.ERROR,
+                _("Enrollment not allowed. Please use your '%s' email to "
+                "enroll.") % course.enrollment_required_email_suffix)
+        return redirect("relate-course_page", course_identifier)
+
     roles = ParticipationRole.objects.filter(
             course=course,
             is_default_for_new_participants=True)
@@ -248,24 +258,6 @@ def email_suffix_matches(email, suffix):
         roles = list(preapproval.roles.all())
 
     try:
-        if preapproval is None:
-            if course.enrollment_required_email_suffix:
-                if not email_suffix_matches(
-                        user.email, course.enrollment_required_email_suffix):
-                    messages.add_message(
-                        request, messages.ERROR,
-                        _("Enrollment not allowed. Please use your '%s' "
-                          "email to enroll.")
-                        % course.enrollment_required_email_suffix)
-                else:
-                    handle_enrollment_request(course, user,
-                                              participation_status.active,
-                                              roles, request)
-
-                    messages.add_message(request, messages.SUCCESS,
-                                         _("Successfully enrolled."))
-                return redirect("relate-course_page", course_identifier)
-
         if course.enrollment_approval_required and preapproval is None:
             participation = handle_enrollment_request(
                     course, user, participation_status.requested,


Test patch:
diff --git a/tests/test_enrollment.py b/tests/test_enrollment.py
--- a/tests/test_enrollment.py
+++ b/tests/test_enrollment.py
@@ -429,7 +429,7 @@ def test_coures_require_inst_id_verified_user_inst_id_not_verified2(self):
         self.assertParticiaptionStatusCallCount([0, 0, 0, 1])
         self.assertEqual(len(mail.outbox), 1)
 
-    def test_coures_require_email_suffix_passed(self):
+    def test_coures_require_email_suffix_but_need_approval(self):
         self.update_require_approval_course(
             enrollment_required_email_suffix="@blabla.com")
 
@@ -440,8 +440,8 @@ def test_coures_require_email_suffix_passed(self):
         self.assertRedirects(
             resp, self.course_page_url, fetch_redirect_response=False)
         self.assertAddMessageCallCount(1)
-        self.assertAddMessageCalledWith(MESSAGE_SUCCESSFULLY_ENROLLED_TEXT)
-        self.assertParticiaptionStatusCallCount([1, 0, 0, 0])
+        self.assertAddMessageCalledWith(MESSAGE_ENROLLMENT_SENT_TEXT)
+        self.assertParticiaptionStatusCallCount([0, 0, 0, 1])
         self.assertEqual(len(mail.outbox), 1)
 
     def test_coures_require_email_suffix_passed_without_at(self):
@@ -455,8 +455,8 @@ def test_coures_require_email_suffix_passed_without_at(self):
         self.assertRedirects(
             resp, self.course_page_url, fetch_redirect_response=False)
         self.assertAddMessageCallCount(1)
-        self.assertAddMessageCalledWith(MESSAGE_SUCCESSFULLY_ENROLLED_TEXT)
-        self.assertParticiaptionStatusCallCount([1, 0, 0, 0])
+        self.assertAddMessageCalledWith(MESSAGE_ENROLLMENT_SENT_TEXT)
+        self.assertParticiaptionStatusCallCount([0, 0, 0, 1])
         self.assertEqual(len(mail.outbox), 1)
 
     def test_coures_require_email_suffix_passed_without_at_pattern2(self):
@@ -470,8 +470,8 @@ def test_coures_require_email_suffix_passed_without_at_pattern2(self):
         self.assertRedirects(
             resp, self.course_page_url, fetch_redirect_response=False)
         self.assertAddMessageCallCount(1)
-        self.assertAddMessageCalledWith(MESSAGE_SUCCESSFULLY_ENROLLED_TEXT)
-        self.assertParticiaptionStatusCallCount([1, 0, 0, 0])
+        self.assertAddMessageCalledWith(MESSAGE_ENROLLMENT_SENT_TEXT)
+        self.assertParticiaptionStatusCallCount([0, 0, 0, 1])
         self.assertEqual(len(mail.outbox), 1)
 
     def test_coures_require_email_suffix_failed(self):