diff --git a/backend/tests/test_exchange.py b/backend/tests/test_exchange.py index 91ba0ad..3a32732 100644 --- a/backend/tests/test_exchange.py +++ b/backend/tests/test_exchange.py @@ -482,6 +482,49 @@ class TestCancelTrade: assert data["status"] == "cancelled_by_user" assert data["cancelled_at"] is not None + @pytest.mark.asyncio + async def test_cancelled_slot_becomes_available_again( + self, client_factory, regular_user, admin_user + ): + """When a trade is cancelled, the slot becomes available for booking again.""" + target_date = await setup_availability_and_price(client_factory, admin_user) + + with mock_price_fetcher(20000.0): + async with client_factory.create(cookies=regular_user["cookies"]) as client: + # Book a slot + book_response = await client.post( + "/api/exchange", + json={ + "slot_start": f"{target_date}T09:00:00Z", + "direction": "buy", + "eur_amount": 10000, + }, + ) + assert book_response.status_code == 200 + trade_id = book_response.json()["id"] + + # Verify the slot is NOT available + slots_response = await client.get( + f"/api/exchange/slots?date={target_date}" + ) + assert slots_response.status_code == 200 + slots = slots_response.json()["slots"] + slot_starts = [s["start_time"] for s in slots] + assert f"{target_date}T09:00:00Z" not in slot_starts + + # Cancel the trade + cancel_response = await client.post(f"/api/trades/{trade_id}/cancel") + assert cancel_response.status_code == 200 + + # Verify the slot IS available again + slots_response = await client.get( + f"/api/exchange/slots?date={target_date}" + ) + assert slots_response.status_code == 200 + slots = slots_response.json()["slots"] + slot_starts = [s["start_time"] for s in slots] + assert f"{target_date}T09:00:00Z" in slot_starts + @pytest.mark.asyncio async def test_cannot_cancel_others_trade( self, client_factory, regular_user, alt_regular_user, admin_user