Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_box_oauth_exception(has_network_response):
status = 'status'
message = 'message'
url = 'https://example.com'
method = 'GET'
headers = {'header': 'value'}
network_response = Mock(DefaultNetworkResponse, headers=headers) if has_network_response else None
box_exception = BoxOAuthException(
status,
message=message,
url=url,
method=method,
network_response=network_response,
)
assert str(box_exception) == '''
Message: {0}
Status: {1}
URL: {2}
Method: {3}
Headers: {4}'''.format(message, status, url, method, headers if has_network_response else 'N/A')
assert box_exception.network_response is network_response
def test_auth_object_is_closed_even_if_revoke_fails(client_id, client_secret, access_token, mock_box_session):
auth = OAuth2(client_id=client_id, client_secret=client_secret, access_token=access_token, session=mock_box_session)
with patch.object(auth, 'revoke', side_effect=BoxOAuthException(status=500)):
with pytest.raises(BoxOAuthException):
auth.close(revoke=True)
assert auth.closed is True
def test_expired_refresh_token_raises(box_oauth, box_client, mock_box):
# pylint:disable=protected-access
mock_box.oauth.expire_token(box_oauth._access_token)
mock_box.oauth.expire_token(box_oauth._refresh_token)
# pylint:enable=protected-access
with pytest.raises(BoxOAuthException):
box_client.folder('0').get()
def test_token_request_raises_box_oauth_exception_when_tokens_are_not_in_the_response(
test_method,
mock_box_session,
network_response_with_missing_tokens,
):
# pylint:disable=redefined-outer-name
mock_box_session.request.return_value = network_response_with_missing_tokens
oauth = OAuth2(
client_id='',
client_secret='',
access_token='fake_access_token',
session=mock_box_session,
)
with pytest.raises(BoxOAuthException):
test_method(oauth)
(None, BoxOAuthException(status=500), BoxOAuthException),
(MyError, BoxOAuthException(status=500), MyError),
])
@pytest.mark.parametrize('close_kwargs', [{}, dict(revoke=False), dict(revoke=True)])
def test_context_manager_reraises_first_exception_after_close(
client_id, client_secret, mock_box_session, close_kwargs, raise_from_block, raise_from_close, expected_exception,
):
auth = OAuth2(client_id=client_id, client_secret=client_secret, session=mock_box_session)
with patch.object(auth, 'close', side_effect=raise_from_close) as mock_close:
with pytest.raises(expected_exception):
with auth.closing(**close_kwargs):
if raise_from_block:
raise raise_from_block
mock_close.assert_called_once_with(**close_kwargs)
url = '{base_auth_url}/revoke'.format(base_auth_url=self._api_config.OAUTH2_API_URL)
try:
network_response = self._session.request(
'POST',
url,
data={
'client_id': self._client_id,
'client_secret': self._client_secret,
'token': token_to_revoke,
},
access_token=access_token,
)
except BoxAPIException as box_api_exception:
six.raise_from(self._oauth_exception(box_api_exception.network_response, url), box_api_exception)
if not network_response.ok:
raise BoxOAuthException(
network_response.status_code,
network_response.content,
url,
'POST',
network_response,
)
self._store_tokens(None, None)
"""
exception_kwargs = dict(
status=network_response.status_code,
url=url,
method='POST',
network_response=network_response,
)
if is_json_response(network_response):
json_response = network_response.json()
exception_kwargs.update(dict(
code=json_response.get('code') or json_response.get('error'),
message=json_response.get('message') or json_response.get('error_description'),
))
else:
exception_kwargs['message'] = network_response.content
return BoxOAuthException(**exception_kwargs)
def exception_handler(self):
try:
yield
except BoxOAuthException:
raise exceptions.AuthFailure(self)
except BoxAPIException:
raise exceptions.ProviderOperationFailure(self)
except ReadTimeout:
raise exceptions.ConnectionFailure(self)
except Exception:
raise exceptions.ProviderOperationFailure(self)
finally:
self._persist_tokens()