From c1dd64c07a5afe82cc7c1ded6d44f9100a6ecf2b Mon Sep 17 00:00:00 2001 From: Adam Palay Date: Fri, 12 Dec 2014 12:10:20 -0500 Subject: [PATCH] fix enrollment issue in instructor dash (ECOM-776) --- lms/djangoapps/instructor/enrollment.py | 10 +++- lms/djangoapps/instructor/tests/test_api.py | 52 ++++++++++++++++----- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lms/djangoapps/instructor/enrollment.py b/lms/djangoapps/instructor/enrollment.py index 1e9735ee11..ca35291118 100644 --- a/lms/djangoapps/instructor/enrollment.py +++ b/lms/djangoapps/instructor/enrollment.py @@ -35,7 +35,7 @@ class EmailEnrollmentState(object): exists_ce = False full_name = None ceas = CourseEnrollmentAllowed.objects.filter(course_id=course_id, email=email).all() - exists_allowed = len(ceas) > 0 + exists_allowed = ceas.exists() state_auto_enroll = exists_allowed and ceas[0].auto_enroll self.user = exists_user @@ -88,7 +88,13 @@ def enroll_email(course_id, student_email, auto_enroll=False, email_students=Fal previous_state = EmailEnrollmentState(course_id, student_email) if previous_state.user: - CourseEnrollment.enroll_by_email(student_email, course_id, previous_state.mode) + # if the student is currently unenrolled, don't enroll them in their + # previous mode + course_mode = u"honor" + if previous_state.enrollment: + course_mode = previous_state.mode + + CourseEnrollment.enroll_by_email(student_email, course_id, course_mode) if email_students: email_params['message'] = 'enrolled_enroll' email_params['email_address'] = student_email diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index 36ce43e068..683490515e 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -1077,17 +1077,7 @@ class TestInstructorAPIEnrollment(ModuleStoreTestCase, LoginEnrollmentTestCase): self.assertEqual(course_enrollment.mode, u'verified') # now re-enroll the student through the instructor dash - url = reverse( - 'students_update_enrollment', - kwargs={'course_id': self.course.id.to_deprecated_string()}, - ) - params = { - 'identifiers': self.enrolled_student.email, - 'action': 'enroll', - 'email_students': True, - } - response = self.client.post(url, params) - self.assertEqual(response.status_code, 200) + self._change_student_enrollment(self.enrolled_student, self.course, 'enroll') # affirm that the student is still in "verified" mode course_enrollment = CourseEnrollment.objects.get( @@ -1095,6 +1085,46 @@ class TestInstructorAPIEnrollment(ModuleStoreTestCase, LoginEnrollmentTestCase): ) self.assertEqual(course_enrollment.mode, u"verified") + def test_unenroll_and_enroll_verified(self): + """ + Test that unenrolling and enrolling a student from a verified track + results in that student being in an honor track + """ + course_enrollment = CourseEnrollment.objects.get( + user=self.enrolled_student, course_id=self.course.id + ) + # upgrade enrollment + course_enrollment.mode = u'verified' + course_enrollment.save() + self.assertEqual(course_enrollment.mode, u'verified') + + self._change_student_enrollment(self.enrolled_student, self.course, 'unenroll') + + self._change_student_enrollment(self.enrolled_student, self.course, 'enroll') + + course_enrollment = CourseEnrollment.objects.get( + user=self.enrolled_student, course_id=self.course.id + ) + self.assertEqual(course_enrollment.mode, u'honor') + + def _change_student_enrollment(self, user, course, action): + """ + Helper function that posts to 'students_update_enrollment' to change + a student's enrollment + """ + url = reverse( + 'students_update_enrollment', + kwargs={'course_id': course.id.to_deprecated_string()}, + ) + params = { + 'identifiers': user.email, + 'action': action, + 'email_students': True, + } + response = self.client.post(url, params) + self.assertEqual(response.status_code, 200) + return response + @ddt.ddt @override_settings(MODULESTORE=TEST_DATA_MOCK_MODULESTORE)