"""Shared test fixtures for the meal tracker.""" import os import tempfile import pytest from app import create_app from models import init_db @pytest.fixture def app(): """Create a Flask app with a temporary database.""" db_fd, db_path = tempfile.mkstemp(suffix=".db") os.environ["DB_PATH"] = db_path app = create_app() app.config["SECRET_KEY"] = "test-secret" app.config["TESTING"] = True with app.app_context(): init_db() yield app os.close(db_fd) os.unlink(db_path) os.environ.pop("DB_PATH", None) @pytest.fixture def client(app): """Flask test client.""" return app.test_client() def _register_and_login(client, username, password, household_action="create", household_name=None, join_household_id=None): """Helper: register (and auto-login) a user.""" if not household_name: household_name = f"{username}-house" resp = client.post("/register", data={ "username": username, "password": password, "confirm": password, "household_action": household_action, "new_household_name": household_name, "household_id": str(join_household_id) if join_household_id else "", }, follow_redirects=True) return resp @pytest.fixture def auth_client(client): """Test client with a logged-in regular user in a 2-person household.""" # Admin creates household client.post("/register", data={ "username": "alice", "password": "alicepass", "confirm": "alicepass", "household_action": "create", "new_household_name": "Test Household", }) client.get("/logout") # Bob joins the same household from models import get_household_by_name hh = get_household_by_name("Test Household") client.post("/register", data={ "username": "bob", "password": "bobpass", "confirm": "bobpass", "household_action": "join", "household_id": str(hh["id"]), }) return client # Logged in as bob (regular user) @pytest.fixture def admin_client(client): """Test client with the admin user logged in (created their own household).""" client.post("/register", data={ "username": "admin", "password": "adminpass", "confirm": "adminpass", "household_action": "create", "new_household_name": "Admin Household", }) return client