From e5872cf8f24b9df7c82847526ebed2c9424bdfc5 Mon Sep 17 00:00:00 2001
From: Christie Rice <8483753+crice100@users.noreply.github.com>
Date: Tue, 2 Mar 2021 14:04:03 -0500
Subject: [PATCH] MICROBA-1032 Add allowlist check and move tests (#26785)

---
 .../management/commands/fix_ungraded_certs.py | 20 ++++++++++++-------
 .../commands}/tests/test_cert_management.py   |  0
 2 files changed, 13 insertions(+), 7 deletions(-)
 rename lms/djangoapps/certificates/{ => management/commands}/tests/test_cert_management.py (100%)

diff --git a/lms/djangoapps/certificates/management/commands/fix_ungraded_certs.py b/lms/djangoapps/certificates/management/commands/fix_ungraded_certs.py
index 533082de6ac..d455860a876 100644
--- a/lms/djangoapps/certificates/management/commands/fix_ungraded_certs.py
+++ b/lms/djangoapps/certificates/management/commands/fix_ungraded_certs.py
@@ -7,6 +7,7 @@ import logging
 
 from django.core.management.base import BaseCommand
 
+from lms.djangoapps.certificates.api import is_using_certificate_allowlist_and_is_on_allowlist
 from lms.djangoapps.certificates.models import GeneratedCertificate
 from lms.djangoapps.courseware import courses
 from lms.djangoapps.grades.api import CourseGradeFactory
@@ -16,7 +17,8 @@ log = logging.getLogger(__name__)
 
 class Command(BaseCommand):
     """
-    Management command to find and grade all students that need to be graded.
+    Management command to find and grade all students that need to be graded, unless the user is on the allowlist
+    for that course run.
     """
 
     help = """
@@ -50,9 +52,13 @@ class Command(BaseCommand):
         ).filter(grade__exact='')
         course = courses.get_course_by_id(course_id)
         for cert in ungraded:
-            # grade the student
-            grade = CourseGradeFactory().read(cert.user, course)
-            log.info('grading %s - %s', cert.user, grade.percent)
-            cert.grade = grade.percent
-            if not options['noop']:
-                cert.save()
+            if is_using_certificate_allowlist_and_is_on_allowlist(cert.user, course_id):
+                log.info(f'{course_id} is using allowlist certificates, and the user {cert.user.id} is on its '
+                         f'allowlist. Certificate will not be regraded')
+            else:
+                # grade the student
+                grade = CourseGradeFactory().read(cert.user, course)
+                log.info('grading %s - %s', cert.user, grade.percent)
+                cert.grade = grade.percent
+                if not options['noop']:
+                    cert.save()
diff --git a/lms/djangoapps/certificates/tests/test_cert_management.py b/lms/djangoapps/certificates/management/commands/tests/test_cert_management.py
similarity index 100%
rename from lms/djangoapps/certificates/tests/test_cert_management.py
rename to lms/djangoapps/certificates/management/commands/tests/test_cert_management.py
-- 
GitLab