Skip to content

Commit 403c0b5

Browse files
committed
Ensure patient name is used to process test items
Safeguard against accidentally processing items without a patient name in the C-FIND criteria. This ensures processing test items are correctly scoped.
1 parent 51480a0 commit 403c0b5

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

src/relay_listener.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,14 @@ def process_action(self, payload: dict):
104104
result = CreateWorklistItem(self.storage).call(payload)
105105
patient_name = payload.get("parameters", {}).get("worklist_item", {}).get("participant", {}).get("name")
106106

107-
def _run_emulator():
108-
try:
109-
self.process_with_modality_emulator(patient_name=patient_name)
110-
except Exception:
111-
logger.exception("Modality emulator processing failed")
107+
if not patient_name:
108+
logger.warning("No patient name provided for ModalityEmulator test item processing")
109+
return {
110+
"status": "error",
111+
"message": "No patient name provided for ModalityEmulator test item processing",
112+
}
112113

113-
try:
114-
loop = asyncio.get_running_loop()
115-
except RuntimeError:
116-
# Called outside an event loop (e.g. unit tests)
117-
_run_emulator()
118-
else:
119-
loop.create_task(asyncio.to_thread(_run_emulator))
114+
self.process_with_modality_emulator(patient_name=patient_name)
120115

121116
return result
122117
elif action_name == "worklist.update_status":
@@ -138,7 +133,19 @@ def process_with_modality_emulator(self, patient_name: str | None = None):
138133
ae.add_requested_context(DigitalMammographyXRayImageStorageForPresentation)
139134
ae.add_requested_context(ModalityWorklistInformationFind)
140135

141-
ModalityEmulator(self.storage).process_worklist_items(ae, patient_name=patient_name)
136+
def _run_emulator():
137+
try:
138+
ModalityEmulator(self.storage).process_worklist_items(ae, patient_name=patient_name)
139+
except Exception:
140+
logger.exception("Modality emulator processing failed")
141+
142+
try:
143+
loop = asyncio.get_running_loop()
144+
except RuntimeError:
145+
# Called outside an event loop (e.g. unit tests)
146+
_run_emulator()
147+
else:
148+
loop.create_task(asyncio.to_thread(_run_emulator))
142149

143150

144151
class RelayURI:

tests/test_relay_listener.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ def test_process_create_test_item_action_triggers_modality_emulator(self, storag
143143
)
144144
)
145145

146+
def test_process_create_test_item_action_without_patient_name_returns_error(self, storage_instance, listener_payload):
147+
"""Process create test item action without a patient name returns an error."""
148+
subject = RelayListener(storage_instance)
149+
payload = dict(listener_payload)
150+
payload["action_type"] = "worklist.create_test_item"
151+
del payload["parameters"]["worklist_item"]["participant"]["name"]
152+
153+
with patch.object(subject, "process_with_modality_emulator") as mock_emulator:
154+
response = subject.process_action(payload)
155+
156+
assert response == {"status": "error", "message": "No patient name provided for ModalityEmulator test item processing"}
157+
146158
def test_process_action_missing_keys(self, storage_instance, listener_payload):
147159
"""Process action missing keys."""
148160
subject = RelayListener(storage_instance)

0 commit comments

Comments
 (0)