Skip to content

Commit

Permalink
feat: allows to set a group code base on the course id
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-canon committed Aug 8, 2023
1 parent 2b28833 commit eaede21
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion eox_nelp/api_clients/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def create_external_certificate(self, certificate_data):
expiration_date <mandatory>: when the certificate expires.
grade <mandatory>: The associated grade with the certificate.
is_passing <mandatory>: Boolean value that represent if the user has passed the course.
group_code ,mandatory>: String, this is a value provided by the client.
user <mandatory>: Dictionary with the following data:
national_id: User National identifier.
englishs_name <optional>: User name in English.
Expand Down Expand Up @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions eox_nelp/api_clients/tests/tests_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
23 changes: 23 additions & 0 deletions eox_nelp/signals/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand All @@ -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)
10 changes: 9 additions & 1 deletion eox_nelp/signals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
"""
Expand All @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit eaede21

Please sign in to comment.