""" Security regression tests """ import pytest from unittest.mock import patch, MagicMock from fastapi.testclient import TestClient class TestInputValidation: """Test input validation and sanitization""" def test_sql_injection_prevention(self, client: TestClient): """Test SQL injection prevention in merchant search""" malicious_input = "'; DROP TABLE merchants; --" response = client.get("/api/v1/merchants/search", params={ "category": malicious_input, "latitude": 40.7128, "longitude": -74.0060 }) # Should either sanitize input or return 400 assert response.status_code in [200, 400] if response.status_code == 200: # If processed, should not contain malicious SQL data = response.json() assert isinstance(data, list) def test_xss_prevention_in_nlp_query(self, client: TestClient): """Test XSS prevention in NLP query processing""" xss_payload = "find salon" with patch('app.services.advanced_nlp.advanced_nlp_pipeline') as mock_nlp: mock_nlp.process_query.return_value = { "query": "find salon", # Should be sanitized "primary_intent": {"intent": "SEARCH_SERVICE", "confidence": 0.8}, "entities": {}, "similar_services": [], "search_parameters": {}, "processing_time": 0.1 } response = client.post("/api/v1/nlp/analyze-query", params={ "query": xss_payload }) assert response.status_code == 200 data = response.json() # Script tags should be removed/sanitized assert "script>alert('xss')</script>" ] for html_input in html_inputs: response = client.post("/api/v1/helpers/process-text", json={ "text": html_input, "latitude": 40.7128, "longitude": -74.0060 }) # Should handle HTML input safely assert response.status_code in [200, 400] if response.status_code == 200: # Response should not contain dangerous HTML response_text = str(response.json()) assert "