def _construct_enrollments(self, program_uuids, course_ids, external_user_key, edx_user=None):
        """
        A helper function to setup the program enrollments for a given learner.
        If the edx user is provided, it will try to SSO the user with the enrollments
        Return the expected info object that should be created based on the model setup
        """
        program_enrollments = []
        for program_uuid in program_uuids:
            course_enrollment = None
            program_enrollment = ProgramEnrollmentFactory.create(
                external_user_key=external_user_key,
                program_uuid=program_uuid,
                user=edx_user
            )

            for course_id in course_ids:
                if edx_user:
                    course_enrollment = CourseEnrollmentFactory.create(
                        course_id=course_id,
                        user=edx_user,
                        mode=CourseMode.MASTERS,
                        is_active=True
                    )

                program_course_enrollment = ProgramCourseEnrollmentFactory.create(  # lint-amnesty, pylint: disable=unused-variable
                    program_enrollment=program_enrollment,
                    course_key=course_id,
                    course_enrollment=course_enrollment,
                    status='active',
                )

            program_enrollments.append(program_enrollment)

        serialized = ProgramEnrollmentSerializer(program_enrollments, many=True)
        return serialized.data
    def _setup_enrollments(self, external_user_key, linked_user=None):
        """
        Create enrollments for testing linking.
        The enrollments can be create with already linked edX user.
        """
        program_enrollment = ProgramEnrollmentFactory.create(
            external_user_key=external_user_key,
            program_uuid=self.program_uuid,
            user=linked_user
        )
        course_enrollment = None
        if linked_user:
            course_enrollment = CourseEnrollmentFactory.create(
                course_id=self.course.id,
                user=linked_user,
                mode=CourseMode.MASTERS,
                is_active=True
            )
        program_course_enrollment = ProgramCourseEnrollmentFactory.create(
            program_enrollment=program_enrollment,
            course_key=self.course.id,
            course_enrollment=course_enrollment,
            status='active'
        )

        return program_enrollment, program_course_enrollment
Example #3
0
 def add_user_to_course_program_team(
     cls, user, add_to_team=True, enroll_in_program=True, connect_enrollments=True, external_user_key=None
 ):
     """
     Set up a test user by enrolling them in self.course, and then optionaly:
         - enroll them in a program
         - link their program and course enrollments
         - give their program enrollment an external_user_key
     """
     course_enrollment = CourseEnrollmentFactory.create(user=user, course_id=cls.course.id)
     if add_to_team:
         cls.team.add_user(user)
     if enroll_in_program:
         program_enrollment = ProgramEnrollmentFactory.create(user=user, external_user_key=external_user_key)
         if connect_enrollments:
             ProgramCourseEnrollmentFactory.create(
                 program_enrollment=program_enrollment, course_enrollment=course_enrollment
             )
Example #4
0
 def _create_waiting_course_enrollment(self, program_enrollment, course_key, status='active'):
     """
     Create a waiting program course enrollment for the given program enrollment,
     course key, and optionally status.
     """
     return ProgramCourseEnrollmentFactory.create(
         program_enrollment=program_enrollment,
         course_key=course_key,
         course_enrollment=None,
         status=status,
     )
Example #5
0
 def create_program_course_enrollment(self,
                                      program_enrollment,
                                      course_status=CourseStatuses.ACTIVE):
     """
     Creates and returns a ProgramCourseEnrollment for the given program_enrollment and
     self.course_key, creating a CourseEnrollment if the program enrollment has a user
     """
     course_enrollment = None
     if program_enrollment.user:
         course_enrollment = CourseEnrollmentFactory.create(
             course_id=self.course_id,
             user=program_enrollment.user,
             mode=CourseMode.MASTERS)
         course_enrollment.is_active = course_status == CourseStatuses.ACTIVE
         course_enrollment.save()
     return ProgramCourseEnrollmentFactory.create(
         program_enrollment=program_enrollment,
         course_key=self.course_id,
         course_enrollment=course_enrollment,
         status=course_status,
     )
Example #6
0
    def _construct_enrollments(self,
                               program_uuids,
                               course_ids,
                               external_user_key,
                               edx_user=None):
        """
        A helper function to setup the program enrollments for a given learner.
        If the edx user is provided, it will try to SSO the user with the enrollments
        Return the expected info object that should be created based on the model setup
        """
        expected_enrollments = []
        for program_uuid in program_uuids:
            expected_enrollment = {}
            expected_course_enrollment = {}
            course_enrollment = None
            program_enrollment = ProgramEnrollmentFactory.create(
                external_user_key=external_user_key,
                program_uuid=program_uuid,
                user=edx_user)
            expected_enrollment['program_enrollment'] = {
                'created':
                self._serialize_datetime(program_enrollment.created),
                'modified':
                self._serialize_datetime(program_enrollment.modified),
                'program_uuid': program_enrollment.program_uuid,
                'external_user_key': external_user_key,
                'status': program_enrollment.status
            }

            for course_id in course_ids:
                if edx_user:
                    course_enrollment = CourseEnrollmentFactory.create(
                        course_id=course_id,
                        user=edx_user,
                        mode=CourseMode.MASTERS,
                        is_active=True)
                    expected_course_enrollment = {
                        'course_id': str(course_enrollment.course_id),
                        'is_active': course_enrollment.is_active,
                        'mode': course_enrollment.mode,
                    }

                program_course_enrollment = ProgramCourseEnrollmentFactory.create(
                    program_enrollment=program_enrollment,
                    course_key=course_id,
                    course_enrollment=course_enrollment,
                    status='active',
                )
                expected_program_course_enrollment = {
                    'created':
                    self._serialize_datetime(
                        program_course_enrollment.created),
                    'modified':
                    self._serialize_datetime(
                        program_course_enrollment.modified),
                    'status':
                    program_course_enrollment.status,
                    'course_key':
                    str(program_course_enrollment.course_key),
                }
                if expected_course_enrollment:
                    expected_program_course_enrollment[
                        'course_enrollment'] = expected_course_enrollment

                expected_enrollment.setdefault(
                    'program_course_enrollments',
                    []).append(expected_program_course_enrollment)

            expected_enrollments.append(expected_enrollment)

        return expected_enrollments