Skip to content

Commit 021a0d1

Browse files
committed
Add a local only private API
1 parent 4e0ed8f commit 021a0d1

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

backend/app/api/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from fastapi import APIRouter
22

3-
from app.api.routes import items, login, users, utils
3+
from app.api.routes import items, login, private, users, utils
4+
from app.core.config import settings
45

56
api_router = APIRouter()
67
api_router.include_router(login.router, tags=["login"])
78
api_router.include_router(users.router, prefix="/users", tags=["users"])
89
api_router.include_router(utils.router, prefix="/utils", tags=["utils"])
910
api_router.include_router(items.router, prefix="/items", tags=["items"])
11+
12+
13+
if settings.ENVIRONMENT == "local":
14+
api_router.include_router(private.router, prefix="/private", tags=["private"])

backend/app/api/routes/private.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import Any
2+
3+
from fastapi import APIRouter
4+
from pydantic import BaseModel
5+
6+
from app.api.deps import SessionDep
7+
from app.core.security import get_password_hash
8+
from app.models import (
9+
User,
10+
UserPublic,
11+
)
12+
13+
router = APIRouter(tags=["private"])
14+
15+
16+
class PrivateCreateUser(BaseModel):
17+
email: str
18+
password: str
19+
full_name: str
20+
is_verified: bool = False
21+
22+
23+
@router.post("/users/", response_model=UserPublic)
24+
def create_user(user_in: PrivateCreateUser, session: SessionDep) -> Any:
25+
"""
26+
Create a new user.
27+
"""
28+
29+
user = User(
30+
email=user_in.email,
31+
full_name=user_in.full_name,
32+
hashed_password=get_password_hash(user_in.password),
33+
)
34+
35+
session.add(user)
36+
session.commit()
37+
38+
return user
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from fastapi.testclient import TestClient
2+
from sqlmodel import Session, select
3+
4+
from app.core.config import settings
5+
from app.models import User
6+
7+
8+
def test_create_user(client: TestClient, db: Session) -> None:
9+
r = client.post(
10+
f"{settings.API_V1_STR}/private/users/",
11+
json={
12+
"email": "[email protected]",
13+
"password": "password123",
14+
"full_name": "Pollo Listo",
15+
},
16+
)
17+
18+
assert r.status_code == 200
19+
20+
data = r.json()
21+
22+
user = db.exec(select(User).where(User.id == data["id"])).first()
23+
24+
assert user
25+
assert user.email == "[email protected]"
26+
assert user.full_name == "Pollo Listo"

0 commit comments

Comments
 (0)