-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
133 lines (96 loc) · 3.62 KB
/
Copy pathtest_app.py
File metadata and controls
133 lines (96 loc) · 3.62 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
import io
import pytest
from app import app, format_response, allowed_file, get_mime_type
@pytest.fixture
def client():
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
with app.test_client() as client:
yield client
def _get_csrf_token(client):
"""Get a CSRF token by making a GET request first."""
response = client.get('/')
# Extract token from the hidden input in HTML
html = response.data.decode()
start = html.find('name="csrf_token" value="') + len('name="csrf_token" value="')
end = html.find('"', start)
return html[start:end]
# --- format_response tests ---
def test_format_response_escapes_html():
result = format_response('<script>alert("xss")</script>')
assert '<script>' not in result
assert '<script>' in result
def test_format_response_bold():
result = format_response('**hello**')
assert '<strong>hello</strong>' in result
def test_format_response_list_items():
result = format_response('* item one\n* item two')
assert '<li>item one</li>' in result
assert '<ul>' in result
# --- allowed_file tests ---
def test_allowed_file_valid():
assert allowed_file('photo.jpg') is True
assert allowed_file('photo.png') is True
assert allowed_file('photo.JPEG') is True
assert allowed_file('photo.webp') is True
def test_allowed_file_invalid():
assert allowed_file('script.py') is False
assert allowed_file('doc.pdf') is False
assert allowed_file('noext') is False
# --- get_mime_type tests ---
def test_get_mime_type_jpg():
assert get_mime_type('photo.jpg') == 'image/jpeg'
def test_get_mime_type_png():
assert get_mime_type('photo.png') == 'image/png'
def test_get_mime_type_unknown_fallback():
assert get_mime_type('') == 'image/jpeg'
assert get_mime_type('noext') == 'image/jpeg'
# --- Route tests ---
def test_get_index(client):
response = client.get('/')
assert response.status_code == 200
assert b'NutriLens AI' in response.data
def test_post_without_file_redirects(client):
token = _get_csrf_token(client)
response = client.post('/', data={
'user_query': 'test',
'provider': 'ollama',
'csrf_token': token,
}, follow_redirects=False)
assert response.status_code == 302
def test_post_without_csrf_returns_403(client):
response = client.post('/', data={
'user_query': 'test',
'provider': 'ollama',
})
assert response.status_code == 403
def test_post_invalid_file_type(client):
token = _get_csrf_token(client)
data = {
'user_query': 'test',
'provider': 'ollama',
'csrf_token': token,
'file': (io.BytesIO(b'not an image'), 'malware.exe'),
}
response = client.post('/', data=data, content_type='multipart/form-data', follow_redirects=True)
assert b'Invalid file type' in response.data
def test_post_invalid_provider(client):
token = _get_csrf_token(client)
data = {
'user_query': 'test',
'provider': 'nonexistent_provider',
'csrf_token': token,
'file': (io.BytesIO(b'\x89PNG\r\n'), 'photo.png'),
}
response = client.post('/', data=data, content_type='multipart/form-data', follow_redirects=True)
assert b'Invalid provider' in response.data
def test_post_query_too_long(client):
token = _get_csrf_token(client)
data = {
'user_query': 'x' * 501,
'provider': 'ollama',
'csrf_token': token,
'file': (io.BytesIO(b'\x89PNG\r\n'), 'photo.png'),
}
response = client.post('/', data=data, content_type='multipart/form-data', follow_redirects=True)
assert b'Query too long' in response.data