Skip to content

Commit

Permalink
feat: ask for proof type when creating (#95)
Browse files Browse the repository at this point in the history
* Pass type on proof upload

* Add test
  • Loading branch information
raphodn committed Dec 22, 2023
1 parent 830315b commit 529cdce
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
5 changes: 4 additions & 1 deletion app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
BackgroundTasks,
Depends,
FastAPI,
Form,
HTTPException,
Query,
Response,
Expand All @@ -28,6 +29,7 @@
from app.auth import OAuth2PasswordBearerOrAuthCookie
from app.config import settings
from app.db import session
from app.enums import ProofTypeEnum
from app.utils import init_sentry

logger = get_logger(level=settings.log_level.to_int())
Expand Down Expand Up @@ -242,6 +244,7 @@ def create_price(
)
def upload_proof(
file: UploadFile,
type: ProofTypeEnum = Form(),
current_user: schemas.UserBase = Depends(get_current_user),
db: Session = Depends(get_db),
):
Expand All @@ -254,7 +257,7 @@ def upload_proof(
This endpoint requires authentication.
"""
file_path, mimetype = crud.create_proof_file(file)
db_proof = crud.create_proof(db, file_path, mimetype, user=current_user)
db_proof = crud.create_proof(db, file_path, mimetype, type=type, user=current_user)
return db_proof


Expand Down
10 changes: 7 additions & 3 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy.sql import func

from app import config
from app.enums import LocationOSMEnum
from app.enums import LocationOSMEnum, ProofTypeEnum
from app.models import Location, Price, Product, Proof, User
from app.schemas import (
LocationBase,
Expand Down Expand Up @@ -156,7 +156,9 @@ def get_user_proofs(db: Session, user: UserBase):
return db.query(Proof).filter(Proof.owner == user.user_id).all()


def create_proof(db: Session, file_path: str, mimetype: str, user: UserBase):
def create_proof(
db: Session, file_path: str, mimetype: str, type: ProofTypeEnum, user: UserBase
):
"""Create a proof in the database.
:param db: the database session
Expand All @@ -165,7 +167,9 @@ def create_proof(db: Session, file_path: str, mimetype: str, user: UserBase):
:param user: the user who uploaded the file
:return: the created proof
"""
db_proof = Proof(file_path=file_path, mimetype=mimetype, owner=user.user_id)
db_proof = Proof(
file_path=file_path, mimetype=mimetype, type=type, owner=user.user_id
)
db.add(db_proof)
db.commit()
db.refresh(db_proof)
Expand Down
12 changes: 7 additions & 5 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,18 @@ class PriceFull(PriceBase):
location: LocationBase | None


class ProofCreate(BaseModel):
# class ProofCreate(BaseModel):
# file: UploadFile
# type: ProofTypeEnum


class ProofBase(BaseModel):
model_config = ConfigDict(from_attributes=True, arbitrary_types_allowed=True)

id: int
file_path: str
mimetype: str
type: ProofTypeEnum | None = None


class ProofBase(ProofCreate):
id: int
owner: str
created: datetime.datetime

Expand Down
Empty file added filename.webp
Empty file.
39 changes: 39 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io

import pytest
from fastapi.encoders import jsonable_encoder
from fastapi.testclient import TestClient
Expand Down Expand Up @@ -273,6 +275,42 @@ def test_get_prices_orders():
assert (response.json()["items"][0]["date"]) == "2023-10-31"


def test_create_proof(user):
# without authentication
response = client.post(
"/api/v1/proofs/upload",
)
assert response.status_code == 401
# with authentication but validation error (file & type missing)
response = client.post(
"/api/v1/proofs/upload",
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 422
# with authentication but validation error (type missing)
response = client.post(
"/api/v1/proofs/upload",
files={"file": ("filename", (io.BytesIO(b"test")), "image/webp")},
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 422
# with authentication but validation error (file missing)
response = client.post(
"/api/v1/proofs/upload",
data={"type": "PRICE_TAG"},
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 422
# with authentication and no validation error
response = client.post(
"/api/v1/proofs/upload",
files={"file": ("filename", (io.BytesIO(b"test")), "image/webp")},
data={"type": "PRICE_TAG"},
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 201


def test_get_proofs(user):
# without authentication
response = client.get("/api/v1/proofs")
Expand All @@ -283,6 +321,7 @@ def test_get_proofs(user):
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 200
assert len(response.json()) == 1


def test_get_product(product):
Expand Down

0 comments on commit 529cdce

Please sign in to comment.