Skip to content

Commit

Permalink
Improve coverage of get_service_authtype()
Browse files Browse the repository at this point in the history
  • Loading branch information
ecederstrand committed Jan 10, 2022
1 parent 6dfcb23 commit ac55fd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
5 changes: 2 additions & 3 deletions exchangelib/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .errors import UnauthorizedError, TransportError
from .util import create_element, add_xml_child, xml_to_str, ns_translation, _back_off_if_needed, \
_retry_after, DummyResponse, CONNECTION_ERRORS
_retry_after, DummyResponse, CONNECTION_ERRORS, RETRY_WAIT

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -128,7 +128,6 @@ def get_service_authtype(service_endpoint, retry_policy, api_versions, name):
# respond when given a valid request. Try all known versions. Gross.
from .protocol import BaseProtocol
retry = 0
wait = 10 # seconds
t_start = time.monotonic()
headers = DEFAULT_HEADERS.copy()
for api_version in api_versions:
Expand All @@ -148,7 +147,7 @@ def get_service_authtype(service_endpoint, retry_policy, api_versions, name):
total_wait = time.monotonic() - t_start
r = DummyResponse(url=service_endpoint, headers={}, request_headers=headers)
if retry_policy.may_retry_on_error(response=r, wait=total_wait):
wait = _retry_after(r, wait)
wait = _retry_after(r, RETRY_WAIT)
log.info("Connection error on URL %s (retry %s, error: %s). Cool down %s secs",
service_endpoint, retry, e, wait)
retry_policy.back_off(wait)
Expand Down
24 changes: 21 additions & 3 deletions tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pickle
import socket
import tempfile
from unittest.mock import Mock
from unittest.mock import Mock, patch
import warnings
try:
import zoneinfo
Expand All @@ -18,10 +18,10 @@
from exchangelib.configuration import Configuration
from exchangelib.items import CalendarItem, SEARCH_SCOPE_CHOICES
from exchangelib.errors import SessionPoolMinSizeReached, ErrorNameResolutionNoResults, ErrorAccessDenied, \
TransportError, SessionPoolMaxSizeReached, TimezoneDefinitionInvalidForYear
TransportError, SessionPoolMaxSizeReached, TimezoneDefinitionInvalidForYear, RateLimitError
from exchangelib.properties import TimeZone, RoomList, FreeBusyView, AlternateId, ID_FORMATS, EWS_ID, \
SearchableMailbox, FailedMailbox, Mailbox, DLMailbox, ItemId, MailboxData, FreeBusyViewOptions
from exchangelib.protocol import Protocol, BaseProtocol, NoVerifyHTTPAdapter, FailFast
from exchangelib.protocol import Protocol, BaseProtocol, NoVerifyHTTPAdapter, FailFast, FaultTolerance
from exchangelib.services import GetRoomLists, GetRooms, ResolveNames, GetSearchableMailboxes, \
SetUserOofSettings, ExpandDL
from exchangelib.settings import OofSettings
Expand Down Expand Up @@ -801,3 +801,21 @@ def test_version_guess(self, m):
e.exception.args[0],
"No valid version headers found in response (ErrorNameResolutionMultipleResults('.'))"
)

@patch('requests.sessions.Session.post', side_effect=ConnectionResetError('XXX'))
def test_get_service_authtype(self, m):
with self.assertRaises(TransportError) as e:
Protocol(config=Configuration(
service_endpoint='https://example.com/EWS/Exchange.asmx',
credentials=Credentials(get_random_string(8), get_random_string(8)),
auth_type=None, version=Version(Build(15, 1)), retry_policy=FailFast()
))
self.assertEqual(e.exception.args[0], 'XXX')

with self.assertRaises(RateLimitError) as e:
Protocol(config=Configuration(
service_endpoint='https://example.com/EWS/Exchange.asmx',
credentials=Credentials(get_random_string(8), get_random_string(8)),
auth_type=None, version=Version(Build(15, 1)), retry_policy=FaultTolerance(max_wait=0.5)
))
self.assertEqual(e.exception.args[0], 'Max timeout reached')

0 comments on commit ac55fd2

Please sign in to comment.