Exemple #1
0
def generate_certificate(self, **kwargs):
    """
    Generates a certificate for a single user.

    kwargs:
        - student: The student for whom to generate a certificate.
        - course_key: The course key for the course that the student is
            receiving a certificate in.
        - expected_verification_status: The expected verification status
            for the user.  When the status has changed, we double check
            that the actual verification status is as expected before
            generating a certificate, in the off chance that the database
            has not yet updated with the user's new verification status.
        - allowlist_certificate: A flag indicating whether to generate an allowlist certificate (which is V2 of
            whitelisted certificates)
        - v2_certificate: A flag indicating whether to generate a v2 course certificate
    """
    original_kwargs = kwargs.copy()
    student = User.objects.get(id=kwargs.pop('student'))
    course_key = CourseKey.from_string(kwargs.pop('course_key'))
    expected_verification_status = kwargs.pop('expected_verification_status', None)
    allowlist_certificate = kwargs.pop('allowlist_certificate', False)
    v2_certificate = kwargs.pop('v2_certificate', False)

    if allowlist_certificate:
        generate_allowlist_certificate(user=student, course_key=course_key)
        return

    if v2_certificate:
        # TODO: will be implemented in MICROBA-923
        log.warning(f'Ignoring v2 certificate task request for {student.id}: {course_key}')
        return

    if expected_verification_status:
        actual_verification_status = IDVerificationService.user_status(student)
        actual_verification_status = actual_verification_status['status']
        if expected_verification_status != actual_verification_status:
            log.warning(
                'Expected verification status {expected} '
                'differs from actual verification status {actual} '
                'for user {user} in course {course}'.format(
                    expected=expected_verification_status,
                    actual=actual_verification_status,
                    user=student.id,
                    course=course_key
                ))
            raise self.retry(kwargs=original_kwargs)
    generate_user_certificates(student=student, course_key=course_key, **kwargs)
Exemple #2
0
    def test_allowlist_generation(self):
        """
        Test allowlist certificate generation
        """
        # Create user, a course run, and an enrollment
        u = UserFactory()
        cr = CourseFactory()
        key = cr.id  # pylint: disable=no-member
        CourseEnrollmentFactory(
            user=u,
            course_id=key,
            is_active=True,
            mode="verified",
        )

        certs = GeneratedCertificate.objects.filter(user=u, course_id=key)
        assert len(certs) == 0

        generated_cert = generate_allowlist_certificate(u, key)
        assert generated_cert.status, CertificateStatuses.downloadable

        certs = GeneratedCertificate.objects.filter(user=u, course_id=key)
        assert len(certs) == 1

        self.assert_event_emitted(
            'edx.certificate.created',
            user_id=u.id,
            course_id=str(key),
            certificate_id=generated_cert.verify_uuid,
            enrollment_mode=generated_cert.mode,
            certificate_url='',
            generation_mode='batch'
        )