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 .gateway .models import GatewayActionStatus
1213from manage_breast_screening .gateway .tests .factories import GatewayActionFactory
1314
15+ from ..authentication import Authentication
1416from ..dicom_recorder import DicomRecorder
1517from ..models import Study
16- from ..token_validator import TokenValidator
1718
1819os .environ ["NINJA_SKIP_REGISTRY" ] = "yes"
1920
2021client = TestClient (api )
2122
2223
2324@pytest .fixture (autouse = True )
24- def enable_api (monkeypatch ):
25+ def setup (monkeypatch ):
2526 monkeypatch .setenv ("API_ENABLED" , "true" )
27+ monkeypatch .setenv ("API_AUDIENCE" , "test_audience" )
28+ monkeypatch .setenv ("TENANT_ID" , "test_tenant_id" )
2629
2730
2831@pytest .fixture
@@ -36,13 +39,13 @@ def dicom_file(dataset) -> bytes:
3639
3740
3841@pytest .fixture
39- def mock_token_validator ():
40- with patch .object (TokenValidator , "authenticate" , return_value = {"sub" : "testuser" }):
42+ def mock_authentication ():
43+ with patch .object (Authentication , "authenticate" , return_value = {"sub" : "testuser" }):
4144 yield
4245
4346
4447@pytest .mark .django_db
45- def test_upload_success (dataset , dicom_file , mock_token_validator ):
48+ def test_upload_success (dataset , dicom_file , mock_authentication ):
4649 with patch .object (DicomRecorder , "appointment_in_progress" , return_value = True ):
4750 response = client .put (
4851 "/dicom/abc123" ,
@@ -60,7 +63,7 @@ def test_upload_success(dataset, dicom_file, mock_token_validator):
6063 assert study .source_message_id == "abc123"
6164
6265
63- def test_upload_no_file (mock_token_validator ):
66+ def test_upload_no_file (mock_authentication ):
6467 response = client .put (
6568 "/dicom/abc123" ,
6669 FILES = {"file" : None },
@@ -70,7 +73,7 @@ def test_upload_no_file(mock_token_validator):
7073 assert response .status_code == 422
7174
7275
73- def test_upload_invalid_file (mock_token_validator ):
76+ def test_upload_invalid_file (mock_authentication ):
7477 invalid_file = SimpleUploadedFile (
7578 "invalid.dcm" , b"not a dicom file" , content_type = "application/dicom"
7679 )
@@ -88,7 +91,7 @@ def test_upload_invalid_file(mock_token_validator):
8891 assert response .json ()["detail" ] == "The uploaded file is not a valid DICOM file."
8992
9093
91- def test_upload_file_thats_too_large (mock_token_validator ):
94+ def test_upload_file_thats_too_large (mock_authentication ):
9295 invalid_file = MagicMock (spec = SimpleUploadedFile , size = 101 * 1024 * 1024 )
9396
9497 response = client .put (
@@ -103,7 +106,7 @@ def test_upload_file_thats_too_large(mock_token_validator):
103106 assert response .json ()["detail" ] == "The file cannot be larger than 100MB"
104107
105108
106- def test_upload_missing_uids (dataset , mock_token_validator ):
109+ def test_upload_missing_uids (dataset , mock_authentication ):
107110 del dataset .StudyInstanceUID
108111 del dataset .SeriesInstanceUID
109112 del dataset .SOPInstanceUID
@@ -131,7 +134,7 @@ def test_upload_missing_uids(dataset, mock_token_validator):
131134 )
132135
133136
134- def test_upload_appointment_not_in_progress (dicom_file , mock_token_validator ):
137+ def test_upload_appointment_not_in_progress (dicom_file , mock_authentication ):
135138 with patch .object (DicomRecorder , "appointment_in_progress" , return_value = False ):
136139 response = client .put (
137140 "/dicom/abc123" ,
@@ -143,7 +146,7 @@ def test_upload_appointment_not_in_progress(dicom_file, mock_token_validator):
143146 assert response .json ()["title" ] == "Internal Server Error"
144147
145148
146- def test_upload_when_api_disabled (dicom_file , mock_token_validator , monkeypatch ):
149+ def test_upload_when_api_disabled (dicom_file , mock_authentication , monkeypatch ):
147150 monkeypatch .setenv ("API_ENABLED" , "false" )
148151
149152 response = client .put (
@@ -181,8 +184,28 @@ def test_upload_invalid_auth(dicom_file):
181184 }
182185
183186
187+ def test_upload_bypass_token_validation (dicom_file ):
188+ with patch .object (Authentication , "bypass_authentication" , return_value = True ):
189+ with patch .object (
190+ DicomRecorder ,
191+ "get_or_create_records" ,
192+ return_value = (
193+ MagicMock (study_instance_uid = generate_uid ()),
194+ MagicMock (series_instance_uid = generate_uid ()),
195+ MagicMock (sop_instance_uid = generate_uid (), id = 1 ),
196+ ),
197+ ):
198+ response = client .put (
199+ "/dicom/abc123" ,
200+ FILES = {"file" : dicom_file },
201+ headers = {"Authorization" : "Bearer anytoken" },
202+ )
203+
204+ assert response .status_code == 201
205+
206+
184207@pytest .mark .django_db
185- def test_report_failure (mock_token_validator ):
208+ def test_report_failure (mock_authentication ):
186209 action = GatewayActionFactory ()
187210
188211 response = client .patch (
@@ -201,7 +224,7 @@ def test_report_failure(mock_token_validator):
201224
202225
203226@pytest .mark .django_db
204- def test_report_failure_action_not_found (mock_token_validator ):
227+ def test_report_failure_action_not_found (mock_authentication ):
205228 response = client .patch (
206229 "/dicom/00000000-0000-0000-0000-000000000000/failure" ,
207230 json = {"error" : "Missing PatientID" },
0 commit comments