def test_get_elements(self): # Test that we can handle SOAP-level error messages # TODO: The request actually raises ErrorInvalidRequest, but we interpret that to mean a wrong API version and # end up throwing ErrorInvalidServerVersion. We should make a more direct test. svc = ResolveNames(self.account.protocol) with self.assertRaises(ErrorInvalidServerVersion): list(svc._get_elements(create_element("XXX")))
def test_wrap(self): # Test payload wrapper with both delegation, impersonation and timezones MockTZ = namedtuple('EWSTimeZone', ['ms_id']) MockAccount = namedtuple('Account', ['access_type', 'primary_smtp_address', 'default_timezone']) content = create_element('AAA') api_version = 'BBB' account = MockAccount(DELEGATE, '*****@*****.**', MockTZ('XXX')) wrapped = wrap(content=content, api_version=api_version, account=account) self.assertEqual( PrettyXmlHandler.prettify_xml(wrapped), b'''<?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <s:Header> <t:RequestServerVersion Version="BBB"/> <t:TimeZoneContext> <t:TimeZoneDefinition Id="XXX"/> </t:TimeZoneContext> </s:Header> <s:Body> <AAA/> </s:Body> </s:Envelope> ''') account = MockAccount(IMPERSONATION, '*****@*****.**', MockTZ('XXX')) wrapped = wrap(content=content, api_version=api_version, account=account) self.assertEqual( PrettyXmlHandler.prettify_xml(wrapped), b'''<?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <s:Header> <t:RequestServerVersion Version="BBB"/> <t:ExchangeImpersonation> <t:ConnectingSID> <t:PrimarySmtpAddress>[email protected]</t:PrimarySmtpAddress> </t:ConnectingSID> </t:ExchangeImpersonation> <t:TimeZoneContext> <t:TimeZoneDefinition Id="XXX"/> </t:TimeZoneContext> </s:Header> <s:Body> <AAA/> </s:Body> </s:Envelope> ''')
def test_handle_backoff(self): # Test that we can handle backoff messages svc = ResolveNames(self.account.protocol) tmp = svc._response_generator orig_policy = self.account.protocol.config.retry_policy try: # We need to fail fast so we don't end up in an infinite loop self.account.protocol.config.retry_policy = FailFast() svc._response_generator = Mock( side_effect=ErrorServerBusy("XXX", back_off=1)) with self.assertRaises(ErrorServerBusy) as e: list(svc._get_elements(create_element("XXX"))) self.assertEqual(e.exception.args[0], "XXX") finally: svc._response_generator = tmp self.account.protocol.config.retry_policy = orig_policy
def test_wrap(self): # Test payload wrapper with both delegation, impersonation and timezones MockTZ = namedtuple("EWSTimeZone", ["ms_id"]) MockAccount = namedtuple( "Account", ["access_type", "identity", "default_timezone"]) content = create_element("AAA") api_version = "BBB" account = MockAccount(access_type=DELEGATE, identity=None, default_timezone=MockTZ("XXX")) wrapped = wrap(content=content, api_version=api_version, timezone=account.default_timezone) self.assertEqual( PrettyXmlHandler.prettify_xml(wrapped), b"""<?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <s:Header> <t:RequestServerVersion Version="BBB"/> <t:TimeZoneContext> <t:TimeZoneDefinition Id="XXX"/> </t:TimeZoneContext> </s:Header> <s:Body> <AAA/> </s:Body> </s:Envelope> """, ) for attr, tag in ( ("primary_smtp_address", "PrimarySmtpAddress"), ("upn", "PrincipalName"), ("sid", "SID"), ("smtp_address", "SmtpAddress"), ): val = f"{attr}@example.com" account = MockAccount(access_type=DELEGATE, identity=Identity(**{attr: val}), default_timezone=MockTZ("XXX")) wrapped = wrap( content=content, api_version=api_version, account_to_impersonate=account.identity, timezone=account.default_timezone, ) self.assertEqual( PrettyXmlHandler.prettify_xml(wrapped), f"""<?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <s:Header> <t:RequestServerVersion Version="BBB"/> <t:ExchangeImpersonation> <t:ConnectingSID> <t:{tag}>{val}</t:{tag}> </t:ConnectingSID> </t:ExchangeImpersonation> <t:TimeZoneContext> <t:TimeZoneDefinition Id="XXX"/> </t:TimeZoneContext> </s:Header> <s:Body> <AAA/> </s:Body> </s:Envelope> """.encode(), )
def test_wrap(self): # Test payload wrapper with both delegation, impersonation and timezones MockTZ = namedtuple('EWSTimeZone', ['ms_id']) MockAccount = namedtuple( 'Account', ['access_type', 'identity', 'default_timezone']) content = create_element('AAA') api_version = 'BBB' account = MockAccount(access_type=DELEGATE, identity=None, default_timezone=MockTZ('XXX')) wrapped = wrap(content=content, api_version=api_version, timezone=account.default_timezone) self.assertEqual( PrettyXmlHandler.prettify_xml(wrapped), b'''<?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <s:Header> <t:RequestServerVersion Version="BBB"/> <t:TimeZoneContext> <t:TimeZoneDefinition Id="XXX"/> </t:TimeZoneContext> </s:Header> <s:Body> <AAA/> </s:Body> </s:Envelope> ''') for attr, tag in ( ('primary_smtp_address', 'PrimarySmtpAddress'), ('upn', 'PrincipalName'), ('sid', 'SID'), ('smtp_address', 'SmtpAddress'), ): val = '*****@*****.**' % attr account = MockAccount(access_type=DELEGATE, identity=Identity(**{attr: val}), default_timezone=MockTZ('XXX')) wrapped = wrap( content=content, api_version=api_version, account_to_impersonate=account.identity, timezone=account.default_timezone, ) self.assertEqual( PrettyXmlHandler.prettify_xml(wrapped), '''<?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <s:Header> <t:RequestServerVersion Version="BBB"/> <t:ExchangeImpersonation> <t:ConnectingSID> <t:{tag}>{val}</t:{tag}> </t:ConnectingSID> </t:ExchangeImpersonation> <t:TimeZoneContext> <t:TimeZoneDefinition Id="XXX"/> </t:TimeZoneContext> </s:Header> <s:Body> <AAA/> </s:Body> </s:Envelope> '''.format(tag=tag, val=val).encode())