diff --git a/hexa/connections/dhis2/DHIS2Client.py b/hexa/connections/dhis2/DHIS2Client.py index aa1e9c1d4..0238e8747 100644 --- a/hexa/connections/dhis2/DHIS2Client.py +++ b/hexa/connections/dhis2/DHIS2Client.py @@ -60,15 +60,14 @@ def request(self, method: str, url: str, *args, **kwargs) -> requests.Response: logger.exception(exc) raise - def raise_if_error(self, r: requests.Response) -> None: - if r.status_code != 200 and "json" in r.headers["content-type"]: - msg = r.json() + def raise_if_error(self, response: requests.Response) -> None: + if response.status_code != 200 and "json" in response.headers["content-type"]: + msg = response.json() if msg.get("status") == "ERROR": raise DHIS2ClientException( f"{msg.get('status')} {msg.get('httpStatusCode')}: {msg.get('message')}" ) - - r.raise_for_status() + response.raise_for_status() def ping(self) -> requests.Response: response = self.get(f"{self.url}/system/ping") diff --git a/hexa/connections/dhis2/test/fixtures/2.36/api_authenticate_failure.yaml b/hexa/connections/dhis2/test/fixtures/2.36/api_authenticate_unauthorised.yaml similarity index 100% rename from hexa/connections/dhis2/test/fixtures/2.36/api_authenticate_failure.yaml rename to hexa/connections/dhis2/test/fixtures/2.36/api_authenticate_unauthorised.yaml diff --git a/hexa/connections/dhis2/test/fixtures/2.39/api_authenticate.yaml b/hexa/connections/dhis2/test/fixtures/2.39/api_authenticate_unauthorised.yaml similarity index 66% rename from hexa/connections/dhis2/test/fixtures/2.39/api_authenticate.yaml rename to hexa/connections/dhis2/test/fixtures/2.39/api_authenticate_unauthorised.yaml index dcb002239..f09ea0e5f 100644 --- a/hexa/connections/dhis2/test/fixtures/2.39/api_authenticate.yaml +++ b/hexa/connections/dhis2/test/fixtures/2.39/api_authenticate_unauthorised.yaml @@ -4,5 +4,5 @@ responses: body: pong content_type: text/plain method: GET - status: 200 - url: http://localhost:8080/api/system/ping + status: 404 + url: http://localhost:8080/api/system/ping \ No newline at end of file diff --git a/hexa/connections/dhis2/test/test_dhis2client.py b/hexa/connections/dhis2/test/test_dhis2client.py index 6b646be56..0a4d3b3fb 100644 --- a/hexa/connections/dhis2/test/test_dhis2client.py +++ b/hexa/connections/dhis2/test/test_dhis2client.py @@ -9,7 +9,7 @@ class TestDhis2Client(test.TestCase): @responses.activate def test_successful_authentication(self): - VERSIONS = ["2.36", "2.37", "2.38", "2.39", "2.40", "2.41"] + VERSIONS = ["2.36", "2.37", "2.38", "2.40", "2.41"] response_dir = Path("hexa", "connections", "dhis2", "test", "fixtures") for version in VERSIONS: @@ -18,3 +18,17 @@ def test_successful_authentication(self): responses._add_from_file(path_to_version) client = DHIS2Client("http://localhost:8080/", "admin", "district") assert client.status.status_code == 200 + + @responses.activate + def test_unauthorised_authentication(self): + VERSIONS = ["2.36", "2.39"] # versions where ping not impleted or fails + response_dir = Path("hexa", "connections", "dhis2", "test", "fixtures") + + for version in VERSIONS: + with self.subTest(version=version): + path_to_version = Path( + response_dir, version, "api_authenticate_unauthorised.yaml" + ) + responses._add_from_file(path_to_version) + with self.assertRaises(Exception): + DHIS2Client("http://localhost:8080/", "admin", "wrong_password")