feat(fulfillment): 增加签名 Webhook 事务 Outbox

- 新增直播履约状态事务 Outbox 与签名投递 worker
- 增加 order_site 状态回传、重试和低频对账兜底
- 补充 Outbox、签名校验与集成接口测试
This commit is contained in:
yml
2026-11-18 10:59:20 +08:00
parent 00c4a268c2
commit bba3d9dac9
17 changed files with 967 additions and 114 deletions
+87
View File
@@ -0,0 +1,87 @@
"""事务 Outbox 与签名 Webhook 基础行为。"""
from __future__ import annotations
from web.backend.models import IntegrationOutbox
from web.backend.services.integration.outbox import (
enqueue_integration_event,
outbox_backoff_seconds,
)
from web.backend.services.integration.security import (
build_webhook_signature,
verify_webhook_signature,
)
def test_webhook_signature_binds_timestamp_and_body():
raw_body = b'{"eventId":"evt-1","items":[]}'
timestamp = "1783394219"
signature = build_webhook_signature("secret-key", timestamp, raw_body)
assert verify_webhook_signature(
secret="secret-key",
timestamp=timestamp,
raw_body=raw_body,
signature=signature,
now_seconds=int(timestamp),
)
assert not verify_webhook_signature(
secret="secret-key",
timestamp=timestamp,
raw_body=raw_body + b" ",
signature=signature,
now_seconds=int(timestamp),
)
def test_webhook_signature_rejects_expired_timestamp():
timestamp = "1783394219"
raw_body = b"{}"
signature = build_webhook_signature("secret-key", timestamp, raw_body)
assert not verify_webhook_signature(
secret="secret-key",
timestamp=timestamp,
raw_body=raw_body,
signature=signature,
tolerance_seconds=300,
now_seconds=int(timestamp) + 301,
)
def test_outbox_enqueue_is_deduplicated_and_backoff_is_bounded(db):
assert enqueue_integration_event(
db,
destination="order_site",
event_type="platform_fulfillment.binding_state_changed",
payload={"items": [{"sessionId": 1}]},
dedupe_key="binding-state:1:same",
)
assert not enqueue_integration_event(
db,
destination="order_site",
event_type="platform_fulfillment.binding_state_changed",
payload={"items": [{"sessionId": 1}]},
dedupe_key="binding-state:1:same",
)
db.commit()
row = db.query(IntegrationOutbox).one()
assert row.status == "pending"
assert row.payload_json["eventId"] == row.event_id
row.status = "dead"
db.commit()
assert enqueue_integration_event(
db,
destination="order_site",
event_type="platform_fulfillment.binding_state_changed",
payload={"items": [{"sessionId": 1}]},
dedupe_key="binding-state:1:same",
)
db.commit()
db.refresh(row)
assert row.status == "pending"
assert row.attempts == 0
assert outbox_backoff_seconds(1) == 5
assert outbox_backoff_seconds(99) == 3600