Skip to content

Commit

Permalink
feat: add create group endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed May 12, 2024
1 parent b6c89e6 commit d276873
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ def create_user(db: Session, user: schemas.UserCreate):
db.commit()
db.refresh(db_user)
return db_user


def create_group(db: Session, group: schemas.GroupCreate, user_id: int):
db_group = models.Group(
owner_id=user_id, name=group.name, description=group.description
)
db.add(db_group)
db.commit()
db.refresh(db_group)
return db_group
20 changes: 16 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from http import HTTPStatus
from fastapi import Depends, FastAPI, HTTPException
from typing import Annotated
from fastapi import Depends, FastAPI, HTTPException, Header
from . import crud, models, schemas
from .database import SessionLocal, engine
from sqlalchemy.orm import Session
Expand All @@ -8,7 +9,6 @@
models.Base.metadata.create_all(bind=engine)


# Dependency
def get_db():
db = SessionLocal()
try:
Expand All @@ -17,11 +17,18 @@ def get_db():
db.close()


def get_user(x_user: Annotated[int | None, Header()]) -> int:
return x_user


app = FastAPI(dependencies=[Depends(get_db)])

DbDependency = Annotated[Session, Depends(get_db)]
UserDependency = Annotated[int, Depends(get_user)]


@app.post("/user/register", status_code=HTTPStatus.CREATED)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
def create_user(user: schemas.UserCreate, db: DbDependency):
db_user = crud.get_user_by_email(db, email=user.email)

if db_user is not None:
Expand All @@ -33,7 +40,7 @@ def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):


@app.post("/user/login", status_code=HTTPStatus.CREATED)
def login(user: schemas.UserLogin, db: Session = Depends(get_db)):
def login(user: schemas.UserLogin, db: DbDependency):
db_user = crud.get_user_by_email(db, email=user.email)

if db_user is None:
Expand All @@ -45,3 +52,8 @@ def login(user: schemas.UserLogin, db: Session = Depends(get_db)):
raise HTTPException(status_code=404, detail="Contraseña incorrecta")

return {"token": db_user.id}


@app.post("/group", status_code=HTTPStatus.CREATED)
def create_group(group: schemas.GroupCreate, db: DbDependency, user_id: UserDependency):
return crud.create_group(db, group, user_id)
9 changes: 9 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ class User(Base):
id = Column(Integer, primary_key=True)
email = Column(String, unique=True, index=True)
password = Column(String)


class Group(Base):
__tablename__ = "groups"

id = Column(Integer, primary_key=True)
owner_id = Column(ForeignKey("users.id"))
name = Column(String)
description = Column(String)
14 changes: 14 additions & 0 deletions src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ class UserCreate(UserBase):

class User(UserBase):
id: int


class GroupBase(BaseModel):
name: str


class GroupCreate(GroupBase):
description: str


class Group(GroupBase):
id: int
owner_id: int
description: str
40 changes: 34 additions & 6 deletions src/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def set_up_db():
Base.metadata.drop_all(bind=engine)


@pytest.fixture()
def some_user_id(set_up_db):
response = client.post(
url="/user/register",
json={"email": "example@example.com", "password": "my_ultra_secret_password"},
)
assert response.status_code == 201
return response.json()["id"]


app.dependency_overrides[get_db] = override_get_db

client = TestClient(app)
Expand All @@ -52,7 +62,7 @@ def test_register_a_user(set_up_db):
url="/user/register",
json={"email": "example@example.com", "password": "my_ultra_secret_password"},
)
assert response.status_code == 200
assert response.status_code == 201
assert "id" in response.json()


Expand All @@ -62,7 +72,7 @@ def test_register_a_user_with_an_email_already_used(set_up_db):
json={"email": "example@example.com", "password": "my_ultra_secret_password"},
)

assert first_response.status_code == 200
assert first_response.status_code == 201
assert "id" in first_response.json()

second_response = client.post(
Expand All @@ -84,15 +94,15 @@ def test_successful_login(set_up_db):
json={"email": "example@example.com", "password": "my_ultra_secret_password"},
)

assert first_response.status_code == 200
assert first_response.status_code == 201
assert "id" in first_response.json()

second_response = client.post(
url="/user/login",
json={"email": "example@example.com", "password": "my_ultra_secret_password"},
)

assert second_response.status_code == 200
assert second_response.status_code == 201
assert "token" in second_response.json()


Expand All @@ -102,7 +112,7 @@ def test_login_with_wrong_password(set_up_db):
json={"email": "example@example.com", "password": "my_ultra_secret_password"},
)

assert first_response.status_code == 200
assert first_response.status_code == 201
assert "id" in first_response.json()

second_response = client.post(
Expand All @@ -120,7 +130,7 @@ def test_login_with_wrong_email(set_up_db):
json={"email": "example@example.com", "password": "my_ultra_secret_password"},
)

assert first_response.status_code == 200
assert first_response.status_code == 201
assert "id" in first_response.json()

second_response = client.post(
Expand All @@ -133,3 +143,21 @@ def test_login_with_wrong_email(set_up_db):

assert second_response.status_code == 404
assert "token" not in second_response.json()


################################################
# GROUPS
################################################


def test_create_group(set_up_db, some_user_id: int):
first_response = client.post(
url="/group",
json={"name": "grupo 1", "description": "really long description 1234"},
headers={"x-user": str(some_user_id)},
)

assert first_response.status_code == 201
response_body = first_response.json()
assert "id" in response_body
assert response_body["owner_id"] == some_user_id

0 comments on commit d276873

Please sign in to comment.