Commit 445a0c93 by jsa

Add tests for enrollment deactivation under various modes.

A bug with the creation of inactive honor enrollment modes was caught and fixed along the way.

XCOM-388
parent 9f6e6281
......@@ -526,24 +526,34 @@ class EnrollmentTest(EnrollmentTestMixin, ModuleStoreTestCase, APITestCase):
self.assertTrue(is_active)
self.assertEqual(course_mode, CourseMode.HONOR)
def test_deactivate_enrollment(self):
@ddt.data(
((CourseMode.HONOR, ), CourseMode.HONOR),
((CourseMode.HONOR, CourseMode.VERIFIED), CourseMode.HONOR),
((CourseMode.HONOR, CourseMode.VERIFIED), CourseMode.VERIFIED),
((CourseMode.PROFESSIONAL, ), CourseMode.PROFESSIONAL),
((CourseMode.NO_ID_PROFESSIONAL_MODE, ), CourseMode.NO_ID_PROFESSIONAL_MODE),
((CourseMode.VERIFIED, CourseMode.CREDIT_MODE), CourseMode.VERIFIED),
((CourseMode.VERIFIED, CourseMode.CREDIT_MODE), CourseMode.CREDIT_MODE),
)
@ddt.unpack
def test_deactivate_enrollment(self, configured_modes, selected_mode):
"""With the right API key, deactivate (i.e., unenroll from) an existing enrollment."""
# Create an honor and verified mode for a course. This allows an update.
for mode in [CourseMode.HONOR, CourseMode.VERIFIED]:
# Configure a set of modes for the course.
for mode in configured_modes:
CourseModeFactory.create(
course_id=self.course.id,
mode_slug=mode,
mode_display_name=mode,
)
# Create a 'verified' enrollment
self.assert_enrollment_status(as_server=True, mode=CourseMode.VERIFIED)
# Create an enrollment with the selected mode.
self.assert_enrollment_status(as_server=True, mode=selected_mode)
# Check that the enrollment is 'verified' and active.
# Check that the enrollment has the correct mode and is active.
self.assertTrue(CourseEnrollment.is_enrolled(self.user, self.course.id))
course_mode, is_active = CourseEnrollment.enrollment_mode_for_user(self.user, self.course.id)
self.assertTrue(is_active)
self.assertEqual(course_mode, CourseMode.VERIFIED)
self.assertEqual(course_mode, selected_mode)
# Verify that a non-Boolean enrollment status is treated as invalid.
self.assert_enrollment_status(
......@@ -554,10 +564,19 @@ class EnrollmentTest(EnrollmentTestMixin, ModuleStoreTestCase, APITestCase):
)
# Verify that the enrollment has been deactivated, and that the mode is unchanged.
self.assert_enrollment_activation(False)
self.assert_enrollment_activation(False, selected_mode)
# Verify that enrollment deactivation is idempotent.
self.assert_enrollment_activation(False)
self.assert_enrollment_activation(False, selected_mode)
# Verify that omitting the mode returns 400 for course configurations
# in which the default (honor) mode doesn't exist.
expected_status = status.HTTP_200_OK if CourseMode.HONOR in configured_modes else status.HTTP_400_BAD_REQUEST
self.assert_enrollment_status(
as_server=True,
is_active=False,
expected_status=expected_status,
)
def test_change_mode_from_user(self):
"""Users should not be able to alter the enrollment mode on an enrollment. """
......
......@@ -425,7 +425,7 @@ class EnrollmentListView(APIView, ApiKeyPermissionMixIn):
response = api.update_enrollment(username, unicode(course_id), mode=mode, is_active=is_active)
else:
# Will reactivate inactive enrollments.
response = api.add_enrollment(username, unicode(course_id), mode=mode)
response = api.add_enrollment(username, unicode(course_id), mode=mode, is_active=is_active)
email_opt_in = request.DATA.get('email_opt_in', None)
if email_opt_in is not None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment