diff --git a/msa_sdk/msa_api.py b/msa_sdk/msa_api.py index 7101542..1b92c0d 100644 --- a/msa_sdk/msa_api.py +++ b/msa_sdk/msa_api.py @@ -4,15 +4,17 @@ import os import random import sys +from typing import Optional import requests +from requests import Response from msa_sdk import constants from msa_sdk import context logger = logging.getLogger("msa-sdk") -def host_port(): +def host_port() -> tuple[str, str]: """ Hostname and port of the API. @@ -37,11 +39,11 @@ class MSA_API(): # pylint: disable=invalid-name def __init__(self): """Initialize.""" - self.url = 'http://{}:{}/ubi-api-rest'.format(*host_port()) - self.path = "" - self.response = None - self.log_response = True - self._content = "" + self.url: str = 'http://{}:{}/ubi-api-rest'.format(*host_port()) + self.path: str = "" + self.response: Optional[Response] = None + self.log_response: bool = True + self._content: str = "" self.action = self.__class__ @classmethod @@ -224,7 +226,7 @@ def check_response(self): """ - if not self.response.ok: + if self.response is not None and not self.response.ok: json_response = self.response.json() self._content = self.process_content(self.FAILED, self.action, json_response['message']) @@ -329,7 +331,7 @@ def _call_delete(self) -> None: self._content = self.response.text self.check_response() - def add_trace_headers(self, headers): + def add_trace_headers(self, headers: dict[str, str]): """Add W3C trace headers.""" if 'TRACEID' not in context: t, s = self.create_trace_id() diff --git a/msa_sdk/profile.py b/msa_sdk/profile.py index fcd357f..11d42e1 100644 --- a/msa_sdk/profile.py +++ b/msa_sdk/profile.py @@ -4,8 +4,7 @@ The Profile class inherits from MSA_API and provides methods to check the existence of profiles and perform other profile-related operations. """ - -import json +from urllib.parse import urlencode from msa_sdk.msa_api import MSA_API @@ -41,7 +40,13 @@ def exist(self, reference) -> bool: True if the profile exists, False otherwise. """ self.action = 'Check Profile exist by reference' - self.path = '{}/v1/exist/{}'.format(self.api_path, reference) - self._call_post() - result = json.loads(self.content) - return result.get('exist', False) \ No newline at end of file + url_encoded = urlencode({'extRef': reference}) + self.path = '{}/ref?{}'.format(self.api_path, url_encoded) + self._call_get() + if self.response is None: + raise Exception("No response received from the server.") + if self.response.status_code == 404: + return False + if self.response.status_code == 200: + return True + raise Exception("Unexpected response code: {}".format(self.response.status_code)) \ No newline at end of file diff --git a/tests/test_profie.py b/tests/test_profie.py deleted file mode 100644 index ad0022f..0000000 --- a/tests/test_profie.py +++ /dev/null @@ -1,20 +0,0 @@ -""" -Test Profile -""" - -from unittest.mock import patch - -from util import profile_fixture - - -def test_exist(profile_fixture): - """ - Test exist profile by reference - """ - with patch('msa_sdk.msa_api.MSA_API._call_post') as mock_call_post: - profile = profile_fixture - reference = "test1" - profile.exist(reference) - - assert profile.path == "/profile/v1/exist/test1" - mock_call_post.assert_called_once() \ No newline at end of file diff --git a/tests/test_profile_exist.py b/tests/test_profile_exist.py new file mode 100644 index 0000000..d83adc5 --- /dev/null +++ b/tests/test_profile_exist.py @@ -0,0 +1,45 @@ +import pytest + +from msa_sdk.profile import Profile + + +class DummyResponse: + def __init__(self, status_code): + self.status_code = status_code + +class DummyProfile(Profile): + def _call_get(self): + # This will be set in the test + pass + +def test_exist_returns_true(monkeypatch): + profile = DummyProfile() + def fake_call_get(): + profile.response = DummyResponse(200) + profile._call_get = fake_call_get + assert profile.exist('ref1') is True + +def test_exist_returns_false(monkeypatch): + profile = DummyProfile() + def fake_call_get(): + profile.response = DummyResponse(404) + profile._call_get = fake_call_get + assert profile.exist('ref2') is False + + +def test_exist_raises_no_response(monkeypatch): + profile = DummyProfile() + def fake_call_get(): + profile.response = None + profile._call_get = fake_call_get + with pytest.raises(Exception, match="No response received from the server."): + profile.exist('ref3') + + +def test_exist_raises_unexpected_code(monkeypatch): + profile = DummyProfile() + def fake_call_get(): + profile.response = DummyResponse(500) + profile._call_get = fake_call_get + with pytest.raises(Exception, match="Unexpected response code: 500"): + profile.exist('ref4')