Skip to content

Commit

Permalink
add unauthorised
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarfil committed Dec 13, 2024
1 parent 2069839 commit 4aad928
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
9 changes: 4 additions & 5 deletions hexa/connections/dhis2/DHIS2Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 15 additions & 1 deletion hexa/connections/dhis2/test/test_dhis2client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")

0 comments on commit 4aad928

Please sign in to comment.