def enroll_user(self, username, access_token): if access_token: try: CourseEnrollment.objects.filter( user__username=username).delete() user_courses = self.get_json( '{}/api/MyCourses'.format(settings.SSO_ML_API_URL), headers={ 'Authorization': 'Bearer {}'.format(access_token) }) if len(user_courses): for course in user_courses: try: create_course_enrollment(username, course["CourseLMSId"], mode='honor', is_active=True) except: pass except Exception as ex: raise Exception( "Failed to fetch courses from Millionlights server. %s" % str(ex)) else: raise Exception("Access token is required")
def test_add_or_update_enrollment_attr(self, course_modes, enrollment_mode): # Create the course modes (if any) required for this test case self._create_course_modes(course_modes) data.create_course_enrollment(self.user.username, unicode(self.course.id), enrollment_mode, True) enrollment_attributes = [{ "namespace": "credit", "name": "provider_id", "value": "hogwarts", }] data.add_or_update_enrollment_attr(self.user.username, unicode(self.course.id), enrollment_attributes) enrollment_attr = data.get_enrollment_attributes( self.user.username, unicode(self.course.id)) self.assertEqual(enrollment_attr[0], enrollment_attributes[0]) enrollment_attributes = [{ "namespace": "credit", "name": "provider_id", "value": "ASU", }] data.add_or_update_enrollment_attr(self.user.username, unicode(self.course.id), enrollment_attributes) enrollment_attr = data.get_enrollment_attributes( self.user.username, unicode(self.course.id)) self.assertEqual(enrollment_attr[0], enrollment_attributes[0])
def test_add_or_update_enrollment_attr(self, course_modes, enrollment_mode): # Create the course modes (if any) required for this test case self._create_course_modes(course_modes) data.create_course_enrollment(self.user.username, unicode(self.course.id), enrollment_mode, True) enrollment_attributes = [ { "namespace": "credit", "name": "provider_id", "value": "hogwarts", } ] data.add_or_update_enrollment_attr(self.user.username, unicode(self.course.id), enrollment_attributes) enrollment_attr = data.get_enrollment_attributes(self.user.username, unicode(self.course.id)) self.assertEqual(enrollment_attr[0], enrollment_attributes[0]) enrollment_attributes = [ { "namespace": "credit", "name": "provider_id", "value": "ASU", } ] data.add_or_update_enrollment_attr(self.user.username, unicode(self.course.id), enrollment_attributes) enrollment_attr = data.get_enrollment_attributes(self.user.username, unicode(self.course.id)) self.assertEqual(enrollment_attr[0], enrollment_attributes[0])
def test_get_user_enrollments(self, course_modes, enrollment_mode): self._create_course_modes(course_modes) # Try to get enrollments before they exist. result = data.get_user_enrollments(self.course.id) self.assertFalse(result.exists()) # Create 10 test users to enroll in the course users = [] for i in xrange(10): users.append( UserFactory.create(username=self.USERNAME + str(i), email=self.EMAIL + str(i), password=self.PASSWORD + str(i))) # Create the original enrollments. created_enrollments = [] for user in users: created_enrollments.append( data.create_course_enrollment(user.username, unicode(self.course.id), enrollment_mode, True)) # Compare the created enrollments with the results # from the get user enrollments request. results = data.get_user_enrollments(self.course.id) self.assertTrue(result.exists()) self.assertEqual( EdraakCourseEnrollmentSerializer(results, many=True, context={ 'request': get_request_or_stub() }).data, created_enrollments)
def test_get_course_enrollments(self, course_modes, course_numbers): # Create all the courses created_courses = [] for course_number in course_numbers: created_courses.append(CourseFactory.create(number=course_number)) created_enrollments = [] for course in created_courses: self._create_course_modes(course_modes, course=course) # Create the original enrollment. created_enrollments.append( data.create_course_enrollment(self.user.username, unicode(course.id), 'honor', True)) # Compare the created enrollments with the results # from the get enrollments request. results = data.get_course_enrollments(self.user.username) self.assertEqual(results, created_enrollments) # Now create a course enrollment with some invalid course (does # not exist in database) for the user and check that the method # 'get_course_enrollments' ignores course enrollments for invalid # or deleted courses CourseEnrollment.objects.create( user=self.user, course_id='InvalidOrg/InvalidCourse/InvalidRun', mode='honor', is_active=True) updated_results = data.get_course_enrollments(self.user.username) self.assertEqual(results, updated_results)
def test_get_enrollments_including_inactive(self): """ Verify that if 'include_inactive' is True, all enrollments are returned including inactive. """ course_modes, course_numbers = ['honor', 'verified', 'audit'], ['1', '2', '3'] created_courses = [] for course_number in course_numbers: created_courses.append(CourseFactory.create(number=course_number)) created_enrollments = [] for course in created_courses: self._create_course_modes(course_modes, course=course) # Create the original enrollment. created_enrollments.append(data.create_course_enrollment( self.user.username, unicode(course.id), 'honor', True )) # deactivate one enrollment data.update_course_enrollment( self.user.username, unicode(created_courses[0].id), 'honor', False ) # by default in-active enrollment will be excluded. results = data.get_course_enrollments(self.user.username) self.assertNotEqual(len(results), len(created_enrollments)) # we can get all enrollments including inactive by passing "include_inactive" results = data.get_course_enrollments(self.user.username, include_inactive=True) self.assertEqual(len(results), len(created_enrollments))
def test_get_user_enrollments(self, course_modes, enrollment_mode): self._create_course_modes(course_modes) # Try to get enrollments before they exist. result = data.get_user_enrollments(self.course.id) self.assertFalse(result.exists()) # Create 10 test users to enroll in the course users = [] for i in xrange(10): users.append(UserFactory.create( username=self.USERNAME + str(i), email=self.EMAIL + str(i), password=self.PASSWORD + str(i) )) # Create the original enrollments. created_enrollments = [] for user in users: created_enrollments.append(data.create_course_enrollment( user.username, unicode(self.course.id), enrollment_mode, True )) # Compare the created enrollments with the results # from the get user enrollments request. results = data.get_user_enrollments( self.course.id ) self.assertTrue(result.exists()) self.assertEqual(CourseEnrollmentSerializer(results, many=True).data, created_enrollments)
def test_get_enrollments_including_inactive(self): """ Verify that if 'include_inactive' is True, all enrollments are returned including inactive. """ course_modes, course_numbers = ['honor', 'verified', 'audit'], ['1', '2', '3'] created_courses = [] for course_number in course_numbers: created_courses.append(CourseFactory.create(number=course_number)) created_enrollments = [] for course in created_courses: self._create_course_modes(course_modes, course=course) # Create the original enrollment. created_enrollments.append( data.create_course_enrollment(self.user.username, unicode(course.id), 'honor', True)) # deactivate one enrollment data.update_course_enrollment(self.user.username, unicode(created_courses[0].id), 'honor', False) # by default in-active enrollment will be excluded. results = data.get_course_enrollments(self.user.username) self.assertNotEqual(len(results), len(created_enrollments)) # we can get all enrollments including inactive by passing "include_inactive" results = data.get_course_enrollments(self.user.username, include_inactive=True) self.assertEqual(len(results), len(created_enrollments))
def test_get_course_enrollments(self, course_modes, course_numbers): # Create all the courses created_courses = [] for course_number in course_numbers: created_courses.append(CourseFactory.create(number=course_number)) created_enrollments = [] for course in created_courses: self._create_course_modes(course_modes, course=course) # Create the original enrollment. created_enrollments.append(data.create_course_enrollment( self.user.username, unicode(course.id), 'honor', True )) # Compare the created enrollments with the results # from the get enrollments request. results = data.get_course_enrollments(self.user.username) self.assertEqual(results, created_enrollments) # Now create a course enrollment with some invalid course (does # not exist in database) for the user and check that the method # 'get_course_enrollments' ignores course enrollments for invalid # or deleted courses CourseEnrollment.objects.create( user=self.user, course_id='InvalidOrg/InvalidCourse/InvalidRun', mode='honor', is_active=True ) updated_results = data.get_course_enrollments(self.user.username) self.assertEqual(results, updated_results)
def test_get_course_enrollment(self, course_modes, enrollment_mode): self._create_course_modes(course_modes) # Try to get an enrollment before it exists. result = data.get_course_enrollment(self.user.username, unicode(self.course.id)) self.assertIsNone(result) # Create the original enrollment. enrollment = data.create_course_enrollment(self.user.username, unicode(self.course.id), enrollment_mode, True) # Get the enrollment and compare it to the original. result = data.get_course_enrollment(self.user.username, unicode(self.course.id)) self.assertEqual(self.user.username, result["user"]) self.assertEqual(enrollment, result)
def test_enroll(self, course_modes, enrollment_mode): # Create the course modes (if any) required for this test case self._create_course_modes(course_modes) enrollment = data.create_course_enrollment(self.user.username, unicode(self.course.id), enrollment_mode, True) 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, enrollment_mode) # Confirm the returned enrollment and the data match up. self.assertEqual(course_mode, enrollment["mode"]) self.assertEqual(is_active, enrollment["is_active"])
def enroll_user(self, username, access_token): if access_token: try: CourseEnrollment.objects.filter(user__username=username).delete() user_courses = self.get_json( '{}/api/MyCourses'.format(settings.SSO_ML_API_URL), headers={'Authorization': 'Bearer {}'.format(access_token)} ) if len(user_courses): for course in user_courses: try: create_course_enrollment( username, course["CourseLMSId"], mode='honor', is_active=True ) except: pass except Exception as ex: raise Exception("Failed to fetch courses from Millionlights server. %s" % str(ex)) else: raise Exception("Access token is required")
def test_get_course_enrollment(self, course_modes, enrollment_mode): self._create_course_modes(course_modes) # Try to get an enrollment before it exists. result = data.get_course_enrollment(self.user.username, unicode(self.course.id)) self.assertIsNone(result) # Create the original enrollment. enrollment = data.create_course_enrollment(self.user.username, unicode(self.course.id), enrollment_mode, True) # Get the enrollment and compare it to the original. result = data.get_course_enrollment(self.user.username, unicode(self.course.id)) self.assertEqual(self.user.username, result['user']) self.assertEqual(enrollment, result)
def test_enroll(self, course_modes, enrollment_mode): # Create the course modes (if any) required for this test case self._create_course_modes(course_modes) enrollment = data.create_course_enrollment(self.user.username, unicode(self.course.id), enrollment_mode, True) 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, enrollment_mode) # Confirm the returned enrollment and the data match up. self.assertEqual(course_mode, enrollment['mode']) self.assertEqual(is_active, enrollment['is_active'])
def test_get_course_enrollments(self, course_modes, course_numbers): # Create all the courses created_courses = [] for course_number in course_numbers: created_courses.append(CourseFactory.create(number=course_number)) created_enrollments = [] for course in created_courses: self._create_course_modes(course_modes, course=course) # Create the original enrollment. created_enrollments.append( data.create_course_enrollment(self.user.username, unicode(course.id), "honor", True) ) # Compare the created enrollments with the results # from the get enrollments request. results = data.get_course_enrollments(self.user.username) self.assertEqual(results, created_enrollments)
def test_enroll(self, course_modes, enrollment_mode): # Create the course modes (if any) required for this test case self._create_course_modes(course_modes) enrollment = data.create_course_enrollment( self.user.username, six.text_type(self.course.id), enrollment_mode, True ) 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, enrollment_mode) # Confirm the returned enrollment and the data match up. self.assertEqual(course_mode, enrollment['mode']) self.assertEqual(is_active, enrollment['is_active']) self.assertEqual(self.course.display_name_with_default, enrollment['course_details']['course_name'])
def test_get_course_enrollments(self, course_modes, course_numbers): # Create all the courses created_courses = [] for course_number in course_numbers: created_courses.append(CourseFactory.create(number=course_number)) created_enrollments = [] for course in created_courses: self._create_course_modes(course_modes, course=course) # Create the original enrollment. created_enrollments.append( data.create_course_enrollment(self.user.username, unicode(course.id), 'honor', True)) # Compare the created enrollments with the results # from the get enrollments request. results = data.get_course_enrollments(self.user.username) self.assertEqual(results, created_enrollments)
def test_enrollment_for_non_existent_course(self): data.create_course_enrollment(self.user.username, "some/fake/course", 'honor', True)
def test_enrollment_for_non_existent_user(self): data.create_course_enrollment("some_fake_user", unicode(self.course.id), 'honor', True)
def test_enrollment_for_non_existent_user(self): with pytest.raises(UserNotFoundError): data.create_course_enrollment("some_fake_user", unicode(self.course.id), 'honor', True)
def test_enrollment_for_closed_course(self, mock_enroll): mock_enroll.side_effect = AlreadyEnrolledError("Bad things happened") data.create_course_enrollment(self.user.username, unicode(self.course.id), 'honor', True)
def test_enrollment_for_full_course(self, mock_enroll): mock_enroll.side_effect = CourseFullError("Bad things happened") data.create_course_enrollment(self.user.username, unicode(self.course.id), 'honor', True)
def test_enrollment_for_non_existent_course(self): with pytest.raises(CourseNotFoundError): data.create_course_enrollment(self.user.username, "some/fake/course", 'honor', True)
def test_enrollment_for_enrolled_course(self, mock_enroll): mock_enroll.side_effect = AlreadyEnrolledError("Bad things happened") with pytest.raises(CourseEnrollmentExistsError): data.create_course_enrollment(self.user.username, unicode(self.course.id), 'honor', True)
def test_enrollment_for_full_course(self, mock_enroll): mock_enroll.side_effect = CourseFullError("Bad things happened") with pytest.raises(CourseEnrollmentFullError): data.create_course_enrollment(self.user.username, six.text_type(self.course.id), 'honor', True)