Spaces:
Runtime error
Runtime error
File size: 9,712 Bytes
da8ce94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | #!/usr/bin/env python
"""
Add new test users to JSON database (Development Only)
This script creates test users from environment variables in .env file.
Passwords must be configured in .env for security.
Usage:
python add_users.py
Environment Variables Required:
- TEST_USER1_USERNAME: First test user username
- TEST_USER1_PASSWORD: First test user password
- TEST_USER2_USERNAME: Second test user username
- TEST_USER2_PASSWORD: Second test user password
Example .env:
TEST_USER1_USERNAME=oviya
TEST_USER1_PASSWORD=your-secure-password
TEST_USER2_USERNAME=testing
TEST_USER2_PASSWORD=your-secure-password
"""
import json
import os
import sys
import logging
import bcrypt
from datetime import datetime
from typing import Dict, List, Tuple, Optional
# Load environment variables from .env file
from dotenv import load_dotenv
load_dotenv()
# Add project to path
sys.path.insert(0, os.path.dirname(__file__))
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Database paths
USERS_FILE = os.path.join(os.path.dirname(__file__), 'data', 'users.json')
def hash_password(password: str) -> str:
"""
Hash password using bcrypt
Args:
password: Plain text password to hash
Returns:
Hashed password string
"""
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
def validate_username(username: str) -> bool:
"""
Validate username format
Args:
username: Username to validate
Returns:
True if valid, raises ValueError otherwise
Raises:
ValueError: If username doesn't meet requirements
"""
if not username:
raise ValueError("Username cannot be empty")
if len(username) < 3:
raise ValueError("Username must be at least 3 characters long")
if len(username) > 50:
raise ValueError("Username must not exceed 50 characters")
if not username.replace('_', '').isalnum():
raise ValueError("Username must contain only letters, numbers, and underscores")
return True
def validate_password(password: str) -> bool:
"""
Validate password requirements
Args:
password: Password to validate
Returns:
True if valid, raises ValueError otherwise
Raises:
ValueError: If password doesn't meet requirements
"""
if not password:
raise ValueError("Password cannot be empty")
if len(password) < 8:
raise ValueError("Password must be at least 8 characters long")
if len(password) > 128:
raise ValueError("Password must not exceed 128 characters")
return True
def load_test_users_config() -> List[Dict[str, str]]:
"""
Load test users from environment variables
Returns:
List of user configuration dictionaries
Raises:
ValueError: If required environment variables are missing
"""
user1_username = os.getenv("TEST_USER1_USERNAME")
user1_password = os.getenv("TEST_USER1_PASSWORD")
user2_username = os.getenv("TEST_USER2_USERNAME")
user2_password = os.getenv("TEST_USER2_PASSWORD")
if not user1_password:
raise ValueError("TEST_USER1_PASSWORD not set in .env file")
if not user2_password:
raise ValueError("TEST_USER2_PASSWORD not set in .env file")
return [
{
"username": user1_username or "oviya",
"password": user1_password,
},
{
"username": user2_username or "testing",
"password": user2_password,
}
]
def user_already_exists(users: Dict, username: str) -> bool:
"""
Check if user already exists in database
Args:
users: Dictionary of existing users
username: Username to check
Returns:
True if user exists, False otherwise
"""
return username.lower() in users
def create_user_record(
username: str,
password: str,
next_id: int,
role: str = 'user'
) -> Tuple[Dict, int]:
"""
Create a user record dictionary
Args:
username: Username for the account
password: Plain text password to hash
next_id: User ID number
role: User role (default: 'user')
Returns:
Tuple of (user_record_dict, next_id_incremented)
Raises:
ValueError: If validation fails
"""
# Validate inputs
validate_username(username)
validate_password(password)
# Create record
user_record = {
'id': next_id,
'username': username.lower(),
'password_hash': hash_password(password),
'role': role,
'created_at': datetime.now().isoformat()
}
return user_record, next_id + 1
def load_users() -> Dict:
"""
Load users from JSON file
Returns:
Dictionary of users, empty dict if file doesn't exist
Raises:
json.JSONDecodeError: If JSON is invalid
IOError: If file cannot be read
"""
try:
with open(USERS_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
logger.warning(f"Users file not found: {USERS_FILE}")
return {}
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON in {USERS_FILE}: {e}")
def save_users(users: Dict) -> bool:
"""
Save users to JSON file
Args:
users: Dictionary of users to save
Returns:
True on success
Raises:
IOError: If file cannot be written
"""
try:
os.makedirs(os.path.dirname(USERS_FILE), exist_ok=True)
with open(USERS_FILE, 'w', encoding='utf-8') as f:
json.dump(users, f, indent=2, ensure_ascii=False)
return True
except IOError as e:
raise IOError(f"Failed to save users to {USERS_FILE}: {e}")
def add_users() -> int:
"""
Add new test users to the database
Returns:
0 on success, 1 on failure
"""
try:
logger.info("Starting user creation process...")
# Load test user configuration
test_users_config = load_test_users_config()
logger.info(f"Loaded configuration for {len(test_users_config)} test users")
# Load existing users
users = load_users()
logger.info(f"Loaded existing users: {list(users.keys())}")
# Get next ID
next_id = max([user.get('id', 0) for user in users.values()]) + 1 if users else 1
logger.info(f"Next user ID will be: {next_id}")
# Track created users for rollback capability
created_users = []
# Create users from configuration
for idx, user_config in enumerate(test_users_config, 1):
username = user_config['username']
password = user_config['password']
try:
# Check if user already exists
if user_already_exists(users, username):
logger.warning(f"User '{username}' already exists, skipping...")
continue
# Create user record
user_record, next_id = create_user_record(username, password, next_id)
# Add to users dictionary
users[username.lower()] = user_record
created_users.append(username)
# Log success (without showing password)
logger.info(f"? Created user {idx}: {username} (ID: {user_record['id']})")
except ValueError as e:
logger.error(f"? Failed to create user '{username}': {e}")
# Continue with next user instead of failing completely
continue
except Exception as e:
logger.error(f"? Unexpected error creating user '{username}': {e}")
continue
# Check if any users were created
if not created_users:
logger.warning("No new users were created (all may already exist)")
return 0
# Save users to file
try:
save_users(users)
logger.info(f"? Successfully saved {len(users)} total users to {USERS_FILE}")
except IOError as e:
logger.error(f"? Failed to save users: {e}")
# Attempt to restore from backup or indicate rollback needed
raise
# Print summary
logger.info("\n" + "="*60)
logger.info("? User creation completed successfully!")
logger.info("="*60)
logger.info(f"Created users: {', '.join(created_users)}")
logger.info(f"Total users in database: {len(users)}")
logger.info(f"All users: {', '.join(sorted(users.keys()))}")
return 0
except ValueError as e:
logger.error(f"Configuration error: {e}")
logger.error("Please check your .env file and ensure TEST_USER passwords are set")
return 1
except IOError as e:
logger.error(f"File I/O error: {e}")
return 1
except Exception as e:
logger.exception(f"Unexpected error: {e}")
return 1
if __name__ == '__main__':
try:
exit_code = add_users()
sys.exit(exit_code)
except KeyboardInterrupt:
logger.info("\nOperation cancelled by user")
sys.exit(1)
except Exception as e:
logger.exception(f"Fatal error: {e}")
sys.exit(1)
|