From 34a60a507c9d6de88c027f17013d0c8ccc9cd55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petra=20C=CC=8Ci=CC=81halova=CC=81?= Date: Mon, 4 Mar 2024 12:03:49 +0100 Subject: [PATCH 1/2] fix for '_generate_error_data_payload_response' and missing 'basename' attribute --- rbac/api/common/exception_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rbac/api/common/exception_handler.py b/rbac/api/common/exception_handler.py index dae74d9c1..bd95c9590 100644 --- a/rbac/api/common/exception_handler.py +++ b/rbac/api/common/exception_handler.py @@ -74,7 +74,7 @@ def _generate_error_data_payload_response(detail: str, context, http_status_code # Some exceptions might be raised from places that are not views. view = context.get("view") - if view: + if view and hasattr(view, "basename"): data["errors"][0]["source"] = view.basename return data From ac32aa5406f2eec48faccf44a91d5019176cd752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petra=20C=CC=8Ci=CC=81halova=CC=81?= Date: Mon, 4 Mar 2024 12:57:54 +0100 Subject: [PATCH 2/2] added test for the missing 'basename' attribute in the view object --- tests/api/common/test_exception_handler.py | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/api/common/test_exception_handler.py b/tests/api/common/test_exception_handler.py index 854fdfc8c..c6a4816a3 100644 --- a/tests/api/common/test_exception_handler.py +++ b/tests/api/common/test_exception_handler.py @@ -207,10 +207,38 @@ def test_generate_error_data_payload_with_view_response(self): self.assertEqual(detail, only_error.get("detail"), f"unexpected detail message in the payload: {result}") + self.assertEqual(mocked_view.basename, only_error.get("source"), f"unexpected source in the payload: {result}") + self.assertEqual( - mocked_view.basename, only_error.get("source"), f"unexpected detail message in the payload: {result}" + str(http_status_code), only_error.get("status"), f"unexpected status code in the payload: {result}" ) + def test_generate_error_data_payload_without_view_basename(self): + """ + Tests that the function under test generates the data payload correctly + when a view is passed in the context without basename attribute.""" + # Prepare a payload with a view in the context without "basename" attribute. + detail = "some error message" + context = {"view": []} + http_status_code = status.HTTP_200_OK + + # Call the function under test. + result = _generate_error_data_payload_response( + detail=detail, context=context, http_status_code=http_status_code + ) + + # Assert that the correct structure is returned. + errors = result.get("errors") + if not errors: + self.fail(f"the errors array was not present in the payload: {result}") + + if len(errors) != 1: + self.fail(f"only one error was expected in the payload: {result}") + + only_error = errors[0] + + self.assertEqual(detail, only_error.get("detail"), f"unexpected detail message in the payload: {result}") + self.assertIsNone(only_error.get("source"), f"unexpected 'source' in the payload: {result}") self.assertEqual( str(http_status_code), only_error.get("status"), f"unexpected status code in the payload: {result}" )