-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_type_test.py
More file actions
executable file
·192 lines (158 loc) · 6.79 KB
/
content_type_test.py
File metadata and controls
executable file
·192 lines (158 loc) · 6.79 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
import requests
import json
import os
import sys
from dotenv import load_dotenv
import io
import tempfile
import numpy as np
import time
# Load environment variables from frontend/.env
load_dotenv('/app/frontend/.env')
# Get the backend URL from environment variables
BACKEND_URL = os.environ.get('REACT_APP_BACKEND_URL')
if not BACKEND_URL:
print("Error: REACT_APP_BACKEND_URL not found in environment variables")
sys.exit(1)
# Ensure the URL ends with /api
API_URL = f"{BACKEND_URL}/api"
print(f"Using API URL: {API_URL}")
# Load JWT secret from backend/.env for testing
load_dotenv('/app/backend/.env')
JWT_SECRET = os.environ.get('JWT_SECRET')
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
if not JWT_SECRET:
print("Warning: JWT_SECRET not found in environment variables. Some tests may fail.")
if not OPENAI_API_KEY or OPENAI_API_KEY == "your_openai_api_key_here":
print("Warning: OPENAI_API_KEY not set or using default value. Transcription will likely fail.")
else:
print(f"Using OPENAI_API_KEY: {OPENAI_API_KEY[:4]}...{OPENAI_API_KEY[-4:]}")
def test_login():
"""Login with admin credentials to get auth token"""
login_data = {
"email": "dino@cytonic.com",
"password": "Observerinho8"
}
url = f"{API_URL}/auth/login"
print(f"Logging in with admin credentials: {url}")
try:
response = requests.post(url, json=login_data)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
response_data = response.json()
auth_token = response_data.get("access_token")
user_data = response_data.get("user", {})
user_id = user_data.get("id")
print(f"Login successful. User ID: {user_id}")
print(f"JWT Token: {auth_token}")
return auth_token
else:
print(f"Login failed: {response.text}")
# Try test login as fallback
print("Trying test login endpoint...")
test_url = f"{API_URL}/auth/test-login"
test_response = requests.post(test_url)
if test_response.status_code == 200:
test_data = test_response.json()
test_token = test_data.get("access_token")
test_user = test_data.get("user", {})
test_id = test_user.get("id")
print(f"Test login successful. User ID: {test_id}")
print(f"JWT Token: {test_token}")
return test_token
else:
print(f"Test login failed: {test_response.text}")
return None
except Exception as e:
print(f"Error during login: {e}")
return None
def test_content_type_validation(auth_token):
"""Test the content-type validation in the transcribe-scenario endpoint"""
if not auth_token:
print("Cannot test content-type validation without authentication")
return False
# Create a simple text file
with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as temp_file:
temp_file.write(b"This is a test file")
temp_file_path = temp_file.name
# Test with different content-types
content_types = [
('text/plain', 'test.txt'),
('application/json', 'test.json'),
('audio/wav', 'test.txt'), # Incorrect content-type for a text file
('audio/mpeg', 'test.mp3'), # Incorrect content-type for a text file
('video/mp4', 'test.mp4'), # Incorrect content-type for a text file
]
results = {}
try:
for content_type, filename in content_types:
print(f"\nTesting with Content-Type: {content_type}, Filename: {filename}")
# Prepare the request
url = f"{API_URL}/speech/transcribe-scenario"
headers = {"Authorization": f"Bearer {auth_token}"}
with open(temp_file_path, 'rb') as f:
files = {'audio': (filename, f, content_type)}
# Send the request
response = requests.post(url, headers=headers, files=files)
print(f"Status Code: {response.status_code}")
# Try to parse JSON response
try:
response_data = response.json()
print(f"Response: {json.dumps(response_data, indent=2)}")
except json.JSONDecodeError:
print(f"Response is not JSON: {response.text}")
response_data = {}
# Store the result
results[content_type] = {
'status_code': response.status_code,
'response': response_data
}
finally:
# Clean up the temporary file
if os.path.exists(temp_file_path):
os.unlink(temp_file_path)
# Analyze the results
print("\nContent-Type Validation Results:")
for content_type, result in results.items():
status_code = result['status_code']
response = result['response']
if content_type.startswith(('audio/', 'video/')) and status_code == 400:
print(f"✅ {content_type}: Correctly rejected with 400 status code (file content doesn't match content-type)")
elif not content_type.startswith(('audio/', 'video/')) and status_code == 400:
print(f"✅ {content_type}: Correctly rejected with 400 status code (not audio/video content-type)")
else:
print(f"❌ {content_type}: Unexpected response - Status: {status_code}")
# Check if all tests passed
all_passed = all(result['status_code'] == 400 for result in results.values())
if all_passed:
print("\n✅ Content-type validation is working correctly")
return True
else:
print("\n❌ Content-type validation has issues")
return False
def main():
"""Run all tests"""
print("="*80)
print("VOICE TRANSCRIPTION CONTENT-TYPE VALIDATION TEST")
print("="*80)
# Login to get auth token
auth_token = test_login()
# Run content-type validation test
if auth_token:
content_type_test = test_content_type_validation(auth_token)
# Print summary
print("\n" + "="*80)
print("TEST SUMMARY")
print("="*80)
print(f"Authentication: {'✅ OK' if auth_token else '❌ Failed'}")
print(f"Content-Type Validation: {'✅ Passed' if content_type_test else '❌ Failed'}")
# Overall result
if content_type_test:
print("\n✅ All tests passed!")
else:
print("\n❌ Some tests failed!")
else:
print("\n❌ Authentication failed, cannot run tests!")
if __name__ == "__main__":
main()