Skip to content

Commit

Permalink
fix: general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-canon committed Aug 9, 2023
1 parent 1d50a43 commit b961cab
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 28 deletions.
11 changes: 6 additions & 5 deletions eox_nelp/api_clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session
from rest_framework import status

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -67,13 +66,14 @@ def make_post(self, path, data):

response = self.session.post(url=url, json=data)

if response.status_code == status.HTTP_200_OK:
if response.ok:
return response.json()

LOGGER.error(
"An error has occurred trying to make post request to %s with status code %s",
"An error has occurred trying to make post request to %s with status code %s and message %s",
url,
response.status_code,
response.json(),
)

return {
Expand All @@ -97,13 +97,14 @@ def make_get(self, path, payload):

response = self.session.get(url=url, params=payload)

if response.status_code == status.HTTP_200_OK:
if response.ok:
return response.json()

LOGGER.error(
"An error has occurred trying to make a get request to %s with status code %s",
"An error has occurred trying to make a get request to %s with status code %s and message %s",
url,
response.status_code,
response.json(),
)

return {
Expand Down
10 changes: 5 additions & 5 deletions eox_nelp/api_clients/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ def create_external_certificate(self, certificate_data):
payload = {
"reference_id": certificate_data["id"],
"date": {
"issuance": str(certificate_data["created_at"]),
"expiration": str(certificate_data["expiration_date"]),
"issuance": certificate_data["created_at"],
"expiration": None,
},
"individual": {
"name_en": user.get("english_name", ""),
"name_ar": user.get("arabic_name", ""),
"name_en": user["english_name"],
"name_ar": user["arabic_name"],
"id": user["national_id"],
"id_type": "saudi",
},
"group_code": certificate_data["group_code"],
"certificate_type": "completion", # What types do we have ?
"metadata": {
"degree": certificate_data["grade"],
"FAIL": certificate_data["is_passing"],
"FAIL": not certificate_data["is_passing"],
}
}

Expand Down
10 changes: 8 additions & 2 deletions eox_nelp/api_clients/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_successful_post(self, requests_mock):
- POST was called with the given data and right url.
"""
response = Mock()
response.ok = True
response.status_code = 200
expected_value = {
"status": {"success": True, "message": "successful", "code": 1}
Expand Down Expand Up @@ -58,12 +59,14 @@ def test_failed_post(self, requests_mock):
- Error was logged.
"""
response = Mock()
response.ok = False
response.status_code = 400
response.json.return_value = {"test": True}
requests_mock.Session.return_value.post.return_value = response
data = {"testing": True, "application": "futurex"}
log_error = (
"An error has occurred trying to make post request to https://testing.com/fake/path with status code 400"
"An error has occurred trying to make post request to https://testing.com/fake/path with status code 400 "
f"and message {response.json()}"
)
with patch.object(self.api_class, "_authenticate") as auth_mock:
auth_mock.return_value = requests_mock.Session()
Expand All @@ -90,6 +93,7 @@ def test_successful_get(self, requests_mock):
- GET was called with the given data and right url.
"""
response = Mock()
response.ok = True
response.status_code = 200
expected_value = {
"status": {"success": True, "message": "successful", "code": 1}
Expand Down Expand Up @@ -119,12 +123,14 @@ def test_failed_get(self, requests_mock):
- Error was logged.
"""
response = Mock()
response.ok = False
response.status_code = 404
response.json.return_value = {"test": True}
requests_mock.Session.return_value.get.return_value = response
params = {"format": "json"}
log_error = (
"An error has occurred trying to make a get request to https://testing.com/fake/path with status code 404"
"An error has occurred trying to make a get request to https://testing.com/fake/path with status code 404 "
f"and message {response.json()}"
)
with patch.object(self.api_class, "_authenticate") as auth_mock:
auth_mock.return_value = requests_mock.Session()
Expand Down
2 changes: 1 addition & 1 deletion eox_nelp/signals/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def certificate_publisher(certificate, metadata, **kwargs): # pylint: disable=u
)
create_external_certificate.delay(
external_certificate_data=_generate_external_certificate_data(
timestamp=metadata.time,
time=metadata.time,
certificate_data=certificate,
)
)
Expand Down
8 changes: 7 additions & 1 deletion eox_nelp/signals/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,10 @@ def create_external_certificate(external_certificate_data):
and will provide of the user certificate data.
"""
api_client = ExternalCertificatesApiClient()
api_client.create_external_certificate(external_certificate_data)
response = api_client.create_external_certificate(external_certificate_data)

logger.info(
"The data %s was sent to the external certificate service. The response was: %s",
external_certificate_data,
response,
)
4 changes: 2 additions & 2 deletions eox_nelp/signals/tests/test_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_create_call(self, create_external_certificate_mock, generate_data_mock)
certificate_publisher(self.certificate_data, self.metadata)

generate_data_mock.assert_called_with(
timestamp=self.metadata.time,
time=self.metadata.time,
certificate_data=self.certificate_data,
)
create_external_certificate_mock.delay.assert_called_with(
Expand Down Expand Up @@ -233,7 +233,7 @@ def test_alternative_mode(self, create_external_certificate_mock, generate_data_
certificate_publisher(certificate_data, self.metadata)

generate_data_mock.assert_called_with(
timestamp=self.metadata.time,
time=self.metadata.time,
certificate_data=certificate_data,
)
create_external_certificate_mock.delay.assert_called_with(
Expand Down
10 changes: 5 additions & 5 deletions eox_nelp/signals/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setUp(self):
course_key=CourseKey.from_string("course-v1:test+Cx105+2022_T4"),
),
mode="audit",
grade=15,
grade=0.85,
current_status="non-passing",
download_url="",
name="",
Expand All @@ -84,9 +84,9 @@ def test_generate_certificate_data(self, generate_certificate_mock, passing_mock

expected_value = {
"id": certificate.id,
"created_at": time,
"expiration_date": time + timezone.timedelta(days=365),
"grade": self.certificate_data.grade,
"created_at": str(time.date()),
"expiration_date": None,
"grade": self.certificate_data.grade * 100,
"is_passing": True,
"group_code": settings.EXTERNAL_CERTIFICATES_GROUP_CODES[str(self.certificate_data.course.course_key)],
"user": {
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_invalid_group_codes(self, generate_certificate_mock, passing_mock):
generate_certificate_mock.objects.get.return_value = certificate
passing_mock.return_value = True
data = {
"timestamp": timezone.now(),
"time": timezone.now(),
"certificate_data": self.certificate_data,
}

Expand Down
12 changes: 5 additions & 7 deletions eox_nelp/signals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
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
from eox_core.edxapp_wrapper.grades import get_course_grade_factory
from opaque_keys.edx.keys import CourseKey
Expand All @@ -16,11 +15,11 @@
User = get_user_model()


def _generate_external_certificate_data(timestamp, certificate_data):
def _generate_external_certificate_data(time, certificate_data):
"""This generates a dictionary from a CertificateData class
Args:
timestamp<Datetime>: Date when the certificate was created.
time<Datetime>: Date when the certificate was created.
certificate<CertificateData>: This an instance of the class defined in this link
https://github.com/eduNEXT/openedx-events/blob/main/openedx_events/learning/data.py#L100
and will provide of the user certificate data.
Expand All @@ -43,10 +42,9 @@ def _generate_external_certificate_data(timestamp, certificate_data):

return {
"id": certificate.id,
"created_at": timestamp,
# 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,
"created_at": str(time.date()),
"expiration_date": None,
"grade": float(certificate_data.grade) * 100,
"is_passing": _user_has_passing_grade(user, course_id),
"group_code": group_codes[course_id],
"user": {
Expand Down

0 comments on commit b961cab

Please sign in to comment.