70 lines
1.5 KiB
Python
70 lines
1.5 KiB
Python
"""Shared test fixtures for the meal tracker."""
|
|
|
|
import os
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
from app import create_app
|
|
from models import init_db, get_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()
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_client(client):
|
|
"""Test client with a logged-in regular user."""
|
|
client.post("/register", data={
|
|
"username": "bob",
|
|
"password": "bobpass",
|
|
"confirm": "bobpass",
|
|
})
|
|
client.get("/logout")
|
|
client.post("/register", data={
|
|
"username": "alice",
|
|
"password": "alicepass",
|
|
"confirm": "alicepass",
|
|
})
|
|
client.get("/logout")
|
|
# Log in as bob (second user, not admin)
|
|
client.post("/login", data={
|
|
"username": "bob",
|
|
"password": "bobpass",
|
|
})
|
|
return client
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_client(client):
|
|
"""Test client with the admin user logged in."""
|
|
client.post("/register", data={
|
|
"username": "admin",
|
|
"password": "adminpass",
|
|
"confirm": "adminpass",
|
|
})
|
|
return client
|