feat(fulfillment): 增加签名 Webhook 事务 Outbox
- 新增直播履约状态事务 Outbox 与签名投递 worker - 增加 order_site 状态回传、重试和低频对账兜底 - 补充 Outbox、签名校验与集成接口测试
This commit is contained in:
@@ -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
|
||||
@@ -468,8 +468,8 @@ class TestBindingAndCompletion(_Base):
|
||||
notified: list[int] = []
|
||||
monkeypatch.setattr(
|
||||
service,
|
||||
"notify_order_site_binding_state_async",
|
||||
lambda task_id: notified.append(task_id),
|
||||
"_emit_order_site_binding_state",
|
||||
lambda _db, task_id: notified.append(task_id) or True,
|
||||
)
|
||||
task = self._allocated()
|
||||
notified.clear()
|
||||
|
||||
@@ -135,7 +135,8 @@ def test_auto_order_sync_is_throttled(monkeypatch):
|
||||
"fetched": 1
|
||||
}
|
||||
assert service.sync_pending_tasks_if_due(object(), limit=100, now=110.0) is None
|
||||
assert service.sync_pending_tasks_if_due(object(), limit=100, now=161.0) == {
|
||||
assert service.sync_pending_tasks_if_due(object(), limit=100, now=999.0) is None
|
||||
assert service.sync_pending_tasks_if_due(object(), limit=100, now=1000.0) == {
|
||||
"fetched": 1
|
||||
}
|
||||
assert calls == [100, 100]
|
||||
|
||||
Reference in New Issue
Block a user