[tests] update client tests

This commit is contained in:
samschott 2022-05-23 22:41:31 +02:00
parent 5ef3347a32
commit d163b98004
4 changed files with 23 additions and 15 deletions

View File

@ -4,7 +4,7 @@ import pytest
from maestral.client import DropboxClient
from maestral.config import remove_configuration
from maestral.keyring import TokenType
from maestral.keyring import TokenType, CredentialStorage
from maestral.exceptions import NotFoundError
from ..lock import DropboxTestLock
@ -22,14 +22,15 @@ def client():
"""
config_name = "test-config"
c = DropboxClient(config_name)
cred_storage = CredentialStorage(config_name)
c = DropboxClient(config_name, cred_storage)
# link with given token and store auth info in keyring for other processes
access_token = os.environ.get("DROPBOX_ACCESS_TOKEN")
refresh_token = os.environ.get("DROPBOX_REFRESH_TOKEN")
token = access_token or refresh_token
token_type = TokenType.Legacy if access_token else TokenType.Offline
c.cred_storage.save_creds("1234", token, token_type)
cred_storage.save_creds("1234", token, token_type)
c.update_path_root()
# acquire test lock
@ -66,4 +67,4 @@ def client():
lock.release()
# remove creds from system keyring
c.cred_storage.delete_creds()
cred_storage.delete_creds()

View File

@ -10,6 +10,7 @@ from maestral.fsevents import Observer
from maestral.client import DropboxClient
from maestral.config import list_configs, remove_configuration
from maestral.daemon import stop_maestral_daemon_process, Stop
from maestral.keyring import CredentialStorage
from maestral.utils.appdirs import get_home_dir
from maestral.utils.path import delete
@ -28,7 +29,7 @@ def sync():
local_dir = osp.join(get_home_dir(), "dummy_dir")
os.mkdir(local_dir)
sync = SyncEngine(DropboxClient("test-config"))
sync = SyncEngine(DropboxClient("test-config", CredentialStorage("test-config")))
sync.fs_events.enable()
sync.dropbox_path = local_dir
@ -47,7 +48,7 @@ def sync():
@pytest.fixture
def client():
yield DropboxClient("test-config")
yield DropboxClient("test-config", CredentialStorage("test-config"))
remove_configuration("test-config")

View File

@ -11,12 +11,13 @@ from watchdog.events import (
from maestral.sync import SyncEngine
from maestral.client import DropboxClient
from maestral.keyring import CredentialStorage
from maestral.config import remove_configuration
@pytest.fixture
def sync():
sync = SyncEngine(DropboxClient("test-config"))
sync = SyncEngine(DropboxClient("test-config", CredentialStorage("test-config")))
sync.dropbox_path = "/"
yield sync

View File

@ -23,33 +23,36 @@ from maestral.exceptions import NotLinkedError
def test_get_auth_url():
client = DropboxClient("test-config")
cred_storage = CredentialStorage("test-config")
client = DropboxClient("test-config", cred_storage)
assert client.get_auth_url().startswith("https://")
def test_link():
client = DropboxClient("test-config")
cred_storage = Mock(spec_set=CredentialStorage)
client = DropboxClient("test-config", cred_storage)
client._auth_flow = Mock(spec_set=DropboxOAuth2FlowNoRedirect)
client.cred_storage = Mock(spec_set=CredentialStorage)
client.update_path_root = Mock()
res = client.link("token")
assert res == 0
client.update_path_root.assert_called_once()
client.cred_storage.save_creds.assert_called_once()
cred_storage.save_creds.assert_called_once()
def test_link_error():
client = DropboxClient("test-config")
cred_storage = CredentialStorage("test-config")
client = DropboxClient("test-config", cred_storage)
with pytest.raises(RuntimeError):
client.link("token")
def test_link_failed_1():
client = DropboxClient("test-config")
cred_storage = CredentialStorage("test-config")
client = DropboxClient("test-config", cred_storage)
client._auth_flow = Mock(spec_set=DropboxOAuth2FlowNoRedirect)
client._auth_flow.finish = Mock(side_effect=requests.exceptions.HTTPError("failed"))
@ -60,7 +63,8 @@ def test_link_failed_1():
def test_link_failed_2():
client = DropboxClient("test-config")
cred_storage = Mock(spec_set=CredentialStorage)
client = DropboxClient("test-config", cred_storage)
client._auth_flow = Mock(spec_set=DropboxOAuth2FlowNoRedirect)
client._auth_flow.finish = Mock(side_effect=ConnectionError("failed"))
@ -78,7 +82,8 @@ def test_link_failed_2():
def test_unlink_error():
client = DropboxClient("test-config")
cred_storage = CredentialStorage("test-config")
client = DropboxClient("test-config", cred_storage)
with pytest.raises(NotLinkedError):
client.unlink()