From 20ef239b292366c3a51e0e54c1c51b3d7785b14a Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 10 Nov 2022 09:50:20 +0100 Subject: [PATCH] Fixed get_form_choices() errors during startup or manage.py when no API has been configured yet --- openformsclient/client.py | 3 +++ openformsclient/utils.py | 3 +++ tests/test_client.py | 4 ++++ tests/test_utils.py | 6 ++++++ 4 files changed, 16 insertions(+) diff --git a/openformsclient/client.py b/openformsclient/client.py index df99b50..1504278 100644 --- a/openformsclient/client.py +++ b/openformsclient/client.py @@ -27,6 +27,9 @@ def _request(self, method, relative_url, **extra_kwargs): return response + def has_config(self) -> bool: + return bool(self.api_root and self.api_token) + def is_healthy(self) -> Tuple[bool, str]: """ """ try: diff --git a/openformsclient/utils.py b/openformsclient/utils.py index 7b96b7b..efd7f18 100644 --- a/openformsclient/utils.py +++ b/openformsclient/utils.py @@ -10,6 +10,9 @@ def get_form_choices(client=None, use_uuids=False): config = Configuration.get_solo() client = config.client + if not client.has_config(): + return [] + response = client.get_forms() key = "uuid" if use_uuids else "slug" diff --git a/tests/test_client.py b/tests/test_client.py index b1ed268..3956099 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -13,6 +13,10 @@ def setUp(self): self.api_token = "token" self.client = Client(self.api_root, self.api_token) + def test_has_config(self, m): + self.assertTrue(self.client.has_config()) + self.assertFalse(Client("", "").has_config()) + def test_is_healthy(self, m): m.head( f"{self.api_root}forms", diff --git a/tests/test_utils.py b/tests/test_utils.py index 83c3efb..6df90a5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -2,6 +2,7 @@ import requests_mock +from openformsclient.client import Client from openformsclient.models import Configuration from openformsclient.utils import get_form_choices @@ -14,6 +15,11 @@ def setUp(self): api_token="token", ) + def test_get_form_choices_without_config(self, m): + client = Client("", "") + result = get_form_choices(client) + self.assertEqual(result, []) + def test_get_form_choices_without_client(self, m): m.get( f"{self.config.api_root}forms",