Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion publsp/marketplace/lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,18 @@ def __init__(
async def verify_order_and_connection(
self,
order: Order) -> Union[OrderResponse, None]:
ad = self.ad_handler.active_ads.ads[order.d]
# the order may reference an ad id we don't have (unknown/typo), or one
# that was just inactivated; guard the lookup so a stray request can't
# crash the fire-and-forget order task with a KeyError/AttributeError
active_ads = getattr(self.ad_handler, 'active_ads', None)
ad = active_ads.ads.get(order.d) if active_ads else None
if ad is None:
logger.error(
f"order references unknown or inactive ad id '{order.d}', cancelling")
return OrderErrorResponse(
code=OrderErrorCode.invalid_params,
error_message="unknown or no longer active offer id",
)
# validate the order request first
checked_order = order.validate_order(ad=ad)
if not checked_order.is_valid:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_order_handler_unknown_ad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Regression tests: an order referencing an unknown or no-longer-active ad id must
be answered with an OrderErrorResponse rather than crashing the order task.

verify_order_and_connection previously did a bare
`self.ad_handler.active_ads.ads[order.d]`, which raised KeyError (unknown id) or
AttributeError (no ad published yet, active_ads is None). Because orders are
handled as fire-and-forget asyncio tasks, that exception was swallowed and the
requesting client got no response at all.

These construct an OrderHandler with stub dependencies; the guarded path returns
before any Lightning-node call, so no node/relay is required.
"""
from types import SimpleNamespace

import pytest

from publsp.blip51.order import Order, OrderErrorResponse
from publsp.marketplace.lsp import OrderHandler


def _order_handler(active_ads) -> OrderHandler:
return OrderHandler(
ln_backend=None,
ad_handler=SimpleNamespace(active_ads=active_ads),
rumor_handler=None,
nostr_client=None,
)


@pytest.mark.asyncio
async def test_no_ad_published_returns_error():
# active_ads is None before any ad has been published
handler = _order_handler(active_ads=None)
result = await handler.verify_order_and_connection(Order(d="any-id"))
assert isinstance(result, OrderErrorResponse)


@pytest.mark.asyncio
async def test_unknown_ad_id_returns_error():
# an ad set exists but does not contain the requested id
handler = _order_handler(active_ads=SimpleNamespace(ads={}))
result = await handler.verify_order_and_connection(Order(d="does-not-exist"))
assert isinstance(result, OrderErrorResponse)