Skip to content

Commit 89e3b0c

Browse files
committed
tests: Add cases for /redact* endpoints
Signed-off-by: Phoevos Kalemkeris <[email protected]>
1 parent 999d521 commit 89e3b0c

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3ITkTP8Tm/5FygcwY2EQ7LgVsuCF0OH7psUqvlXnOPNCfX86CobHBiSFjG9o5ZeajPtTXaf1thUodgpJZVZSqpVTXwGKo8r0COMO87IcwYigkZZgG/WmZgoZART+AA0+JvjFGxflJAxSv7puGlf82E+u5Wz2psLBSDO5qrnmaDZTvPh5eX84cocahVVI7X09/kI+sZiKauM69yoy1bdx16YIIeNm0M9qqS3tTrjouQiJfZ8jUKSZ44Na/81LMVw5O46+5GvwD+OsR43kQ0TexMwgtHxQQsiXLWHCDNy2ZzkzukDYRwA3V2lwVjtQN0WjxHg24BTBDBM+v7iQ7cbweQIDAQAB
3+
-----END PUBLIC KEY-----

tests/integration/test_api.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from pathlib import Path
23

34
import pytest
@@ -26,6 +27,7 @@
2627

2728
TEST_ASSETS_DIR = Path("tests/integration/assets")
2829
MULTI_TEXT_FILE_PATH = TEST_ASSETS_DIR / "multi_text_file.json"
30+
PUBLIC_KEY_PEM_PATH = TEST_ASSETS_DIR / "public_key.pem"
2931

3032

3133
@pytest.fixture(scope="module", autouse=True)
@@ -283,3 +285,69 @@ def test_process_bulk_file(client: TestClient, config: Config, test_model_servic
283285
assert annotation["label_name"] == "Loss Of Kidney Function"
284286

285287
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)

tests/integration/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ def download_result_object(
315315
result_json = json.loads(result.decode("utf-8"))
316316
elif format == "jsonl":
317317
result_json = [json.loads(line) for line in result.decode("utf-8").split("\n") if line]
318+
elif format == "text":
319+
result_json = result.decode("utf-8")
318320
else:
319321
pytest.fail(f"Unsupported format: {format}")
320322
except json.JSONDecodeError as e:

0 commit comments

Comments
 (0)