Skip to content

Commit

Permalink
Fix seed script user creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Sep 13, 2024
1 parent e994279 commit 7968d21
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions thallium-backend/scripts/seed.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import asyncio
from datetime import UTC, datetime

import argon2
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.ext.asyncio import AsyncSession

from src.dto import UserPermission
from src.orm import User, Voucher
from src.settings import Connections

ph = argon2.PasswordHasher()


async def create_vouchers(session: AsyncSession) -> None:
"""Create some test vouchers in the db."""
Expand All @@ -22,10 +26,34 @@ async def create_vouchers(session: AsyncSession) -> None:
async def create_users(session: AsyncSession) -> None:
"""Create some test vouchers in the db."""
entries = [
{"permissions": UserPermission.VIEW_TEMPLATES | UserPermission.VIEW_VOUCHERS},
{"permissions": UserPermission.MANAGE_USERS},
{"permissions": UserPermission.ISSUE_VOUCHERS | UserPermission.REVOKE_VOUCHERS},
{"permissions": UserPermission.UPDATE_TEMPLATES},
{
"username": "cj",
"password_hash": ph.hash("12345"),
"permissions": UserPermission.VIEW_TEMPLATES | UserPermission.VIEW_VOUCHERS,
"password_set_at": datetime.now(UTC),
"require_password_change": False,
},
{
"username": "joe",
"password_hash": ph.hash("hunter2"),
"permissions": UserPermission.MANAGE_USERS,
"password_set_at": datetime.now(UTC),
"require_password_change": False,
},
{
"username": "bella",
"password_hash": ph.hash("france_forever"),
"permissions": UserPermission.ISSUE_VOUCHERS | UserPermission.REVOKE_VOUCHERS,
"password_set_at": datetime.now(UTC),
"require_password_change": False,
},
{
"username": "jc",
"password_hash": ph.hash("tor"),
"permissions": UserPermission.UPDATE_TEMPLATES,
"password_set_at": datetime.now(UTC),
"require_password_change": False,
},
]
stmt = insert(User).values(entries).on_conflict_do_nothing()
await session.execute(stmt)
Expand Down

0 comments on commit 7968d21

Please sign in to comment.