def setup_test_config( config_name: str = "test-config", access_token: Optional[str] = env_token ) -> Maestral: """ Sets up a new maestral configuration and links it to a Dropbox account with the given token. Creates a new local Dropbox folder for the config. The token must be an "access token" which can be used to directly make Dropbox API calls and not a "refresh token". Both short lived and long lived access token will work but short lived tokens must not expire before the tests are complete. :param config_name: Config name to use or create. :param access_token: The access token to use to link the config to an account. :returns: A linked Maestral instance. """ m = Maestral(config_name) m.log_level = logging.DEBUG # link with given token m.client._init_sdk_with_token(access_token=access_token) # get corresponding Dropbox ID and store in keyring for other processes res = m.client.get_account_info() m.client.auth._account_id = res.account_id m.client.auth._access_token = access_token m.client.auth._token_access_type = "legacy" m.client.auth.save_creds() home = get_home_dir() local_dropbox_dir = generate_cc_name( os.path.join(home, "Dropbox"), suffix="test runner" ) m.create_dropbox_directory(local_dropbox_dir) return m
def m(): config_name = "test-config" m = Maestral(config_name) m.log_level = logging.DEBUG # link with given token access_token = os.environ.get("DROPBOX_ACCESS_TOKEN") refresh_token = os.environ.get("DROPBOX_REFRESH_TOKEN") if access_token: m.client._init_sdk_with_token(access_token=access_token) m.client.auth._access_token = access_token m.client.auth._token_access_type = "legacy" elif refresh_token: m.client._init_sdk_with_token(refresh_token=refresh_token) m.client.auth._refresh_token = refresh_token m.client.auth._token_access_type = "offline" else: raise RuntimeError( "Either access token or refresh token must be given as environment " "variable DROPBOX_ACCESS_TOKEN or DROPBOX_REFRESH_TOKEN." ) # get corresponding Dropbox ID and store in keyring for other processes res = m.client.get_account_info() m.client.auth._account_id = res.account_id m.client.auth.loaded = True m.client.auth.save_creds() # set local Dropbox directory home = get_home_dir() local_dropbox_dir = generate_cc_name(home + "/Dropbox", suffix="test runner") m.create_dropbox_directory(local_dropbox_dir) # acquire test lock and perform initial sync lock = DropboxTestLock(m) if not lock.acquire(timeout=60 * 60): raise TimeoutError("Could not acquire test lock") # create / clean our temporary test folder m.test_folder_dbx = "/sync_tests" m.test_folder_local = m.to_local_path(m.test_folder_dbx) try: m.client.remove(m.test_folder_dbx) except NotFoundError: pass m.client.make_dir(m.test_folder_dbx) # start syncing m.start_sync() wait_for_idle(m) # return synced and running instance yield m # stop syncing and clean up remote folder m.stop_sync() try: m.client.remove(m.test_folder_dbx) except NotFoundError: pass try: m.client.remove("/.mignore") except NotFoundError: pass # remove all shared links res = m.client.list_shared_links() for link in res.links: m.revoke_shared_link(link.url) # remove creds from system keyring m.client.auth.delete_creds() # remove local files and folders delete(m.dropbox_path) remove_configuration(m.config_name) # release lock lock.release()
def m(): m = Maestral("test-config") m.log_level = logging.DEBUG m._conf.save() yield m remove_configuration(m.config_name)