#!/usr/bin/env python3 """ Test script for the media management system """ import os import sys import tempfile from io import BytesIO # Add the app directory to the path sys.path.insert(0, os.path.dirname(__file__)) from app import create_app from app.models import Post, PostImage, User from app.extensions import db from app.media_config import MediaConfig from PIL import Image def test_media_folder_creation(): """Test that media folders are created correctly""" app = create_app() with app.app_context(): # Create a test user test_user = User.query.filter_by(email='test@example.com').first() if not test_user: test_user = User( nickname='testuser', email='test@example.com' ) test_user.set_password('testpass') db.session.add(test_user) db.session.commit() # Create a test post test_post = Post( title='Test Media Post', subtitle='Testing media folder creation', content='This is a test post for media functionality', difficulty=3, media_folder='test_post_12345678_20250723', published=True, author_id=test_user.id ) db.session.add(test_post) db.session.commit() # Check that media folder methods work media_path = test_post.get_media_folder_path() media_url = test_post.get_media_url_path() print(f"✅ Post created with ID: {test_post.id}") print(f"✅ Media folder: {test_post.media_folder}") print(f"✅ Media path: {media_path}") print(f"✅ Media URL: {media_url}") # Test media config config_path = MediaConfig.get_media_path(app, test_post.media_folder, 'images') config_url = MediaConfig.get_media_url(test_post.media_folder, 'images', 'test.jpg') print(f"✅ Config path: {config_path}") print(f"✅ Config URL: {config_url}") # Test file validation valid_image = MediaConfig.is_allowed_file('test.jpg', 'images') valid_gpx = MediaConfig.is_allowed_file('route.gpx', 'gpx') invalid_file = MediaConfig.is_allowed_file('bad.exe', 'images') print(f"✅ Valid image file: {valid_image}") print(f"✅ Valid GPX file: {valid_gpx}") print(f"✅ Invalid file rejected: {not invalid_file}") return True def test_image_processing(): """Test image processing functionality""" print("\n🖼️ Testing Image Processing...") # Create a test image img = Image.new('RGB', (800, 600), color='red') img_buffer = BytesIO() img.save(img_buffer, format='JPEG') img_buffer.seek(0) # Test image size limits max_size = MediaConfig.IMAGE_MAX_SIZE thumbnail_size = MediaConfig.THUMBNAIL_SIZE print(f"✅ Max image size: {max_size}") print(f"✅ Thumbnail size: {thumbnail_size}") print(f"✅ Image quality: {MediaConfig.IMAGE_QUALITY}") return True def test_file_extensions(): """Test file extension validation""" print("\n📁 Testing File Extensions...") # Test image extensions image_exts = MediaConfig.UPLOAD_EXTENSIONS['images'] gpx_exts = MediaConfig.UPLOAD_EXTENSIONS['gpx'] print(f"✅ Allowed image extensions: {image_exts}") print(f"✅ Allowed GPX extensions: {gpx_exts}") # Test MIME types valid_mimes = list(MediaConfig.ALLOWED_MIME_TYPES.keys()) print(f"✅ Allowed MIME types: {valid_mimes}") return True def main(): """Run all tests""" print("🧪 Media Management System Tests") print("=" * 40) try: # Test media folder creation print("\n📁 Testing Media Folder Creation...") test_media_folder_creation() # Test image processing test_image_processing() # Test file extensions test_file_extensions() print("\n✅ All tests passed!") print("\n📋 Media System Summary:") print(" - Media folders created per post") print(" - Images automatically resized and compressed") print(" - Thumbnails generated for all images") print(" - GPX files validated and statistics extracted") print(" - File type validation enforced") print(" - Organized folder structure maintained") except Exception as e: print(f"❌ Test failed: {str(e)}") return False return True if __name__ == '__main__': success = main() sys.exit(0 if success else 1)