Skip to content

Commit

Permalink
Add a local only private API
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 committed Nov 22, 2024
1 parent e345273 commit 5f438c9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
7 changes: 6 additions & 1 deletion backend/app/api/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from fastapi import APIRouter

from app.api.routes import items, login, users, utils
from app.api.routes import items, login, private, users, utils
from app.core.config import settings

api_router = APIRouter()
api_router.include_router(login.router, tags=["login"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
api_router.include_router(utils.router, prefix="/utils", tags=["utils"])
api_router.include_router(items.router, prefix="/items", tags=["items"])


if settings.ENVIRONMENT == "local":
api_router.include_router(private.router, prefix="/private", tags=["private"])
38 changes: 38 additions & 0 deletions backend/app/api/routes/private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Any

from fastapi import APIRouter
from pydantic import BaseModel

from app.api.deps import SessionDep
from app.core.security import get_password_hash
from app.models import (
User,
UserPublic,
)

router = APIRouter(tags=["private"])


class CreateUser(BaseModel):
email: str
password: str
full_name: str
is_verified: bool = False


@router.post("/users/", response_model=UserPublic)
def create_user(user_in: CreateUser, session: SessionDep) -> Any:
"""
Create a new user.
"""

user = User(
email=user_in.email,
full_name=user_in.full_name,
hashed_password=get_password_hash(user_in.password),
)

session.add(user)
session.commit()

return user
27 changes: 27 additions & 0 deletions backend/app/tests/api/routes/test_private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from fastapi.testclient import TestClient
from sqlmodel import Session, select

from app.core.config import settings
from app.models import User
from app.tests.utils.user import create_random_user


def test_create_user(client: TestClient, db: Session) -> None:
r = client.post(
f"{settings.API_V1_STR}/private/users/",
json={
"email": "[email protected]",
"password": "password123",
"full_name": "Pollo Listo",
},
)

assert r.status_code == 200

data = r.json()

user = db.exec(select(User).where(User.id == data["id"])).first()

assert user
assert user.email == "[email protected]"
assert user.full_name == "Pollo Listo"

0 comments on commit 5f438c9

Please sign in to comment.