From af780cf33f07ca1797449ed6bff5b0aaee3991e8 Mon Sep 17 00:00:00 2001 From: Jacob Magnusson Date: Tue, 27 Apr 2021 16:56:32 +0200 Subject: [PATCH] Fix failing tests and add basic REST API and Restlet tests --- netsuite/rest_api.py | 8 ++++++-- netsuite/restlet.py | 8 ++++++-- tests/conftest.py | 16 ++++++++++++++++ tests/test_base.py | 29 ----------------------------- tests/test_rest_api.py | 6 ++++++ tests/test_restlet.py | 6 ++++++ tests/test_soap_api.py | 19 +++++++++++++++++++ 7 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 tests/conftest.py delete mode 100644 tests/test_base.py create mode 100644 tests/test_rest_api.py create mode 100644 tests/test_restlet.py create mode 100644 tests/test_soap_api.py diff --git a/netsuite/rest_api.py b/netsuite/rest_api.py index c509dda..b4adbff 100644 --- a/netsuite/rest_api.py +++ b/netsuite/rest_api.py @@ -3,6 +3,7 @@ from .config import Config from .rest_api_base import RestApiBase +from .util import cached_property logger = logging.getLogger(__name__) @@ -18,10 +19,13 @@ def __init__( concurrent_requests: int = 10, ): self._config = config - self._hostname = self._make_hostname() self._default_timeout = default_timeout self._concurrent_requests = concurrent_requests + @cached_property + def hostname(self) -> str: + return self._make_hostname() + async def get(self, subpath: str, **request_kw): return await self._request("GET", subpath, **request_kw) @@ -85,7 +89,7 @@ def _make_hostname(self): return f"{self._config.account_slugified}.suitetalk.api.netsuite.com" def _make_url(self, subpath: str): - return f"https://{self._hostname}/services/rest{subpath}" + return f"https://{self.hostname}/services/rest{subpath}" def _make_default_headers(self): return { diff --git a/netsuite/restlet.py b/netsuite/restlet.py index 97455e4..c55ed1d 100644 --- a/netsuite/restlet.py +++ b/netsuite/restlet.py @@ -2,6 +2,7 @@ from .config import Config from .rest_api_base import RestApiBase +from .util import cached_property logger = logging.getLogger(__name__) @@ -17,10 +18,13 @@ def __init__( concurrent_requests: int = 10, ): self._config = config - self._hostname = self._make_hostname() self._default_timeout = default_timeout self._concurrent_requests = concurrent_requests + @cached_property + def hostname(self) -> str: + return self._make_hostname() + async def get(self, script_id: int, *, deploy: int = 1, **request_kw): subpath = self._make_restlet_params(script_id, deploy) return await self._request("GET", subpath, **request_kw) @@ -44,4 +48,4 @@ def _make_hostname(self): return f"{self._config.account_slugified}.restlets.api.netsuite.com" def _make_url(self, subpath: str) -> str: - return f"https://{self._hostname}/app/site/hosting/restlet.nl{subpath}" + return f"https://{self.hostname}/app/site/hosting/restlet.nl{subpath}" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..959e312 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,16 @@ +import pytest + +from netsuite import Config + + +@pytest.fixture +def dummy_config(): + return Config( + account="123456_SB1", + auth={ + "consumer_key": "abcdefghijklmnopqrstuvwxyz0123456789", + "consumer_secret": "abcdefghijklmnopqrstuvwxyz0123456789", + "token_id": "abcdefghijklmnopqrstuvwxyz0123456789", + "token_secret": "abcdefghijklmnopqrstuvwxyz0123456789", + }, + ) diff --git a/tests/test_base.py b/tests/test_base.py deleted file mode 100644 index 1926a84..0000000 --- a/tests/test_base.py +++ /dev/null @@ -1,29 +0,0 @@ -import pytest - -from netsuite import Config, NetSuiteSoapApi - - -@pytest.fixture -def dummy_config(): - return Config( - account="123456", - auth={ - "consumer_key": "abcdefghijklmnopqrstuvwxyz0123456789", - "consumer_secret": "abcdefghijklmnopqrstuvwxyz0123456789", - "token_id": "abcdefghijklmnopqrstuvwxyz0123456789", - "token_secret": "abcdefghijklmnopqrstuvwxyz0123456789", - }, - ) - - -def test_netsuite_hostname(dummy_config): - soap_api = NetSuiteSoapApi(dummy_config) - assert soap_api.hostname == "123456.suitetalk.api.netsuite.com" - - -def test_netsuite_wsdl_url(dummy_config): - soap_api = NetSuiteSoapApi(dummy_config) - assert ( - soap_api.wsdl_url - == "https://123456.suitetalk.api.netsuite.com/wsdl/v2021_1_0/netsuite.wsdl" - ) diff --git a/tests/test_rest_api.py b/tests/test_rest_api.py new file mode 100644 index 0000000..39f1bd6 --- /dev/null +++ b/tests/test_rest_api.py @@ -0,0 +1,6 @@ +from netsuite import NetSuiteRestApi + + +def test_expected_hostname(dummy_config): + rest_api = NetSuiteRestApi(dummy_config) + assert rest_api.hostname == "123456-sb1.suitetalk.api.netsuite.com" diff --git a/tests/test_restlet.py b/tests/test_restlet.py new file mode 100644 index 0000000..402823f --- /dev/null +++ b/tests/test_restlet.py @@ -0,0 +1,6 @@ +from netsuite import NetSuiteRestlet + + +def test_expected_hostname(dummy_config): + restlet = NetSuiteRestlet(dummy_config) + assert restlet.hostname == "123456-sb1.restlets.api.netsuite.com" diff --git a/tests/test_soap_api.py b/tests/test_soap_api.py new file mode 100644 index 0000000..710aed1 --- /dev/null +++ b/tests/test_soap_api.py @@ -0,0 +1,19 @@ +import pytest + +from netsuite import NetSuiteSoapApi +from netsuite.soap_api.zeep import ZEEP_INSTALLED + +pytestmark = pytest.mark.skipif(not ZEEP_INSTALLED, reason="Requires zeep") + + +def test_netsuite_hostname(dummy_config): + soap_api = NetSuiteSoapApi(dummy_config) + assert soap_api.hostname == "123456-sb1.suitetalk.api.netsuite.com" + + +def test_netsuite_wsdl_url(dummy_config): + soap_api = NetSuiteSoapApi(dummy_config) + assert ( + soap_api.wsdl_url + == "https://123456-sb1.suitetalk.api.netsuite.com/wsdl/v2021_1_0/netsuite.wsdl" + )