Skip to content

Commit

Permalink
Merge pull request #4 from open-formulieren/fix/startup-errors
Browse files Browse the repository at this point in the history
Fixed get_form_choices() errors during startup or manage.py when no API has been configured yet
  • Loading branch information
joeribekker authored Nov 30, 2022
2 parents 038125b + 20ef239 commit 8ba95a5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions openformsclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions openformsclient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import requests_mock

from openformsclient.client import Client
from openformsclient.models import Configuration
from openformsclient.utils import get_form_choices

Expand All @@ -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",
Expand Down

0 comments on commit 8ba95a5

Please sign in to comment.