Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 105 additions & 2 deletions tests/unit/app/endpoints/test_vector_stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from collections.abc import Iterator
from typing import Any

import httpx2
import pytest
from fastapi import HTTPException, Request, status
from ogx_client import ApiException, BadRequestError
from openai import APIStatusError
from pytest_mock import MockerFixture

from app.endpoints.vector_stores import (
Expand Down Expand Up @@ -218,13 +220,86 @@ async def test_create_vector_store_success(mocker: MockerFixture) -> None:

request = get_test_request()
auth = get_test_auth()
body = VectorStoreCreateRequest(name="test_store")
body = VectorStoreCreateRequest(
name="test_store",
provider_id="test-provider",
embedding_model="test-embedding",
embedding_dimension=768,
)

response = await create_vector_store(request=request, auth=auth, body=body)
assert response is not None
assert response.id == "vs_123"
assert response.name == "test_store"
assert response.status == "active"
mock_client.vector_stores.create.assert_awaited_once_with(
name="test_store",
extra_body={
"provider_id": "test-provider",
"embedding_model": "test-embedding",
"embedding_dimension": 768,
},
)


@pytest.mark.asyncio
@pytest.mark.parametrize(
("method", "endpoint", "kwargs"),
[
(
"create",
create_vector_store,
{"body": VectorStoreCreateRequest(name="test")},
),
("list", list_vector_stores, {}),
("retrieve", get_vector_store, {"vector_store_id": "vs_123"}),
(
"update",
update_vector_store,
{
"vector_store_id": "vs_123",
"body": VectorStoreUpdateRequest(name="test"),
},
),
("delete", delete_vector_store, {"vector_store_id": "vs_123"}),
],
ids=["create", "list", "retrieve", "update", "delete"],
)
@pytest.mark.parametrize("backend", ["ogx", "openai"])
async def test_vector_store_status_error_maps_to_quota_exceeded(
mocker: MockerFixture,
method: str,
endpoint: Any,
kwargs: dict[str, Any],
backend: str,
) -> None:
"""Translate upstream rate limits into the public quota error."""
mock_authorization_resolvers(mocker)
cfg = AppConfig()
cfg.init_from_dict(get_test_config())
mocker.patch("app.endpoints.vector_stores.configuration", cfg)
client = mocker.AsyncMock()
mocker.patch(
"app.endpoints.vector_stores.AsyncOgxClientHolder.get_client",
return_value=client,
)
error: ApiException | APIStatusError
if backend == "ogx":
error = ApiException(status=429, reason="Rate limit exceeded")
else:
response = httpx2.Response(
429, request=httpx2.Request("GET", "https://example.test/vector_stores")
)
error = APIStatusError("Rate limit exceeded", response=response, body=None)
operation = getattr(client.vector_stores, method)
operation.side_effect = error

with pytest.raises(HTTPException) as exc_info:
await endpoint(request=get_test_request(), auth=get_test_auth(), **kwargs)

assert exc_info.value.status_code == status.HTTP_429_TOO_MANY_REQUESTS
assert exc_info.value.__cause__ is error
operation.assert_awaited_once()


@pytest.mark.asyncio
Expand Down Expand Up @@ -436,7 +511,7 @@ async def test_create_file_success(mocker: MockerFixture) -> None:

# mock_client is a plain AsyncMock, not an AsyncOGXAsLibraryClient, so
# create_file takes the service-client branch: a (filename, fileobj)
# tuple passed straight to client.files.create() for httpx to stream.
# tuple passed straight to client.files.create() for httpx2 to stream.
file_arg = mock_client.files.create.call_args.kwargs["file"]
filename_arg, fileobj_arg = file_arg
assert filename_arg == "test.txt"
Expand Down Expand Up @@ -1165,6 +1240,34 @@ async def test_create_file_content_length_too_large(mocker: MockerFixture) -> No
assert "too large" in str(e.value.detail).lower()


@pytest.mark.asyncio
@pytest.mark.parametrize("reported_size", [None, 1], ids=["missing", "understated"])
async def test_create_file_rejects_oversized_stream(
mocker: MockerFixture, reported_size: int | None
) -> None:
"""Reject the measured size before reading the stream or uploading it."""
mock_authorization_resolvers(mocker)
cfg = AppConfig()
cfg.init_from_dict(get_test_config())
mocker.patch("app.endpoints.vector_stores.configuration", cfg)
mocker.patch("app.endpoints.vector_stores.DEFAULT_MAX_FILE_UPLOAD_SIZE", 4)
upload = mocker.patch(
"app.endpoints.vector_stores.AsyncOgxClientHolder.upload_file"
)
stream = io.BytesIO(b"12345")
read = mocker.spy(stream, "read")
file = mocker.AsyncMock(filename="upload.txt", size=reported_size, file=stream)

with pytest.raises(HTTPException) as exc_info:
await create_file(request=get_test_request(), auth=get_test_auth(), file=file)

assert exc_info.value.status_code == status.HTTP_413_CONTENT_TOO_LARGE
assert stream.tell() == 0
read.assert_not_called()
file.read.assert_not_called()
upload.assert_not_awaited()


@pytest.mark.asyncio
async def test_add_file_to_vector_store_connection_error(
mocker: MockerFixture,
Expand Down
114 changes: 114 additions & 0 deletions tests/unit/pii_redaction_engine/test_anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pytest
from presidio_analyzer import RecognizerResult
from pytest_mock import MockerFixture

from pii_redaction_engine.anonymizer import (
DEFAULT_ENTITIES,
Expand Down Expand Up @@ -133,3 +134,116 @@ def test_is_allowed_regex_match() -> None:
checker = _allow_checker(["rh.*"], match="regex")
assert checker._is_allowed("RHEL") is True
assert checker._is_allowed("Fedora") is False


@pytest.fixture(name="anonymizer")
def anonymizer_fixture(mocker: MockerFixture) -> PIIAnonymizer:
"""Use the real registry and anonymizer with a model-free analyzer."""
mocker.patch.object(PIIAnonymizer, "_require_installed", return_value="mock_model")
mocker.patch("pii_redaction_engine.anonymizer.NlpEngineProvider")
mocker.patch("pii_redaction_engine.anonymizer.AnalyzerEngine")
return PIIAnonymizer(
RedactionConfig(
entities={"LOCATION", "PERSON", "IP_ADDRESS"},
allow_list=["RHEL"],
deny_list={"INTERNAL_PROJECT": ["project cedar"]},
)
)


def test_anonymize_text_adds_recased_match_and_preserves_allowed_term(
anonymizer: PIIAnonymizer, mocker: MockerFixture
) -> None:
"""Apply new matches to the original text and preserve allow-listed spelling."""
analyze = mocker.patch.object(
anonymizer.analyzer,
"analyze",
side_effect=[
[],
[
RecognizerResult("LOCATION", 6, 17, 0.9),
RecognizerResult("LOCATION", 23, 27, 0.9),
],
],
)

assert anonymizer.anonymize_text("go to KATRINEHOLM with RHEL") == (
"go to <LOCATION> with RHEL"
)
original, recased = analyze.call_args_list
assert original.kwargs["text"] == "go to KATRINEHOLM with RHEL"
assert recased.kwargs["text"] == "go to Katrineholm with Rhel"
assert recased.kwargs["entities"] == ["LOCATION"]


def test_anonymize_text_keeps_first_pass_overlap(
anonymizer: PIIAnonymizer, mocker: MockerFixture
) -> None:
"""A higher-scoring second-pass match cannot override a first-pass span."""
analyze = mocker.patch.object(
anonymizer.analyzer,
"analyze",
side_effect=[
[RecognizerResult("PERSON", 0, 4, 0.6)],
[RecognizerResult("LOCATION", 0, 4, 0.9)],
],
)

assert anonymizer.anonymize_text("JANE arrived") == "<PERSON> arrived"
assert analyze.call_count == 2


def test_anonymize_text_prefers_structural_match(
anonymizer: PIIAnonymizer, mocker: MockerFixture
) -> None:
"""Use the structural replacement even when the NER guess scores higher."""
mocker.patch.object(
anonymizer.analyzer,
"analyze",
return_value=[
RecognizerResult("IP_ADDRESS", 3, 12, 0.6),
RecognizerResult("PERSON", 3, 12, 0.9),
],
)

assert anonymizer.anonymize_text("at 192.0.2.1 now") == "at <IP_ADDRESS> now"


@pytest.mark.parametrize(
("text", "case_normalization", "entities"),
[
("visit ROME", False, ["LOCATION"]),
("visit ROME", True, ["PERSON"]),
("visit Rome", True, ["LOCATION"]),
],
ids=["disabled", "no-eligible-entities", "unchanged-text"],
)
def test_anonymize_text_skips_unneeded_second_pass(
anonymizer: PIIAnonymizer,
mocker: MockerFixture,
text: str,
case_normalization: bool,
entities: list[str],
) -> None:
"""Call the analyzer once when case normalization cannot add matches."""
anonymizer.case_normalization = case_normalization
anonymizer.entities = entities
analyze = mocker.patch.object(anonymizer.analyzer, "analyze", return_value=[])

assert anonymizer.anonymize_text(text) == text
analyze.assert_called_once()


def test_anonymize_text_registers_and_requests_deny_list_entity(
anonymizer: PIIAnonymizer, mocker: MockerFixture
) -> None:
"""Run the configured deny-list recognizer and redact its match."""
text = "open project cedar"
recognizer = anonymizer.registry.get_recognizers(
language="en", entities=["INTERNAL_PROJECT"]
)[0]
matches = recognizer.analyze(text, entities=["INTERNAL_PROJECT"])
analyze = mocker.patch.object(anonymizer.analyzer, "analyze", return_value=matches)

assert anonymizer.anonymize_text(text) == "open <INTERNAL_PROJECT>"
assert "INTERNAL_PROJECT" in analyze.call_args.kwargs["entities"]
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
IMAGE_URL = f"data:image/png;base64,{IMAGE_B64}"
IMAGE = ImageUrl(url=IMAGE_URL, media_type="image/png")
IMAGE_PART = {"image_url": IMAGE_URL, "type": "input_image", "detail": "auto"}
IMAGE_PROMPT = ["new question", IMAGE]
IMAGE_PROMPT: list[str | ImageUrl] = ["new question", IMAGE]
IMAGE_MESSAGES: list[ModelMessage] = [
ModelRequest(parts=[UserPromptPart(content=IMAGE_PROMPT)])
]
Expand Down Expand Up @@ -139,12 +139,16 @@ async def test_image_prompt_adds_the_image_to_the_new_query(
"role": "user",
"content": [{"text": "new question", "type": "input_text"}, IMAGE_PART],
}
extra_body = settings["extra_body"]
assert isinstance(extra_body, dict)
assert result == {
**settings,
"extra_body": {**settings["extra_body"], "input": [*HISTORY, new_query]},
"extra_body": {**extra_body, "input": [*HISTORY, new_query]},
}
# the settings passed in are not modified
assert settings["extra_body"]["input"][-1]["content"] == "new question"
original_input = extra_body["input"]
assert isinstance(original_input, list)
assert original_input[-1]["content"] == "new question"


@pytest.mark.asyncio
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/utils/test_conversation_compaction_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ async def test_summarizer_gets_redacted_user_turns(mocker: MockerFixture) -> Non
buffer_turns=2,
)

summarized = summarize.await_args.args[2]
await_args = summarize.await_args
assert await_args is not None
summarized = await_args.args[2]
assert [item.content for item in summarized] == [
"ask [EMAIL]",
"bob@example.org is out",
]
assert summarize.await_args.kwargs["summarized_through_turn"] == 4
assert await_args.kwargs["summarized_through_turn"] == 4
Loading