Skip to content

Commit

Permalink
[IMP] shopinvader_api_sale_loyalty: add "current" alias on cart routes
Browse files Browse the repository at this point in the history
  • Loading branch information
marielejeune committed Jan 18, 2024
1 parent 7ca5781 commit 39c4f27
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
10 changes: 6 additions & 4 deletions shopinvader_api_sale_loyalty/routers/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
from ..schemas import LoyaltyCardInput, LoyaltyRewardInput, Sale


@cart_router.post("/apply_coupon", deprecated=True)
@cart_router.post("/apply_coupon/{uuid}", deprecated=True)
@cart_router.post("/coupon")
@cart_router.post("/apply_coupon", deprecated=True)
@cart_router.post("/{uuid}/coupon")
@cart_router.post("/current/coupon")
@cart_router.post("/coupon")
def apply_coupon(
data: LoyaltyCardInput,
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
Expand All @@ -44,10 +45,11 @@ def apply_coupon(
return Sale.from_sale_order(cart) if cart else None


@cart_router.post("/apply_reward", deprecated=True)
@cart_router.post("/apply_reward/{uuid}", deprecated=True)
@cart_router.post("/reward")
@cart_router.post("/apply_reward", deprecated=True)
@cart_router.post("/{uuid}/reward")
@cart_router.post("/current/reward")
@cart_router.post("/reward")
def apply_reward(
data: LoyaltyRewardInput,
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
Expand Down
73 changes: 73 additions & 0 deletions shopinvader_api_sale_loyalty/tests/test_apply_coupon_or_reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,45 @@ def test_code_promotion_program(self):
"The promo should've been applied",
)

def test_route_current_coupon(self):
"""
Test that the route /current/coupon is reachable
"""
# Buy 1 C + Enter code, 10% discount on C
with self._create_test_client(router=cart_router) as test_client:
data = {
"transactions": [
{
"uuid": self.dummy_uuid,
"product_id": self.product_C.id,
"qty": 1,
}
]
}
response: Response = test_client.post("/sync", content=json.dumps(data))
self.assertEqual(response.status_code, 201)
res = response.json()
self.assertEqual(
len(self.cart.order_line),
1,
"The promo offer shouldn't have been applied as the code hasn't "
"been entered yet",
)
self.assertEqual(
res["programs"],
[],
"The promo offer shouldn't have been applied as the code hasn't "
"been entered yet",
)
# Enter an invalid code
with self._create_test_client(
router=cart_router
) as test_client, self.assertRaisesRegex(
UserError, r"This code is invalid \(fakecode\)\."
):
data = {"code": "fakecode"}
test_client.post("/current/coupon", content=json.dumps(data))

def test_deprecated_route_apply_coupon(self):
"""
Test that the deprecated route /apply_coupon is still reachable.
Expand Down Expand Up @@ -647,6 +686,40 @@ def test_program_auto_product_choice(self):
)
self.assertEqual(res["claimable_rewards"], [])

def test_route_current_reward(self):
"""
Check that route /current/reward is reachable.
"""
program = self._create_program_choice_reward_auto(self.product_A)
with self._create_test_client(router=cart_router) as test_client:
data = {
"transactions": [
{"uuid": self.dummy_uuid, "product_id": self.product_A.id, "qty": 1}
]
}
response: Response = test_client.post("/sync", content=json.dumps(data))
self.assertEqual(response.status_code, 201)
self.assertEqual(
len(self.cart.order_line),
1,
"The promotion shouldn't be applied as there is a reward choice",
)
res = response.json()
claimable_rewards = res["claimable_rewards"]
self.assertEqual(
len(claimable_rewards), 2, "The two possible rewards should be claimable."
)
self.assertEqual(
{claimable_rewards[0]["id"], claimable_rewards[1]["id"]},
set(program.reward_ids.ids),
)
with self._create_test_client(router=cart_router) as test_client:
data = {"reward_id": claimable_rewards[0]["id"]}
response: Response = test_client.post(
"/current/reward", content=json.dumps(data)
)
self.assertEqual(response.status_code, 200)

def test_deprecated_route_apply_reward(self):
"""
Check that deprecated route /apply_reward is still reachable.
Expand Down

0 comments on commit 39c4f27

Please sign in to comment.