import os import tempfile import zipfile from fastapi.testclient import TestClient from fastapi import UploadFile import pytest from api.main import app client = TestClient(app) def create_sample_pptx(): """Create a minimal PPTX file for testing.""" # This creates a very basic PPTX structure for testing pptx_content = { '[Content_Types].xml': ''' ''', '_rels/.rels': ''' ''', 'ppt/presentation.xml': ''' ''', 'ppt/_rels/presentation.xml.rels': ''' ''', 'ppt/slides/slide1.xml': ''' ''' } with tempfile.NamedTemporaryFile(suffix='.pptx', delete=False) as temp_file: with zipfile.ZipFile(temp_file.name, 'w') as zip_file: for path, content in pptx_content.items(): zip_file.writestr(path, content) return temp_file.name def test_pptx_slides_processing(): """Test the PPTX slides processing endpoint.""" # Create a sample PPTX file pptx_path = create_sample_pptx() try: with open(pptx_path, 'rb') as pptx_file: files = {'pptx_file': ('test.pptx', pptx_file, 'application/vnd.openxmlformats-officedocument.presentationml.presentation')} response = client.post("/api/v1/ppt/pptx-slides/process", files=files) # Check response assert response.status_code == 200 data = response.json() assert data['success'] == True assert 'slides' in data assert 'total_slides' in data assert data['total_slides'] > 0 # Check slide data structure if data['slides']: slide = data['slides'][0] assert 'slide_number' in slide assert 'screenshot_url' in slide assert 'xml_content' in slide assert slide['slide_number'] == 1 assert slide['xml_content'] != '' print(f"✅ Test passed! Processed {data['total_slides']} slides successfully") finally: # Clean up if os.path.exists(pptx_path): os.unlink(pptx_path) def test_invalid_file_type(): """Test that non-PPTX files are rejected.""" # Create a text file and try to upload it with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as temp_file: temp_file.write(b"This is not a PPTX file") temp_file.flush() try: with open(temp_file.name, 'rb') as txt_file: files = {'pptx_file': ('test.txt', txt_file, 'text/plain')} response = client.post("/api/v1/ppt/pptx-slides/process", files=files) # Should return 400 for invalid file type assert response.status_code == 400 data = response.json() assert 'Invalid file type' in data['detail'] print("✅ Invalid file type test passed!") finally: os.unlink(temp_file.name) if __name__ == "__main__": print("Running PPTX slides processing tests...") test_pptx_slides_processing() test_invalid_file_type() print("🎉 All tests completed!")