def test_get_roomlists_parsing(self): # Test static XML since server has no roomlists xml = b'''\ <?xml version="1.0" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <m:GetRoomListsResponse ResponseClass="Success" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <m:ResponseCode>NoError</m:ResponseCode> <m:RoomLists> <t:Address> <t:Name>Roomlist</t:Name> <t:EmailAddress>[email protected]</t:EmailAddress> <t:RoutingType>SMTP</t:RoutingType> <t:MailboxType>PublicDL</t:MailboxType> </t:Address> <t:Address> <t:Name>Roomlist</t:Name> <t:EmailAddress>[email protected]</t:EmailAddress> <t:RoutingType>SMTP</t:RoutingType> <t:MailboxType>PublicDL</t:MailboxType> </t:Address> </m:RoomLists> </m:GetRoomListsResponse> </s:Body> </s:Envelope>''' ws = GetRoomLists(self.account.protocol) self.assertSetEqual({rl.email_address for rl in ws.parse(xml)}, {'*****@*****.**', '*****@*****.**'})
def test_get_roomlists(self): # The test server is not guaranteed to have any room lists which makes this test less useful ws = GetRoomLists(self.account.protocol) roomlists = ws.call() self.assertEqual(list(roomlists), []) # Test shortcut self.assertEqual(list(self.account.protocol.get_roomlists()), [])
def test_error_schema_validation(self): # Test that we can parse extra info with ErrorSchemaValidation xml = b"""\ <?xml version="1.0" encoding="utf-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorSchemaValidation </faultcode> <faultstring>XXX</faultstring> <detail> <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors"> ErrorSchemaValidation</e:ResponseCode> <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">YYY</e:Message> <t:MessageXml xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <t:LineNumber>123</t:LineNumber> <t:LinePosition>456</t:LinePosition> <t:Violation>ZZZ</t:Violation> </t:MessageXml> </detail> </s:Fault> </s:Body> </s:Envelope>""" version = mock_version(build=EXCHANGE_2010) ws = GetRoomLists( mock_protocol(version=version, service_endpoint="example.com")) with self.assertRaises(ErrorSchemaValidation) as e: ws.parse(xml) self.assertEqual(e.exception.args[0], "YYY ZZZ (line: 123 position: 456)")
def test_error_server_busy(self): # Test that we can parse an exception response via SOAP body xml = b"""\ <?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorServerBusy</faultcode> <faultstring xml:lang="en-US">The server cannot service this request right now. Try again later.</faultstring> <detail> <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors"> ErrorServerBusy </e:ResponseCode> <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors"> The server cannot service this request right now. Try again later. </e:Message> <t:MessageXml xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <t:Value Name="BackOffMilliseconds">297749</t:Value> </t:MessageXml> </detail> </s:Fault> </s:Body> </s:Envelope>""" version = mock_version(build=EXCHANGE_2010) ws = GetRoomLists( mock_protocol(version=version, service_endpoint="example.com")) with self.assertRaises(ErrorServerBusy) as e: ws.parse(xml) self.assertEqual( e.exception.back_off, 297.749 ) # Test that we correctly parse the BackOffMilliseconds value
def test_error_server_busy(self): # Test that we can parse an ErrorServerBusy response version = mock_version(build=EXCHANGE_2010) ws = GetRoomLists(mock_protocol(version=version, service_endpoint='example.com')) xml = b'''\ <?xml version='1.0' encoding='utf-8'?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorServerBusy</faultcode> <faultstring xml:lang="en-US">The server cannot service this request right now. Try again later.</faultstring> <detail> <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors"> ErrorServerBusy </e:ResponseCode> <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors"> The server cannot service this request right now. Try again later. </e:Message> <t:MessageXml xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <t:Value Name="BackOffMilliseconds">297749</t:Value> </t:MessageXml> </detail> </s:Fault> </s:Body> </s:Envelope>''' header, body = ws._get_soap_parts(response=MockResponse(xml)) with self.assertRaises(ErrorServerBusy) as cm: ws._get_elements_in_response(response=ws._get_soap_messages(body=body)) self.assertEqual(cm.exception.back_off, 297.749)
def test_get_roomlists_parsing(self): # Test static XML since server has no roomlists ws = GetRoomLists(self.account.protocol) xml = b'''\ <?xml version="1.0" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <m:GetRoomListsResponse ResponseClass="Success" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <m:ResponseCode>NoError</m:ResponseCode> <m:RoomLists> <t:Address> <t:Name>Roomlist</t:Name> <t:EmailAddress>[email protected]</t:EmailAddress> <t:RoutingType>SMTP</t:RoutingType> <t:MailboxType>PublicDL</t:MailboxType> </t:Address> <t:Address> <t:Name>Roomlist</t:Name> <t:EmailAddress>[email protected]</t:EmailAddress> <t:RoutingType>SMTP</t:RoutingType> <t:MailboxType>PublicDL</t:MailboxType> </t:Address> </m:RoomLists> </m:GetRoomListsResponse> </s:Body> </s:Envelope>''' _, body = ws._get_soap_parts(response=MockResponse(xml)) res = ws._get_elements_in_response(response=ws._get_soap_messages(body=body)) self.assertSetEqual( {RoomList.from_xml(elem=elem, account=None).email_address for elem in res}, {'*****@*****.**', '*****@*****.**'} )
def test_invalid_server_version(self): # Test that we get a client-side error if we call a service that was only implemented in a later version version = mock_version(build=EXCHANGE_2007) account = mock_account(version=version, protocol=mock_protocol(version=version, service_endpoint='example.com')) with self.assertRaises(NotImplementedError): list(GetServerTimeZones(protocol=account.protocol).call()) with self.assertRaises(NotImplementedError): list(GetRoomLists(protocol=account.protocol).call()) with self.assertRaises(NotImplementedError): list(GetRooms(protocol=account.protocol).call('XXX'))
def test_soap_error(self): xml_template = """\ <?xml version="1.0" encoding="utf-8" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode>{faultcode}</faultcode> <faultstring>{faultstring}</faultstring> <faultactor>https://CAS01.example.com/EWS/Exchange.asmx</faultactor> <detail> <ResponseCode xmlns="http://schemas.microsoft.com/exchange/services/2006/errors">{responsecode}</ResponseCode> <Message xmlns="http://schemas.microsoft.com/exchange/services/2006/errors">{message}</Message> </detail> </s:Fault> </s:Body> </s:Envelope>""" version = mock_version(build=EXCHANGE_2010) protocol = mock_protocol(version=version, service_endpoint="example.com") ws = GetRoomLists(protocol=protocol) xml = xml_template.format(faultcode="YYY", faultstring="AAA", responsecode="XXX", message="ZZZ").encode("utf-8") with self.assertRaises(SOAPError) as e: ws.parse(xml) self.assertIn("AAA", e.exception.args[0]) self.assertIn("YYY", e.exception.args[0]) self.assertIn("ZZZ", e.exception.args[0]) xml = xml_template.format(faultcode="ErrorNonExistentMailbox", faultstring="AAA", responsecode="XXX", message="ZZZ").encode("utf-8") with self.assertRaises(ErrorNonExistentMailbox) as e: ws.parse(xml) self.assertIn("AAA", e.exception.args[0]) xml = xml_template.format(faultcode="XXX", faultstring="AAA", responsecode="ErrorNonExistentMailbox", message="YYY").encode("utf-8") with self.assertRaises(ErrorNonExistentMailbox) as e: ws.parse(xml) self.assertIn("YYY", e.exception.args[0]) # Test bad XML (no body) xml = b"""\ <?xml version="1.0" encoding="utf-8" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <t:ServerVersionInfo MajorVersion="8" MinorVersion="0" MajorBuildNumber="685" MinorBuildNumber="8" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" /> </s:Header> </s:Body> </s:Envelope>""" with self.assertRaises(MalformedResponseError): ws.parse(xml) # Test bad XML (no fault) xml = b"""\ <?xml version="1.0" encoding="utf-8" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <t:ServerVersionInfo MajorVersion="8" MinorVersion="0" MajorBuildNumber="685" MinorBuildNumber="8" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" /> </s:Header> <s:Body> <s:Fault> </s:Fault> </s:Body> </s:Envelope>""" with self.assertRaises(SOAPError) as e: ws.parse(xml) self.assertEqual( e.exception.args[0], "SOAP error code: None string: None actor: None detail: None")
def test_soap_error(self): soap_xml = """\ <?xml version="1.0" encoding="utf-8" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode>{faultcode}</faultcode> <faultstring>{faultstring}</faultstring> <faultactor>https://CAS01.example.com/EWS/Exchange.asmx</faultactor> <detail> <ResponseCode xmlns="http://schemas.microsoft.com/exchange/services/2006/errors">{responsecode}</ResponseCode> <Message xmlns="http://schemas.microsoft.com/exchange/services/2006/errors">{message}</Message> </detail> </s:Fault> </s:Body> </s:Envelope>""" version = mock_version(build=EXCHANGE_2010) protocol = mock_protocol(version=version, service_endpoint='example.com') ws = GetRoomLists(protocol=protocol) _, body = ws._get_soap_parts(response=MockResponse(soap_xml.format( faultcode='YYY', faultstring='AAA', responsecode='XXX', message='ZZZ' ).encode('utf-8'))) with self.assertRaises(SOAPError) as e: ws._get_soap_messages(body=body) self.assertIn('AAA', e.exception.args[0]) self.assertIn('YYY', e.exception.args[0]) self.assertIn('ZZZ', e.exception.args[0]) _, body = ws._get_soap_parts(response=MockResponse(soap_xml.format( faultcode='ErrorNonExistentMailbox', faultstring='AAA', responsecode='XXX', message='ZZZ' ).encode('utf-8'))) with self.assertRaises(ErrorNonExistentMailbox) as e: ws._get_soap_messages(body=body) self.assertIn('AAA', e.exception.args[0]) _, body = ws._get_soap_parts(response=MockResponse(soap_xml.format( faultcode='XXX', faultstring='AAA', responsecode='ErrorNonExistentMailbox', message='YYY' ).encode('utf-8'))) with self.assertRaises(ErrorNonExistentMailbox) as e: ws._get_soap_messages(body=body) self.assertIn('YYY', e.exception.args[0]) # Test bad XML (no body) soap_xml = b"""\ <?xml version="1.0" encoding="utf-8" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <t:ServerVersionInfo MajorVersion="8" MinorVersion="0" MajorBuildNumber="685" MinorBuildNumber="8" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" /> </s:Header> </s:Body> </s:Envelope>""" with self.assertRaises(MalformedResponseError): ws._get_soap_parts(response=MockResponse(soap_xml)) # Test bad XML (no fault) soap_xml = b"""\ <?xml version="1.0" encoding="utf-8" ?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <t:ServerVersionInfo MajorVersion="8" MinorVersion="0" MajorBuildNumber="685" MinorBuildNumber="8" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" /> </s:Header> <s:Body> <s:Fault> </s:Fault> </s:Body> </s:Envelope>""" _, body = ws._get_soap_parts(response=MockResponse(soap_xml)) with self.assertRaises(TransportError): ws._get_soap_messages(body=body)