From 7fa6d3d4b92cb8a7e9ae50b6cc726408ed33ee58 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Tue, 8 Aug 2023 16:18:39 -0500 Subject: [PATCH] feat: allows to set a group code base on the course id --- eox_nelp/api_clients/certificates.py | 3 ++- .../api_clients/tests/tests_certificates.py | 1 + eox_nelp/signals/tests/test_utils.py | 23 +++++++++++++++++++ eox_nelp/signals/utils.py | 10 +++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/eox_nelp/api_clients/certificates.py b/eox_nelp/api_clients/certificates.py index e65f1c63..0f6e1e2a 100644 --- a/eox_nelp/api_clients/certificates.py +++ b/eox_nelp/api_clients/certificates.py @@ -30,6 +30,7 @@ def create_external_certificate(self, certificate_data): expiration_date : when the certificate expires. grade : The associated grade with the certificate. is_passing : Boolean value that represent if the user has passed the course. + group_code ,mandatory>: String, this is a value provided by the client. user : Dictionary with the following data: national_id: User National identifier. englishs_name : User name in English. @@ -57,7 +58,7 @@ def create_external_certificate(self, certificate_data): "id": user["national_id"], "id_type": "saudi", }, - "group_code": "fail", # This is not clear + "group_code": certificate_data["group_code"], "certificate_type": "completion", # What types do we have ? "metadata": { "degree": certificate_data["grade"], diff --git a/eox_nelp/api_clients/tests/tests_certificates.py b/eox_nelp/api_clients/tests/tests_certificates.py index f20e4d7c..b63348d3 100644 --- a/eox_nelp/api_clients/tests/tests_certificates.py +++ b/eox_nelp/api_clients/tests/tests_certificates.py @@ -43,6 +43,7 @@ def test_create_certificate(self, post_mock): "grade": 10, "is_passing": True, "user": user, + "group_code": "ABC123", } api_client = self.api_class() diff --git a/eox_nelp/signals/tests/test_utils.py b/eox_nelp/signals/tests/test_utils.py index cac59524..2ac29a8d 100644 --- a/eox_nelp/signals/tests/test_utils.py +++ b/eox_nelp/signals/tests/test_utils.py @@ -5,7 +5,9 @@ """ import unittest +from django.conf import settings from django.contrib.auth import get_user_model +from django.test import override_settings from django.utils import timezone from mock import Mock, patch from opaque_keys.edx.keys import CourseKey @@ -63,6 +65,7 @@ def setUp(self): name="", ) + @override_settings(EXTERNAL_CERTIFICATES_GROUP_CODES={"course-v1:test+Cx105+2022_T4": "ABC123"}) @patch("eox_nelp.signals.utils._user_has_passing_grade") @patch("eox_nelp.signals.utils.GeneratedCertificate") def test_generate_certificate_data(self, generate_certificate_mock, passing_mock): @@ -85,6 +88,7 @@ def test_generate_certificate_data(self, generate_certificate_mock, passing_mock "expiration_date": time + timezone.timedelta(days=365), "grade": self.certificate_data.grade, "is_passing": True, + "group_code": settings.EXTERNAL_CERTIFICATES_GROUP_CODES[str(self.certificate_data.course.course_key)], "user": { "national_id": self.user.username, "english_name": self.certificate_data.user.pii.name, @@ -103,3 +107,22 @@ def test_generate_certificate_data(self, generate_certificate_mock, passing_mock self.user, str(self.certificate_data.course.course_key) ) + + @patch("eox_nelp.signals.utils._user_has_passing_grade") + @patch("eox_nelp.signals.utils.GeneratedCertificate") + def test_invalid_group_codes(self, generate_certificate_mock, passing_mock): + """This tests when the EXTERNAL_CERTIFICATES_GROUP_CODES value has not been set. + + Expected behavior: + - Raise KeyError + """ + certificate = Mock() + certificate.id = 85 + generate_certificate_mock.objects.get.return_value = certificate + passing_mock.return_value = True + data = { + "timestamp": timezone.now(), + "certificate_data": self.certificate_data, + } + + self.assertRaises(KeyError, _generate_external_certificate_data, **data) diff --git a/eox_nelp/signals/utils.py b/eox_nelp/signals/utils.py index 97a1b32a..174ce850 100644 --- a/eox_nelp/signals/utils.py +++ b/eox_nelp/signals/utils.py @@ -4,6 +4,7 @@ _generate_external_certificate_data: Generates dict data from CertificateData. _user_has_passing_grade: Determines if the user has a passing grade """ +from django.conf import settings from django.contrib.auth import get_user_model from django.utils import timezone from eox_core.edxapp_wrapper.certificates import get_generated_certificate @@ -24,6 +25,10 @@ def _generate_external_certificate_data(timestamp, certificate_data): https://github.com/eduNEXT/openedx-events/blob/main/openedx_events/learning/data.py#L100 and will provide of the user certificate data. + Raises: + KeyError: if the current course has not been set with its group code the method will raise + this exception. + Returns: Dict: certificate data """ @@ -32,6 +37,8 @@ def _generate_external_certificate_data(timestamp, certificate_data): user=user, course_id=certificate_data.course.course_key, ) + group_codes = getattr(settings, "EXTERNAL_CERTIFICATES_GROUP_CODES", {}) + course_id = str(certificate_data.course.course_key) extra_info = getattr(user, "extrainfo", None) return { @@ -40,7 +47,8 @@ def _generate_external_certificate_data(timestamp, certificate_data): # Certificate doesn't have an expiration date, so this is a thing that the client must define. "expiration_date": timestamp + timezone.timedelta(days=365), "grade": certificate_data.grade, - "is_passing": _user_has_passing_grade(user, str(certificate_data.course.course_key)), + "is_passing": _user_has_passing_grade(user, course_id), + "group_code": group_codes[course_id], "user": { "national_id": user.username, "english_name": certificate_data.user.pii.name,