def setUp(self): """ Add a student """ super(TestRecentEnrollments, self).setUp() self.student = UserFactory() self.student.set_password(self.PASSWORD) self.student.save() # Old Course old_course_location = locator.CourseLocator('Org0', 'Course0', 'Run0') __, enrollment = self._create_course_and_enrollment( old_course_location) enrollment.created = datetime.datetime(1900, 12, 31, 0, 0, 0, 0, tzinfo=UTC) enrollment.save() # New Course course_location = locator.CourseLocator('Org1', 'Course1', 'Run1') self.course, self.enrollment = self._create_course_and_enrollment( course_location)
def test_enrollments_sorted_most_recent(self): """ Test that the list of newly created courses are properly sorted to show the most recent enrollments first. """ self._configure_message_timeout(600) # Create a number of new enrollments and courses, and force their creation behind # the first enrollment courses = [] for idx, seconds_past in zip(range(2, 6), [5, 10, 15, 20]): course_location = locator.CourseLocator( 'Org{num}'.format(num=idx), 'Course{num}'.format(num=idx), 'Run{num}'.format(num=idx) ) course, enrollment = self._create_course_and_enrollment(course_location) enrollment.created = datetime.datetime.now(UTC) - datetime.timedelta(seconds=seconds_past) enrollment.save() courses.append(course) courses_list = list(get_course_enrollments(self.student, None, [])) self.assertEqual(len(courses_list), 6) recent_course_list = _get_recently_enrolled_courses(courses_list) self.assertEqual(len(recent_course_list), 5) self.assertEqual(recent_course_list[1].course.id, courses[0].id) self.assertEqual(recent_course_list[2].course.id, courses[1].id) self.assertEqual(recent_course_list[3].course.id, courses[2].id) self.assertEqual(recent_course_list[4].course.id, courses[3].id)
def test_dashboard_rendering_with_two_courses(self): """ Tests that the dashboard renders the recent enrollment message appropriately for two courses. """ self._configure_message_timeout(600) course_location = locator.CourseLocator('Org2', 'Course2', 'Run2') course, _ = self._create_course_and_enrollment(course_location) self.client.login(username=self.student.username, password=self.PASSWORD) response = self.client.get(reverse("dashboard")) courses_enrollments = list( get_course_enrollments(self.student, None, [])) courses_enrollments.sort(key=lambda x: x.created, reverse=True) self.assertEqual(len(courses_enrollments), 3) recent_course_enrollments = _get_recently_enrolled_courses( courses_enrollments) self.assertEqual(len(recent_course_enrollments), 2) self.assertContains( response, "Thank you for enrolling in:".format( course_name=self.course.display_name)) self.assertContains( response, ' and '.join(enrollment.course.display_name for enrollment in recent_course_enrollments))
def test_enrollments_sorted_most_recent(self): """ Test that the list of newly created courses are properly sorted to show the most recent enrollments first. """ config = DashboardConfiguration(recent_enrollment_time_delta=600) config.save() # Create a number of new enrollments and courses, and force their creation behind # the first enrollment course_location = locator.CourseLocator('Org2', 'Course2', 'Run2') _, enrollment2 = self._create_course_and_enrollment(course_location) enrollment2.created = datetime.datetime.now(UTC) - datetime.timedelta( seconds=5) enrollment2.save() course_location = locator.CourseLocator('Org3', 'Course3', 'Run3') _, enrollment3 = self._create_course_and_enrollment(course_location) enrollment3.created = datetime.datetime.now(UTC) - datetime.timedelta( seconds=10) enrollment3.save() course_location = locator.CourseLocator('Org4', 'Course4', 'Run4') _, enrollment4 = self._create_course_and_enrollment(course_location) enrollment4.created = datetime.datetime.now(UTC) - datetime.timedelta( seconds=15) enrollment4.save() course_location = locator.CourseLocator('Org5', 'Course5', 'Run5') _, enrollment5 = self._create_course_and_enrollment(course_location) enrollment5.created = datetime.datetime.now(UTC) - datetime.timedelta( seconds=20) enrollment5.save() courses_list = list(get_course_enrollment_pairs( self.student, None, [])) self.assertEqual(len(courses_list), 6) recent_course_list = _get_recently_enrolled_courses(courses_list) self.assertEqual(len(recent_course_list), 5) self.assertEqual(recent_course_list[1][1], enrollment2) self.assertEqual(recent_course_list[2][1], enrollment3) self.assertEqual(recent_course_list[3][1], enrollment4) self.assertEqual(recent_course_list[4][1], enrollment5)
def _create_course_and_enroll(self, student, org, course, run): """ Creates a course and associated enrollment. """ course_location = locator.CourseLocator(org, course, run) course = CourseFactory.create(org=course_location.org, number=course_location.course, run=course_location.run) enrollment = CourseEnrollment.enroll(student, course.id) enrollment.created = datetime.datetime(2000, 12, 31, 0, 0, 0, 0) enrollment.save()
def test_transfer_students(self): student = UserFactory() student.set_password(self.PASSWORD) # pylint: disable=E1101 student.save() # pylint: disable=E1101 # Original Course original_course_location = locator.CourseLocator( 'Org0', 'Course0', 'Run0') course = self._create_course(original_course_location) # Enroll the student in 'verified' CourseEnrollment.enroll(student, course.id, mode="verified") # New Course 1 course_location_one = locator.CourseLocator('Org1', 'Course1', 'Run1') new_course_one = self._create_course(course_location_one) # New Course 2 course_location_two = locator.CourseLocator('Org2', 'Course2', 'Run2') new_course_two = self._create_course(course_location_two) original_key = unicode(course.id) new_key_one = unicode(new_course_one.id) new_key_two = unicode(new_course_two.id) # Run the actual management command transfer_students.Command().handle(source_course=original_key, dest_course_list=new_key_one + "," + new_key_two) # Confirm the enrollment mode is verified on the new courses, and enrollment is enabled as appropriate. self.assertEquals( ('verified', False), CourseEnrollment.enrollment_mode_for_user(student, course.id)) self.assertEquals( ('verified', True), CourseEnrollment.enrollment_mode_for_user(student, new_course_one.id)) self.assertEquals( ('verified', True), CourseEnrollment.enrollment_mode_for_user(student, new_course_two.id))
def test_enrollments_sorted_most_recent(self): """ Test that the list of newly created courses are properly sorted to show the most recent enrollments first. Also test recent enrollment message rendered appropriately for more than two courses. """ self._configure_message_timeout(600) # Create a number of new enrollments and courses, and force their creation behind # the first enrollment courses = [] for idx, seconds_past in zip(list(range(2, 6)), [5, 10, 15, 20]): course_location = locator.CourseLocator( 'Org{num}'.format(num=idx), 'Course{num}'.format(num=idx), 'Run{num}'.format(num=idx) ) course, enrollment = self._create_course_and_enrollment(course_location) enrollment.created = now() - datetime.timedelta(seconds=seconds_past) enrollment.save() courses.append(course) courses_list = list(get_course_enrollments(self.student, None, [])) self.assertEqual(len(courses_list), 6) recent_course_list = _get_recently_enrolled_courses(courses_list) self.assertEqual(len(recent_course_list), 5) self.assertEqual(recent_course_list[1].course.id, courses[0].id) self.assertEqual(recent_course_list[2].course.id, courses[1].id) self.assertEqual(recent_course_list[3].course.id, courses[2].id) self.assertEqual(recent_course_list[4].course.id, courses[3].id) self.client.login(username=self.student.username, password=self.PASSWORD) response = self.client.get(reverse("dashboard")) # verify recent enrollment message self.assertContains( response, 'Thank you for enrolling in:'.format(course_name=self.course.display_name) ) self.assertContains( response, ', '.join(enrollment.course.display_name for enrollment in recent_course_list) )
def test_dashboard_escaped_rendering(self): """ Tests that the dashboard renders the escaped recent enrollment messages appropriately. """ self._configure_message_timeout(600) self.client.login(username=self.student.username, password=self.PASSWORD) # New Course course_location = locator.CourseLocator('TestOrg', 'TestCourse', 'TestRun') xss_content = "<script>alert('XSS')</script>" course = CourseFactory.create( org=course_location.org, number=course_location.course, run=course_location.run, display_name=xss_content ) CourseEnrollment.enroll(self.student, course.id) response = self.client.get(reverse("dashboard")) self.assertContains(response, "Thank you for enrolling in") # Check if response is escaped self.assert_no_xss(response, xss_content)
def test_transfer_students(self): """ Verify the transfer student command works as intended. """ student = UserFactory.create() student.set_password(self.PASSWORD) student.save() mode = 'verified' # Original Course original_course_location = locator.CourseLocator( 'Org0', 'Course0', 'Run0') course = self._create_course(original_course_location) # Enroll the student in 'verified' CourseEnrollment.enroll(student, course.id, mode='verified') # Create and purchase a verified cert for the original course. self._create_and_purchase_verified(student, course.id) # New Course 1 course_location_one = locator.CourseLocator('Org1', 'Course1', 'Run1') new_course_one = self._create_course(course_location_one) # New Course 2 course_location_two = locator.CourseLocator('Org2', 'Course2', 'Run2') new_course_two = self._create_course(course_location_two) original_key = text_type(course.id) new_key_one = text_type(new_course_one.id) new_key_two = text_type(new_course_two.id) # Run the actual management command call_command( 'transfer_students', '--from', original_key, '--to', new_key_one, new_key_two, ) self.assertTrue(self.signal_fired) # Confirm the analytics event was emitted. self.mock_tracker.emit.assert_has_calls([ call(EVENT_NAME_ENROLLMENT_ACTIVATED, { 'course_id': original_key, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_MODE_CHANGED, { 'course_id': original_key, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_DEACTIVATED, { 'course_id': original_key, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_ACTIVATED, { 'course_id': new_key_one, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_MODE_CHANGED, { 'course_id': new_key_one, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_ACTIVATED, { 'course_id': new_key_two, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_MODE_CHANGED, { 'course_id': new_key_two, 'user_id': student.id, 'mode': mode }) ]) self.mock_tracker.reset_mock() # Confirm the enrollment mode is verified on the new courses, and enrollment is enabled as appropriate. self.assertEqual( (mode, False), CourseEnrollment.enrollment_mode_for_user(student, course.id)) self.assertEqual( (mode, True), CourseEnrollment.enrollment_mode_for_user(student, new_course_one.id)) self.assertEqual( (mode, True), CourseEnrollment.enrollment_mode_for_user(student, new_course_two.id))
def test_transfer_students(self): """ Verify the transfer student command works as intended. """ student = UserFactory.create() student.set_password(self.PASSWORD) student.save() mode = 'verified' # Original Course original_course_location = locator.CourseLocator( 'Org0', 'Course0', 'Run0') course = self._create_course(original_course_location) # Enroll the student in 'verified' CourseEnrollment.enroll(student, course.id, mode="verified") # Create and purchase a verified cert for the original course. self._create_and_purchase_verified(student, course.id) # New Course 1 course_location_one = locator.CourseLocator('Org1', 'Course1', 'Run1') new_course_one = self._create_course(course_location_one) # New Course 2 course_location_two = locator.CourseLocator('Org2', 'Course2', 'Run2') new_course_two = self._create_course(course_location_two) original_key = unicode(course.id) new_key_one = unicode(new_course_one.id) new_key_two = unicode(new_course_two.id) # Run the actual management command transfer_students.Command().handle(source_course=original_key, dest_course_list=new_key_one + "," + new_key_two) self.assertTrue(self.signal_fired) # Confirm the analytics event was emitted. self.mock_tracker.emit.assert_has_calls( # pylint: disable=maybe-no-member [ call(EVENT_NAME_ENROLLMENT_ACTIVATED, { 'course_id': original_key, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_MODE_CHANGED, { 'course_id': original_key, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_DEACTIVATED, { 'course_id': original_key, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_ACTIVATED, { 'course_id': new_key_one, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_MODE_CHANGED, { 'course_id': new_key_one, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_ACTIVATED, { 'course_id': new_key_two, 'user_id': student.id, 'mode': mode }), call(EVENT_NAME_ENROLLMENT_MODE_CHANGED, { 'course_id': new_key_two, 'user_id': student.id, 'mode': mode }) ]) self.mock_tracker.reset_mock() # Confirm the enrollment mode is verified on the new courses, and enrollment is enabled as appropriate. self.assertEquals( (mode, False), CourseEnrollment.enrollment_mode_for_user(student, course.id)) self.assertEquals( (mode, True), CourseEnrollment.enrollment_mode_for_user(student, new_course_one.id)) self.assertEquals( (mode, True), CourseEnrollment.enrollment_mode_for_user(student, new_course_two.id)) # Confirm the student has not be refunded. target_certs = CertificateItem.objects.filter(course_id=course.id, user_id=student, status='purchased', mode=mode) self.assertTrue(target_certs[0]) self.assertFalse(target_certs[0].refund_requested_time) self.assertEquals(target_certs[0].order.status, 'purchased')