some precommit stuff w/ burnettk
This commit is contained in:
parent
579f0902ed
commit
49eefc561e
|
@ -6,10 +6,10 @@ import pytest
|
|||
from flask.app import Flask
|
||||
from flask_bpmn.models.db import db
|
||||
from flask_bpmn.models.db import SpiffworkflowBaseDBModel
|
||||
from spiffworkflow_backend.models.active_task_user import ActiveTaskUserModel
|
||||
from tests.spiffworkflow_backend.helpers.base_test import BaseTest
|
||||
from tests.spiffworkflow_backend.helpers.test_data import load_test_spec
|
||||
|
||||
from spiffworkflow_backend.models.active_task_user import ActiveTaskUserModel
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
|
||||
from spiffworkflow_backend.models.user import UserModel
|
||||
from spiffworkflow_backend.services.process_instance_processor import (
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
from __future__ import with_statement
|
||||
|
||||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@ from spiffworkflow_backend.models.user_group_assignment import (
|
|||
) # noqa: F401
|
||||
|
||||
from spiffworkflow_backend.models.active_task import ActiveTaskModel # noqa: F401
|
||||
from spiffworkflow_backend.models.active_task_user import (ActiveTaskUserModel)
|
||||
from spiffworkflow_backend.models.bpmn_process_id_lookup import (
|
||||
BpmnProcessIdLookup,
|
||||
) # noqa: F401
|
||||
|
|
|
@ -9,18 +9,17 @@ from flask_bpmn.models.db import SpiffworkflowBaseDBModel
|
|||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.orm import RelationshipProperty
|
||||
from spiffworkflow_backend.models.group import GroupModel
|
||||
|
||||
from spiffworkflow_backend.models.principal import PrincipalModel
|
||||
from spiffworkflow_backend.models.group import GroupModel
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
|
||||
from spiffworkflow_backend.models.task import Task
|
||||
from spiffworkflow_backend.models.user import UserModel
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from spiffworkflow_backend.models.active_task_user import (
|
||||
from spiffworkflow_backend.models.active_task_user import ( # noqa: F401
|
||||
ActiveTaskUserModel,
|
||||
) # noqa: F401
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -34,9 +33,7 @@ class ActiveTaskModel(SpiffworkflowBaseDBModel):
|
|||
),
|
||||
)
|
||||
|
||||
actual_owner: RelationshipProperty[UserModel] = relationship(
|
||||
UserModel
|
||||
)
|
||||
actual_owner: RelationshipProperty[UserModel] = relationship(UserModel)
|
||||
id: int = db.Column(db.Integer, primary_key=True)
|
||||
process_instance_id: int = db.Column(
|
||||
ForeignKey(ProcessInstanceModel.id), nullable=False # type: ignore
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
"""Active_task_user."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from flask_bpmn.models.db import db
|
||||
|
@ -11,6 +13,7 @@ from spiffworkflow_backend.models.user import UserModel
|
|||
|
||||
@dataclass
|
||||
class ActiveTaskUserModel(SpiffworkflowBaseDBModel):
|
||||
"""ActiveTaskUserModel."""
|
||||
|
||||
__tablename__ = "active_task_user"
|
||||
|
||||
|
@ -26,6 +29,4 @@ class ActiveTaskUserModel(SpiffworkflowBaseDBModel):
|
|||
active_task_id = db.Column(
|
||||
ForeignKey(ActiveTaskModel.id), nullable=False, index=True # type: ignore
|
||||
)
|
||||
user_id = db.Column(
|
||||
ForeignKey(UserModel.id), nullable=False, index=True
|
||||
)
|
||||
user_id = db.Column(ForeignKey(UserModel.id), nullable=False, index=True)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""User."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import jwt
|
||||
|
|
|
@ -1298,7 +1298,6 @@ def find_active_task_by_id_or_raise(
|
|||
process_instance_id: int, task_id: str, principal_id: PrincipalModel
|
||||
) -> ActiveTaskModel:
|
||||
"""Find_active_task_by_id_or_raise."""
|
||||
|
||||
# TODO: update
|
||||
active_task_assigned_to_me = ActiveTaskModel.query.filter_by(
|
||||
process_instance_id=process_instance_id,
|
||||
|
|
|
@ -70,7 +70,6 @@ from spiffworkflow_backend.models.message_correlation_property import (
|
|||
)
|
||||
from spiffworkflow_backend.models.message_instance import MessageInstanceModel
|
||||
from spiffworkflow_backend.models.message_instance import MessageModel
|
||||
from spiffworkflow_backend.models.principal import PrincipalModel
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceStatus
|
||||
from spiffworkflow_backend.models.process_model import ProcessModelInfo
|
||||
|
@ -112,7 +111,7 @@ class ProcessInstanceProcessorError(Exception):
|
|||
|
||||
|
||||
class NoPotentialOwnersForTaskError(Exception):
|
||||
pass
|
||||
"""NoPotentialOwnersForTaskError."""
|
||||
|
||||
|
||||
class CustomBpmnScriptEngine(PythonScriptEngine): # type: ignore
|
||||
|
@ -518,7 +517,6 @@ class ProcessInstanceProcessor:
|
|||
if self.bpmn_process_instance.is_completed():
|
||||
self.process_instance_model.end_in_seconds = round(time.time())
|
||||
|
||||
|
||||
active_tasks = ActiveTaskModel.query.filter_by(
|
||||
process_instance_id=self.process_instance_model.id
|
||||
).all()
|
||||
|
@ -534,7 +532,7 @@ class ProcessInstanceProcessor:
|
|||
# filter out non-usertasks
|
||||
task_spec = ready_or_waiting_task.task_spec
|
||||
if not self.bpmn_process_instance._is_engine_task(task_spec):
|
||||
user_id = ready_or_waiting_task.data["current_user"]["id"]
|
||||
ready_or_waiting_task.data["current_user"]["id"]
|
||||
# principal = PrincipalModel.query.filter_by(user_id=user_id).first()
|
||||
# if principal is None:
|
||||
# raise (
|
||||
|
@ -545,19 +543,29 @@ class ProcessInstanceProcessor:
|
|||
# )
|
||||
# )
|
||||
# import pdb; pdb.set_trace()
|
||||
task_lane = 'process_initiator'
|
||||
task_lane = "process_initiator"
|
||||
if task_spec.lane is not None:
|
||||
task_lane = task_spec.lane
|
||||
|
||||
potential_owner_ids = []
|
||||
lane_assignment_id = None
|
||||
if re.match(r"(process.?)initiator", task_lane, re.IGNORECASE):
|
||||
potential_owner_ids = [self.process_instance_model.process_initiator_id]
|
||||
potential_owner_ids = [
|
||||
self.process_instance_model.process_initiator_id
|
||||
]
|
||||
else:
|
||||
group_model = GroupModel.query.filter_by(identifier=task_lane).first()
|
||||
group_model = GroupModel.query.filter_by(
|
||||
identifier=task_lane
|
||||
).first()
|
||||
if group_model is None:
|
||||
raise (NoPotentialOwnersForTaskError(f"Could not find a group with name matching lane: {task_lane}"))
|
||||
potential_owner_ids = [i.user_id for i in group_model.user_group_assignments]
|
||||
raise (
|
||||
NoPotentialOwnersForTaskError(
|
||||
f"Could not find a group with name matching lane: {task_lane}"
|
||||
)
|
||||
)
|
||||
potential_owner_ids = [
|
||||
i.user_id for i in group_model.user_group_assignments
|
||||
]
|
||||
lane_assignment_id = group_model.id
|
||||
|
||||
extensions = ready_or_waiting_task.task_spec.extensions
|
||||
|
@ -588,14 +596,15 @@ class ProcessInstanceProcessor:
|
|||
task_title=ready_or_waiting_task.task_spec.description,
|
||||
task_type=ready_or_waiting_task.task_spec.__class__.__name__,
|
||||
task_status=ready_or_waiting_task.get_state_name(),
|
||||
task_data=json.dumps(ready_or_waiting_task.data),
|
||||
lane_assignment_id=lane_assignment_id,
|
||||
)
|
||||
db.session.add(active_task)
|
||||
db.session.commit()
|
||||
|
||||
for potential_owner_id in potential_owner_ids:
|
||||
active_task_user = ActiveTaskUserModel(user_id=potential_owner_id,active_task_id=active_task.id)
|
||||
active_task_user = ActiveTaskUserModel(
|
||||
user_id=potential_owner_id, active_task_id=active_task.id
|
||||
)
|
||||
db.session.add(active_task_user)
|
||||
db.session.commit()
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@ from flask_bpmn.api.api_error import ApiError
|
|||
from flask_bpmn.models.db import db
|
||||
from SpiffWorkflow.task import Task as SpiffTask # type: ignore
|
||||
from SpiffWorkflow.util.deep_merge import DeepMerge
|
||||
from spiffworkflow_backend.models.active_task import ActiveTaskModel # type: ignore
|
||||
|
||||
from spiffworkflow_backend.models.active_task import ActiveTaskModel # type: ignore
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceApi
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceStatus
|
||||
|
@ -28,11 +28,11 @@ from spiffworkflow_backend.services.process_model_service import ProcessModelSer
|
|||
|
||||
|
||||
class ActiveTaskNotFoundError(Exception):
|
||||
pass
|
||||
"""ActiveTaskNotFoundError."""
|
||||
|
||||
|
||||
class UserDoesNotHaveAccessToTaskError(Exception):
|
||||
pass
|
||||
"""UserDoesNotHaveAccessToTaskError."""
|
||||
|
||||
|
||||
class ProcessInstanceService:
|
||||
|
@ -279,12 +279,21 @@ class ProcessInstanceService:
|
|||
Abstracted here because we need to do it multiple times when completing all tasks in
|
||||
a multi-instance task.
|
||||
"""
|
||||
active_task = ActiveTaskModel.query.filter_by(task_name=spiff_task.task_spec.name, process_instance_id=processor.process_instance_model.id).first()
|
||||
active_task = ActiveTaskModel.query.filter_by(
|
||||
task_name=spiff_task.task_spec.name,
|
||||
process_instance_id=processor.process_instance_model.id,
|
||||
).first()
|
||||
if active_task is None:
|
||||
raise ActiveTaskNotFoundError("Could find an active task with task name '{spiff_task.task_spec.name}' for process instance '{processor.process_instance_model.id}'")
|
||||
raise ActiveTaskNotFoundError(
|
||||
f"Could find an active task with task name '{spiff_task.task_spec.name}'"
|
||||
f" for process instance '{processor.process_instance_model.id}'"
|
||||
)
|
||||
|
||||
if user not in active_task.potential_owners:
|
||||
raise UserDoesNotHaveAccessToTaskError("User {user.username} does not have access to update task'{spiff_task.task_spec.name}' for process instance '{processor.process_instance_model.id}'")
|
||||
raise UserDoesNotHaveAccessToTaskError(
|
||||
f"User {user.username} does not have access to update task'{spiff_task.task_spec.name}'"
|
||||
f" for process instance '{processor.process_instance_model.id}'"
|
||||
)
|
||||
|
||||
dot_dct = ProcessInstanceService.create_dot_dict(data)
|
||||
spiff_task.update_data(dot_dct)
|
||||
|
|
|
@ -207,7 +207,10 @@ class BaseTest:
|
|||
# return public_access_token
|
||||
|
||||
def create_process_instance_from_process_model(
|
||||
self, process_model: ProcessModelInfo, status: Optional[str] = "not_started", user: Optional[UserModel] = None
|
||||
self,
|
||||
process_model: ProcessModelInfo,
|
||||
status: Optional[str] = "not_started",
|
||||
user: Optional[UserModel] = None,
|
||||
) -> ProcessInstanceModel:
|
||||
"""Create_process_instance_from_process_model."""
|
||||
if user is None:
|
||||
|
|
|
@ -22,6 +22,7 @@ class TestAuthorizationService(BaseTest):
|
|||
def test_does_not_fail_if_user_not_created(
|
||||
self, app: Flask, with_db_and_bpmn_file_cleanup: None
|
||||
) -> None:
|
||||
"""Test_does_not_fail_if_user_not_created."""
|
||||
AuthorizationService.import_permissions_from_yaml_file()
|
||||
|
||||
def test_can_import_permissions_from_yaml(
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
"""Test_process_instance_processor."""
|
||||
import pytest
|
||||
from flask.app import Flask
|
||||
from spiffworkflow_backend.models.group import GroupModel
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel, ProcessInstanceStatus
|
||||
from spiffworkflow_backend.services.authorization_service import AuthorizationService
|
||||
from spiffworkflow_backend.services.process_instance_service import ProcessInstanceService, UserDoesNotHaveAccessToTaskError
|
||||
from tests.spiffworkflow_backend.helpers.base_test import BaseTest
|
||||
from tests.spiffworkflow_backend.helpers.test_data import load_test_spec
|
||||
|
||||
from spiffworkflow_backend.models.group import GroupModel
|
||||
from spiffworkflow_backend.models.process_instance import ProcessInstanceStatus
|
||||
from spiffworkflow_backend.services.authorization_service import AuthorizationService
|
||||
from spiffworkflow_backend.services.process_instance_processor import (
|
||||
ProcessInstanceProcessor,
|
||||
)
|
||||
from spiffworkflow_backend.services.process_instance_service import (
|
||||
ProcessInstanceService,
|
||||
)
|
||||
from spiffworkflow_backend.services.process_instance_service import (
|
||||
UserDoesNotHaveAccessToTaskError,
|
||||
)
|
||||
|
||||
|
||||
class TestProcessInstanceProcessor(BaseTest):
|
||||
|
@ -46,6 +51,7 @@ class TestProcessInstanceProcessor(BaseTest):
|
|||
app: Flask,
|
||||
with_db_and_bpmn_file_cleanup: None,
|
||||
) -> None:
|
||||
"""Test_sets_permission_correctly_on_active_task."""
|
||||
testuser1 = self.find_or_create_user("testuser1")
|
||||
testuser2 = self.find_or_create_user("testuser2")
|
||||
assert testuser1.principal is not None
|
||||
|
@ -55,8 +61,12 @@ class TestProcessInstanceProcessor(BaseTest):
|
|||
finance_group = GroupModel.query.filter_by(identifier="Finance Team").first()
|
||||
assert finance_group is not None
|
||||
|
||||
process_model = load_test_spec(process_model_id="model_with_lanes", bpmn_file_name="lanes.bpmn")
|
||||
process_instance = self.create_process_instance_from_process_model(process_model=process_model, user=testuser1)
|
||||
process_model = load_test_spec(
|
||||
process_model_id="model_with_lanes", bpmn_file_name="lanes.bpmn"
|
||||
)
|
||||
process_instance = self.create_process_instance_from_process_model(
|
||||
process_model=process_model, user=testuser1
|
||||
)
|
||||
processor = ProcessInstanceProcessor(process_instance)
|
||||
processor.do_engine_steps(save=True)
|
||||
|
||||
|
@ -66,14 +76,14 @@ class TestProcessInstanceProcessor(BaseTest):
|
|||
assert len(active_task.potential_owners) == 1
|
||||
assert active_task.potential_owners[0] == testuser1
|
||||
|
||||
spiff_task = processor.__class__.get_task_by_bpmn_identifier(active_task.task_name, processor.bpmn_process_instance)
|
||||
spiff_task = processor.__class__.get_task_by_bpmn_identifier(
|
||||
active_task.task_name, processor.bpmn_process_instance
|
||||
)
|
||||
with pytest.raises(UserDoesNotHaveAccessToTaskError):
|
||||
ProcessInstanceService.complete_form_task(
|
||||
processor, spiff_task, {}, testuser2
|
||||
)
|
||||
ProcessInstanceService.complete_form_task(
|
||||
processor, spiff_task, {}, testuser1
|
||||
)
|
||||
ProcessInstanceService.complete_form_task(processor, spiff_task, {}, testuser1)
|
||||
|
||||
assert len(process_instance.active_tasks) == 1
|
||||
active_task = process_instance.active_tasks[0]
|
||||
|
@ -81,24 +91,24 @@ class TestProcessInstanceProcessor(BaseTest):
|
|||
assert len(active_task.potential_owners) == 1
|
||||
assert active_task.potential_owners[0] == testuser2
|
||||
|
||||
spiff_task = processor.__class__.get_task_by_bpmn_identifier(active_task.task_name, processor.bpmn_process_instance)
|
||||
spiff_task = processor.__class__.get_task_by_bpmn_identifier(
|
||||
active_task.task_name, processor.bpmn_process_instance
|
||||
)
|
||||
with pytest.raises(UserDoesNotHaveAccessToTaskError):
|
||||
ProcessInstanceService.complete_form_task(
|
||||
processor, spiff_task, {}, testuser1
|
||||
)
|
||||
|
||||
ProcessInstanceService.complete_form_task(
|
||||
processor, spiff_task, {}, testuser2
|
||||
)
|
||||
ProcessInstanceService.complete_form_task(processor, spiff_task, {}, testuser2)
|
||||
assert len(process_instance.active_tasks) == 1
|
||||
active_task = process_instance.active_tasks[0]
|
||||
assert active_task.lane_assignment_id is None
|
||||
assert len(active_task.potential_owners) == 1
|
||||
assert active_task.potential_owners[0] == testuser1
|
||||
|
||||
spiff_task = processor.__class__.get_task_by_bpmn_identifier(active_task.task_name, processor.bpmn_process_instance)
|
||||
ProcessInstanceService.complete_form_task(
|
||||
processor, spiff_task, {}, testuser1
|
||||
spiff_task = processor.__class__.get_task_by_bpmn_identifier(
|
||||
active_task.task_name, processor.bpmn_process_instance
|
||||
)
|
||||
ProcessInstanceService.complete_form_task(processor, spiff_task, {}, testuser1)
|
||||
|
||||
assert process_instance.status == ProcessInstanceStatus.complete.value
|
||||
|
|
Loading…
Reference in New Issue