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_item_request_call_on_unauthorized_error(self):
account_id = 'dummy_acct'
mock_repo = mock.MagicMock(account_id=account_id,
**{'authenticator.refresh_session.return_value': 0, 'other.side_effect': KeyError})
od_api_helper.item_request_call(
mock_repo, self.dummy_api_call,
error.OneDriveError(prop_dict={'code': error.ErrorCode.Unauthenticated, 'message': 'dummy'},
status_code=requests.codes.unauthorized))
self.assertEqual(1, len(mock_repo.mock_calls))
name, args, kwargs = mock_repo.method_calls[0]
self.assertEqual('authenticator.refresh_session', name)
self.assertEqual((account_id,), args)
"""
try:
response = HttpResponse(404, None, json.dumps({"error":{"code":"itemNotFound", "message":"The resource could not be found"}}))
assert False
except OneDriveError as e:
assert e.status_code == 404
assert e.code == ErrorCode.ItemNotFound
try:
response = HttpResponse(403, None, json.dumps({"error":{"code":"generalException", "message":"TestMessage", "innererror":{"code":"accessDenied", "message":"TestMessage", "innererror":{"code":"unauthenticated", "message":"TestMessage"}}}}))
assert False
except OneDriveError as e:
assert e.status_code == 403
assert e.code == ErrorCode.GeneralException
assert e.matches(ErrorCode.AccessDenied)
assert e.matches(ErrorCode.Unauthenticated)
assert not e.matches(ErrorCode.NotSupported)
ErrorCode.AccessDenied: exceptions.AuthFailure,
ErrorCode.ActivityLimitReached: exceptions.ConnectionFailure,
ErrorCode.GeneralException: exceptions.ProviderOperationFailure,
ErrorCode.InvalidRange: exceptions.ProviderOperationFailure,
ErrorCode.InvalidRequest: exceptions.ProviderOperationFailure,
ErrorCode.ItemNotFound: exceptions.ProviderOperationFailure,
ErrorCode.Malformed: exceptions.ProviderOperationFailure,
ErrorCode.MalwareDetected: exceptions.ProviderOperationFailure,
ErrorCode.NameAlreadyExists: exceptions.ProviderOperationFailure,
ErrorCode.NotAllowed: exceptions.ProviderOperationFailure,
ErrorCode.NotSupported: exceptions.ProviderOperationFailure,
ErrorCode.QuotaLimitReached: exceptions.ConnectionFailure,
ErrorCode.ResourceModified: exceptions.ProviderOperationFailure,
ErrorCode.ResyncRequired: exceptions.ProviderOperationFailure,
ErrorCode.ServiceNotAvailable: exceptions.ConnectionFailure,
ErrorCode.Unauthenticated: exceptions.AuthFailure
}
try:
yield
except OneDriveError as e:
logger.error("OneDriveError in OneDriveProvider: %s", e)
raise error_map.get(e.message.split()[0], exceptions.ProviderOperationFailure)(self)
except Exception as e:
logger.error("General error in OneDriveProvider: %s", e)
raise exceptions.ProviderOperationFailure(self)
def item_request_call(repo, request_func, *args, **kwargs):
while True:
try:
return request_func(*args, **kwargs)
except onedrivesdk.error.OneDriveError as e:
logging.error('Encountered API Error: %s.', e)
if e.code == onedrivesdk.error.ErrorCode.ActivityLimitReached:
time.sleep(THROTTLE_PAUSE_SEC)
elif e.code == onedrivesdk.error.ErrorCode.Unauthenticated:
repo.authenticator.refresh_session(repo.account_id)
else:
raise e
except requests.ConnectionError as e:
logging.error('Encountered connection error: %s. Retry in %d sec.', e, THROTTLE_PAUSE_SEC)
time.sleep(THROTTLE_PAUSE_SEC)