Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect permissions errors in Autotask's HTTP 500 responses #216

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion djautotask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
VERSION = (1, 5, 4, 'final')
VERSION = (1, 5, 5, 'final')

# pragma: no cover
if VERSION[-1] != "final":
Expand Down
34 changes: 17 additions & 17 deletions djautotask/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,17 @@ def _prepare_error_response(self, response):
# decode the bytes encoded error to a string
# error = error.args[0].decode("utf-8")
error = error.replace('\r\n', '')
messages = []

try:
error = json.loads(error)
stripped_message = error.get('message').rstrip('.') if \
error.get('message') else 'No message'
primary_error_msg = '{}.'.format(stripped_message)
if error.get('errors'):
for error_message in error.get('errors'):
messages.append(
'{}.'.format(error_message.rstrip('.'))
)

messages = ' The error was: '.join(messages)

msg = '{} {}'.format(primary_error_msg, messages)

error_json = json.loads(error)
error_list = error_json.get('errors', [])
if len(error_list) > 1:
msg = 'Errors: {}'.format(', '.join(error_list))
elif len(error_list) == 1:
msg = error_list[0]
else:
# No errors given
msg = 'No error message given.'
except json.decoder.JSONDecodeError:
# JSON decoding failed
msg = 'An error occurred: {} {}'.format(response.status_code,
Expand Down Expand Up @@ -516,8 +510,14 @@ def _fetch_resource(endpoint_url, request_retry_counter=None,
self._prepare_error_response(response))
elif response.status_code == 500:
self._log_failed(response)
raise AutotaskAPIServerError(
self._prepare_error_response(response))
msg = self._prepare_error_response(response)
if FORBIDDEN_ERROR_MESSAGE in msg:
# Standards, who needs em?
raise AutotaskSecurityPermissionsException(msg)
else:
raise AutotaskAPIServerError(
self._prepare_error_response(response)
)
else:
self._log_failed(response)
raise AutotaskAPIError(
Expand Down
1 change: 1 addition & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'django.contrib.auth',
'django.contrib.sessions',
),
SECRET_KEY='correct horse battery staple',
AUTOTASK_SERVER_URL='https://localhost',
AUTOTASK_CREDENTIALS={
'username': '',
Expand Down
Loading