feat: add user seeding script and update environment variables for testing
Some checks failed
Chore App Build, Test, and Push Docker Images / build-and-push (push) Failing after 2m57s

This commit is contained in:
2026-04-17 13:58:58 -04:00
parent f5dfdfbb42
commit cf2ac4b5e8
3 changed files with 100 additions and 0 deletions

View File

@@ -142,6 +142,11 @@ jobs:
DIGEST_TOKEN_SECRET=${{ secrets.DIGEST_TOKEN_SECRET }}
VAPID_PUBLIC_KEY=${{ secrets.VAPID_PUBLIC_KEY }}
VAPID_PRIVATE_KEY=${{ secrets.VAPID_PRIVATE_KEY }}
SEED_EMAIL=${{ secrets.SEED_EMAIL }}
SEED_PASSWORD=${{ secrets.SEED_PASSWORD }}
SEED_PIN=${{ secrets.SEED_PIN }}
SEED_FIRST_NAME=${{ secrets.SEED_FIRST_NAME }}
SEED_LAST_NAME=${{ secrets.SEED_LAST_NAME }}
EOF
echo "SECRET_KEY is set: $(grep -q 'SECRET_KEY=' .env && echo YES || echo NO)"
@@ -152,6 +157,11 @@ jobs:
docker-compose -f docker-compose.test.yml pull
docker-compose -f docker-compose.test.yml up -d
echo "Waiting for backend to be ready..."
sleep 10
echo "Seeding test user..."
docker exec chores-test-app-backend python scripts/seed_test_user.py
- name: Send mail
if: always() # Runs on success or failure
uses: dawidd6/action-send-mail@v3

View File

@@ -0,0 +1,85 @@
"""
Script to seed a test user account from environment variables.
Intended for use in the test/staging deployment pipeline.
Required environment variables:
SEED_EMAIL - User email address
SEED_PASSWORD - Plain-text password (will be hashed)
SEED_PIN - 4-6 digit parent PIN (stored as plain text)
SEED_FIRST_NAME - User first name
SEED_LAST_NAME - User last name
Usage:
python scripts/seed_test_user.py
"""
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from db.db import users_db
from models.user import User
from werkzeug.security import generate_password_hash
from tinydb import Query
import uuid
def seed_test_user() -> bool:
email = os.environ.get('SEED_EMAIL', '').strip().lower()
password = os.environ.get('SEED_PASSWORD', '').strip()
pin = os.environ.get('SEED_PIN', '').strip()
first_name = os.environ.get('SEED_FIRST_NAME', '').strip()
last_name = os.environ.get('SEED_LAST_NAME', '').strip()
missing = [name for name, val in [
('SEED_EMAIL', email),
('SEED_PASSWORD', password),
('SEED_PIN', pin),
('SEED_FIRST_NAME', first_name),
('SEED_LAST_NAME', last_name),
] if not val]
if missing:
print(f"Error: Missing required environment variables: {', '.join(missing)}")
return False
if not (4 <= len(pin) <= 6 and pin.isdigit()):
print("Error: SEED_PIN must be 4-6 digits")
return False
hashed_password = generate_password_hash(password)
Query_ = Query()
existing = users_db.get(Query_.email == email)
if existing:
users_db.update(
{'password': hashed_password, 'pin': pin},
Query_.email == email
)
print(f"✓ Test user updated successfully")
print(f" Email: {email}")
print(f" Name: {first_name} {last_name}")
print(f" PIN: {'*' * len(pin)}")
else:
user = User(
id=str(uuid.uuid4()),
email=email,
first_name=first_name,
last_name=last_name,
password=hashed_password,
pin=pin,
verified=True,
role='user',
)
users_db.insert(user.to_dict())
print(f"✓ Test user created successfully")
print(f" Email: {email}")
print(f" Name: {first_name} {last_name}")
print(f" PIN: {'*' * len(pin)}")
return True
if __name__ == '__main__':
print("=== Seed Test User ===\n")
success = seed_test_user()
sys.exit(0 if success else 1)

View File

@@ -14,6 +14,11 @@ services:
- DIGEST_TOKEN_SECRET=${DIGEST_TOKEN_SECRET}
- VAPID_PUBLIC_KEY=${VAPID_PUBLIC_KEY}
- VAPID_PRIVATE_KEY=${VAPID_PRIVATE_KEY}
- SEED_EMAIL=${SEED_EMAIL}
- SEED_PASSWORD=${SEED_PASSWORD}
- SEED_PIN=${SEED_PIN}
- SEED_FIRST_NAME=${SEED_FIRST_NAME}
- SEED_LAST_NAME=${SEED_LAST_NAME}
# Add volumes, networks, etc., as needed
chores-test-app-frontend: # Test frontend service name