#!/usr/bin/env python3 """ Test script for Age service functionality. """ import sys import tempfile from pathlib import Path # Add the backend to Python path sys.path.append(str(Path(__file__).parent)) from app.services.age_service import AgeService def test_age_service(): """Test basic Age service functionality.""" print("šŸ” Testing Age Service") print("=" * 50) try: # Initialize Age service age_service = AgeService() print(f"āœ… Age service initialized") print(f" Age binary: {age_service.age_binary}") print(f" Keys storage: {age_service.keys_storage_path}") # Test key generation (without passphrase first) print("\nšŸ”‘ Testing key generation...") project_id = "test-project-age" result = age_service.generate_master_key_pair( project_id=project_id, passphrase=None # Test without passphrase first ) print(f"āœ… Key pair generated successfully") print(f" Key ID: {result['key_id']}") print(f" Public key: {result['public_key']}") print(f" Private key stored: {result['private_key_stored']}") print(f" Encrypted: {result['encrypted']}") # Test key listing print("\nšŸ“‹ Testing key listing...") keys = age_service.list_project_keys(project_id) print(f"āœ… Found {len(keys)} keys for project {project_id}") if keys: key = keys[0] print(f" Key ID: {key['key_id']}") print(f" Created: {key['created_at']}") print(f" Encrypted: {key['encrypted']}") # Test key validation print("\nšŸ” Testing key validation...") if keys: key_id = keys[0]['key_id'] validation = age_service.validate_key_access(project_id, key_id) print(f"āœ… Key validation completed") print(f" Accessible: {validation['accessible']}") print(f" Private key exists: {validation['private_key_exists']}") print(f" Public key exists: {validation['public_key_exists']}") print(f" Metadata exists: {validation['metadata_exists']}") # Test encryption/decryption print("\nšŸ”’ Testing encryption/decryption...") if keys: public_key = keys[0]['public_key'] test_data = "This is a test message for Age encryption!" # Encrypt data encrypted_data = age_service.encrypt_data(test_data, [public_key]) print(f"āœ… Data encrypted successfully") print(f" Original: {test_data}") print(f" Encrypted length: {len(encrypted_data)} characters") # Test decryption (would need private key and passphrase) try: private_key = age_service.decrypt_private_key( project_id, key_id, None # No passphrase for unencrypted key ) decrypted_data = age_service.decrypt_data(encrypted_data, private_key) print(f"āœ… Data decrypted successfully") print(f" Decrypted: {decrypted_data}") print(f" Match: {decrypted_data == test_data}") except Exception as e: print(f"āš ļø Decryption test skipped: {e}") # Test backup print("\nšŸ’¾ Testing key backup...") if keys: with tempfile.TemporaryDirectory() as temp_dir: backup_success = age_service.backup_key( project_id, key_id, temp_dir ) print(f"āœ… Backup test: {backup_success}") # Check backup files backup_files = list(Path(temp_dir).glob("*")) print(f" Backup files created: {len(backup_files)}") for file in backup_files: print(f" - {file.name}") # Test recovery phrase generation print("\nšŸ”¤ Testing recovery phrase...") if keys: recovery_phrase = age_service.generate_recovery_phrase(project_id, key_id) print(f"āœ… Recovery phrase generated") print(f" Phrase: {recovery_phrase}") print(f" Word count: {len(recovery_phrase.split())}") print(f"\nšŸŽ‰ All Age service tests completed successfully!") return True except Exception as e: print(f"āŒ Age service test failed: {e}") import traceback print(f" Traceback: {traceback.format_exc()}") return False if __name__ == "__main__": success = test_age_service() sys.exit(0 if success else 1)