apac-ops-bot/backend/scripts/create_test_users.py
SamoilenkoVadym 8149a98bd6 Fix test user authentication issues
Fixed email validation and token hashing:
- Changed test user emails from @test.local to @example.com (valid domain)
- Replaced passlib bcrypt for JWT token hashing with SHA-256 (no length limit)
- Improved error handling in SimpleLogin component for validation errors
- Deleted old test users and recreated with valid emails

Credentials:
- Admin: admin@example.com / admin
- User: user@example.com / user

Note: bcrypt still used for password hashing (in auth_service.py),
but SHA-256 for JWT token hashing to avoid 72-byte limit.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-27 20:09:07 +00:00

107 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Script to create test users in the database
Run: python -m scripts.create_test_users
"""
import asyncio
import sys
import uuid
from pathlib import Path
# Add backend directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
import bcrypt
from app.config import settings
from app.models.user import User
def hash_password(password: str) -> str:
"""Hash password using bcrypt"""
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
async def create_test_users():
"""Create test users in database"""
# Create async engine
engine = create_async_engine(settings.DATABASE_URL, echo=True)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as session:
# Check if test users already exist
from sqlalchemy import select
# Admin user
result = await session.execute(
select(User).where(User.email == "admin@example.com")
)
admin = result.scalar_one_or_none()
if not admin:
admin = User(
id=uuid.uuid4(),
azure_ad_id="test_admin", # Special flag for test users
email="admin@example.com",
display_name="Test Admin",
given_name="Admin",
surname="User",
role="admin",
is_active=True,
preferences={},
meta_data={
"is_test_user": True,
"password_hash": hash_password("admin")
}
)
session.add(admin)
print(f"✅ Created admin user: admin@example.com / admin")
else:
print(f" Admin user already exists")
# Regular user
result = await session.execute(
select(User).where(User.email == "user@example.com")
)
user = result.scalar_one_or_none()
if not user:
user = User(
id=uuid.uuid4(),
azure_ad_id="test_user", # Special flag for test users
email="user@example.com",
display_name="Test User",
given_name="Test",
surname="User",
role="user",
is_active=True,
preferences={},
meta_data={
"is_test_user": True,
"password_hash": hash_password("user")
}
)
session.add(user)
print(f"✅ Created regular user: user@example.com / user")
else:
print(f" Regular user already exists")
await session.commit()
await engine.dispose()
print("\n" + "="*60)
print("Test users created successfully!")
print("="*60)
print("\n📝 Login credentials:")
print(" Admin: admin@example.com / admin")
print(" User: user@example.com / user")
print("\n🔗 Use these at: http://localhost:3000")
print("="*60)
if __name__ == "__main__":
asyncio.run(create_test_users())