From 3f77e6fbff8efcfe8e73dd2700c052826f9bd373 Mon Sep 17 00:00:00 2001 From: auslin-aot <99173163+auslin-aot@users.noreply.github.com> Date: Wed, 22 Nov 2023 14:23:08 +0530 Subject: [PATCH] Update insights response (#1759) --- forms-flow-api/src/formsflow_api/constants/__init__.py | 4 ++++ .../formsflow_api/services/external/analytics_api.py | 2 ++ forms-flow-api/tests/unit/api/test_dashboards.py | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/forms-flow-api/src/formsflow_api/constants/__init__.py b/forms-flow-api/src/formsflow_api/constants/__init__.py index 2a721052a0..2904cbef84 100644 --- a/forms-flow-api/src/formsflow_api/constants/__init__.py +++ b/forms-flow-api/src/formsflow_api/constants/__init__.py @@ -35,6 +35,10 @@ class BusinessErrorCode(ErrorCodeMixin, Enum): "Invalid response received from insights", HTTPStatus.BAD_REQUEST, ) + INSIGHTS_NOTFOUND = ( + "Analytics is not enabled for this tenant", + HTTPStatus.BAD_REQUEST, + ) INVALID_BPM_RESPONSE = "Invalid response received from bpm", HTTPStatus.BAD_REQUEST BPM_BASE_URL_NOT_SET = "BPM_API_URL not set environment", HTTPStatus.BAD_REQUEST MISSING_PAGINATION_PARAMETERS = ( diff --git a/forms-flow-api/src/formsflow_api/services/external/analytics_api.py b/forms-flow-api/src/formsflow_api/services/external/analytics_api.py index 0de6257f74..a0bb98a8c6 100644 --- a/forms-flow-api/src/formsflow_api/services/external/analytics_api.py +++ b/forms-flow-api/src/formsflow_api/services/external/analytics_api.py @@ -32,6 +32,8 @@ def get_request(url_path, page_no=None, limit=None, **kwargs): current_app.logger.debug("Response from analytics %s", response.json()) if response.ok: return response.json() + if response.status_code == 404: + raise BusinessException(BusinessErrorCode.INSIGHTS_NOTFOUND) raise BusinessException(BusinessErrorCode.INVALID_INSIGHTS_RESPONSE) except requests.ConnectionError as e: raise BusinessException(ExternalError.INSIGHTS_SERVICE_UNAVAILABLE) from e diff --git a/forms-flow-api/tests/unit/api/test_dashboards.py b/forms-flow-api/tests/unit/api/test_dashboards.py index 93384661d6..7b97b27b4e 100644 --- a/forms-flow-api/tests/unit/api/test_dashboards.py +++ b/forms-flow-api/tests/unit/api/test_dashboards.py @@ -36,3 +36,13 @@ def test_get_dashboard_error_details(app, client, session, jwt): rv = client.get("/dashboards/10000", headers=headers) assert rv.json == {"message": "Dashboard - 10000 not accessible"} + + +def test_get_dashboard_error_details_tenant(app, client, session, jwt): + """Get dashboards for tenant with analytics not created.""" + token = get_token(jwt, tenant_key="test-tenant") + headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"} + + rv = client.get("/dashboards", headers=headers) + assert rv.status_code == 400 + assert rv.json["message"] == "Analytics is not enabled for this tenant"