Exemplo n.º 1
0
    def test_same_user_key_in_multiple_organizations(self):
        uox_program_enrollment = self._create_waiting_program_enrollment()

        second_organization = OrganizationFactory.create()
        SAMLProviderConfigFactory.create(organization=second_organization, slug='aiu')
        catalog_org = CatalogOrganizationFactory.create(key=second_organization.short_name)
        program_uuid = self._create_catalog_program(catalog_org)['uuid']

        # aiu enrollment with the same student key as our uox user
        aiu_program_enrollment = ProgramEnrollmentFactory.create(
            user=None,
            external_user_key=self.external_id,
            program_uuid=program_uuid
        )

        UserSocialAuth.objects.create(
            user=UserFactory.create(),
            uid='{0}:{1}'.format('not_used', self.external_id),
        )

        UserSocialAuth.objects.create(
            user=self.user,
            uid='{0}:{1}'.format(self.provider_slug, self.external_id),
        )
        self._assert_program_enrollment_user(uox_program_enrollment, self.user)

        aiu_user = UserFactory.create()
        UserSocialAuth.objects.create(
            user=aiu_user,
            uid='{0}:{1}'.format('aiu', self.external_id),
        )
        self._assert_program_enrollment_user(aiu_program_enrollment, aiu_user)
Exemplo n.º 2
0
 def _create_waiting_program_enrollment(self):
     """ helper method to create a waiting program enrollment """
     return ProgramEnrollmentFactory.create(
         user=None,
         external_user_key=self.external_id,
         program_uuid=self.program_uuid,
     )
Exemplo n.º 3
0
 def create_program_enrollment(self, external_user_key, user=False):
     """
     Creates and returns a ProgramEnrollment for the given external_user_key and
     user if specified.
     """
     program_enrollment = ProgramEnrollmentFactory.create(
         external_user_key=external_user_key,
         program_uuid=self.program_uuid,
     )
     if user is not False:
         program_enrollment.user = user
         program_enrollment.save()
     return program_enrollment
Exemplo n.º 4
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
             )
Exemplo n.º 5
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
        """
        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
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
    def test_enrollment_already_linked_to_target_user(self):
        self._create_waiting_enrollment(self.program, '0001')
        program_enrollment = ProgramEnrollmentFactory.create(
            user=self.user_2,
            program_uuid=self.program,
            external_user_key='0002',
        )
        self._assert_no_program_enrollment(self.user_1,
                                           self.program,
                                           refresh=False)
        self._assert_program_enrollment(self.user_2,
                                        self.program,
                                        '0002',
                                        refresh=False)

        with LogCapture() as logger:
            self.call_command(self.program, ('0001', self.user_1.username),
                              ('0002', self.user_2.username))
            logger.check_present(
                (COMMAND_PATH, 'WARNING',
                 get_existing_user_message(program_enrollment, self.user_2)))

        self._assert_program_enrollment(self.user_1, self.program, '0001')
        self._assert_program_enrollment(self.user_2, self.program, '0002')
Exemplo n.º 8
0
    def test_enrollment_already_linked_to_target_user(self):
        self._create_waiting_enrollment(self.program, '0001')
        program_enrollment = ProgramEnrollmentFactory.create(
            user=self.user_2,
            program_uuid=self.program,
            external_user_key='0002',
        )
        self._assert_no_program_enrollment(self.user_1, self.program, refresh=False)
        self._assert_program_enrollment(self.user_2, self.program, '0002', refresh=False)

        with LogCapture() as logger:
            errors = link_program_enrollments(
                self.program,
                {
                    '0001': self.user_1.username,
                    '0002': self.user_2.username
                }
            )
            expected_error_msg = _user_already_linked_message(program_enrollment, self.user_2)
            logger.check_present((LOG_PATH, 'WARNING', expected_error_msg))

        self.assertDictEqual(errors, {'0002': expected_error_msg})
        self._assert_program_enrollment(self.user_1, self.program, '0001')
        self._assert_program_enrollment(self.user_2, self.program, '0002')
Exemplo n.º 9
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
Exemplo n.º 10
0
 def _create_waiting_program_enrollment(self):
     """ helper method to create a waiting program enrollment """
     return ProgramEnrollmentFactory.create(
         user=None, external_user_key=self.external_id)