- 新增直播履约状态事务 Outbox 与签名投递 worker - 增加 order_site 状态回传、重试和低频对账兜底 - 补充 Outbox、签名校验与集成接口测试
88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
"""事务 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
|