fix stream wrapper matching on accounts with subdomain url-s

This commit is contained in:
Gergő Jedlicska
2022-09-26 20:15:45 +02:00
parent 8078a4b596
commit 361ba6bfcd
4 changed files with 80 additions and 14 deletions
+5 -1
View File
@@ -110,7 +110,11 @@ class StreamWrapper:
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,
)
+27
View File
@@ -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")
+15 -12
View File
@@ -8,6 +8,7 @@ from appdirs import user_data_dir
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 @@ class SQLiteTransport(AbstractTransport):
@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
+33 -1
View File
@@ -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,31 @@ 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")
path.unlink(missing_ok=True)
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