Skip to content

Commit

Permalink
[IMP] shopinvader_api_cart: add GET /current , POST /current/sync and…
Browse files Browse the repository at this point in the history
… /current/update routes

This is done to avoid problems when adding new routes, such as /payable,
that would conflict with /{uuid}.
  • Loading branch information
marielejeune committed Jan 18, 2024
1 parent 5e49856 commit dc1ab95
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
13 changes: 8 additions & 5 deletions shopinvader_api_cart/routers/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
cart_router = APIRouter(tags=["carts"])


@cart_router.get("/")
@cart_router.get("/{uuid}")
@cart_router.get("/current")
@cart_router.get("/")
def get(
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
partner: Annotated["ResPartner", Depends(authenticated_partner)],
Expand All @@ -38,9 +39,10 @@ def get(
return Sale.from_sale_order(cart) if cart else Response(status_code=204)


@cart_router.post("/sync", status_code=201)
@cart_router.post("/{uuid}/sync", status_code=201)
@cart_router.post("/sync/{uuid}", status_code=201, deprecated=True)
@cart_router.post("/{uuid}/sync", status_code=201)
@cart_router.post("/current/sync", status_code=201)
@cart_router.post("/sync", status_code=201)
def sync(
data: CartSyncInput,
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
Expand All @@ -54,9 +56,10 @@ def sync(
return Sale.from_sale_order(cart) if cart else Response(status_code=204)


@cart_router.post("/update")
@cart_router.post("/{uuid}/update")
@cart_router.post("/update/{uuid}", deprecated=True)
@cart_router.post("/{uuid}/update")
@cart_router.post("/current/update")
@cart_router.post("/update")
def update(
data: CartUpdateInput,
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
Expand Down
51 changes: 50 additions & 1 deletion shopinvader_api_cart/tests/test_shopinvader_api_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ def test_get_authenticated_exists_cart_no_uuid(self) -> None:
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["id"], so.id)

def test_get_current(self) -> None:
"""
Check that route GET /current is reachable
"""
so = self.env["sale.order"]._create_empty_cart(
self.default_fastapi_authenticated_partner.id
)
with self._create_test_client(router=cart_router) as test_client:
response: Response = test_client.get("/current")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["id"], so.id)

def test_sync_authenticated_no_uuid_one_transactions_no_cart_exists(self) -> None:
"""
Provide no uuid but at least one transaction. Since no cart exists,
Expand All @@ -101,6 +113,21 @@ def test_sync_authenticated_no_uuid_one_transactions_no_cart_exists(self) -> Non
so = self.env["sale.order"].browse(response_json["id"])
self.assertEqual(self.trans_uuid_1, so.applied_cart_api_transaction_uuids)

def test_sync_current_cart(self) -> None:
"""
Check that the route /current/sync is reachable.
"""
data = {
"transactions": [
{"uuid": self.trans_uuid_1, "product_id": self.product_1.id, "qty": 1}
]
}
with self._create_test_client(router=cart_router) as test_client:
response: Response = test_client.post(
"/current/sync", content=json.dumps(data)
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_sync_authenticated_no_rights(self) -> None:
data = {
"transactions": [
Expand Down Expand Up @@ -430,7 +457,29 @@ def test_update(self) -> None:
self.assertEqual(so.partner_shipping_id, address)
self.assertEqual(so.partner_invoice_id, partner)

def test_deprecated_route_update_uuidq(self) -> None:
def test_route_update_current(self) -> None:
"""
Check that the route /current/update is reachable.
"""
partner = self.default_fastapi_authenticated_partner
address = self.env["res.partner"].create(
{
"name": "Delivery",
"parent_id": partner.id,
"type": "delivery",
}
)
self.env["sale.order"]._create_empty_cart(
self.default_fastapi_authenticated_partner.id
)
data = {"delivery": {"address_id": address.id}}
with self._create_test_client(router=cart_router) as test_client:
response: Response = test_client.post(
"/current/update", content=json.dumps(data)
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_deprecated_route_update_uuid(self) -> None:
"""
Check that the deprecated /update/{uuid} route is still callable.
"""
Expand Down

0 comments on commit dc1ab95

Please sign in to comment.