Skip to content

Commit

Permalink
Merge pull request RedHatInsights#1054 from petracihalova/payload-err…
Browse files Browse the repository at this point in the history
…-fix

[RHCLOUD-31297] fix for '_generate_error_data_payload_response' and missing 'basename' attribute
  • Loading branch information
petracihalova authored Mar 4, 2024
2 parents 2bf3144 + ac32aa5 commit 6b9c805
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rbac/api/common/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion tests/api/common/test_exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
Expand Down

0 comments on commit 6b9c805

Please sign in to comment.