def test_protocol_instance_caching(self, m): # Verify that we get the same Protocol instance for the same combination of (endpoint, credentials) config = Configuration( service_endpoint="https://example.com/Foo.asmx", credentials=Credentials(get_random_string(8), get_random_string(8)), auth_type=NTLM, version=Version(Build(15, 1)), retry_policy=FailFast(), ) # Test CachingProtocol.__getitem__ with self.assertRaises(KeyError): _ = Protocol[config] base_p = Protocol(config=config) self.assertEqual(base_p, Protocol[config][0]) # Make sure we always return the same item when creating a Protocol with the same endpoint and creds for _ in range(10): p = Protocol(config=config) self.assertEqual(base_p, p) self.assertEqual(id(base_p), id(p)) self.assertEqual(hash(base_p), hash(p)) self.assertEqual(id(base_p._session_pool), id(p._session_pool)) # Test CachingProtocol.__delitem__ del Protocol[config] with self.assertRaises(KeyError): _ = Protocol[config] # Make sure we get a fresh instance after we cleared the cache p = Protocol(config=config) self.assertNotEqual(base_p, p) Protocol.clear_cache()
def test_decrease_poolsize(self): # Test increasing and decreasing the pool size max_connections = 3 protocol = Protocol(config=Configuration( service_endpoint='https://example.com/Foo.asmx', credentials=Credentials(get_random_string(8), get_random_string( 8)), auth_type=NTLM, version=Version(Build(15, 1)), retry_policy=FailFast(), max_connections=max_connections, )) self.assertEqual(protocol._session_pool.qsize(), 0) self.assertEqual(protocol.session_pool_size, 0) protocol.increase_poolsize() protocol.increase_poolsize() protocol.increase_poolsize() with self.assertRaises(SessionPoolMaxSizeReached): protocol.increase_poolsize() self.assertEqual(protocol._session_pool.qsize(), max_connections) self.assertEqual(protocol.session_pool_size, max_connections) protocol.decrease_poolsize() protocol.decrease_poolsize() with self.assertRaises(SessionPoolMinSizeReached): protocol.decrease_poolsize() self.assertEqual(protocol._session_pool.qsize(), 1)
def test_pickle(self): # Test that we can pickle, hash, repr, str and compare various credentials types for o in ( Identity("XXX", "YYY", "ZZZ", "WWW"), Credentials("XXX", "YYY"), OAuth2Credentials("XXX", "YYY", "ZZZZ"), OAuth2Credentials("XXX", "YYY", "ZZZZ", identity=Identity("AAA")), OAuth2AuthorizationCodeCredentials(client_id="WWW", client_secret="XXX"), OAuth2AuthorizationCodeCredentials( client_id="WWW", client_secret="XXX", authorization_code="YYY", access_token={"access_token": "ZZZ"}, tenant_id="ZZZ", identity=Identity("AAA"), ), ): with self.subTest(o=o): pickled_o = pickle.dumps(o) unpickled_o = pickle.loads(pickled_o) self.assertIsInstance(unpickled_o, type(o)) self.assertEqual(o, unpickled_o) self.assertEqual(hash(o), hash(unpickled_o)) self.assertEqual(repr(o), repr(unpickled_o)) self.assertEqual(str(o), str(unpickled_o))
def test_protocol_default_values(self): # Test that retry_policy and auth_type always get a value regardless of how we create an Account c = Credentials(self.settings["username"], self.settings["password"]) a = Account( self.account.primary_smtp_address, autodiscover=False, config=Configuration( server=self.settings["server"], credentials=c, ), ) self.assertIsNotNone(a.protocol.auth_type) self.assertIsNotNone(a.protocol.retry_policy) a = Account( self.account.primary_smtp_address, autodiscover=True, config=Configuration( server=self.settings["server"], credentials=c, ), ) self.assertIsNotNone(a.protocol.auth_type) self.assertIsNotNone(a.protocol.retry_policy) a = Account(self.account.primary_smtp_address, autodiscover=True, credentials=c) self.assertIsNotNone(a.protocol.auth_type) self.assertIsNotNone(a.protocol.retry_policy)
def setUpClass(cls): # There's no official Exchange server we can test against, and we can't really provide credentials for our # own test server to everyone on the Internet. Travis-CI uses the encrypted settings.yml.enc for testing. # # If you want to test against your own server and account, create your own settings.yml with credentials for # that server. 'settings.yml.sample' is provided as a template. try: with open(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'settings.yml')) as f: settings = safe_load(f) except FileNotFoundError: print('Skipping %s - no settings.yml file found' % cls.__name__) print('Copy settings.yml.sample to settings.yml and enter values for your test server') raise unittest.SkipTest('Skipping %s - no settings.yml file found' % cls.__name__) cls.settings = settings cls.verify_ssl = settings.get('verify_ssl', True) if not cls.verify_ssl: # Allow unverified TLS if requested in settings file BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter # Create an account shared by all tests tz = zoneinfo.ZoneInfo('Europe/Copenhagen') cls.retry_policy = FaultTolerance(max_wait=600) config = Configuration( server=settings['server'], credentials=Credentials(settings['username'], settings['password']), retry_policy=cls.retry_policy, ) cls.account = Account(primary_smtp_address=settings['account'], access_type=DELEGATE, config=config, locale='da_DK', default_timezone=tz)
def get_test_protocol(**kwargs): return AutodiscoverProtocol( config=Configuration( service_endpoint=kwargs.get("service_endpoint", "https://example.com/Autodiscover/Autodiscover.xml"), credentials=kwargs.get("credentials", Credentials(get_random_string(8), get_random_string(8))), auth_type=kwargs.get("auth_type", NTLM), retry_policy=kwargs.get("retry_policy", FailFast()), ) )
def test_session(self, m): protocol = Protocol(config=Configuration( service_endpoint='https://example.com/Foo.asmx', credentials=Credentials(get_random_string(8), get_random_string(8)), auth_type=NTLM, version=Version(Build(15, 1)), retry_policy=FailFast() )) session = protocol.create_session() new_session = protocol.renew_session(session) self.assertNotEqual(id(session), id(new_session))
def test_failed_login_via_account(self): with self.assertRaises(AutoDiscoverFailed): Account( primary_smtp_address=self.account.primary_smtp_address, access_type=DELEGATE, credentials=Credentials(self.account.protocol.credentials.username, "WRONG_PASSWORD"), autodiscover=True, locale="da_DK", )
def test_protocol_instance_caching(self, m): # Verify that we get the same Protocol instance for the same combination of (endpoint, credentials) user, password = get_random_string(8), get_random_string(8) base_p = Protocol(config=Configuration( service_endpoint='https://example.com/Foo.asmx', credentials=Credentials(user, password), auth_type=NTLM, version=Version(Build(15, 1)), retry_policy=FailFast() )) for _ in range(10): p = Protocol(config=Configuration( service_endpoint='https://example.com/Foo.asmx', credentials=Credentials(user, password), auth_type=NTLM, version=Version(Build(15, 1)), retry_policy=FailFast() )) self.assertEqual(base_p, p) self.assertEqual(id(base_p), id(p)) self.assertEqual(hash(base_p), hash(p)) self.assertEqual(id(base_p._session_pool), id(p._session_pool)) Protocol.clear_cache()
def test_hardcode_all(self, m): # Test that we can hardcode everything without having a working server. This is useful if neither tasting or # guessing missing values works. Configuration( server="example.com", credentials=Credentials(get_random_string(8), get_random_string(8)), auth_type=NTLM, version=Version(build=Build(15, 1, 2, 3), api_version="foo"), )
def test_magic(self): config = Configuration( server="example.com", credentials=Credentials(get_random_string(8), get_random_string(8)), auth_type=NTLM, version=Version(build=Build(15, 1, 2, 3), api_version="foo"), ) # Just test that these work str(config) repr(config)
def test_pickle(self): # Test that we can pickle, repr and str Protocols o = Protocol(config=Configuration( service_endpoint='https://example.com/Foo.asmx', credentials=Credentials(get_random_string(8), get_random_string(8)), auth_type=NTLM, version=Version(Build(15, 1)), retry_policy=FailFast() )) pickled_o = pickle.dumps(o) unpickled_o = pickle.loads(pickled_o) self.assertIsInstance(unpickled_o, type(o)) self.assertEqual(repr(o), repr(unpickled_o)) self.assertEqual(str(o), str(unpickled_o))
def test_autodiscover_direct_gc(self, m): # Test garbage collection of the autodiscover cache c = Credentials(get_random_string(8), get_random_string(8)) autodiscover_cache[('example.com', c)] = AutodiscoverProtocol( config=Configuration( service_endpoint= 'https://example.com/Autodiscover/Autodiscover.xml', credentials=c, auth_type=NTLM, retry_policy=FailFast(), )) self.assertEqual(len(autodiscover_cache), 1) autodiscover_cache.__del__()
def test_close_autodiscover_connections(self, m): # A live test that we can close TCP connections c = Credentials(get_random_string(8), get_random_string(8)) autodiscover_cache[('example.com', c)] = AutodiscoverProtocol( config=Configuration( service_endpoint= 'https://example.com/Autodiscover/Autodiscover.xml', credentials=c, auth_type=NTLM, retry_policy=FailFast(), )) self.assertEqual(len(autodiscover_cache), 1) close_connections()
def test_hash(self): # Test that we can use credentials as a dict key self.assertEqual(hash(Credentials("a", "b")), hash(Credentials("a", "b"))) self.assertNotEqual(hash(Credentials("a", "b")), hash(Credentials("a", "a"))) self.assertNotEqual(hash(Credentials("a", "b")), hash(Credentials("b", "b")))
def test_tzlocal_failure(self, m): a = Account( primary_smtp_address=self.account.primary_smtp_address, access_type=DELEGATE, config=Configuration( service_endpoint=self.account.protocol.service_endpoint, credentials=Credentials(self.account.protocol.credentials.username, "WRONG_PASSWORD"), version=self.account.version, auth_type=self.account.protocol.auth_type, retry_policy=self.retry_policy, ), autodiscover=False, ) self.assertEqual(a.default_timezone, UTC)
def get_test_protocol(**kwargs): return Protocol(config=Configuration( server=kwargs.get("server"), service_endpoint=kwargs.get( "service_endpoint", f"https://{get_random_hostname()}/Foo.asmx"), credentials=kwargs.get( "credentials", Credentials(get_random_string(8), get_random_string(8))), auth_type=kwargs.get("auth_type", NTLM), version=kwargs.get("version", Version(Build(15, 1))), retry_policy=kwargs.get("retry_policy", FailFast()), max_connections=kwargs.get("max_connections"), ))
def test_close(self): # Don't use example.com here - it does not resolve or answer on all ISPs proc = psutil.Process() ip_addresses = { info[4][0] for info in socket.getaddrinfo('httpbin.org', 80, socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP) } def conn_count(): return len( [p for p in proc.connections() if p.raddr[0] in ip_addresses]) self.assertGreater(len(ip_addresses), 0) protocol = Protocol(config=Configuration( service_endpoint='http://httpbin.org', credentials=Credentials(get_random_string(8), get_random_string( 8)), auth_type=NOAUTH, version=Version(Build(15, 1)), retry_policy=FailFast(), max_connections=3)) # Merely getting a session should not create conections session = protocol.get_session() self.assertEqual(conn_count(), 0) # Open one URL - we have 1 connection session.get('http://httpbin.org') self.assertEqual(conn_count(), 1) # Open the same URL - we should still have 1 connection session.get('http://httpbin.org') self.assertEqual(conn_count(), 1) # Open some more connections s2 = protocol.get_session() s2.get('http://httpbin.org') s3 = protocol.get_session() s3.get('http://httpbin.org') self.assertEqual(conn_count(), 3) # Releasing the sessions does not close the connections protocol.release_session(session) protocol.release_session(s2) protocol.release_session(s3) self.assertEqual(conn_count(), 3) # But closing explicitly does protocol.close() self.assertEqual(conn_count(), 0)
def test_autodiscover_cache(self, m): # Mock the default endpoint that we test in step 1 of autodiscovery m.post(self.dummy_ad_endpoint, status_code=200, content=self.dummy_ad_response) discovery = Autodiscovery( email=self.account.primary_smtp_address, credentials=self.account.protocol.credentials, ) # Not cached self.assertNotIn(discovery._cache_key, autodiscover_cache) discovery.discover() # Now it's cached self.assertIn(discovery._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 with a failing autodiscover endpoint. discover() must handle this and rebuild the cache p = self.get_test_protocol() autodiscover_cache[discovery._cache_key] = p m.post("https://example.com/Autodiscover/Autodiscover.xml", status_code=404) discovery.discover() self.assertIn(discovery._cache_key, autodiscover_cache) # Make sure that the cache is actually used on the second call to discover() _orig = discovery._step_1 def _mock(slf, *args, **kwargs): raise NotImplementedError() discovery._step_1 = MethodType(_mock, discovery) discovery.discover() # 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() discovery.discover() discovery._step_1 = _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[discovery._cache_key] # This should also work if the cache does not contain the entry anymore del autodiscover_cache[discovery._cache_key]
def test_magic(self, m): # Just test we don't fail when calling repr() and str(). Insert a dummy cache entry for testing c = Credentials(get_random_string(8), get_random_string(8)) autodiscover_cache[('example.com', c)] = AutodiscoverProtocol( config=Configuration( service_endpoint= 'https://example.com/Autodiscover/Autodiscover.xml', credentials=c, auth_type=NTLM, retry_policy=FailFast(), )) self.assertEqual(len(autodiscover_cache), 1) str(autodiscover_cache) repr(autodiscover_cache) for protocol in autodiscover_cache._protocols.values(): str(protocol) repr(protocol)
def test_login_failure_and_credentials_update(self): # Create an account that does not need to create any connections account = Account( primary_smtp_address=self.account.primary_smtp_address, access_type=DELEGATE, config=Configuration( service_endpoint=self.account.protocol.service_endpoint, credentials=Credentials(self.account.protocol.credentials.username, 'WRONG_PASSWORD'), version=self.account.version, auth_type=self.account.protocol.auth_type, retry_policy=self.retry_policy, ), autodiscover=False, locale='da_DK', ) # Should fail when credentials are wrong, but UnauthorizedError is caught and retried. Mock the needed methods import exchangelib.util _orig1 = exchangelib.util._may_retry_on_error _orig2 = exchangelib.util._raise_response_errors def _mock1(response, retry_policy, wait): if response.status_code == 401: return False return _orig1(response, retry_policy, wait) def _mock2(response, protocol): if response.status_code == 401: raise UnauthorizedError('Invalid credentials for %s' % response.url) return _orig2(response, protocol) exchangelib.util._may_retry_on_error = _mock1 exchangelib.util._raise_response_errors = _mock2 try: with self.assertRaises(UnauthorizedError): account.root.refresh() finally: exchangelib.util._may_retry_on_error = _orig1 exchangelib.util._raise_response_errors = _orig2 # Cannot update from Configuration object with self.assertRaises(AttributeError): account.protocol.config.credentials = self.account.protocol.credentials # Should succeed after credentials update account.protocol.credentials = self.account.protocol.credentials account.root.refresh()
def test_login_failure_and_credentials_update(self): # Create an account that does not need to create any connections account = Account( primary_smtp_address=self.account.primary_smtp_address, access_type=DELEGATE, config=Configuration( service_endpoint=self.account.protocol.service_endpoint, credentials=Credentials( self.account.protocol.credentials.username, 'WRONG_PASSWORD'), version=self.account.version, auth_type=self.account.protocol.auth_type, retry_policy=self.retry_policy, ), autodiscover=False, locale='da_DK', ) # Should fail when credentials are wrong, but UnauthorizedError is caught and retried. Mock the needed methods class Mock1(FaultTolerance): def may_retry_on_error(self, response, wait): if response.status_code == 401: return False return super().may_retry_on_error(response, wait) def raise_response_errors(self, response): if response.status_code == 401: raise UnauthorizedError('Invalid credentials for %s' % response.url) return super().raise_response_errors(response) try: account.protocol.config.retry_policy = Mock1() with self.assertRaises(UnauthorizedError): account.root.refresh() finally: account.protocol.config.retry_policy = self.retry_policy # Cannot update from Configuration object with self.assertRaises(AttributeError): account.protocol.config.credentials = self.account.protocol.credentials # Should succeed after credentials update account.protocol.credentials = self.account.protocol.credentials account.root.refresh()
def test_pickle(self): # Test that we can pickle, hash, repr, str and compare various credentials types for o in ( Credentials('XXX', 'YYY'), OAuth2Credentials('XXX', 'YYY', 'ZZZZ'), OAuth2Credentials('XXX', 'YYY', 'ZZZZ', identity=Identity('AAA')), OAuth2AuthorizationCodeCredentials(client_id='WWW', client_secret='XXX'), OAuth2AuthorizationCodeCredentials( client_id='WWW', client_secret='XXX', authorization_code='YYY', access_token={'access_token': 'ZZZ'}, tenant_id='ZZZ', identity=Identity('AAA') ), ): with self.subTest(o=o): pickled_o = pickle.dumps(o) unpickled_o = pickle.loads(pickled_o) self.assertIsInstance(unpickled_o, type(o)) self.assertEqual(o, unpickled_o) self.assertEqual(hash(o), hash(unpickled_o)) self.assertEqual(repr(o), repr(unpickled_o)) self.assertEqual(str(o), str(unpickled_o))
def test_hash(self): # Test that we can use credentials as a dict key self.assertEqual(hash(Credentials('a', 'b')), hash(Credentials('a', 'b'))) self.assertNotEqual(hash(Credentials('a', 'b')), hash(Credentials('a', 'a'))) self.assertNotEqual(hash(Credentials('a', 'b')), hash(Credentials('b', 'b')))
def test_equality(self): self.assertEqual(Credentials("a", "b"), Credentials("a", "b")) self.assertNotEqual(Credentials("a", "b"), Credentials("a", "a")) self.assertNotEqual(Credentials("a", "b"), Credentials("b", "b"))
def test_type(self): self.assertEqual(Credentials("a", "b").type, Credentials.UPN) self.assertEqual( Credentials("*****@*****.**", "b").type, Credentials.EMAIL) self.assertEqual(Credentials("a\\n", "b").type, Credentials.DOMAIN)
def test_plain(self): Credentials("XXX", "YYY").refresh("XXX") # No-op
def test_equality(self): self.assertEqual(Credentials('a', 'b'), Credentials('a', 'b')) self.assertNotEqual(Credentials('a', 'b'), Credentials('a', 'a')) self.assertNotEqual(Credentials('a', 'b'), Credentials('b', 'b'))
def test_type(self): self.assertEqual(Credentials('a', 'b').type, Credentials.UPN) self.assertEqual(Credentials('*****@*****.**', 'b').type, Credentials.EMAIL) self.assertEqual(Credentials('a\\n', 'b').type, Credentials.DOMAIN)
def main(): parser = argparse.ArgumentParser(parents=[tools.argparser]) parser.add_argument( '--date', help= 'What date to start looking at the calendar? Use format YYYY-MM-DD.') parser.add_argument( '--look_ahead_days', help='How many days to look ahead from the starting date?') parser.add_argument('--name', help='Which person are you?') parser.add_argument('--google_calendar', action='store_true') parser.add_argument('--outlook_calendar', action='store_true') parser.add_argument( '--spreadsheet_id', help='The ID of the ECBU Luminate Support Weekly Schedule spreadsheet', default='1RgDgDRcyAFDdkEyRH7m_4QOtJ7e-kv324hEWE4JuwgI') parser.add_argument( '--exchange_username', help= 'The username you use in Outlook, should be [email protected]' ) parser.add_argument( '--primary_smtp_address', help= 'Your Outlook email address, should be [email protected]' ) parser.add_argument('--exchange_password', help='The password you use in Outlook') flags = parser.parse_args() print("Running with args: " + str(sys.argv)) if not flags.google_calendar or flags.outlook_calendar: print( "You need to specify --google_calendar and/or --outlook_calendar") return today = arrow.get(flags.date, 'YYYY-MM-DD') dates = [ today.replace(days=+n) for n in range(0, int(flags.look_ahead_days)) ] credentials = get_credentials(flags) http = credentials.authorize(httplib2.Http()) sheets_service = discovery.build('sheets', 'v4', http=http) google_calendar_service = None if flags.google_calendar: google_calendar_service = discovery.build('calendar', 'v3', http=http) exchange_account = None if flags.outlook_calendar: exchange_credentials = Credentials(username=flags.exchange_username, password=flags.exchange_password) exchange_account = Account( primary_smtp_address=flags.primary_smtp_address, credentials=exchange_credentials, autodiscover=True, access_type=DELEGATE) for date in dates: row = row_for_name(sheets_service, flags.spreadsheet_id, flags.name, date) if not row: print( "Could not find row for {name} on {date}, will skip to next day" .format(name=flags.name, date=date)) continue midnight = arrow.Arrow(date.year, date.month, date.day, tzinfo='America/Chicago') appointments = appointments_from_google_sheet(sheets_service, flags.spreadsheet_id, row, midnight) if google_calendar_service: events_made = create_google_calendar_events( appointments, google_calendar_service) if events_made == 0: print("No shifts found for {0}".format(date)) if exchange_account: events_made = create_outlook_calendar_events( appointments, exchange_account) if events_made == 0: print("No shifts found for {0}".format(date))