mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
Merge pull request #182 from grillazz/179-reflect-latest-and-greatest-fastapi-improvements
179 reflect latest and greatest fastapi improvements
This commit is contained in:
commit
c7775c78e6
2
.env
2
.env
@ -5,7 +5,7 @@ PYTHONUNBUFFERED=1
|
||||
POSTGRES_HOST=db
|
||||
POSTGRES_PORT=5432
|
||||
POSTGRES_DB=devdb
|
||||
POSTGRES_USER=user
|
||||
POSTGRES_USER=devdb
|
||||
POSTGRES_PASSWORD=secret
|
||||
|
||||
# Redis
|
||||
|
2
Makefile
2
Makefile
@ -27,7 +27,7 @@ docker-create-db-migration: ## Create new alembic database migration aka databa
|
||||
|
||||
.PHONY: docker-test
|
||||
docker-test: ## Run project tests
|
||||
docker compose -f compose.yml -f test-compose.yml run --rm app pytest tests
|
||||
docker compose -f compose.yml -f test-compose.yml run --rm app pytest tests --durations=0
|
||||
|
||||
.PHONY: docker-test-snapshot
|
||||
docker-test-snapshot: ## Run project tests with inline snapshot
|
||||
|
@ -1,4 +1,6 @@
|
||||
from fastapi import APIRouter, Depends, status, Request, HTTPException
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, status, Request, HTTPException, Form
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import get_db
|
||||
@ -29,7 +31,9 @@ async def create_user(
|
||||
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
|
||||
)
|
||||
async def get_token_for_user(
|
||||
user: UserLogin, request: Request, db_session: AsyncSession = Depends(get_db)
|
||||
user: Annotated[UserLogin, Form()],
|
||||
request: Request,
|
||||
db_session: AsyncSession = Depends(get_db),
|
||||
):
|
||||
_user: User = await User.find(db_session, [User.email == user.email])
|
||||
|
||||
|
@ -27,4 +27,8 @@ AsyncSessionFactory = async_sessionmaker(
|
||||
async def get_db() -> AsyncGenerator:
|
||||
async with AsyncSessionFactory() as session:
|
||||
# logger.debug(f"ASYNC Pool: {engine.pool.status()}")
|
||||
yield session
|
||||
try:
|
||||
yield session
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting database session: {e}")
|
||||
raise
|
||||
|
@ -67,7 +67,7 @@ app.include_router(
|
||||
dependencies=[Depends(AuthBearer())],
|
||||
)
|
||||
|
||||
_scheduler_data_store = SQLAlchemyDataStore(engine)
|
||||
_scheduler_data_store = SQLAlchemyDataStore(engine, schema="scheduler")
|
||||
_scheduler_event_broker = RedisEventBroker(
|
||||
client_or_url=global_settings.redis_url.unicode_string()
|
||||
)
|
||||
|
@ -6,6 +6,9 @@ from app.models.user import User
|
||||
|
||||
from fastapi import Request, HTTPException
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from app.utils.logging import AppLogger
|
||||
|
||||
logger = AppLogger().get_logger()
|
||||
|
||||
|
||||
async def get_from_redis(request: Request, key: str):
|
||||
@ -37,6 +40,7 @@ class AuthBearer(HTTPBearer):
|
||||
raise HTTPException(
|
||||
status_code=403, detail="Invalid token or expired token."
|
||||
)
|
||||
logger.info(f"Token verified: {credentials.credentials}")
|
||||
return credentials.credentials
|
||||
|
||||
|
||||
|
@ -18,7 +18,7 @@ services:
|
||||
- "8080:8080"
|
||||
depends_on:
|
||||
- db
|
||||
- redis
|
||||
- inmemory
|
||||
|
||||
db:
|
||||
container_name: fsap_db
|
||||
|
@ -1,11 +1,11 @@
|
||||
-- DROP DATABASE IF EXISTS devdb;
|
||||
-- CREATE DATABASE devdb;
|
||||
\connect devdb;
|
||||
CREATE SCHEMA shakespeare;
|
||||
CREATE SCHEMA happy_hog;
|
||||
CREATE SCHEMA scheduler;
|
||||
|
||||
DROP DATABASE IF EXISTS testdb;
|
||||
CREATE DATABASE testdb;
|
||||
\connect testdb;
|
||||
CREATE SCHEMA shakespeare;
|
||||
CREATE SCHEMA happy_hog;
|
||||
CREATE SCHEMA scheduler;
|
||||
|
@ -1,5 +1,3 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
app:
|
||||
environment:
|
||||
|
@ -38,7 +38,11 @@ async def test_add_user(client: AsyncClient):
|
||||
# TODO: parametrize test with diff urls including 404 and 401
|
||||
async def test_get_token(client: AsyncClient):
|
||||
payload = {"email": "joe@grillazz.com", "password": "s1lly"}
|
||||
response = await client.post("/user/token", json=payload)
|
||||
response = await client.post(
|
||||
"/user/token",
|
||||
data=payload,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
claimset = jwt.decode(
|
||||
response.json()["access_token"], options={"verify_signature": False}
|
||||
|
Loading…
x
Reference in New Issue
Block a user