diff --git a/eox_nelp/api_clients/__init__.py b/eox_nelp/api_clients/__init__.py index 3ebe4eaf..0f9e9964 100644 --- a/eox_nelp/api_clients/__init__.py +++ b/eox_nelp/api_clients/__init__.py @@ -20,6 +20,8 @@ class AbstractApiClient(ABC): """Abstract api client class, this implement a basic authentication method and defines methods POST and GET""" + extra_headers_key = None + @property @abstractmethod def base_url(self): @@ -31,12 +33,24 @@ def __init__(self): Abstract ApiClient creator, this will set the session based on the authenticate result. """ self.session = self._authenticate() + self.session.headers.update(self._get_extra_headers()) @abstractmethod def _authenticate(self): """Abstract method that should return a requests Session instance in its implementation.""" raise NotImplementedError + def _get_extra_headers(self): + """This verify the extra_headers_key attribute and returns its value from the django settings. + + Returns + Dict: The extra_headers_key must be set a dictionary. + """ + if self.extra_headers_key: + return getattr(settings, self.extra_headers_key, {}) + + return {} + def make_post(self, path, data): """This method uses the session attribute to perform a POST request based on the base_url attribute and the given path, if the response has a status code 200 diff --git a/eox_nelp/api_clients/certificates.py b/eox_nelp/api_clients/certificates.py index 01d2f146..1e8bc09e 100644 --- a/eox_nelp/api_clients/certificates.py +++ b/eox_nelp/api_clients/certificates.py @@ -11,6 +11,8 @@ class ExternalCertificatesApiClient(AbstractBasicAuthApiClient): """Allow to perform multiple external certificates operations.""" + extra_headers_key = "EXTERNAL_CERTIFICATES_EXTRA_HEADERS" + def __init__(self): self.user = getattr(settings, "EXTERNAL_CERTIFICATES_USER") self.password = getattr(settings, "EXTERNAL_CERTIFICATES_PASSWORD") diff --git a/eox_nelp/api_clients/tests/tests_certificates.py b/eox_nelp/api_clients/tests/tests_certificates.py index b63348d3..585204b1 100644 --- a/eox_nelp/api_clients/tests/tests_certificates.py +++ b/eox_nelp/api_clients/tests/tests_certificates.py @@ -5,6 +5,7 @@ """ import unittest +from django.test import override_settings from django.utils import timezone from mock import Mock, patch @@ -59,6 +60,17 @@ def test_failed_create_certificate(self): - Raise KeyError exception. """ data = {} - api_client = ExternalCertificatesApiClient() + api_client = self.api_class() self.assertRaises(KeyError, api_client.create_external_certificate, data) + + @override_settings(EXTERNAL_CERTIFICATES_EXTRA_HEADERS={"HTTP_CUSTOM_HEADER": "ABC123"}) + def test_extra_headers(self): + """Test if the extra header has been added to the session + + Expected behavior: + - Custom Header in session headers. + """ + api_client = self.api_class() + + self.assertEqual(api_client.session.headers["HTTP_CUSTOM_HEADER"], "ABC123") diff --git a/eox_nelp/api_clients/tests/tests_futurex.py b/eox_nelp/api_clients/tests/tests_futurex.py index 0013b847..4a603391 100644 --- a/eox_nelp/api_clients/tests/tests_futurex.py +++ b/eox_nelp/api_clients/tests/tests_futurex.py @@ -5,6 +5,7 @@ """ import unittest +import requests from mock import patch from eox_nelp.api_clients.futurex import FuturexApiClient, FuturexMissingArguments @@ -27,7 +28,7 @@ def test_enrollment_progress(self, auth_mock, post_mock): - Response is the expected value - make_post was called with the right values. """ - auth_mock.return_value = {} + auth_mock.return_value = requests.Session() expected_value = { "status": {"success": True, "message": "successful", "code": 1} } @@ -55,7 +56,7 @@ def test_failed_enrollment_progress(self, auth_mock): Expected behavior: - Raise FuturexMissingArguments exception """ - auth_mock.return_value = {} + auth_mock.return_value = requests.Session() data = { "courseId": "course-v1:lms152", "userId": 52,