Check in admin page in working state
This commit is contained in:
45
packages/sshecret-admin/src/sshecret_admin/testing.py
Normal file
45
packages/sshecret-admin/src/sshecret_admin/testing.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""Testing helper functions."""
|
||||
|
||||
import os
|
||||
|
||||
import bcrypt
|
||||
|
||||
from sqlalchemy import Engine
|
||||
from sqlmodel import Session, select
|
||||
from .auth_models import User
|
||||
|
||||
|
||||
def get_test_user_details() -> tuple[str, str]:
|
||||
"""Resolve testing user."""
|
||||
test_user = os.getenv("SSHECRET_TEST_USERNAME") or "test"
|
||||
test_password = os.getenv("SSHECRET_TEST_PASSWORD") or "test"
|
||||
if test_user and test_password:
|
||||
return (test_user, test_password)
|
||||
|
||||
raise RuntimeError(
|
||||
"Error: No testing username and password registered in environment."
|
||||
)
|
||||
|
||||
|
||||
def is_testing_mode() -> bool:
|
||||
"""Check if we're running in test mode.
|
||||
|
||||
We will determine this by looking for the environment variable SSHECRET_TEST_MODE=1
|
||||
"""
|
||||
if os.environ.get("PYTEST_VERSION") is not None:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def create_test_user(session: Session, username: str, password: str) -> User:
|
||||
"""Create test user.
|
||||
|
||||
We create a user with whatever username and password is supplied.
|
||||
"""
|
||||
salt = bcrypt.gensalt()
|
||||
hashed_password = bcrypt.hashpw(password.encode(), salt)
|
||||
user = User(username=username, hashed_password=hashed_password.decode())
|
||||
session.add(user)
|
||||
session.commit()
|
||||
return user
|
||||
|
||||
Reference in New Issue
Block a user