Files
govoplan-workflow/tests/test_bpmn.py
T

86 lines
2.9 KiB
Python

from __future__ import annotations
import unittest
from govoplan_workflow.backend.bpmn import (
BpmnInspectionError,
inspect_bpmn_xml,
)
BPMN = """<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions
xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
id="Definitions_1"
targetNamespace="https://govoplan.example.test/workflow">
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:startEvent id="Start_1" />
<bpmn:userTask id="Review_1" name="Review request" />
<bpmn:exclusiveGateway id="Decision_1" />
<bpmn:endEvent id="End_1" />
<bpmn:sequenceFlow id="Flow_1" sourceRef="Start_1" targetRef="Review_1" />
<bpmn:sequenceFlow id="Flow_2" sourceRef="Review_1" targetRef="Decision_1" />
<bpmn:sequenceFlow id="Flow_3" sourceRef="Decision_1" targetRef="End_1" />
</bpmn:process>
<bpmn:collaboration id="Collaboration_1">
<bpmn:participant id="Participant_1" processRef="Process_1" />
</bpmn:collaboration>
</bpmn:definitions>
"""
class BpmnInspectionTests(unittest.TestCase):
def test_inventory_classifies_native_and_interchange_elements(self) -> None:
result = inspect_bpmn_xml(BPMN)
self.assertTrue(result.valid_xml)
self.assertEqual(1, result.process_count)
self.assertEqual(1, result.executable_process_count)
self.assertEqual(1, result.collaboration_count)
self.assertEqual(3, result.element_counts["sequenceFlow"])
review = next(
item for item in result.elements if item.element_id == "Review_1"
)
collaboration = next(
item
for item in result.elements
if item.element_id == "Collaboration_1"
)
self.assertEqual("native_execution", review.support_level)
self.assertEqual("interchange_only", collaboration.support_level)
def test_dangling_references_are_reported(self) -> None:
result = inspect_bpmn_xml(
BPMN.replace('targetRef="End_1"', 'targetRef="Missing_1"')
)
self.assertFalse(result.valid_xml)
self.assertTrue(
any(
item.code == "dangling_bpmn_reference"
for item in result.diagnostics
)
)
def test_entities_are_rejected(self) -> None:
unsafe = """<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
id="Definitions_1" targetNamespace="x">&xxe;</bpmn:definitions>"""
with self.assertRaisesRegex(
BpmnInspectionError,
"not safe and well formed",
):
inspect_bpmn_xml(unsafe)
def test_non_bpmn_root_is_rejected(self) -> None:
with self.assertRaisesRegex(
BpmnInspectionError,
"bpmn:definitions",
):
inspect_bpmn_xml("<definitions />")
if __name__ == "__main__":
unittest.main()