Spaces:
Running
Running
| import pytest | |
| from app.rag.security import ( | |
| OutputParserError, | |
| UnsafePromptError, | |
| classify_user_input, | |
| parse_agent_output, | |
| validate_user_input, | |
| ) | |
| def test_prompt_injection_classifier_blocks_adversarial_phrases(query): | |
| classification = classify_user_input(query) | |
| assert classification.label == "prompt_injection" | |
| assert classification.is_safe is False | |
| with pytest.raises(UnsafePromptError): | |
| validate_user_input(query) | |
| def test_prompt_injection_classifier_allows_normal_document_question(): | |
| classification = classify_user_input("What does the document say about revenue growth?") | |
| assert classification.label == "safe" | |
| assert classification.is_safe is True | |
| def test_parse_agent_output_accepts_strict_answer_json(): | |
| assert parse_agent_output('{"answer":"Revenue increased by 12%."}') == "Revenue increased by 12%." | |
| assert parse_agent_output('Final Answer: {"answer":"Use the cited evidence."}') == "Use the cited evidence." | |
| def test_parse_agent_output_rejects_malformed_or_loose_output(raw_output): | |
| with pytest.raises(OutputParserError): | |
| parse_agent_output(raw_output) | |