infrasynth-backend-kit/tests/helpers.py
jcv-dev 551b42eab5 feat: production-hardening pass across the kit
Close the gaps between the documented contract (API-STANDARD, TENANCY,
ENTITLEMENTS) and the implementation, and remove committed build artifacts.

Security:
- verify + process inbound webhooks (HMAC/handler verify, size limit,
  timestamp tolerance, idempotency via InboundEvent.external_id)
- real 2FA login flow (pre-auth challenge; tokens only after verify/recovery)
- wire HybridPermission into security/audit views; add API-key rotate and
  users/<id>/permissions|roles endpoints
- tenant-scoped throttling on by default; webhook replay protection
- verify MercadoPago webhook signatures
- login brute-force guard, configurable password policy, real ALTCHA PoW

Correctness:
- apply verified billing webhooks idempotently (subscription/entitlement/
  invoice/PaymentTransaction); scheduled payment lifecycle jobs
- capture audit update diffs automatically; add audit retention purge
- working notification retries, per-channel rate limits, log retention
- pluggable virus scanner, upload-size limit, pipeline toggle
- feature rollout %/environment targeting; settings-driven registrations
- workflow guards (instance cap, route depth, self-assignment, clone on re-entry)
- wire every previously-dead INFRASYNTH_* setting; drop truly dead ones

Delivery:
- README + CHANGELOG; CI format check + coverage gate
- keep test media out of the tree; untrack .coverage, __pycache__,
  egg-info, docs/ and invoice artifacts
2026-09-24 10:41:21 -05:00

59 lines
1.8 KiB
Python

from infrasynth.billing.gateways.base import (
BasePaymentGateway,
CheckoutSessionResult,
WebhookResult,
)
class FakeGateway(BasePaymentGateway):
"""Deterministic in-memory gateway for tests and demos."""
gateway_slug = "fake"
def __init__(self, config: dict | None = None):
self.config = config or {}
self.create_should_fail = self.config.get("create_should_fail", False)
def create_checkout_session(self, plan, user, **kwargs) -> CheckoutSessionResult:
if self.create_should_fail:
raise ValueError("Fake gateway failure")
return CheckoutSessionResult(
session_id="cs_fake_123",
checkout_url="https://checkout.example.com/cs_fake_123",
client_secret="secret_123",
)
def handle_webhook(self, payload, headers) -> WebhookResult:
if payload.get("event") == "ignored":
return WebhookResult(event_type="ignored", is_handled=False, data={})
return WebhookResult(
event_type=payload.get("event", "checkout.session.completed"),
is_handled=True,
data={"id": payload.get("data", {}).get("id")},
)
def cancel_subscription(self, subscription) -> bool:
return True
def sync_subscription(self, subscription) -> dict:
return {
"status": "active",
"current_period_end": "2026-12-31T00:00:00Z",
"metadata": {"synced": True},
}
def get_invoice(self, invoice) -> dict:
return {
"external_id": "inv_fake_1",
"status": "paid",
"amount": 10.0,
"currency": "USD",
}
def health_check(self) -> bool:
return True
def noop_task(*args, **kwargs):
"""Plain callable used by scheduler tests (accepts the tenant_id kwarg)."""
return "ok"