88from ninja .testing import TestClient
99
1010from manage_breast_screening .core .api import api
11- from manage_breast_screening .dicom .dicom_recorder import DicomRecorder
1211from manage_breast_screening .dicom .models import Study
1312from manage_breast_screening .gateway .models import GatewayActionStatus
1413from manage_breast_screening .gateway .tests .factories import GatewayActionFactory
14+ from manage_breast_screening .participants .models .appointment import (
15+ AppointmentStatusNames ,
16+ )
17+ from manage_breast_screening .participants .tests .factories import AppointmentFactory
1518
1619os .environ ["NINJA_SKIP_REGISTRY" ] = "yes"
1720
@@ -28,14 +31,26 @@ def dicom_file(dataset) -> bytes:
2831 )
2932
3033
34+ @pytest .fixture
35+ def appointment_stub ():
36+ return AppointmentFactory .stub (
37+ is_in_progress = MagicMock (return_value = True ),
38+ )
39+
40+
3141@pytest .mark .django_db
3242def test_upload_success (dataset , dicom_file , monkeypatch ):
3343 monkeypatch .setenv ("API_ENABLED" , "true" )
3444 monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
3545
36- with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
46+ appointment = AppointmentFactory (current_status = AppointmentStatusNames .IN_PROGRESS )
47+
48+ with patch (
49+ "manage_breast_screening.dicom.dicom_recorder.lookup_appointment" ,
50+ return_value = appointment ,
51+ ):
3752 response = client .put (
38- "/dicom/abc123 " ,
53+ f "/dicom/{ appointment . pk } " ,
3954 FILES = {"file" : dicom_file },
4055 headers = {"Authorization" : "Bearer " + os .getenv ("API_AUTH_TOKEN" , "" )},
4156 )
@@ -47,7 +62,7 @@ def test_upload_success(dataset, dicom_file, monkeypatch):
4762 assert json ["series_instance_uid" ] == dataset .SeriesInstanceUID
4863 assert json ["sop_instance_uid" ] == dataset .SOPInstanceUID
4964 assert json ["instance_id" ] == str (study .images ().first ().id )
50- assert study .source_message_id == "abc123"
65+ assert study .source_message_id == str ( appointment . pk )
5166
5267
5368def test_upload_no_file (monkeypatch ):
@@ -63,15 +78,18 @@ def test_upload_no_file(monkeypatch):
6378 assert response .status_code == 422
6479
6580
66- def test_upload_invalid_file (monkeypatch ):
81+ def test_upload_invalid_file (monkeypatch , appointment_stub ):
6782 monkeypatch .setenv ("API_ENABLED" , "true" )
6883 monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
6984
7085 invalid_file = SimpleUploadedFile (
7186 "invalid.dcm" , b"not a dicom file" , content_type = "application/dicom"
7287 )
7388
74- with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
89+ with patch (
90+ "manage_breast_screening.dicom.dicom_recorder.lookup_appointment" ,
91+ return_value = appointment_stub ,
92+ ):
7593 response = client .put (
7694 "/dicom/abc123" ,
7795 FILES = {"file" : invalid_file },
@@ -102,7 +120,7 @@ def test_upload_file_thats_too_large(monkeypatch):
102120 assert response .json ()["detail" ] == "The file cannot be larger than 100MB"
103121
104122
105- def test_upload_missing_uids (dataset , monkeypatch ):
123+ def test_upload_missing_uids (dataset , monkeypatch , appointment_stub ):
106124 monkeypatch .setenv ("API_ENABLED" , "true" )
107125 monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
108126
@@ -117,7 +135,10 @@ def test_upload_missing_uids(dataset, monkeypatch):
117135 "temp.dcm" , buffer .read (), content_type = "application/dicom"
118136 )
119137
120- with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
138+ with patch (
139+ "manage_breast_screening.dicom.dicom_recorder.lookup_appointment" ,
140+ return_value = appointment_stub ,
141+ ):
121142 response = client .put (
122143 "/dicom/abc123" ,
123144 FILES = {"file" : dicom_file },
@@ -133,11 +154,16 @@ def test_upload_missing_uids(dataset, monkeypatch):
133154 )
134155
135156
136- def test_upload_appointment_not_in_progress (dicom_file , monkeypatch ):
157+ def test_upload_appointment_not_in_progress (dicom_file , monkeypatch , appointment_stub ):
137158 monkeypatch .setenv ("API_ENABLED" , "true" )
138159 monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
139160
140- with patch .object (DicomRecorder , "appointment_in_progress" , return_value = False ):
161+ appointment_stub .is_in_progress .return_value = False
162+
163+ with patch (
164+ "manage_breast_screening.dicom.dicom_recorder.lookup_appointment" ,
165+ return_value = appointment_stub ,
166+ ):
141167 response = client .put (
142168 "/dicom/abc123" ,
143169 FILES = {"file" : dicom_file },
0 commit comments