diff --git a/create_superuser.py b/create_superuser.py deleted file mode 100644 index 1006a54..0000000 --- a/create_superuser.py +++ /dev/null @@ -1,117 +0,0 @@ -#!/usr/bin/env python3 -""" -Create superuser script for Internal Admin Example. - -This script creates a superuser account for testing the admin interface. -""" - -import sys -import getpass -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker -from sqlalchemy.exc import IntegrityError - -# Import from the example -from example import User, Base -from internal_admin.auth.security import hash_password, initialize_security -from internal_admin.config import AdminConfig - - -def create_superuser(): - """Create a superuser account.""" - - # Initialize security manager with proper config - config = AdminConfig( - database_url="sqlite:///./example.db", - secret_key="demo-secret-key-for-superuser-creation", - user_model=User, - debug=True - ) - initialize_security(config) - - # Database setup - engine = create_engine(config.database_url) - - # Create tables if they don't exist - Base.metadata.create_all(engine) - - # Create session - Session = sessionmaker(bind=engine) - session = Session() - - try: - print("๐Ÿ” Create Superuser Account") - print("=" * 40) - - # Get user input - username = input("Username: ").strip() - if not username: - print("โŒ Username cannot be empty") - return False - - email = input("Email (optional): ").strip() or None - first_name = input("First Name (optional): ").strip() or None - last_name = input("Last Name (optional): ").strip() or None - - # Get password securely - password = getpass.getpass("Password: ") - password_confirm = getpass.getpass("Confirm Password: ") - - if password != password_confirm: - print("โŒ Passwords don't match") - return False - - if len(password) < 4: - print("โŒ Password must be at least 4 characters long") - return False - - if len(password) > 50: - print("โŒ Password must be less than 50 characters long") - return False - - # Check if username already exists - existing_user = session.query(User).filter(User.username == username).first() - if existing_user: - print(f"โŒ User '{username}' already exists") - return False - - # Create user - user = User( - username=username, - email=email, - first_name=first_name, - last_name=last_name, - password_hash=hash_password(password), - is_active=True, - is_superuser=True - ) - - session.add(user) - session.commit() - - print(f"โœ… Superuser '{username}' created successfully!") - print(f"๐Ÿ“Š You can now log in to the admin at: http://localhost:8000/admin/") - print() - print("To start the development server, run:") - print("python example.py") - - return True - - except IntegrityError as e: - session.rollback() - print(f"โŒ Database error: {e}") - return False - except KeyboardInterrupt: - print("\nโŒ Cancelled by user") - return False - except Exception as e: - session.rollback() - print(f"โŒ Unexpected error: {e}") - return False - finally: - session.close() - - -if __name__ == "__main__": - success = create_superuser() - sys.exit(0 if success else 1) \ No newline at end of file diff --git a/test_auth.py b/test_auth.py deleted file mode 100644 index 3271734..0000000 --- a/test_auth.py +++ /dev/null @@ -1,99 +0,0 @@ -""" -Test script to verify authentication functionality. -""" - -import os -import asyncio -from fastapi import FastAPI -from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime -from sqlalchemy.orm import declarative_base, sessionmaker -from sqlalchemy.sql import func - -from internal_admin.config import AdminConfig -from internal_admin.site import AdminSite -from internal_admin.auth.models import AdminUser - -# Create test database -Base = declarative_base() - - -class TestUser(Base): - """Test user model for authentication.""" - __tablename__ = "test_users" - - id = Column(Integer, primary_key=True) - username = Column(String(50), unique=True, nullable=False) - password_hash = Column(String(255), nullable=False) - is_active = Column(Boolean, default=True) - is_superuser = Column(Boolean, default=False) - created_at = Column(DateTime(timezone=True), server_default=func.now()) - - @property - def display_name(self): - return self.username - - def has_permission(self, permission: str) -> bool: - return self.is_superuser if self.is_active else False - - -def create_test_app(): - """Create test FastAPI app with admin.""" - # Configure admin first - config = AdminConfig( - database_url="sqlite:///test_admin.db", - secret_key="test-secret-key-change-in-production", - user_model=TestUser, - debug=True - ) - - # Setup test database - engine = create_engine("sqlite:///test_admin.db", echo=True) - Base.metadata.create_all(engine) - - # Initialize security for password hashing - from internal_admin.auth.security import initialize_security, hash_password - initialize_security(config) - - # Create test user - SessionLocal = sessionmaker(bind=engine) - session = SessionLocal() - - existing_user = session.query(TestUser).filter(TestUser.username == "admin").first() - if not existing_user: - test_user = TestUser( - username="admin", - password_hash=hash_password("password123"), - is_active=True, - is_superuser=True - ) - session.add(test_user) - session.commit() - print("Created test user: admin / password123") - else: - print("Test user already exists: admin / password123") - - session.close() - - # Create app and mount admin - app = FastAPI(title="Test Admin") - admin_site = AdminSite(config) - - # Register test user model itself for admin management - admin_site.register(TestUser) - - # Mount admin - admin_site.mount(app) - - return app - - -if __name__ == "__main__": - import uvicorn - - app = create_test_app() - - print("Starting test server...") - print("Admin panel: http://localhost:8001/admin/") - print("Login: admin / password123") - - uvicorn.run(app, host="0.0.0.0", port=8000, reload=False) \ No newline at end of file diff --git a/test_manual.py b/test_manual.py deleted file mode 100644 index a555c1a..0000000 --- a/test_manual.py +++ /dev/null @@ -1,139 +0,0 @@ -#!/usr/bin/env python3 -""" -Manual testing guide for Internal Admin Framework. - -This demonstrates how to test the framework components manually -without relying on automated tests that have dependency issues. -""" - -import sys -from pathlib import Path - -# Add project to path -sys.path.insert(0, str(Path(__file__).parent)) - -from internal_admin import AdminSite, AdminConfig, ModelAdmin -from example import User, Category, Product, CategoryAdmin, ProductAdmin - - -def test_framework_components(): - """Test the core framework components manually.""" - - print("๐Ÿงช Manual Testing Guide for Internal Admin Framework") - print("=" * 60) - - tests_passed = 0 - tests_total = 0 - - # Test 1: Configuration - print("\n1๏ธโƒฃ Testing AdminConfig") - try: - config = AdminConfig( - database_url="sqlite:///./test.db", - secret_key="test-secret-key", - user_model=User, - debug=True - ) - print("โœ… AdminConfig created successfully") - tests_passed += 1 - except Exception as e: - print(f"โŒ AdminConfig failed: {e}") - tests_total += 1 - - # Test 2: AdminSite Creation - print("\n2๏ธโƒฃ Testing AdminSite") - try: - admin = AdminSite(config) - print("โœ… AdminSite created successfully") - tests_passed += 1 - except Exception as e: - print(f"โŒ AdminSite failed: {e}") - tests_total += 1 - - # Test 3: Model Registration - print("\n3๏ธโƒฃ Testing Model Registration") - try: - admin.register(Category, CategoryAdmin) - admin.register(Product, ProductAdmin) - admin.register(User) # Default ModelAdmin - print("โœ… Models registered successfully") - tests_passed += 1 - except Exception as e: - print(f"โŒ Model registration failed: {e}") - tests_total += 1 - - # Test 4: Registry Functionality - print("\n4๏ธโƒฃ Testing Registry") - try: - registered_models = admin.get_registered_models() - assert len(registered_models) == 3 - print(f"โœ… Registry working: {len(registered_models)} models registered") - - # Show registered models - for model_class, admin_class in registered_models.items(): - print(f" โ€ข {model_class.__name__} โ†’ {admin_class.__name__}") - tests_passed += 1 - except Exception as e: - print(f"โŒ Registry test failed: {e}") - tests_total += 1 - - # Test 5: ModelAdmin Configuration - print("\n5๏ธโƒฃ Testing ModelAdmin Configuration") - try: - category_admin = admin.get_model_admin(Category) - product_admin = admin.get_model_admin(Product) - - # Test configuration methods - assert category_admin.get_list_display() == ["id", "name", "is_active", "created_at"] - assert category_admin.get_search_fields() == ["name", "description"] - assert product_admin.get_list_display() == ["id", "name", "price", "is_active"] - - print("โœ… ModelAdmin configurations working correctly") - print(f" โ€ข CategoryAdmin list_display: {category_admin.get_list_display()}") - print(f" โ€ข ProductAdmin search_fields: {product_admin.get_search_fields()}") - tests_passed += 1 - except Exception as e: - print(f"โŒ ModelAdmin configuration failed: {e}") - tests_total += 1 - - # Test 6: FastAPI App Integration (basic) - print("\n6๏ธโƒฃ Testing FastAPI Integration") - try: - from fastapi import FastAPI - app = FastAPI() - - # This tests the mounting without running the server - # (avoids auth/password issues) - print("โœ… FastAPI integration ready (mount step works)") - tests_passed += 1 - except Exception as e: - print(f"โŒ FastAPI integration failed: {e}") - tests_total += 1 - - # Test Summary - print("\n" + "=" * 60) - print(f"๐Ÿ Test Summary: {tests_passed}/{tests_total} tests passed") - - if tests_passed == tests_total: - print("๐ŸŽ‰ All core framework components working correctly!") - print("\n๐Ÿ“‹ Manual Testing Steps:") - print("1. The core framework components are working") - print("2. Model registration system is functional") - print("3. Admin configuration system is working") - print("4. FastAPI integration is ready") - - print("\n๐Ÿš€ To test the full web interface:") - print("1. Fix bcrypt dependency: pip install bcrypt==4.0.1") - print("2. Run: python example.py") - print("3. Visit: http://localhost:8000/admin/") - print("4. Use demo mode or create test users manually") - - else: - print("โš ๏ธ Some issues found - check the errors above") - - return tests_passed == tests_total - - -if __name__ == "__main__": - success = test_framework_components() - sys.exit(0 if success else 1) \ No newline at end of file