Skip to content

Commit

Permalink
Merge pull request #215 from specklesystems/gergo/urlCompare
Browse files Browse the repository at this point in the history
gergo/urlCompare
  • Loading branch information
gjedlicska authored Sep 27, 2022
2 parents c838835 + 625bd5c commit 41f1823
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
6 changes: 5 additions & 1 deletion specklepy/api/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
27 changes: 27 additions & 0 deletions specklepy/paths.py
Original file line number Diff line number Diff line change
@@ -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")
27 changes: 15 additions & 12 deletions specklepy/transports/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion tests/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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": "[email protected]"},
}

user_path.write_text(json.dumps(account))
wrap = StreamWrapper("http://bar.baz/streams/bogus")

account = wrap.get_account()

assert account.userInfo.email is None

0 comments on commit 41f1823

Please sign in to comment.