PYTHONPython

conftest

real world projects / password manager / tests

PYTHON
conftest.py🐍
"""
Pytest fixtures for password manager tests.
"""

import pytest
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Generator

from pwm.manager import PasswordManager
from pwm.storage import VaultStorage
from pwm.crypto import derive_key


@pytest.fixture
def temp_dir() -> Generator[Path, None, None]:
    """Provide a temporary directory for tests."""
    with TemporaryDirectory() as tmpdir:
        yield Path(tmpdir)


@pytest.fixture
def vault_path(temp_dir: Path) -> Path:
    """Provide path for test vault."""
    return temp_dir / "test_vault.pwm"


@pytest.fixture
def master_password() -> str:
    """Provide test master password."""
    return "TestPassword123!"


@pytest.fixture
def manager(vault_path: Path) -> PasswordManager:
    """Provide password manager instance."""
    return PasswordManager(vault_path)


@pytest.fixture
def initialized_manager(manager: PasswordManager, master_password: str) -> PasswordManager:
    """Provide initialized and unlocked manager."""
    manager.initialize(master_password)
    return manager


@pytest.fixture
def storage(vault_path: Path) -> VaultStorage:
    """Provide vault storage instance."""
    return VaultStorage(vault_path)


@pytest.fixture
def sample_key(master_password: str) -> bytes:
    """Provide sample encryption key."""
    salt = b"0" * 16  # Fixed salt for testing
    return derive_key(master_password, salt)


@pytest.fixture
def populated_manager(initialized_manager: PasswordManager) -> PasswordManager:
    """Provide manager with sample entries."""
    initialized_manager.add(
        name="GitHub",
        username="user@example.com",
        password="github_pass123!",
        url="https://github.com",
        category="Development",
        tags=["code", "vcs"]
    )
    
    initialized_manager.add(
        name="Gmail",
        username="user@gmail.com",
        password="gmail_pass456!",
        url="https://gmail.com",
        category="Email"
    )
    
    initialized_manager.add(
        name="AWS",
        username="admin",
        password="aws_admin_789!",
        category="Cloud",
        notes="Production account"
    )
    
    return initialized_manager
PreviousNext