11import io
22import os
3- from unittest .mock import MagicMock
3+ from unittest .mock import MagicMock , patch
44
55import pydicom
66import pytest
77from django .core .files .uploadedfile import SimpleUploadedFile
88from ninja .testing import TestClient
99
1010from manage_breast_screening .core .api import api
11+ from manage_breast_screening .dicom .dicom_recorder import DicomRecorder
1112from manage_breast_screening .dicom .models import Study
1213
1314os .environ ["NINJA_SKIP_REGISTRY" ] = "yes"
@@ -30,20 +31,21 @@ def test_upload_success(dataset, dicom_file, monkeypatch):
3031 monkeypatch .setenv ("API_ENABLED" , "true" )
3132 monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
3233
33- response = client .put (
34- "/dicom/abc123" ,
35- FILES = {"file" : dicom_file },
36- headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
37- )
34+ with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
35+ response = client .put (
36+ "/dicom/abc123" ,
37+ FILES = {"file" : dicom_file },
38+ headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
39+ )
3840
39- assert response .status_code == 201
40- assert response .json () == {
41- "study_instance_uid" : dataset .StudyInstanceUID ,
42- "series_instance_uid" : dataset .SeriesInstanceUID ,
43- "sop_instance_uid" : dataset .SOPInstanceUID ,
44- "instance_id" : 1 ,
45- }
46- assert Study .objects .last ().source_message_id == "abc123"
41+ assert response .status_code == 201
42+ assert response .json () == {
43+ "study_instance_uid" : dataset .StudyInstanceUID ,
44+ "series_instance_uid" : dataset .SeriesInstanceUID ,
45+ "sop_instance_uid" : dataset .SOPInstanceUID ,
46+ "instance_id" : 1 ,
47+ }
48+ assert Study .objects .last ().source_message_id == "abc123"
4749
4850
4951def test_upload_no_file (monkeypatch ):
@@ -67,11 +69,12 @@ def test_upload_invalid_file(monkeypatch):
6769 "invalid.dcm" , b"not a dicom file" , content_type = "application/dicom"
6870 )
6971
70- response = client .put (
71- "/dicom/abc123" ,
72- FILES = {"file" : invalid_file },
73- headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
74- )
72+ with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
73+ response = client .put (
74+ "/dicom/abc123" ,
75+ FILES = {"file" : invalid_file },
76+ headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
77+ )
7578
7679 assert response .status_code == 400
7780 assert response .json ()["title" ] == "Invalid DICOM file"
@@ -112,11 +115,12 @@ def test_upload_missing_uids(dataset, monkeypatch):
112115 "temp.dcm" , buffer .read (), content_type = "application/dicom"
113116 )
114117
115- response = client .put (
116- "/dicom/abc123" ,
117- FILES = {"file" : dicom_file },
118- headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
119- )
118+ with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
119+ response = client .put (
120+ "/dicom/abc123" ,
121+ FILES = {"file" : dicom_file },
122+ headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
123+ )
120124
121125 assert response .status_code == 400
122126 assert response .json ()["title" ] == "Missing DICOM attributes"
@@ -127,6 +131,21 @@ def test_upload_missing_uids(dataset, monkeypatch):
127131 )
128132
129133
134+ def test_upload_appointment_not_in_progress (dicom_file , monkeypatch ):
135+ monkeypatch .setenv ("API_ENABLED" , "true" )
136+ monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
137+
138+ with patch .object (DicomRecorder , "appointment_in_progress" , return_value = False ):
139+ response = client .put (
140+ "/dicom/abc123" ,
141+ FILES = {"file" : dicom_file },
142+ headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
143+ )
144+
145+ assert response .status_code == 500
146+ assert response .json ()["title" ] == "Internal Server Error"
147+
148+
130149@pytest .mark .django_db
131150def test_upload_when_api_disabled (dicom_file , monkeypatch ):
132151 monkeypatch .setenv ("API_ENABLED" , "false" )
0 commit comments