Skip to content

Commit

Permalink
fix: changing date time to date and applying general imrpovements
Browse files Browse the repository at this point in the history
Changes dateTime to date
Considers all the  2xx status codes
Improves response error logs
  • Loading branch information
andrey-canon committed Aug 9, 2023
1 parent 0d182c4 commit 24f7dcc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 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 @@ -53,13 +52,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 @@ -83,13 +83,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: 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
4 changes: 2 additions & 2 deletions eox_nelp/signals/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ 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),
"created_at": time.date(),
"expiration_date": time.date() + 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)],
Expand Down
5 changes: 3 additions & 2 deletions eox_nelp/signals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ def _generate_external_certificate_data(timestamp, certificate_data):
group_codes = getattr(settings, "EXTERNAL_CERTIFICATES_GROUP_CODES", {})
course_id = str(certificate_data.course.course_key)
extra_info = getattr(user, "extrainfo", None)
time = timestamp.date()

return {
"id": certificate.id,
"created_at": timestamp,
"created_at": time,
# Certificate doesn't have an expiration date, so this is a thing that the client must define.
"expiration_date": timestamp + timezone.timedelta(days=365),
"expiration_date": time + timezone.timedelta(days=365),
"grade": certificate_data.grade,
"is_passing": _user_has_passing_grade(user, course_id),
"group_code": group_codes[course_id],
Expand Down

0 comments on commit 24f7dcc

Please sign in to comment.