From e8d2396b38663fb986ec894aaa12e2c12a150842 Mon Sep 17 00:00:00 2001 From: jbirddog <100367399+jbirddog@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:56:24 -0400 Subject: [PATCH] Upsearch api (#1259) * Run partial tests, add sqlite var * Add upsearch api * Getting ./bin/pyl to pass * Getting ./bin/pyl to pass * Clean up fixtures * Revert this change * PR feedback - use actual schema for api.yml * Hopefully fix the permissions --- Makefile | 13 +++++--- .../src/spiffworkflow_backend/api.yml | 25 ++++++++++++++ .../routes/upsearch_controller.py | 14 ++++++++ .../services/authorization_service.py | 2 ++ .../services/upsearch_service.py | 4 +-- .../integration/test_upsearch_controller.py | 33 +++++++++++++++++++ .../unit/test_authorization_service.py | 1 + 7 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 spiffworkflow-backend/src/spiffworkflow_backend/routes/upsearch_controller.py create mode 100644 spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_upsearch_controller.py diff --git a/Makefile b/Makefile index d93470502..f96a4cfc3 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,9 @@ IN_FRONTEND ?= $(DOCKER_COMPOSE) run $(FRONTEND_CONTAINER) SPIFFWORKFLOW_BACKEND_ENV ?= local_development +BACKEND_SQLITE_FILE ?= src/instance/db_$(SPIFFWORKFLOW_BACKEND_ENV).sqlite3 +JUST ?= + YML_FILES := -f docker-compose.yml \ -f $(BACKEND_DEV_OVERLAY) \ -f $(FRONTEND_DEV_OVERLAY) \ @@ -67,17 +70,17 @@ be-sh: $(IN_BACKEND) /bin/bash be-sqlite: - @if [ ! -f "$(BACKEND_CONTAINER)/src/instance/db_$(SPIFFWORKFLOW_BACKEND_ENV).sqlite3" ]; then \ - echo "SQLite database file does not exist: $(BACKEND_CONTAINER)/src/instance/db_$(SPIFFWORKFLOW_BACKEND_ENV).sqlite3"; \ + @if [ ! -f "$(BACKEND_CONTAINER)/$(BACKEND_SQLITE_FILE)" ]; then \ + echo "SQLite database file does not exist: $(BACKEND_CONTAINER)/$(BACKEND_SQLITE_FILE)"; \ exit 1; \ fi - $(IN_BACKEND) sqlite3 src/instance/db_$(SPIFFWORKFLOW_BACKEND_ENV).sqlite3 + $(IN_BACKEND) sqlite3 $(BACKEND_SQLITE_FILE) be-tests: be-clear-log-file - $(IN_BACKEND) poetry run pytest + $(IN_BACKEND) poetry run pytest tests/spiffworkflow_backend/$(JUST) be-tests-par: be-clear-log-file - $(IN_BACKEND) poetry run pytest -n auto -x --random-order + $(IN_BACKEND) poetry run pytest -n auto -x --random-order tests/spiffworkflow_backend/$(JUST) fe-lint-fix: $(IN_FRONTEND) npm run lint:fix diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/api.yml b/spiffworkflow-backend/src/spiffworkflow_backend/api.yml index 4668787f9..6af522772 100755 --- a/spiffworkflow-backend/src/spiffworkflow_backend/api.yml +++ b/spiffworkflow-backend/src/spiffworkflow_backend/api.yml @@ -457,6 +457,27 @@ paths: schema: $ref: "#/components/schemas/ProcessGroup" + /upsearch-locations: + parameters: + - name: location + in: query + required: false + description: The location to perform the upsearch + schema: + type: string + get: + operationId: spiffworkflow_backend.routes.upsearch_controller.upsearch_locations + summary: Returns upsearch locations for the given location + tags: + - Upsearch + responses: + "200": + description: Upsearch Locations. + content: + application/json: + schema: + $ref: "#/components/schemas/Locations" + /process-models: parameters: - name: process_group_identifier @@ -3159,6 +3180,10 @@ components: ok: type: boolean example: true + Locations: + properties: + locations: + type: array User: properties: uid: diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/routes/upsearch_controller.py b/spiffworkflow-backend/src/spiffworkflow_backend/routes/upsearch_controller.py new file mode 100644 index 000000000..651217451 --- /dev/null +++ b/spiffworkflow-backend/src/spiffworkflow_backend/routes/upsearch_controller.py @@ -0,0 +1,14 @@ +from typing import Any + +from flask import jsonify +from flask import make_response + +from spiffworkflow_backend.services.upsearch_service import UpsearchService + + +def upsearch_locations( + location: str | None, +) -> Any: + upsearch_locations = UpsearchService.upsearch_locations(location) + + return make_response(jsonify({"locations": upsearch_locations}), 200) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py index b4095d2ba..cbc5edd95 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py @@ -542,6 +542,8 @@ class AuthorizationService: permissions_to_assign.append(PermissionToAssign(permission="read", target_uri="/script-assist/enabled")) permissions_to_assign.append(PermissionToAssign(permission="create", target_uri="/script-assist/process-message")) + permissions_to_assign.append(PermissionToAssign(permission="read", target_uri="/upsearch-locations")) + for permission in ["create", "read", "update", "delete"]: permissions_to_assign.append(PermissionToAssign(permission=permission, target_uri="/process-instances/reports/*")) permissions_to_assign.append(PermissionToAssign(permission=permission, target_uri="/public/*")) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/upsearch_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/upsearch_service.py index 2539230ed..edff568ed 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/upsearch_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/upsearch_service.py @@ -3,8 +3,8 @@ import os class UpsearchService: @classmethod - def upsearch_locations(cls, process_model_identifier: str) -> list[str]: - location = process_model_identifier + def upsearch_locations(cls, process_model_identifier: str | None) -> list[str]: + location = process_model_identifier or "" locations = [] while location != "": diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_upsearch_controller.py b/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_upsearch_controller.py new file mode 100644 index 000000000..27e2c7299 --- /dev/null +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_upsearch_controller.py @@ -0,0 +1,33 @@ +import pytest +from flask.app import Flask +from flask.testing import FlaskClient +from spiffworkflow_backend.models.user import UserModel + +from tests.spiffworkflow_backend.helpers.base_test import BaseTest + + +class TestUpsearchControllerController(BaseTest): + @pytest.mark.parametrize( + "location,expected", + [ + ("a/b/c/d", ["a/b/c/d", "a/b/c", "a/b", "a", ""]), + ("a", ["a", ""]), + ("", [""]), + ], + ) + def test_return_upsearch_locations_for_path( + self, + app: Flask, + client: FlaskClient, + with_db_and_bpmn_file_cleanup: None, + with_super_admin_user: UserModel, + location: str, + expected: list[str], + ) -> None: + response = client.get( + f"/v1.0/upsearch-locations?location={location}", headers=self.logged_in_headers(with_super_admin_user) + ) + + assert response.status_code == 200 + assert "locations" in response.json + assert response.json["locations"] == expected diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_authorization_service.py b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_authorization_service.py index 38b850f24..0b68eb961 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_authorization_service.py +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_authorization_service.py @@ -474,6 +474,7 @@ class TestAuthorizationService(BaseTest): ("/user-groups/for-current-user", "read"), ("/users/exists/by-username", "create"), ("/users/search", "read"), + ("/upsearch-locations", "read"), ] )