From e916dcf8d2ef9a0878b34a6c19ff2344d43e7e3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 17 Apr 2019 11:57:13 +0200 Subject: [PATCH] api: don't crash /api/forms when there are no formdefs (#32415) --- tests/test_api.py | 4 ++++ wcs/api.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index cfa6456fb..912e7b70f 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1863,6 +1863,10 @@ def test_api_global_listing(pub, local_user): role = Role(name='test') role.store() + # check there's no crash if there are no formdefs + resp = get_app(pub).get(sign_uri('/api/forms/', user=local_user)) + assert len(resp.json['data']) == 0 + FormDef.wipe() formdef = FormDef() formdef.name = 'test' diff --git a/wcs/api.py b/wcs/api.py index dc75e23ea..906701620 100644 --- a/wcs/api.py +++ b/wcs/api.py @@ -217,6 +217,13 @@ class ApiFormsDirectory(Directory): self.check_access() get_request().user = get_user_from_api_query_string() or get_request().user + if FormDef.count() == 0: + # early return, this avoids running a query against a missing SQL view. + get_response().set_content_type('application/json') + return json.dumps({'data': []}, + cls=misc.JSONEncoder, + encoding=get_publisher().site_charset) + from wcs import sql management_directory = ManagementDirectory()