def test_select_srv_host(self):
     from exchangelib.autodiscover.discovery import _select_srv_host, SrvRecord
     with self.assertRaises(ValueError):
         # Empty list
         _select_srv_host([])
     with self.assertRaises(ValueError):
         # No records with TLS port
         _select_srv_host(
             [SrvRecord(priority=1, weight=2, port=3, srv='example.com')])
     # One record
     self.assertEqual(
         _select_srv_host(
             [SrvRecord(priority=1, weight=2, port=443,
                        srv='example.com')]), 'example.com')
     # Highest priority record
     self.assertEqual(
         _select_srv_host([
             SrvRecord(priority=10,
                       weight=2,
                       port=443,
                       srv='10.example.com'),
             SrvRecord(priority=1, weight=2, port=443, srv='1.example.com'),
         ]), '10.example.com')
     # Highest priority record no matter how it's sorted
     self.assertEqual(
         _select_srv_host([
             SrvRecord(priority=1, weight=2, port=443, srv='1.example.com'),
             SrvRecord(priority=10,
                       weight=2,
                       port=443,
                       srv='10.example.com'),
         ]), '10.example.com')
 def test_select_srv_host(self):
     with self.assertRaises(ValueError):
         # Empty list
         _select_srv_host([])
     with self.assertRaises(ValueError):
         # No records with TLS port
         _select_srv_host([SrvRecord(priority=1, weight=2, port=3, srv="example.com")])
     # One record
     self.assertEqual(
         _select_srv_host([SrvRecord(priority=1, weight=2, port=443, srv="example.com")]), "example.com"
     )
     # Highest priority record
     self.assertEqual(
         _select_srv_host(
             [
                 SrvRecord(priority=10, weight=2, port=443, srv="10.example.com"),
                 SrvRecord(priority=1, weight=2, port=443, srv="1.example.com"),
             ]
         ),
         "10.example.com",
     )
     # Highest priority record no matter how it's sorted
     self.assertEqual(
         _select_srv_host(
             [
                 SrvRecord(priority=1, weight=2, port=443, srv="1.example.com"),
                 SrvRecord(priority=10, weight=2, port=443, srv="10.example.com"),
             ]
         ),
         "10.example.com",
     )
    def test_autodiscover_path_1_2_3_4_invalid_srv(self, m):
        # Test steps 1 -> 2 -> 3 -> 4 -> invalid SRV URL
        d = Autodiscovery(email=self.account.primary_smtp_address, credentials=self.account.protocol.credentials)
        m.post(self.dummy_ad_endpoint, status_code=501)
        m.post(f"https://autodiscover.{self.domain}/Autodiscover/Autodiscover.xml", status_code=501)
        m.get(f"http://autodiscover.{self.domain}/Autodiscover/Autodiscover.xml", status_code=200)

        tmp = d._get_srv_records
        d._get_srv_records = Mock(return_value=[SrvRecord(1, 1, 443, get_random_hostname())])
        try:
            with self.assertRaises(AutoDiscoverFailed):
                # Fails in step 4 with invalid response
                ad_response, _ = d.discover()
        finally:
            d._get_srv_records = tmp
示例#4
0
    def test_get_srv_records(self):
        from exchangelib.autodiscover.discovery import SrvRecord
        ad = Autodiscovery('*****@*****.**')
        # Unknown domain
        self.assertEqual(ad._get_srv_records('example.XXXXX'), [])
        # No SRV record
        self.assertEqual(ad._get_srv_records('example.com'), [])
        # Finding a real server that has a correct SRV record is not easy. Mock it
        _orig = dns.resolver.Resolver

        class _Mock1:
            @staticmethod
            def resolve(hostname, cat):
                class A:
                    @staticmethod
                    def to_text():
                        # Return a valid record
                        return '1 2 3 example.com.'

                return [A()]

        dns.resolver.Resolver = _Mock1
        del ad.resolver
        # Test a valid record
        self.assertEqual(
            ad._get_srv_records('example.com.'),
            [SrvRecord(priority=1, weight=2, port=3, srv='example.com')])

        class _Mock2:
            @staticmethod
            def resolve(hostname, cat):
                class A:
                    @staticmethod
                    def to_text():
                        # Return malformed data
                        return 'XXXXXXX'

                return [A()]

        dns.resolver.Resolver = _Mock2
        del ad.resolver
        # Test an invalid record
        self.assertEqual(ad._get_srv_records('example.com'), [])
        dns.resolver.Resolver = _orig
        del ad.resolver
    def test_autodiscover_path_1_2_3_4_valid_srv_valid_response(self, m):
        # Test steps 1 -> 2 -> 3 -> 4 -> 5
        d = Autodiscovery(email=self.account.primary_smtp_address, credentials=self.account.protocol.credentials)
        redirect_srv = "httpbin.org"
        ews_url = f"https://{redirect_srv}/EWS/Exchange.asmx"
        redirect_email = f"john@redirected.{redirect_srv}"
        m.post(self.dummy_ad_endpoint, status_code=501)
        m.post(f"https://autodiscover.{self.domain}/Autodiscover/Autodiscover.xml", status_code=501)
        m.get(f"http://autodiscover.{self.domain}/Autodiscover/Autodiscover.xml", status_code=200)
        m.head(f"https://{redirect_srv}/Autodiscover/Autodiscover.xml", status_code=200)
        m.post(
            f"https://{redirect_srv}/Autodiscover/Autodiscover.xml",
            status_code=200,
            content=self.settings_xml(redirect_email, ews_url),
        )

        tmp = d._get_srv_records
        d._get_srv_records = Mock(return_value=[SrvRecord(1, 1, 443, redirect_srv)])
        try:
            ad_response, _ = d.discover()
            self.assertEqual(ad_response.autodiscover_smtp_address, redirect_email)
            self.assertEqual(ad_response.protocol.ews_url, ews_url)
        finally:
            d._get_srv_records = tmp