From 5e9d65dd668affe1bbadf388070fc8284b46c4d2 Mon Sep 17 00:00:00 2001 From: Jj Date: Thu, 17 Mar 2022 16:00:34 -0500 Subject: [PATCH] Require site_url only for token login --- src/pycrunch/elements.py | 6 ++++-- tests/test_elements.py | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/pycrunch/elements.py b/src/pycrunch/elements.py index 510d835..b6eb29a 100644 --- a/src/pycrunch/elements.py +++ b/src/pycrunch/elements.py @@ -316,18 +316,20 @@ class ElementSession(lemonpy.Session): def __init__( self, email=None, password=None, token=None, site_url=None, progress_tracking=None ): - if not site_url: + if not site_url and token: raise ValueError("Must include a `site_url` host to connect to") self.__email = email self.__password = password self.token = token self.site_url = site_url - self.domain = urlparse(site_url).netloc + self.domain = urlparse(site_url).netloc if site_url else None self.progress_tracking = progress_tracking or DefaultProgressTracking() super(ElementSession, self).__init__() @property def root(self): + if not self.site_url: + raise ValueError("Session must be initialized with `site_url`") return self.get(self.site_url).payload @property diff --git a/tests/test_elements.py b/tests/test_elements.py index feb25ae..b67da82 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -284,7 +284,21 @@ def test_root(self): root = session.root assert root == api_root - def test_require_host(self): + def test_root_no_site_url(self): + email = "abc@example.com" + session = elements.ElementSession(email=email, password="abx") + with self.assertRaises(ValueError) as err: + _ = session.root + assert str(err.exception) == "Session must be initialized with `site_url`" + + def test_require_host_with_token(self): with self.assertRaises(ValueError) as err: elements.ElementSession(token="abc") assert str(err.exception) == "Must include a `site_url` host to connect to" + + def test_host_not_required_on_email(self): + email = "abc@example.com" + session = elements.ElementSession(email=email, password="abx") + assert session.site_url is None + assert session.email is email +