164 lines
5.1 KiB
Python
164 lines
5.1 KiB
Python
"""
|
|
Test script to upload article and image to localtris.com S3 bucket
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from src.database.session import db_manager
|
|
from src.database.repositories import SiteDeploymentRepository
|
|
from src.deployment.storage_factory import create_storage_client
|
|
from src.generation.image_upload import upload_image_to_storage
|
|
from src.generation.url_generator import generate_public_url
|
|
|
|
def test_article_upload(site):
|
|
"""Test uploading an article (HTML content)"""
|
|
print("\n" + "="*60)
|
|
print("TEST 1: Article Upload (HTML)")
|
|
print("="*60)
|
|
|
|
client = create_storage_client(site)
|
|
print(f"Storage client: {type(client).__name__}")
|
|
|
|
# Test HTML content
|
|
test_html = """<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Test Article - S3 Upload</title>
|
|
<meta charset="UTF-8">
|
|
</head>
|
|
<body>
|
|
<h1>Test Article Upload</h1>
|
|
<p>This is a test article uploaded to verify S3 article upload functionality.</p>
|
|
<p>If you can see this, the article upload is working correctly!</p>
|
|
</body>
|
|
</html>"""
|
|
|
|
test_file_path = "test-article-s3-upload.html"
|
|
|
|
print(f"\nUploading article: {test_file_path}")
|
|
print(f"Content length: {len(test_html)} characters")
|
|
|
|
result = client.upload_file(
|
|
site=site,
|
|
file_path=test_file_path,
|
|
content=test_html
|
|
)
|
|
|
|
print(f"\nUpload Result:")
|
|
print(f" Success: {result.success}")
|
|
print(f" File Path: {result.file_path}")
|
|
print(f" Message: {result.message}")
|
|
|
|
if result.success:
|
|
url = generate_public_url(site, test_file_path)
|
|
print(f"\nPublic URL: {url}")
|
|
print(f"\nArticle should be accessible at: {url}")
|
|
return True
|
|
else:
|
|
print(f"\nERROR: Upload failed - {result.message}")
|
|
return False
|
|
|
|
def test_image_upload(site):
|
|
"""Test uploading an image (binary data)"""
|
|
print("\n" + "="*60)
|
|
print("TEST 2: Image Upload (Binary)")
|
|
print("="*60)
|
|
|
|
# Create a simple test image (1x1 red PNG)
|
|
# PNG signature + minimal valid PNG structure
|
|
test_image_bytes = bytes([
|
|
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, # PNG signature
|
|
0x00, 0x00, 0x00, 0x0D, # IHDR chunk length
|
|
0x49, 0x48, 0x44, 0x52, # IHDR
|
|
0x00, 0x00, 0x00, 0x01, # width = 1
|
|
0x00, 0x00, 0x00, 0x01, # height = 1
|
|
0x08, 0x02, 0x00, 0x00, 0x00, # bit depth, color type, etc.
|
|
0x90, 0x77, 0x53, 0xDE, # CRC
|
|
0x00, 0x00, 0x00, 0x0C, # IDAT chunk length
|
|
0x49, 0x44, 0x41, 0x54, # IDAT
|
|
0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, # compressed data
|
|
0x0D, 0x0A, 0x2D, 0xB4, # CRC
|
|
0x00, 0x00, 0x00, 0x00, # IEND chunk length
|
|
0x49, 0x45, 0x4E, 0x44, # IEND
|
|
0xAE, 0x42, 0x60, 0x82 # CRC
|
|
])
|
|
|
|
test_file_path = "images/test-s3-upload.png"
|
|
|
|
print(f"\nUploading image: {test_file_path}")
|
|
print(f"Image size: {len(test_image_bytes)} bytes")
|
|
|
|
image_url = upload_image_to_storage(
|
|
site=site,
|
|
image_bytes=test_image_bytes,
|
|
file_path=test_file_path
|
|
)
|
|
|
|
if image_url:
|
|
print(f"\nImage Upload Result:")
|
|
print(f" Success: True")
|
|
print(f" Public URL: {image_url}")
|
|
print(f"\nImage should be accessible at: {image_url}")
|
|
return True
|
|
else:
|
|
print(f"\nERROR: Image upload failed")
|
|
return False
|
|
|
|
def main():
|
|
"""Run all tests for localtris.com"""
|
|
print("Testing S3 uploads to localtris.com...")
|
|
|
|
db_manager.initialize()
|
|
session = db_manager.get_session()
|
|
site_repo = SiteDeploymentRepository(session)
|
|
|
|
try:
|
|
# Get the site
|
|
site = site_repo.get_by_hostname("localtris.com")
|
|
if not site:
|
|
print("ERROR: Site 'localtris.com' not found")
|
|
return
|
|
|
|
print(f"\nSite found:")
|
|
print(f" ID: {site.id}")
|
|
print(f" Name: {site.site_name}")
|
|
print(f" Storage Provider: {site.storage_provider}")
|
|
print(f" S3 Bucket: {site.s3_bucket_name}")
|
|
print(f" S3 Region: {site.s3_bucket_region}")
|
|
print(f" Custom Domain: {site.s3_custom_domain}")
|
|
|
|
if site.storage_provider != 's3':
|
|
print(f"\nERROR: Site is not an S3 site (storage_provider={site.storage_provider})")
|
|
return
|
|
|
|
# Run tests
|
|
article_success = test_article_upload(site)
|
|
image_success = test_image_upload(site)
|
|
|
|
# Summary
|
|
print("\n" + "="*60)
|
|
print("TEST SUMMARY")
|
|
print("="*60)
|
|
print(f"Article Upload: {'PASS' if article_success else 'FAIL'}")
|
|
print(f"Image Upload: {'PASS' if image_success else 'FAIL'}")
|
|
print("="*60)
|
|
|
|
if article_success and image_success:
|
|
print("\nAll tests passed!")
|
|
else:
|
|
print("\nSome tests failed. Check errors above.")
|
|
|
|
except Exception as e:
|
|
print(f"\nERROR: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
finally:
|
|
session.close()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|