diff --git a/specklepy/api/wrapper.py b/specklepy/api/wrapper.py index 8e2319c4..558118eb 100644 --- a/specklepy/api/wrapper.py +++ b/specklepy/api/wrapper.py @@ -110,7 +110,11 @@ def get_account(self, token: str = None) -> Account: return self._account self._account = next( - (a for a in get_local_accounts() if self.host in a.serverInfo.url), + ( + a + for a in get_local_accounts() + if self.host == urlparse(a.serverInfo.url).netloc + ), None, ) diff --git a/specklepy/paths.py b/specklepy/paths.py new file mode 100644 index 00000000..24f736c3 --- /dev/null +++ b/specklepy/paths.py @@ -0,0 +1,27 @@ +import sys +from pathlib import Path +from appdirs import user_data_dir + + +def base_path(app_name) -> Path: + # from appdirs https://github.com/ActiveState/appdirs/blob/master/appdirs.py + # default mac path is not the one we use (we use unix path), so using special case for this + system = sys.platform + if system.startswith("java"): + import platform + + os_name = platform.java_ver()[3][0] + if os_name.startswith("Mac"): + system = "darwin" + + if system == "darwin": + return Path(Path.home(), ".config", app_name) + + return Path(user_data_dir(appname=app_name, appauthor=False, roaming=True)) + + +def accounts_path(app_name: str = "Speckle") -> Path: + """ + Gets the path where the Speckle applications are looking for accounts. + """ + return base_path(app_name).joinpath("Accounts") diff --git a/specklepy/transports/sqlite.py b/specklepy/transports/sqlite.py index 6efee6aa..b6ca8add 100644 --- a/specklepy/transports/sqlite.py +++ b/specklepy/transports/sqlite.py @@ -8,6 +8,7 @@ from contextlib import closing from specklepy.transports.abstract_transport import AbstractTransport from specklepy.logging.exceptions import SpeckleException +from specklepy.paths import base_path class SQLiteTransport(AbstractTransport): @@ -56,21 +57,23 @@ def __repr__(self) -> str: @staticmethod def get_base_path(app_name): - # from appdirs https://github.com/ActiveState/appdirs/blob/master/appdirs.py - # default mac path is not the one we use (we use unix path), so using special case for this - system = sys.platform - if system.startswith("java"): - import platform + # # from appdirs https://github.com/ActiveState/appdirs/blob/master/appdirs.py + # # default mac path is not the one we use (we use unix path), so using special case for this + # system = sys.platform + # if system.startswith("java"): + # import platform - os_name = platform.java_ver()[3][0] - if os_name.startswith("Mac"): - system = "darwin" + # os_name = platform.java_ver()[3][0] + # if os_name.startswith("Mac"): + # system = "darwin" - if system != "darwin": - return user_data_dir(appname=app_name, appauthor=False, roaming=True) + # if system != "darwin": + # return user_data_dir(appname=app_name, appauthor=False, roaming=True) - path = os.path.expanduser("~/.config/") - return os.path.join(path, app_name) + # path = os.path.expanduser("~/.config/") + # return os.path.join(path, app_name) + + return str(base_path(app_name)) def save_object_from_transport( self, id: str, source_transport: AbstractTransport diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index a3840d56..a818f3c0 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -1,5 +1,9 @@ -import pytest +import json from specklepy.api.wrapper import StreamWrapper +from specklepy.transports.sqlite import SQLiteTransport +from specklepy.paths import accounts_path +from pathlib import Path +import pytest def test_parse_stream(): @@ -79,3 +83,39 @@ def test_get_transport_with_token(): assert transport is not None assert client.account.token == "super-secret-token" + + +@pytest.fixture +def user_path() -> Path: + path = accounts_path().joinpath("test_acc.json") + # hey, py37 doesn't support the missing_ok argument + try: + path.unlink() + except: + pass + try: + path.unlink(missing_ok=True) + except: + pass + path.parent.absolute().mkdir(exist_ok=True) + yield path + path.unlink() + + +def test_wrapper_url_match(user_path) -> None: + """ + The stream wrapper should only recognize exact url matches for the account + definitions and not match for subdomains. + """ + account = { + "token": "foobar", + "serverInfo": {"name": "foo", "url": "http://foo.bar.baz", "company": "Foo"}, + "userInfo": {"id": "bla", "name": "A rando tester", "email": "rando@tester.me"}, + } + + user_path.write_text(json.dumps(account)) + wrap = StreamWrapper("http://bar.baz/streams/bogus") + + account = wrap.get_account() + + assert account.userInfo.email is None