def test_autodiscover_from_account(self): from exchangelib.autodiscover import _autodiscover_cache _autodiscover_cache.clear() account = Account( primary_smtp_address=self.account.primary_smtp_address, credentials=self.account.protocol.credentials, autodiscover=True, locale='da_DK') self.assertEqual(account.primary_smtp_address, self.account.primary_smtp_address) self.assertEqual(account.protocol.service_endpoint.lower(), self.account.protocol.service_endpoint.lower()) self.assertEqual(account.protocol.version.build, self.account.protocol.version.build) # Make sure cache is full self.assertTrue((account.domain, self.account.protocol.credentials, True) in _autodiscover_cache) # Test that autodiscover works with a full cache account = Account( primary_smtp_address=self.account.primary_smtp_address, credentials=self.account.protocol.credentials, autodiscover=True, locale='da_DK') self.assertEqual(account.primary_smtp_address, self.account.primary_smtp_address) # Test cache manipulation key = (account.domain, self.account.protocol.credentials, True) self.assertTrue(key in _autodiscover_cache) del _autodiscover_cache[key] self.assertFalse(key in _autodiscover_cache) del _autodiscover_cache
def test_autodiscover_failure(self): # Empty the cache from exchangelib.autodiscover import _autodiscover_cache _autodiscover_cache.clear() with self.assertRaises(ErrorNonExistentMailbox): # Test that error is raised with an empty cache discover(email='XXX.' + self.account.primary_smtp_address, credentials=self.account.protocol.credentials) with self.assertRaises(ErrorNonExistentMailbox): # Test that error is raised with a full cache discover(email='XXX.' + self.account.primary_smtp_address, credentials=self.account.protocol.credentials)
def test_autodiscover_cache(self, m): # Empty the cache from exchangelib.autodiscover import _autodiscover_cache _autodiscover_cache.clear() cache_key = (self.account.domain, self.account.protocol.credentials) # Not cached self.assertNotIn(cache_key, _autodiscover_cache) discover(email=self.account.primary_smtp_address, credentials=self.account.protocol.credentials) # Now it's cached self.assertIn(cache_key, _autodiscover_cache) # Make sure the cache can be looked by value, not by id(). This is important for multi-threading/processing self.assertIn( (self.account.primary_smtp_address.split('@')[1], Credentials(self.account.protocol.credentials.username, self.account.protocol.credentials.password), True), _autodiscover_cache) # Poison the cache. discover() must survive and rebuild the cache _autodiscover_cache[cache_key] = AutodiscoverProtocol( config=Configuration( service_endpoint='https://example.com/blackhole.asmx', credentials=Credentials('leet_user', 'cannaguess'), auth_type=NTLM, retry_policy=FailFast(), )) m.post('https://example.com/blackhole.asmx', status_code=404) discover(email=self.account.primary_smtp_address, credentials=self.account.protocol.credentials) self.assertIn(cache_key, _autodiscover_cache) # Make sure that the cache is actually used on the second call to discover() _orig = exchangelib.autodiscover._try_autodiscover def _mock(*args, **kwargs): raise NotImplementedError() exchangelib.autodiscover._try_autodiscover = _mock discover(email=self.account.primary_smtp_address, credentials=self.account.protocol.credentials) # Fake that another thread added the cache entry into the persistent storage but we don't have it in our # in-memory cache. The cache should work anyway. _autodiscover_cache._protocols.clear() discover(email=self.account.primary_smtp_address, credentials=self.account.protocol.credentials) exchangelib.autodiscover._try_autodiscover = _orig # Make sure we can delete cache entries even though we don't have it in our in-memory cache _autodiscover_cache._protocols.clear() del _autodiscover_cache[cache_key] # This should also work if the cache does not contain the entry anymore del _autodiscover_cache[cache_key]
print(item.subject, item.sender, item.datetime_received) write_log(file_name, item.subject + item.sender + item.datetime_received) except Exception as error: print('! FAIL: ', error) write_log(file_name, error) pass '''print('------NTLM test') write_log(file_name, '------NTLM test') try: account = connect_discoff_ntlm(server, mail, domain+username, password) ews_url = account.protocol.service_endpoint ews_auth_type = account.protocol.auth_type primary_smtp_address = account.primary_smtp_address config = Configuration(service_endpoint = ews_url, credentials = credentials, auth_type = ews_auth_type) account = Account(primary_smtp_address = primary_smtp_address, config = config, autodiscover = False, access_type = DELEGATE) print(account.root.tree()) write_log(file_name, account.root.tree()) for item in account.inbox.all().order_by('-datetime_received')[:2]: print(item.subject, item.sender, item.datetime_received) write_log(file_name, item.subject + item.sender + item.datetime_received) except Exception as error: print('! FAIL: ', error) write_log(file_name, error)''' from exchangelib.autodiscover import _autodiscover_cache _autodiscover_cache.clear() f.close() print('End of program') write_log(file_name, 'End of program')