added ability to filter process instances by process initiator

This commit is contained in:
jasquat 2023-01-04 16:11:52 -05:00
parent 9d848effa3
commit ac079f8815
6 changed files with 110 additions and 13 deletions

10
bin/get_routes Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
function error_handler() {
>&2 echo "Exited with BAD EXIT CODE '${2}' in ${0} script at line: ${1}."
exit "$2"
}
trap 'error_handler ${LINENO} $?' ERR
set -o errtrace -o errexit -o nounset -o pipefail
grep -E '^ +\/' src/spiffworkflow_backend/api.yml | sort

View File

@ -1414,11 +1414,34 @@ paths:
items:
$ref: "#/components/schemas/Task"
/users/search:
parameters:
- name: username_prefix
in: query
required: true
description: The prefix of the user
schema:
type: string
get:
tags:
- Users
operationId: spiffworkflow_backend.routes.users_controller.user_search
summary: Returns a list of users that the search param
responses:
"200":
description: list of users
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/User"
/user-groups/for-current-user:
get:
tags:
- Process Instances
operationId: spiffworkflow_backend.routes.process_api_blueprint.user_group_list_for_current_user
- User Groups
operationId: spiffworkflow_backend.routes.users_controller.user_group_list_for_current_user
summary: Group identifiers for current logged in user
responses:
"200":

View File

@ -1,6 +1,8 @@
"""User."""
from __future__ import annotations
from dataclasses import dataclass
import jwt
import marshmallow
from flask import current_app
@ -16,15 +18,18 @@ class UserNotFoundError(Exception):
"""UserNotFoundError."""
@dataclass
class UserModel(SpiffworkflowBaseDBModel):
"""UserModel."""
__tablename__ = "user"
__table_args__ = (db.UniqueConstraint("service", "service_id", name="service_key"),)
id = db.Column(db.Integer, primary_key=True)
username = db.Column(
id: int = db.Column(db.Integer, primary_key=True)
username: str = db.Column(
db.String(255), nullable=False, unique=True
) # should always be a unique value
)
service = db.Column(
db.String(255), nullable=False, unique=False
) # not 'openid' -- google, aws

View File

@ -71,14 +71,6 @@ def permissions_check(body: Dict[str, Dict[str, list[str]]]) -> flask.wrappers.R
return make_response(jsonify({"results": response_dict}), 200)
def user_group_list_for_current_user() -> flask.wrappers.Response:
"""User_group_list_for_current_user."""
groups = g.user.groups
# TODO: filter out the default group and have a way to know what is the default group
group_identifiers = [i.identifier for i in groups if i.identifier != "everybody"]
return make_response(jsonify(sorted(group_identifiers)), 200)
def process_list() -> Any:
"""Returns a list of all known processes.

View File

@ -0,0 +1,26 @@
"""users_controller."""
import flask
from spiffworkflow_backend.models.user import UserModel
from flask import make_response
from flask import jsonify
from flask import g
def user_search(username_prefix: str) -> flask.wrappers.Response:
"""User_search."""
found_users = UserModel.query.filter(UserModel.username.like(f"{username_prefix}%")).all() # type: ignore
response_json = {
"users": found_users,
"username_prefix": username_prefix,
}
return make_response(jsonify(response_json), 200)
def user_group_list_for_current_user() -> flask.wrappers.Response:
"""User_group_list_for_current_user."""
groups = g.user.groups
# TODO: filter out the default group and have a way to know what is the default group
group_identifiers = [i.identifier for i in groups if i.identifier != "everybody"]
return make_response(jsonify(sorted(group_identifiers)), 200)

View File

@ -0,0 +1,41 @@
"""test_users_controller."""
from tests.spiffworkflow_backend.helpers.base_test import BaseTest
from spiffworkflow_backend.models.user import UserModel
from flask.testing import FlaskClient
from flask.app import Flask
class TestUsersController(BaseTest):
"""TestUsersController."""
def test_user_search_returns_a_user(
self,
app: Flask,
client: FlaskClient,
with_db_and_bpmn_file_cleanup: None,
with_super_admin_user: UserModel,
) -> None:
"""Test_user_search_returns_a_user."""
self.find_or_create_user(username="aa")
self.find_or_create_user(username="ab")
self.find_or_create_user(username="abc")
self.find_or_create_user(username="ac")
self._assert_search_has_count(client, with_super_admin_user, 'aa', 1)
self._assert_search_has_count(client, with_super_admin_user, 'ab', 2)
self._assert_search_has_count(client, with_super_admin_user, 'ac', 1)
self._assert_search_has_count(client, with_super_admin_user, 'ad', 0)
self._assert_search_has_count(client, with_super_admin_user, 'a', 4)
def _assert_search_has_count(self, client: FlaskClient, with_super_admin_user: UserModel, username_prefix: str, expected_count: int) -> None:
"""_assert_search_has_count."""
response = client.get(
f"/v1.0/users/search?username_prefix={username_prefix}",
headers=self.logged_in_headers(with_super_admin_user),
)
assert response.status_code == 200
assert response.json
assert response.json['users'] is not None
assert response.json['username_prefix'] == username_prefix
assert len(response.json['users']) == expected_count