예제 #1
0
    def test_get_users_allowlist(self):
        """
        Test that allowlisted users are returned correctly
        """
        u1 = UserFactory()
        u2 = UserFactory()
        u3 = UserFactory()
        u4 = UserFactory()

        cr1 = CourseFactory()
        key1 = cr1.id  # pylint: disable=no-member
        cr2 = CourseFactory()
        key2 = cr2.id  # pylint: disable=no-member
        cr3 = CourseFactory()
        key3 = cr3.id  # pylint: disable=no-member

        CourseEnrollmentFactory(
            user=u1,
            course_id=key1,
            is_active=True,
            mode="verified",
        )
        CourseEnrollmentFactory(
            user=u2,
            course_id=key1,
            is_active=True,
            mode="verified",
        )
        CourseEnrollmentFactory(
            user=u3,
            course_id=key1,
            is_active=True,
            mode="verified",
        )
        CourseEnrollmentFactory(
            user=u4,
            course_id=key2,
            is_active=True,
            mode="verified",
        )

        # Add user to the allowlist
        CertificateAllowlistFactory.create(course_id=key1, user=u1)
        # Add user to the allowlist, but set allowlist to false
        CertificateAllowlistFactory.create(course_id=key1,
                                           user=u2,
                                           allowlist=False)
        # Add user to the allowlist in the other course
        CertificateAllowlistFactory.create(course_id=key2, user=u4)

        users = get_allowlisted_users(key1)
        assert 1 == users.count()
        assert users[0].id == u1.id

        users = get_allowlisted_users(key2)
        assert 1 == users.count()
        assert users[0].id == u4.id

        users = get_allowlisted_users(key3)
        assert 0 == users.count()
예제 #2
0
    def test_get_users_allowlist_false(self):
        """
        Test
        """
        users = get_allowlisted_users(self.course_run_key)
        assert 0 == users.count()

        users = get_allowlisted_users(self.second_course_run_key)
        assert 0 == users.count()

        users = get_allowlisted_users(self.third_course_run_key)
        assert 0 == users.count()
예제 #3
0
    def test_get_users_allowlist(self):
        """
        Test that allowlisted users are returned correctly
        """
        users = get_allowlisted_users(self.course_run_key)
        assert 1 == users.count()
        assert users[0].id == self.user.id

        users = get_allowlisted_users(self.second_course_run_key)
        assert 1 == users.count()
        assert users[0].id == self.user4.id

        users = get_allowlisted_users(self.third_course_run_key)
        assert 0 == users.count()
예제 #4
0
def _invalidate_generated_certificates(course_id, enrolled_students,
                                       certificate_statuses):
    """
    Invalidate generated certificates for all enrolled students in the given course having status in
    'certificate_statuses', if the student is not on the course's allowlist.

    Generated Certificates are invalidated by marking its status 'unavailable' and updating verify_uuid, download_uuid,
    download_url and grade with empty string.

    :param course_id: Course Key for the course whose generated certificates need to be removed
    :param enrolled_students: (queryset or list) students enrolled in the course
    :param certificate_statuses: certificates statuses for whom to remove generated certificate
    """
    certificates = GeneratedCertificate.objects.filter(
        user__in=enrolled_students,
        course_id=course_id,
        status__in=certificate_statuses,
    )

    allowlisted_users = get_allowlisted_users(course_id)

    # Invalidate each cert that is not allowlisted. We loop over the certs and invalidate each individually in order to
    # save a history of the change.
    for c in certificates:
        if c.user in allowlisted_users:
            log.info(
                f'Certificate for user {c.user.id} will not be invalidated because they are on the allowlist for '
                f'course {course_id}')
        else:
            log.info(
                f'About to invalidate certificate for user {c.user.id} in course {course_id}'
            )
            c.invalidate()
예제 #5
0
def _invalidate_generated_certificates(course_id, enrolled_students,
                                       certificate_statuses):
    """
    Invalidate generated certificates for all enrolled students in the given course having status in
    'certificate_statuses', if the student is not on the course's allowlist.

    Generated Certificates are invalidated by marking its status 'unavailable' and updating error_reason, download_uuid,
    download_url and grade with empty string.

    If V2 of Course Certificates is enabled for this course-run, this step will be skipped.

    :param course_id: Course Key for the course whose generated certificates need to be removed
    :param enrolled_students: (queryset or list) students enrolled in the course
    :param certificate_statuses: certificates statuses for whom to remove generated certificate
    """
    if is_using_v2_course_certificates(course_id):
        log.info(
            f"Course {course_id} is using V2 certificates. Skipping certificate invalidation step of bulk "
            "regeneration.")
    else:
        certificates = GeneratedCertificate.objects.filter(
            user__in=enrolled_students,
            course_id=course_id,
            status__in=certificate_statuses,
        )

        allowlisted_users = get_allowlisted_users(course_id)

        # Invalidate each cert that is not allowlisted. We loop over the certs and invalidate each individually in order
        # to save a history of the change.
        for c in certificates:
            if c.user in allowlisted_users:
                log.info(
                    f'Certificate for user {c.user.id} will not be invalidated because they are on the allowlist '
                    f'for course {course_id}')
            else:
                log.info(
                    f'About to invalidate certificate for user {c.user.id} in course {course_id}'
                )
                c.invalidate(source='bulk_certificate_regeneration')