Build a Complete FastAPI Todo API in 4 Days (Days 9‑12)

After eight days of isolated concepts, Days 9‑12 guide you through assembling a functional FastAPI Todo API—setting up Alembic migrations, implementing CRUD endpoints with JWT authentication and user‑level permission, and writing pytest‑httpx tests that yield a complete project with 47 passing tests.

Tech Ocean
Tech Ocean
Tech Ocean
Build a Complete FastAPI Todo API in 4 Days (Days 9‑12)

Project selection

Todo API chosen because the data model is simple, the authentication scenario is natural, it provides a full set of CRUD operations, and it is a common interview question.

Implemented endpoints

/auth/register

(POST) – user registration /auth/login (POST) – login, returns JWT /auth/me (GET) – retrieve current user /todos (POST) – create a Todo /todos (GET) – list Todos with pagination /todos/{id} (GET) – get Todo details /todos/{id} (DELETE) – delete a Todo

Day 9 – Scaffold and migration

Project structure prepared on Day 8. Alembic used for database migrations:

alembic init alembic
alembic revision --autogenerate -m "init"
alembic upgrade head

Migration scripts synchronize schema changes across team members, avoiding manual table alterations.

Day 10 – Todo CRUD

Core route example for creating a Todo:

@router.post("", response_model=TodoPublic, status_code=201)
def create_todo(session: SessionDep, current_user: CurrentUser, todo_in: TodoCreate):
    todo = Todo(**todo_in.model_dump(), owner_id=current_user.id)
    session.add(todo)
    session.commit()
    session.refresh(todo)
    return todo
SessionDep

and CurrentUser are dependency‑injected objects introduced earlier, providing the database session and authenticated user automatically.

Day 11 – Authentication and permission

All Todo operations depend on CurrentUser. Dependency defined in api/deps.py:

def get_current_user(session: SessionDep, token: Annotated[str, Depends(oauth2_scheme)]) -> User:
    # Parse JWT token to obtain the current user
    ...

Each route declares current_user: CurrentUser. Permission enforced by adding owner_id = current_user.id to queries, ensuring users can only manipulate their own Todos.

Day 12 – Testing

Tests written with pytest and httpx.AsyncClient:

def test_create_todo(client):
    # Register and log in to obtain a token
    # Use the token to create a Todo
    # Verify the response structure

Key technique: replace the database dependency via app.dependency_overrides with an in‑memory SQLite instance, rebuilding tables before each test so that tests run in isolation.

Four‑day deliverables

A complete Todo API (registration, login, CRUD)

JWT‑based authentication

User isolation – each user can only operate on their own data

Comprehensive pytest test coverage

All 47 tests pass

Reference

FastAPI official documentation (Chinese): https://fastapi.tiangolo.com/zh/tutorial/testing/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonfastapipytestJWT authenticationAlembicTodo API
Tech Ocean
Written by

Tech Ocean

Focused on AI programming, sharing ready-to-use development efficiency solutions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.