diff --git a/common/djangoapps/student/tests/tests.py b/common/djangoapps/student/tests/tests.py index c5845ac8bc..cce4d9e66f 100644 --- a/common/djangoapps/student/tests/tests.py +++ b/common/djangoapps/student/tests/tests.py @@ -430,6 +430,88 @@ class EnrollInCourseTest(TestCase): self.assert_enrollment_event_was_emitted(user, course_id) +@override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE) +@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') +class ChangeEnrollmentViewTest(ModuleStoreTestCase): + """Tests the student.views.change_enrollment view""" + + def setUp(self): + super(ChangeEnrollmentViewTest, self).setUp() + self.course = CourseFactory.create() + self.user = UserFactory.create(password='secret') + self.client.login(username=self.user.username, password='secret') + self.url = reverse('change_enrollment') + + def enroll_through_view(self, course): + response = self.client.post( + reverse('change_enrollment'), { + 'course_id': course.id.to_deprecated_string(), + 'enrollment_action': 'enroll' + } + ) + return response + + def test_enroll_as_honor(self): + """Tests that a student can successfully enroll through this view""" + response = self.enroll_through_view(self.course) + self.assertEqual(response.status_code, 200) + enrollment_mode, is_active = CourseEnrollment.enrollment_mode_for_user( + self.user, self.course.id + ) + self.assertTrue(is_active) + self.assertEqual(enrollment_mode, u'honor') + + def test_cannot_enroll_if_already_enrolled(self): + """ + Tests that a student will not be able to enroll through this view if + they are already enrolled in the course + """ + CourseEnrollment.enroll(self.user, self.course.id) + self.assertTrue(CourseEnrollment.is_enrolled(self.user, self.course.id)) + # now try to enroll that student + response = self.enroll_through_view(self.course) + self.assertEqual(response.status_code, 400) + + def test_change_to_honor_if_verified(self): + """ + Tests that a student that is a currently enrolled verified student cannot + accidentally change their enrollment to verified + """ + CourseEnrollment.enroll(self.user, self.course.id, mode=u'verified') + self.assertTrue(CourseEnrollment.is_enrolled(self.user, self.course.id)) + # now try to enroll the student in the honor mode: + response = self.enroll_through_view(self.course) + self.assertEqual(response.status_code, 400) + enrollment_mode, is_active = CourseEnrollment.enrollment_mode_for_user( + self.user, self.course.id + ) + self.assertTrue(is_active) + self.assertEqual(enrollment_mode, u'verified') + + def test_change_to_honor_if_verified_not_active(self): + """ + Tests that one can renroll for a course if one has already unenrolled + """ + # enroll student + CourseEnrollment.enroll(self.user, self.course.id, mode=u'verified') + # now unenroll student: + CourseEnrollment.unenroll(self.user, self.course.id) + # check that they are verified but inactive + enrollment_mode, is_active = CourseEnrollment.enrollment_mode_for_user( + self.user, self.course.id + ) + self.assertFalse(is_active) + self.assertEqual(enrollment_mode, u'verified') + # now enroll them through the view: + response = self.enroll_through_view(self.course) + self.assertEqual(response.status_code, 200) + enrollment_mode, is_active = CourseEnrollment.enrollment_mode_for_user( + self.user, self.course.id + ) + self.assertTrue(is_active) + self.assertEqual(enrollment_mode, u'honor') + + @override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE) class PaidRegistrationTest(ModuleStoreTestCase): """ diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 9b9dea4a48..941069c316 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -613,6 +613,12 @@ def change_enrollment(request): if is_course_full: return HttpResponseBadRequest(_("Course is full")) + # check to see if user is currently enrolled in that course + if CourseEnrollment.is_enrolled(user, course_id): + return HttpResponseBadRequest( + _("Student is already enrolled") + ) + # If this course is available in multiple modes, redirect them to a page # where they can choose which mode they want. available_modes = CourseMode.modes_for_course(course_id)