66import pytest
77from django .core .files .uploadedfile import SimpleUploadedFile
88from ninja .testing import TestClient
9+ from pydicom .uid import generate_uid
910
1011from manage_breast_screening .core .api import api
1112from manage_breast_screening .dicom .models import Study
1617)
1718from manage_breast_screening .participants .tests .factories import AppointmentFactory
1819
20+ from ..authentication import Authentication
1921from ..dicom_recorder import DicomRecorder
2022from ..models import Study
21- from ..token_validator import TokenValidator
2223
2324os .environ ["NINJA_SKIP_REGISTRY" ] = "yes"
2425
2526client = TestClient (api )
2627
2728
2829@pytest .fixture (autouse = True )
29- def enable_api (monkeypatch ):
30+ def setup (monkeypatch ):
3031 monkeypatch .setenv ("API_ENABLED" , "true" )
32+ monkeypatch .setenv ("API_AUDIENCE" , "test_audience" )
33+ monkeypatch .setenv ("TENANT_ID" , "test_tenant_id" )
3134
3235
3336@pytest .fixture
@@ -46,27 +49,21 @@ def appointment_stub():
4649 is_in_progress = MagicMock (return_value = True ),
4750 )
4851
52+
4953@pytest .fixture
50- def mock_token_validator ():
51- with patch .object (TokenValidator , "authenticate" , return_value = {"sub" : "testuser" }):
54+ def mock_authentication ():
55+ with patch .object (Authentication , "authenticate" , return_value = {"sub" : "testuser" }):
5256 yield
5357
5458
5559@pytest .mark .django_db
56- def test_upload_success (dataset , dicom_file , monkeypatch ):
57- monkeypatch .setenv ("API_ENABLED" , "true" )
58- monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
59-
60+ def test_upload_success (dataset , dicom_file , mock_authentication , appointment_stub ):
6061 appointment = AppointmentFactory (current_status = AppointmentStatusNames .IN_PROGRESS )
6162
6263 with patch (
6364 "manage_breast_screening.dicom.dicom_recorder.lookup_appointment" ,
6465 return_value = appointment ,
6566 ):
66-
67- @pytest .mark .django_db
68- def test_upload_success (dataset , dicom_file , mock_token_validator ):
69- with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
7067 response = client .put (
7168 f"/dicom/{ appointment .pk } " ,
7269 FILES = {"file" : dicom_file },
@@ -83,7 +80,7 @@ def test_upload_success(dataset, dicom_file, mock_token_validator):
8380 assert study .source_message_id == str (appointment .pk )
8481
8582
86- def test_upload_no_file (mock_token_validator ):
83+ def test_upload_no_file (mock_authentication ):
8784 response = client .put (
8885 "/dicom/abc123" ,
8986 FILES = {"file" : None },
@@ -93,9 +90,7 @@ def test_upload_no_file(mock_token_validator):
9390 assert response .status_code == 422
9491
9592
96- def test_upload_invalid_file (monkeypatch , mock_token_validator ):
97- monkeypatch .setenv ("API_ENABLED" , "true" )
98- monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
93+ def test_upload_invalid_file (mock_authentication , appointment_stub ):
9994 invalid_file = SimpleUploadedFile (
10095 "invalid.dcm" , b"not a dicom file" , content_type = "application/dicom"
10196 )
@@ -116,7 +111,7 @@ def test_upload_invalid_file(monkeypatch, mock_token_validator):
116111 assert response .json ()["detail" ] == "The uploaded file is not a valid DICOM file."
117112
118113
119- def test_upload_file_thats_too_large (mock_token_validator ):
114+ def test_upload_file_thats_too_large (mock_authentication ):
120115 invalid_file = MagicMock (spec = SimpleUploadedFile , size = 101 * 1024 * 1024 )
121116
122117 response = client .put (
@@ -131,9 +126,7 @@ def test_upload_file_thats_too_large(mock_token_validator):
131126 assert response .json ()["detail" ] == "The file cannot be larger than 100MB"
132127
133128
134- def test_upload_missing_uids (dataset , monkeypatch , appointment_stub ):
135- monkeypatch .setenv ("API_ENABLED" , "true" )
136- monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
129+ def test_upload_missing_uids (dataset , mock_authentication , appointment_stub ):
137130 del dataset .StudyInstanceUID
138131 del dataset .SeriesInstanceUID
139132 del dataset .SOPInstanceUID
@@ -164,10 +157,7 @@ def test_upload_missing_uids(dataset, monkeypatch, appointment_stub):
164157 )
165158
166159
167- def test_upload_appointment_not_in_progress (dicom_file , monkeypatch , appointment_stub ):
168- monkeypatch .setenv ("API_ENABLED" , "true" )
169- monkeypatch .setenv ("API_AUTH_TOKEN" , "testtoken" )
170-
160+ def test_upload_appointment_not_in_progress (dicom_file , mock_authentication , appointment_stub ):
171161 appointment_stub .is_in_progress .return_value = False
172162
173163 with patch (
@@ -184,7 +174,7 @@ def test_upload_appointment_not_in_progress(dicom_file, monkeypatch, appointment
184174 assert response .json ()["title" ] == "Internal Server Error"
185175
186176
187- def test_upload_when_api_disabled (dicom_file , mock_token_validator , monkeypatch ):
177+ def test_upload_when_api_disabled (dicom_file , mock_authentication , monkeypatch ):
188178 monkeypatch .setenv ("API_ENABLED" , "false" )
189179
190180 response = client .put (
@@ -222,8 +212,28 @@ def test_upload_invalid_auth(dicom_file):
222212 }
223213
224214
215+ def test_upload_bypass_token_validation (dicom_file ):
216+ with patch .object (Authentication , "bypass_authentication" , return_value = True ):
217+ with patch .object (
218+ DicomRecorder ,
219+ "get_or_create_records" ,
220+ return_value = (
221+ MagicMock (study_instance_uid = generate_uid ()),
222+ MagicMock (series_instance_uid = generate_uid ()),
223+ MagicMock (sop_instance_uid = generate_uid (), id = 1 ),
224+ ),
225+ ):
226+ response = client .put (
227+ "/dicom/abc123" ,
228+ FILES = {"file" : dicom_file },
229+ headers = {"Authorization" : "Bearer anytoken" },
230+ )
231+
232+ assert response .status_code == 201
233+
234+
225235@pytest .mark .django_db
226- def test_report_failure (mock_token_validator ):
236+ def test_report_failure (mock_authentication ):
227237 action = GatewayActionFactory ()
228238
229239 response = client .patch (
@@ -242,7 +252,7 @@ def test_report_failure(mock_token_validator):
242252
243253
244254@pytest .mark .django_db
245- def test_report_failure_action_not_found (mock_token_validator ):
255+ def test_report_failure_action_not_found (mock_authentication ):
246256 response = client .patch (
247257 "/dicom/00000000-0000-0000-0000-000000000000/failure" ,
248258 json = {"error" : "Missing PatientID" },
0 commit comments