Skip to content

Commit

Permalink
fix: Retry CCG and JWT auth requests on connection reset error
Browse files Browse the repository at this point in the history
Closes: SDK-2886
  • Loading branch information
lukaszsocha2 committed Jan 9, 2023
1 parent 028ce87 commit ebedb4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 1 addition & 3 deletions boxsdk/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,14 @@ def _get_retry_request_callable(
Callable that, when called, will retry the request. Takes the same parameters as :meth:`_send_request`.
"""
# pylint:disable=unused-argument
if self._is_server_auth_type(kwargs):
return None
if network_response is None:
return partial(
self._network_layer.retry_after,
self.get_retry_after_time(attempt_number, None),
self._send_request,
)
code = network_response.status_code
if (code in (202, 429) or code >= 500) and code not in skip_retry_codes:
if (code in (202, 429) or code >= 500) and code not in skip_retry_codes and not self._is_server_auth_type(kwargs):
return partial(
self._network_layer.retry_after,
self.get_retry_after_time(attempt_number, network_response.headers.get('Retry-After', None)),
Expand Down
18 changes: 18 additions & 0 deletions test/unit/session/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

from boxsdk import CCGAuth
from boxsdk.auth.oauth2 import OAuth2
from boxsdk.config import API, Proxy
from boxsdk.exception import BoxAPIException, BoxException
Expand Down Expand Up @@ -50,6 +51,14 @@ def box_session(mock_oauth, mock_network_layer, translator):
return AuthorizedSession(oauth=mock_oauth, network_layer=mock_network_layer, translator=translator)


@pytest.fixture
def ccg_auth(client_id, client_secret, mock_user_id, mock_enterprise_id, box_session) -> CCGAuth:
# pylint:disable=protected-access
auth = CCGAuth(client_id=client_id, client_secret=client_secret, user=mock_user_id, enterprise_id=mock_enterprise_id)
auth._session = box_session
return auth


@pytest.mark.parametrize('test_method', [
Session.get,
Session.post,
Expand Down Expand Up @@ -230,6 +239,15 @@ def test_box_session_retries_connection_aborted_exception(box_session, mock_netw
assert box_response.status_code == 200


def test_box_session_retries_connection_aborted_exception_on_ccg_auth_call(successful_token_response, ccg_auth, mock_user_id, access_token):
# pylint:disable=protected-access
ccg_auth._session._network_layer.request.side_effect = [RequestsConnectionError('Connection aborted'), successful_token_response]
ccg_auth._session._network_layer.retry_after.side_effect = lambda delay, request, *args, **kwargs: request(*args, **kwargs)

new_access_token = ccg_auth.authenticate_user(mock_user_id)
assert new_access_token == access_token


def test_box_session_retries_requests_library_exceptions_only_once(box_session, mock_network_layer, test_url, generic_successful_request_response):
mock_network_layer.request.side_effect = [
RequestException('Connection aborted'),
Expand Down

0 comments on commit ebedb4a

Please sign in to comment.