File size: 2,279 Bytes
e561e67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa723a
 
 
 
 
 
 
 
 
 
 
 
 
e561e67
 
 
 
7fa723a
e561e67
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Tests for RAG upload sanitisation (regex path; no live OpenAI)."""

from __future__ import annotations

import pytest

from app.services.document_sanitiser import (
    regex_sanitise_text,
    sanitise_text_for_rag_sync,
    should_sanitise_for_rag,
)


def test_should_skip_kb_tenant(monkeypatch: pytest.MonkeyPatch) -> None:
    from app.config import settings

    monkeypatch.setattr(settings, "enable_rag_upload_sanitisation", True)
    monkeypatch.setattr(settings, "rag_sanitisation_skip_kb_tenant", True)
    monkeypatch.setattr(settings, "knowledge_base_tenant_id", "__rics_kb__")
    assert should_sanitise_for_rag("tenant-a") is True
    assert should_sanitise_for_rag("__rics_kb__") is False


def test_regex_removes_postcode_and_email() -> None:
    raw = (
        "The roof covering is slate. Client: jane.doe@example.com. "
        "Property at 12 High Street, London SW1A 1AA. Price £450,000."
    )
    out = regex_sanitise_text(raw)
    assert "jane.doe@example.com" not in out
    assert "SW1A" not in out
    assert "450,000" not in out
    assert "slate" in out.lower()


def test_sanitise_sync_regex_only_without_llm_flag(monkeypatch: pytest.MonkeyPatch) -> None:
    from app.config import settings

    monkeypatch.setattr(settings, "enable_rag_upload_sanitisation", True)
    monkeypatch.setattr(settings, "rag_sanitisation_use_llm", False)
    monkeypatch.setattr(settings, "openai_api_key", "sk-test")
    monkeypatch.setattr(settings, "rag_sanitisation_fail_closed", False)
    text = "Defect noted at 10 Church Road, Bristol BS1 4ST."
    out = sanitise_text_for_rag_sync(text, tenant_id="firm-1")
    assert "BS1" not in out
    assert "defect" in out.lower()


def test_sanitise_sync_without_openai_key(monkeypatch: pytest.MonkeyPatch) -> None:
    from app.config import settings

    monkeypatch.setattr(settings, "enable_rag_upload_sanitisation", True)
    monkeypatch.setattr(settings, "rag_sanitisation_use_llm", False)
    monkeypatch.setattr(settings, "openai_api_key", "")
    monkeypatch.setattr(settings, "rag_sanitisation_fail_closed", False)
    text = "Defect noted at 10 Church Road, Bristol BS1 4ST."
    out = sanitise_text_for_rag_sync(text, tenant_id="firm-1")
    assert "BS1" not in out
    assert "defect" in out.lower()