Skip to content

Commit

Permalink
fix: add /api/v1 prefix to all relevant routes (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 authored Nov 29, 2023
1 parent dac5c36 commit 353e59f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
18 changes: 10 additions & 8 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def main_page(request: Request):
)


@app.post("/auth")
@app.post("/api/v1/auth")
def authentication(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
response: Response,
Expand Down Expand Up @@ -171,7 +171,7 @@ def authentication(
)


@app.get("/prices", response_model=Page[schemas.PriceBase])
@app.get("/api/v1/prices", response_model=Page[schemas.PriceBase])
def get_price(
filters: schemas.PriceFilter = FilterDepends(schemas.PriceFilter),
db: Session = Depends(get_db),
Expand All @@ -180,7 +180,9 @@ def get_price(


@app.post(
"/prices", response_model=schemas.PriceBase, status_code=status.HTTP_201_CREATED
"/api/v1/prices",
response_model=schemas.PriceBase,
status_code=status.HTTP_201_CREATED,
)
def create_price(
price: schemas.PriceCreate,
Expand Down Expand Up @@ -239,7 +241,7 @@ def create_price(


@app.post(
"/proofs/upload",
"/api/v1/proofs/upload",
response_model=schemas.ProofBase,
status_code=status.HTTP_201_CREATED,
)
Expand All @@ -261,7 +263,7 @@ def upload_proof(
return db_proof


@app.get("/proofs", response_model=list[schemas.ProofBase])
@app.get("/api/v1/proofs", response_model=list[schemas.ProofBase])
def get_user_proofs(
current_user: schemas.UserBase = Depends(get_current_user),
db: Session = Depends(get_db),
Expand All @@ -274,7 +276,7 @@ def get_user_proofs(
return crud.get_user_proofs(db, user=current_user)


@app.get("/products/{product_id}", response_model=schemas.ProductBase)
@app.get("/api/v1/products/{product_id}", response_model=schemas.ProductBase)
def get_product(product_id: int, db: Session = Depends(get_db)):
db_product = crud.get_product_by_id(db, id=product_id)
if not db_product:
Expand All @@ -285,7 +287,7 @@ def get_product(product_id: int, db: Session = Depends(get_db)):
return db_product


@app.get("/locations/{location_id}", response_model=schemas.LocationBase)
@app.get("/api/v1/locations/{location_id}", response_model=schemas.LocationBase)
def get_location(location_id: int, db: Session = Depends(get_db)):
db_location = crud.get_location_by_id(db, id=location_id)
if not db_location:
Expand All @@ -296,7 +298,7 @@ def get_location(location_id: int, db: Session = Depends(get_db)):
return db_location


@app.get("/status")
@app.get("/api/v1/status")
def status_endpoint():
return {"status": "running"}

Expand Down
46 changes: 23 additions & 23 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ def test_hello():
def test_create_price(user, db=override_get_db()):
# without authentication
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_1),
)
assert response.status_code == 401
# with authentication
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_1),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -110,7 +110,7 @@ def test_create_price_required_fields_validation(user):
for price_field in REQUIRED_FIELDS:
PRICE_WITH_FIELD_MISSING = PRICE_1.model_copy(update={price_field: None})
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_FIELD_MISSING),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -125,7 +125,7 @@ def test_create_price_product_code_pattern_validation(user):
update={"product_code": wrong_price_product_code}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_PRODUCT_CODE_ERROR),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -140,7 +140,7 @@ def test_create_price_category_tag_pattern_validation(user):
update={"product_code": None, "category_tag": wrong_price_category_tag}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_CATEGORY_TAG_ERROR),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -155,7 +155,7 @@ def test_create_price_currency_validation(user):
update={"currency": wrong_price_currency}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_CURRENCY_ERROR),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -169,7 +169,7 @@ def test_create_price_location_osm_type_validation(user):
update={"location_osm_type": wrong_price_location_osm_type}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_LOCATION_OSM_TYPE_ERROR),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -182,15 +182,15 @@ def test_create_price_code_category_exclusive_validation(user):
update={"product_code": None}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_CODE_AND_CATEGORY_MISSING),
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 422
# only product_code: ok
PRICE_WITH_ONLY_PRODUCT_CODE = PRICE_1.model_copy()
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_ONLY_PRODUCT_CODE),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -200,7 +200,7 @@ def test_create_price_code_category_exclusive_validation(user):
update={"product_code": None, "category_tag": "en:tomatoes"}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_ONLY_CATEGORY),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -210,7 +210,7 @@ def test_create_price_code_category_exclusive_validation(user):
update={"category_tag": "en:tomatoes"}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_BOTH_CODE_AND_CATEGORY),
headers={"Authorization": f"Bearer {user.token}"},
)
Expand All @@ -225,65 +225,65 @@ def test_create_price_labels_tags_pattern_validation(user):
update={"labels_tags": wrong_price_labels_tags}
)
response = client.post(
"/prices",
"/api/v1/prices",
json=jsonable_encoder(PRICE_WITH_LABELS_TAGS_ERROR),
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 422


def test_get_prices():
response = client.get("/prices")
response = client.get("/api/v1/prices")
assert response.status_code == 200
assert len(response.json()["items"]) == 3
for price_field in ["product_id", "location_id", "proof_id"]:
assert price_field in response.json()["items"][0]


def test_get_prices_pagination():
response = client.get("/prices")
response = client.get("/api/v1/prices")
assert response.status_code == 200
for key in ["items", "total", "page", "size", "pages"]:
assert key in response.json()


def test_get_prices_filters():
response = client.get(f"/prices?product_code={PRICE_1.product_code}")
response = client.get(f"/api/v1/prices?product_code={PRICE_1.product_code}")
assert response.status_code == 200
assert len(response.json()["items"]) == 2
response = client.get("/prices?price__gt=5")
response = client.get("/api/v1/prices?price__gt=5")
assert response.status_code == 200
assert len(response.json()["items"]) == 0
response = client.get("/prices?date=2023-10-31")
response = client.get("/api/v1/prices?date=2023-10-31")
assert response.status_code == 200
assert len(response.json()["items"]) == 3


def test_get_proofs(user):
# without authentication
response = client.get("/proofs")
response = client.get("/api/v1/proofs")
assert response.status_code == 401
# with authentication
response = client.get(
"/proofs",
"/api/v1/proofs",
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 200


def test_get_product(product):
# product exists
response = client.get(f"/products/{product.id}")
response = client.get(f"/api/v1/products/{product.id}")
assert response.status_code == 200
# product does not exist
response = client.get(f"/products/{product.id+1}")
response = client.get(f"/api/v1/products/{product.id+1}")
assert response.status_code == 404


def test_get_location(location):
# location exists
response = client.get(f"/locations/{location.id}")
response = client.get(f"/api/v1/locations/{location.id}")
assert response.status_code == 200
# location does not exist
response = client.get(f"/locations/{location.id+1}")
response = client.get(f"/api/v1/locations/{location.id+1}")
assert response.status_code == 404

0 comments on commit 353e59f

Please sign in to comment.