#!/usr/bin/env python3 """ Quickstart validation script for the Todo API backend This script validates that all core functionality works as expected """ import subprocess import sys import time import requests import json from datetime import datetime, timedelta from jose import jwt import sys import os sys.path.append(os.path.join(os.path.dirname(__file__))) from src.core.config import settings def validate_project_structure(): """Validate that all required project files and directories exist""" print("šŸ” Validating project structure...") required_files = [ "src/main.py", "src/models/task.py", "src/services/task_service.py", "src/api/v1/tasks.py", "src/auth/security.py", "src/auth/deps.py", "src/core/config.py", "src/core/database.py", "requirements.txt" ] missing_files = [] for file in required_files: full_path = f"./{file}" try: with open(full_path, 'r'): pass except FileNotFoundError: missing_files.append(full_path) if missing_files: print(f"āŒ Missing required files: {missing_files}") return False print("āœ… All required files exist") return True def validate_dependencies(): """Validate that all required dependencies are available""" print("šŸ” Validating dependencies...") try: import fastapi import sqlmodel import jose import pydantic print("āœ… All required dependencies are available") return True except ImportError as e: print(f"āŒ Missing dependency: {e}") return False def validate_config(): """Validate that configuration is properly set up""" print("šŸ” Validating configuration...") try: # Check that settings object has required attributes assert hasattr(settings, 'DATABASE_URL'), "DATABASE_URL not configured" assert hasattr(settings, 'SECRET_KEY'), "SECRET_KEY not configured" assert hasattr(settings, 'JWT_ALGORITHM'), "JWT_ALGORITHM not configured" assert hasattr(settings, 'JWT_EXPIRATION_DELTA'), "JWT_EXPIRATION_DELTA not configured" print("āœ… Configuration is properly set up") return True except AssertionError as e: print(f"āŒ Configuration error: {e}") return False def validate_token_functionality(): """Validate JWT token creation and verification functionality""" print("šŸ” Validating token functionality...") try: # Create a test token test_data = {"user_id": "test_user_123", "role": "user"} from src.auth.security import create_access_token, verify_token token = create_access_token(data=test_data) assert token is not None, "Token creation failed" assert isinstance(token, str), "Token should be a string" assert len(token) > 0, "Token should not be empty" # Verify the token payload = verify_token(token) assert payload is not None, "Token verification failed" assert payload["user_id"] == "test_user_123", "User ID mismatch in payload" assert payload["role"] == "user", "Role mismatch in payload" assert "exp" in payload, "Expiration not in payload" print("āœ… Token functionality works correctly") return True except Exception as e: print(f"āŒ Token functionality error: {e}") return False def validate_models(): """Validate that the data models are properly defined""" print("šŸ” Validating data models...") try: from src.models.task import Task, TaskCreate, TaskUpdate, TaskResponse # Test creating a task model instance task_create = TaskCreate( title="Test task", description="Test description", user_id="test_user_123" ) assert task_create.title == "Test task" assert task_create.user_id == "test_user_123" print("āœ… Data models are properly defined") return True except Exception as e: print(f"āŒ Data model error: {e}") return False def validate_services(): """Validate that the service layer is properly implemented""" print("šŸ” Validating service layer...") try: from src.services.task_service import TaskService # Just check that the service class exists and has required methods assert hasattr(TaskService, 'create_task'), "create_task method missing" assert hasattr(TaskService, 'get_tasks_by_user_id'), "get_tasks_by_user_id method missing" assert hasattr(TaskService, 'update_task'), "update_task method missing" assert hasattr(TaskService, 'delete_task'), "delete_task method missing" assert hasattr(TaskService, 'toggle_task_completion'), "toggle_task_completion method missing" print("āœ… Service layer is properly implemented") return True except Exception as e: print(f"āŒ Service layer error: {e}") return False def validate_api_endpoints(): """Validate that API endpoints are properly defined""" print("šŸ” Validating API endpoints...") try: from src.api.v1.tasks import router # Check that the router is properly defined assert router is not None, "API router not defined" print("āœ… API endpoints are properly defined") return True except Exception as e: print(f"āŒ API endpoint error: {e}") return False def validate_logging(): """Validate that logging functionality works""" print("šŸ” Validating logging functionality...") try: from src.core.logging import log_operation, log_error, log_authentication_event, log_authorization_decision, log_token_validation_result # Test logging functions log_operation("QUICKSTART_TEST_OPERATION", user_id="test_user") log_authentication_event("QUICKSTART_TEST", user_id="test_user") log_authorization_decision("read", "test_user", "task_123", True) log_token_validation_result("QUICKSTART_VALID", user_id="test_user") print("āœ… Logging functionality works") return True except Exception as e: print(f"āŒ Logging error: {e}") return False def run_complete_validation(): """Run all validation checks""" print("šŸš€ Starting quickstart validation for Todo API Backend...\n") all_checks = [ ("Project Structure", validate_project_structure), ("Dependencies", validate_dependencies), ("Configuration", validate_config), ("Token Functionality", validate_token_functionality), ("Data Models", validate_models), ("Service Layer", validate_services), ("API Endpoints", validate_api_endpoints), ("Logging", validate_logging) ] results = [] for check_name, check_func in all_checks: print(f"\nšŸ“‹ {check_name} check:") result = check_func() results.append((check_name, result)) print(f"\nšŸ Validation Summary:") total_checks = len(results) passed_checks = sum(1 for _, result in results if result) failed_checks = total_checks - passed_checks for check_name, result in results: status = "āœ… PASS" if result else "āŒ FAIL" print(f" {status}: {check_name}") print(f"\nšŸ“Š Total: {total_checks}, Passed: {passed_checks}, Failed: {failed_checks}") if failed_checks == 0: print("\nšŸŽ‰ All validation checks passed! The Todo API backend is ready for use.") return True else: print(f"\nāš ļø {failed_checks} validation checks failed. Please review the issues above.") return False if __name__ == "__main__": success = run_complete_validation() sys.exit(0 if success else 1)