From 8e752a26689e37f206db65711b24604b293e5f44 Mon Sep 17 00:00:00 2001 From: Mark Bishop Date: Thu, 4 May 2023 15:24:17 -0400 Subject: [PATCH 1/2] Fix issue parsing API responses that are just a string --- duo_client/client.py | 2 +- tests/test_client.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/duo_client/client.py b/duo_client/client.py index d959c62..72abd9b 100644 --- a/duo_client/client.py +++ b/duo_client/client.py @@ -585,7 +585,7 @@ def raise_error(msg): raise_error('Received error response: %s' % data) response = data['response'] metadata = data.get('metadata', {}) - if not metadata and not isinstance(response, list): + if not metadata and isinstance(response, dict): metadata = response.get('metadata', {}) return (response, metadata) diff --git a/tests/test_client.py b/tests/test_client.py index 92e37bb..8372f42 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -628,6 +628,22 @@ def test_response_contains_invalid_json(self): self.assertEqual(e.exception.reason, api_res.reason) self.assertEqual(e.exception.data, response) + def test_response_is_a_string(self): + """ + Some API requests return just a string in their response. + We want to make sure we handle those correctly + """ + api_res = self.APIResponse(200, 'Fake reason') + response = { + 'response': "just a string", + 'stat': 'OK' + } + try: + self.client.parse_json_response_and_metadata(api_res, json.dumps(response)) + except Exception: + self.fail("parsing raised exception for string response") + + def test_response_stat_isnot_OK(self): api_res = self.APIResponse(200, 'Fake reason') From 0a0dc34838aa740c16fc12d40c5a0238735fe22e Mon Sep 17 00:00:00 2001 From: Mark Bishop Date: Thu, 4 May 2023 16:01:57 -0400 Subject: [PATCH 2/2] Added test for generic list response --- tests/test_client.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_client.py b/tests/test_client.py index 8372f42..28d030c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -643,6 +643,22 @@ def test_response_is_a_string(self): except Exception: self.fail("parsing raised exception for string response") + def test_response_is_a_list(self): + """ + Some API requests return just a list in their response. with + metadata included at the top level. + We want to make sure we handle those correctly + """ + api_res = self.APIResponse(200, "Fake reason") + expected_metadata = { "offset": 4 } + expected_response = ["multiple", "elements"] + response = { + "response": expected_response, + "metadata": expected_metadata, + "stat": "OK" + } + response, metadata = self.client.parse_json_response_and_metadata(api_res, json.dumps(response)) + self.assertEqual(metadata, expected_metadata) def test_response_stat_isnot_OK(self): api_res = self.APIResponse(200, 'Fake reason')