-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_input_limits.py
More file actions
136 lines (104 loc) · 4.55 KB
/
test_input_limits.py
File metadata and controls
136 lines (104 loc) · 4.55 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Tests for input size limit enforcement (CRITICAL-003).
"""
import pytest
import os
import tempfile
from pathlib import Path
from fastapi.testclient import TestClient
from fastapi import FastAPI
from app.services.document_service import DocumentService
from app.services.rag_service import RAGService
from app.middleware.input_validation import InputValidationMiddleware
def test_document_service_file_size_limit():
"""Test that DocumentService enforces file size limits."""
service = DocumentService()
# Create a temporary file that exceeds the limit
with tempfile.NamedTemporaryFile(delete=False, suffix='.txt') as tmp_file:
# Write content larger than max_file_size
large_content = b'x' * (service.max_file_size + 1)
tmp_file.write(large_content)
tmp_file_path = tmp_file.name
try:
# Should raise ValueError
with pytest.raises(ValueError, match="exceeds maximum allowed size"):
service.extract_text_from_file(tmp_file_path)
finally:
# Cleanup
Path(tmp_file_path).unlink()
def test_document_service_text_length_limit():
"""Test that DocumentService enforces extracted text length limits."""
service = DocumentService()
# Temporarily set a low limit for testing
original_limit = service.max_text_length
service.max_text_length = 1000 # 1KB for testing
try:
# Create a file that will extract to text larger than limit
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as tmp_file:
large_text = 'x' * 2000 # 2KB text
tmp_file.write(large_text)
tmp_file_path = tmp_file.name
try:
# Should raise ValueError after extraction
with pytest.raises(ValueError, match="exceeds maximum allowed length"):
service.extract_text_from_file(tmp_file_path)
finally:
Path(tmp_file_path).unlink()
finally:
# Restore original limit
service.max_text_length = original_limit
def test_rag_service_batch_size_limit():
"""Test that RAGService enforces batch size limits."""
service = RAGService()
# Create a batch larger than max_batch_size
large_batch = ['text'] * (service.max_batch_size + 1)
# Should not raise error, but should split into smaller batches
# Note: This test requires Ollama to be running, so we'll just test the validation logic
assert len(large_batch) > service.max_batch_size
def test_rag_service_chunk_text_length_limit():
"""Test that RAGService enforces text length limits before chunking."""
service = RAGService()
# Temporarily set a low limit for testing
import os
original_limit = os.environ.get("MAX_CHUNK_TEXT_LENGTH", "52428800") # 50MB default
os.environ["MAX_CHUNK_TEXT_LENGTH"] = "1000" # 1KB for testing
try:
# Create text larger than limit
large_text = 'x' * 2000 # 2KB text
# Should raise ValueError
with pytest.raises(ValueError, match="exceeds maximum allowed length"):
service.chunk_text(large_text)
finally:
# Restore original limit
if original_limit:
os.environ["MAX_CHUNK_TEXT_LENGTH"] = original_limit
else:
os.environ.pop("MAX_CHUNK_TEXT_LENGTH", None)
def test_input_validation_middleware():
"""Test that InputValidationMiddleware validates request size."""
app = FastAPI()
@app.post("/test")
async def test_endpoint():
return {"status": "ok"}
# Add middleware with low limit for testing
app.add_middleware(InputValidationMiddleware, max_request_size=1000)
client = TestClient(app)
# Small request should succeed
response = client.post("/test", json={"data": "small"})
assert response.status_code == 200
# Large request should fail (413 Payload Too Large)
# Note: FastAPI TestClient doesn't set Content-Length, so this test is limited
# In production, the middleware will check Content-Length header
def test_configurable_limits():
"""Test that limits are configurable via environment variables."""
# Test DocumentService
os.environ["MAX_FILE_SIZE"] = "5000000" # 5MB
service = DocumentService()
assert service.max_file_size == 5000000
# Test RAGService
os.environ["MAX_EMBEDDING_BATCH_SIZE"] = "500"
service = RAGService()
assert service.max_batch_size == 500
# Cleanup
os.environ.pop("MAX_FILE_SIZE", None)
os.environ.pop("MAX_EMBEDDING_BATCH_SIZE", None)