Beispiel #1
0
    def test_has_staff_access_to_preview_mode(self):
        """
        Test that preview mode is only accessible by staff users.
        """
        course_key = self.course.id
        CourseEnrollmentFactory(user=self.student, course_id=self.course.id)

        for user in [
                self.global_staff, self.course_staff, self.course_instructor
        ]:
            self.assertTrue(
                access.has_staff_access_to_preview_mode(user, course_key))

        self.assertFalse(
            access.has_staff_access_to_preview_mode(self.student, course_key))

        # we don't want to restrict a staff user, masquerading as student,
        # to access preview mode.

        # Note that self.student now have access to preview mode,
        # `is_masquerading_as_student == True` means user is staff and is
        # masquerading as a student.
        with patch(
                'lms.djangoapps.courseware.access.is_masquerading_as_student'
        ) as mock_masquerade:
            mock_masquerade.return_value = True
            for user in [
                    self.global_staff, self.course_staff,
                    self.course_instructor, self.student
            ]:
                self.assertTrue(
                    access.has_staff_access_to_preview_mode(user, course_key))
Beispiel #2
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    user_enrollments = None
    audit_enrollments = None
    has_non_audit_enrollments = False
    try:
        user_enrollments = CourseEnrollment.objects.select_related(
            'course', 'schedule').filter(user_id=user.id)
        has_non_audit_enrollments = user_enrollments.exclude(
            mode__in=CourseMode.UPSELL_TO_VERIFIED_MODES).exists()
        # TODO: clean up as part of REVO-28 (END)
        enrollment = CourseEnrollment.objects.select_related(
            'course', 'schedule').get(user_id=user.id, course_id=course.id)
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, use the default values

    has_entitlements = False
    if user.is_authenticated():
        has_entitlements = CourseEntitlement.objects.filter(user=user).exists()

    context = get_base_experiment_metadata_context(course, user, enrollment,
                                                   user_enrollments)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(
            Role.objects.filter(
                users=user,
                course_id=course.id).values_list('name').distinct())

    # get user partition data
    if user.is_authenticated():
        partition_groups = get_all_partitions_for_course(course)
        user_partitions = get_user_partition_groups(course.id,
                                                    partition_groups, user,
                                                    'name')
    else:
        user_partitions = {}

    # TODO: clean up as part of REVO-28 (START)
    context[
        'has_non_audit_enrollments'] = has_non_audit_enrollments or has_entitlements
    # TODO: clean up as part of REVO-28 (END)
    context['has_staff_access'] = has_staff_access
    context['forum_roles'] = forum_roles
    context['partition_groups'] = user_partitions
    return context
Beispiel #3
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used for Optimizely experiments, exposed via user_metadata.html:
    view from the DOM in those calling views using: JSON.parse($("#user-metadata").text());
    Most views call this function with both parameters, but student dashboard has only a user
    """
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    user_enrollments = None
    audit_enrollments = None
    has_non_audit_enrollments = False
    context = {}
    if course is not None:
        try:
            user_enrollments = CourseEnrollment.objects.select_related('course', 'schedule').filter(user_id=user.id)
            has_non_audit_enrollments = user_enrollments.exclude(mode__in=CourseMode.UPSELL_TO_VERIFIED_MODES).exists()
            # TODO: clean up as part of REVO-28 (END)
            enrollment = CourseEnrollment.objects.select_related(
                'course', 'schedule'
            ).get(user_id=user.id, course_id=course.id)
        except CourseEnrollment.DoesNotExist:
            pass  # Not enrolled, use the default values

        has_entitlements = False
        if user.is_authenticated:
            has_entitlements = CourseEntitlement.objects.filter(user=user).exists()

        context = get_base_experiment_metadata_context(course, user, enrollment, user_enrollments)
        has_staff_access = has_staff_access_to_preview_mode(user, course.id)
        forum_roles = []
        if user.is_authenticated:
            forum_roles = list(Role.objects.filter(users=user, course_id=course.id).values_list('name').distinct())

        # get user partition data
        if user.is_authenticated:
            partition_groups = get_all_partitions_for_course(course)
            user_partitions = get_user_partition_groups(course.id, partition_groups, user, 'name')
        else:
            user_partitions = {}

        # TODO: clean up as part of REVO-28 (START)
        context['has_non_audit_enrollments'] = has_non_audit_enrollments or has_entitlements
        # TODO: clean up as part of REVO-28 (END)
        context['has_staff_access'] = has_staff_access
        context['forum_roles'] = forum_roles
        context['partition_groups'] = user_partitions

    user_metadata = {
        key: context.get(key)
        for key in (
            'username',
            'user_id',
            'course_id',
            'course_display_name',
            'enrollment_mode',
            'upgrade_link',
            'upgrade_price',
            'audit_access_deadline',
            'course_duration',
            'pacing_type',
            'has_staff_access',
            'forum_roles',
            'partition_groups',
            # TODO: clean up as part of REVO-28 (START)
            'has_non_audit_enrollments',
            # TODO: clean up as part of REVO-28 (END)
            # TODO: clean up as part of REVEM-199 (START)
            'program_key_fields',
            # TODO: clean up as part of REVEM-199 (END)
        )
    }

    if user:
        user_metadata['username'] = user.username
        user_metadata['user_id'] = user.id
        if hasattr(user, 'email'):
            user_metadata['email'] = user.email

    for datekey in (
            'schedule_start',
            'enrollment_time',
            'course_start',
            'course_end',
            'dynamic_upgrade_deadline',
            'course_upgrade_deadline',
            'audit_access_deadline',
    ):
        user_metadata[datekey] = (
            context.get(datekey).isoformat() if context.get(datekey) else None
        )

    for timedeltakey in (
        'course_duration',
    ):
        user_metadata[timedeltakey] = (
            context.get(timedeltakey).total_seconds() if context.get(timedeltakey) else None
        )

    course_key = context.get('course_key')
    if course and not course_key:
        course_key = course.id

    if course_key:
        if isinstance(course_key, CourseKey):
            user_metadata['course_key_fields'] = {
                'org': course_key.org,
                'course': course_key.course,
                'run': course_key.run,
            }

            if not context.get('course_id'):
                user_metadata['course_id'] = six.text_type(course_key)
        elif isinstance(course_key, six.string_types):
            user_metadata['course_id'] = course_key

    context['user_metadata'] = user_metadata
    return context