|
| 1 | +from qwed_sdk.guards.sovereignty_guard import SovereigntyGuard |
| 2 | + |
| 3 | +def test_sovereignty_guard_allows_safe_data_to_external(): |
| 4 | + guard = SovereigntyGuard() |
| 5 | + result = guard.verify_routing(prompt="What is the capital of France?", target_provider="openai") |
| 6 | + |
| 7 | + assert result["verified"] is True |
| 8 | + assert result["risk"] is None |
| 9 | + assert "message" in result |
| 10 | + |
| 11 | +def test_sovereignty_guard_blocks_sensitive_data_to_external(): |
| 12 | + guard = SovereigntyGuard() |
| 13 | + prompt = "Here is the CONFIDENTIAL contract for our new client." |
| 14 | + result = guard.verify_routing(prompt=prompt, target_provider="openai") |
| 15 | + |
| 16 | + assert result["verified"] is False |
| 17 | + assert result["risk"] == "DATA_SOVEREIGNTY_VIOLATION" |
| 18 | + assert "message" in result |
| 19 | + |
| 20 | +def test_sovereignty_guard_blocks_ssn_to_external(): |
| 21 | + guard = SovereigntyGuard() |
| 22 | + prompt = "My SSN is 123-45-6789. Please process my application." |
| 23 | + result = guard.verify_routing(prompt=prompt, target_provider="anthropic") |
| 24 | + |
| 25 | + assert result["verified"] is False |
| 26 | + assert result["risk"] == "DATA_SOVEREIGNTY_VIOLATION" |
| 27 | + assert "message" in result |
| 28 | + |
| 29 | +def test_sovereignty_guard_allows_sensitive_data_to_local(): |
| 30 | + guard = SovereigntyGuard() |
| 31 | + prompt = "Here is the CONFIDENTIAL contract. SSN: 123-45-6789." |
| 32 | + # Ollama is in the default local_providers list |
| 33 | + result = guard.verify_routing(prompt=prompt, target_provider="ollama") |
| 34 | + |
| 35 | + assert result["verified"] is True |
| 36 | + assert result["risk"] is None |
| 37 | + assert "message" in result |
| 38 | + |
| 39 | +def test_sovereignty_guard_blocks_space_separated_ssn_to_external(): |
| 40 | + guard = SovereigntyGuard() |
| 41 | + prompt = "My SSN is 123 45 6789. Please process my application." |
| 42 | + result = guard.verify_routing(prompt=prompt, target_provider="anthropic") |
| 43 | + |
| 44 | + assert result["verified"] is False |
| 45 | + assert result["risk"] == "DATA_SOVEREIGNTY_VIOLATION" |
| 46 | + assert "message" in result |
| 47 | + |
| 48 | +def test_sovereignty_guard_blocks_contiguous_ssn_to_external(): |
| 49 | + guard = SovereigntyGuard() |
| 50 | + prompt = "SSN: 123456789" |
| 51 | + result = guard.verify_routing(prompt=prompt, target_provider="openai") |
| 52 | + |
| 53 | + assert result["verified"] is False |
| 54 | + assert result["risk"] == "DATA_SOVEREIGNTY_VIOLATION" |
| 55 | + assert "message" in result |
| 56 | + |
| 57 | +def test_sovereignty_guard_allows_vllm_local(): |
| 58 | + guard = SovereigntyGuard() |
| 59 | + prompt = "Here is the CONFIDENTIAL contract. SSN: 123-45-6789." |
| 60 | + result = guard.verify_routing(prompt=prompt, target_provider="vllm_local") |
| 61 | + |
| 62 | + assert result["verified"] is True |
| 63 | + assert result["risk"] is None |
| 64 | + assert "message" in result |
| 65 | + |
| 66 | +def test_sovereignty_guard_case_insensitive_provider(): |
| 67 | + guard = SovereigntyGuard() |
| 68 | + prompt = "Here is the CONFIDENTIAL contract. SSN: 123-45-6789." |
| 69 | + result = guard.verify_routing(prompt=prompt, target_provider="OLLAMA") |
| 70 | + |
| 71 | + assert result["verified"] is True |
| 72 | + assert result["risk"] is None |
| 73 | + assert "message" in result |
| 74 | + |
| 75 | +def test_sovereignty_guard_custom_local_provider(): |
| 76 | + guard = SovereigntyGuard(required_local_providers=["my_local_llm"]) |
| 77 | + prompt = "Here is the CONFIDENTIAL contract. SSN: 123-45-6789." |
| 78 | + |
| 79 | + # Allowed local provider |
| 80 | + result_local = guard.verify_routing(prompt=prompt, target_provider="my_local_llm") |
| 81 | + assert result_local["verified"] is True |
| 82 | + assert "message" in result_local |
| 83 | + |
| 84 | + # Default 'ollama' is now external since required_local_providers overrode it |
| 85 | + result_external = guard.verify_routing(prompt=prompt, target_provider="ollama") |
| 86 | + assert result_external["verified"] is False |
| 87 | + assert result_external["risk"] == "DATA_SOVEREIGNTY_VIOLATION" |
| 88 | + assert "message" in result_external |
| 89 | + |
| 90 | +import pytest |
| 91 | + |
| 92 | +def test_sovereignty_guard_raises_on_empty_prompt(): |
| 93 | + guard = SovereigntyGuard() |
| 94 | + with pytest.raises(ValueError, match="prompt must be a non-empty string"): |
| 95 | + guard.verify_routing(prompt="", target_provider="ollama") |
| 96 | + |
| 97 | + with pytest.raises(ValueError, match="prompt must be a non-empty string"): |
| 98 | + guard.verify_routing(prompt=None, target_provider="ollama") |
| 99 | + |
| 100 | +def test_sovereignty_guard_raises_on_empty_provider(): |
| 101 | + guard = SovereigntyGuard() |
| 102 | + with pytest.raises(ValueError, match="target_provider must be a non-empty string"): |
| 103 | + guard.verify_routing(prompt="Valid prompt", target_provider="") |
| 104 | + |
| 105 | + with pytest.raises(ValueError, match="target_provider must be a non-empty string"): |
| 106 | + guard.verify_routing(prompt="Valid prompt", target_provider=None) |
0 commit comments