70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test invalid file upload scenarios
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
import tempfile
|
|
import os
|
|
|
|
# Add project root to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from app import create_app
|
|
|
|
def test_invalid_files():
|
|
"""Test various invalid file scenarios"""
|
|
app = create_app()
|
|
|
|
with app.test_client() as client:
|
|
print("Testing invalid file scenarios...")
|
|
|
|
# Test 1: No file in request
|
|
print("\n1. Testing upload without file...")
|
|
response = client.post('/api/upload')
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Response: {response.get_json()}")
|
|
|
|
# Test 2: Empty filename
|
|
print("\n2. Testing upload with empty filename...")
|
|
data = {'file': (b'', '')}
|
|
response = client.post('/api/upload', data=data, content_type='multipart/form-data')
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Response: {response.get_json()}")
|
|
|
|
# Test 3: Invalid file extension
|
|
print("\n3. Testing upload with invalid extension...")
|
|
with tempfile.NamedTemporaryFile(suffix='.txt', delete=True) as tmp:
|
|
tmp.write(b'This is not a STEP file')
|
|
tmp.flush()
|
|
|
|
data = {'file': (tmp, 'test.txt')}
|
|
response = client.post('/api/upload', data=data, content_type='multipart/form-data')
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Response: {response.get_json()}")
|
|
|
|
# Test 4: Invalid STEP file content
|
|
print("\n4. Testing upload with invalid STEP content...")
|
|
with tempfile.NamedTemporaryFile(suffix='.step', delete=True) as tmp:
|
|
tmp.write(b'This is not a valid STEP file content')
|
|
tmp.flush()
|
|
|
|
data = {'file': (tmp, 'invalid.step')}
|
|
response = client.post('/api/upload', data=data, content_type='multipart/form-data')
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Response: {response.get_json()}")
|
|
|
|
# Test 5: Empty STEP file
|
|
print("\n5. Testing upload with empty STEP file...")
|
|
with tempfile.NamedTemporaryFile(suffix='.step', delete=True) as tmp:
|
|
tmp.flush() # Create empty file
|
|
|
|
data = {'file': (tmp, 'empty.step')}
|
|
response = client.post('/api/upload', data=data, content_type='multipart/form-data')
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Response: {response.get_json()}")
|
|
|
|
if __name__ == '__main__':
|
|
print("CAE Mesh Generator Invalid File Test")
|
|
print("=" * 50)
|
|
test_invalid_files() |