Fix various small bugs

This commit is contained in:
2025-05-12 07:47:11 +02:00
parent a2ec2173ac
commit 458863de3d
6 changed files with 61 additions and 71 deletions

View File

@ -43,7 +43,10 @@ class PasswordContext:
)
if entry and overwrite:
entry.password = secret
elif entry:
self.keepass.save()
return
if entry:
raise ValueError("Error: A secret with this name already exists.")
LOG.debug("Add secret entry to keepass: %s", entry_name)
entry = self.keepass.add_entry(

View File

@ -85,7 +85,7 @@ class SecretUpdate(BaseModel):
"""
if isinstance(self.value, str):
return self.value
secret = secrets.token_urlsafe(self.value.length)
secret = secrets.token_urlsafe(32)[:self.value.length]
return secret

View File

@ -1,40 +1,17 @@
"""Testing helper functions."""
"""Testing helper functions.
This allows creation of a user from within tests.
"""
import os
import bcrypt
from sqlmodel import Session
from sshecret_admin.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.
"""
"""Create test user."""
salt = bcrypt.gensalt()
hashed_password = bcrypt.hashpw(password.encode(), salt)
user = User(username=username, hashed_password=hashed_password.decode())