|
| 1 | +import json |
1 | 2 | from pathlib import Path |
2 | 3 |
|
3 | 4 | import pytest |
|
26 | 27 |
|
27 | 28 | TEST_ASSETS_DIR = Path("tests/integration/assets") |
28 | 29 | MULTI_TEXT_FILE_PATH = TEST_ASSETS_DIR / "multi_text_file.json" |
| 30 | +PUBLIC_KEY_PEM_PATH = TEST_ASSETS_DIR / "public_key.pem" |
29 | 31 |
|
30 | 32 |
|
31 | 33 | @pytest.fixture(scope="module", autouse=True) |
@@ -283,3 +285,69 @@ def test_process_bulk_file(client: TestClient, config: Config, test_model_servic |
283 | 285 | assert annotation["label_name"] == "Loss Of Kidney Function" |
284 | 286 |
|
285 | 287 | verify_results_match_api_info(client, task, res) |
| 288 | + |
| 289 | + |
| 290 | +def test_redact(client: TestClient, config: Config, test_model_service_ip: str): |
| 291 | + response = client.post( |
| 292 | + f"/models/{test_model_service_ip}/redact", |
| 293 | + data="Patient diagnosed with kidney failure", |
| 294 | + headers={"Content-Type": "text/plain"}, |
| 295 | + ) |
| 296 | + response_json = validate_api_response(response, expected_status_code=200, return_json=True) |
| 297 | + |
| 298 | + tm: TaskManager = config.task_manager |
| 299 | + verify_task_submitted_successfully(response_json["uuid"], tm) |
| 300 | + |
| 301 | + task = wait_for_task_completion(response_json["uuid"], tm, expected_status=Status.SUCCEEDED) |
| 302 | + |
| 303 | + key = f"{task.uuid}_payload.txt" |
| 304 | + expected_payload = b"Patient diagnosed with kidney failure" |
| 305 | + verify_task_payload_in_object_store(key, expected_payload, config.task_object_store_manager) |
| 306 | + |
| 307 | + verify_queue_is_empty(config.queue_manager) |
| 308 | + |
| 309 | + res, parsed = download_result_object(task.result, config.results_object_store_manager, "text") |
| 310 | + |
| 311 | + assert parsed == "Patient diagnosed with [Loss Of Kidney Function]" |
| 312 | + |
| 313 | + verify_results_match_api_info(client, task, res) |
| 314 | + |
| 315 | + |
| 316 | +def test_redact_with_encryption(client: TestClient, config: Config, test_model_service_ip: str): |
| 317 | + with open(PUBLIC_KEY_PEM_PATH) as f: |
| 318 | + payload = { |
| 319 | + "text": "Patient diagnosed with kidney failure", |
| 320 | + "public_key_pem": f.read(), |
| 321 | + } |
| 322 | + response = client.post( |
| 323 | + f"/models/{test_model_service_ip}/redact_with_encryption", |
| 324 | + json=payload, |
| 325 | + ) |
| 326 | + response_json = validate_api_response(response, expected_status_code=200, return_json=True) |
| 327 | + |
| 328 | + tm: TaskManager = config.task_manager |
| 329 | + verify_task_submitted_successfully(response_json["uuid"], tm) |
| 330 | + |
| 331 | + task = wait_for_task_completion(response_json["uuid"], tm, expected_status=Status.SUCCEEDED) |
| 332 | + |
| 333 | + key = f"{task.uuid}_payload.json" |
| 334 | + expected_payload = json.dumps(payload).encode() |
| 335 | + verify_task_payload_in_object_store(key, expected_payload, config.task_object_store_manager) |
| 336 | + |
| 337 | + verify_queue_is_empty(config.queue_manager) |
| 338 | + |
| 339 | + res, parsed = download_result_object(task.result, config.results_object_store_manager) |
| 340 | + |
| 341 | + encrypted_label = "[REDACTED_0]" |
| 342 | + assert "redacted_text" in parsed |
| 343 | + assert parsed["redacted_text"] == f"Patient diagnosed with {encrypted_label}" |
| 344 | + assert "encryptions" in parsed |
| 345 | + assert len(parsed["encryptions"]) == 1 |
| 346 | + |
| 347 | + assert "label" in parsed["encryptions"][0] |
| 348 | + assert parsed["encryptions"][0]["label"] == encrypted_label |
| 349 | + assert "encryption" in parsed["encryptions"][0] |
| 350 | + assert isinstance(parsed["encryptions"][0]["encryption"], str) |
| 351 | + assert len(parsed["encryptions"][0]["encryption"]) > 0 |
| 352 | + |
| 353 | + verify_results_match_api_info(client, task, res) |
0 commit comments