Repository Public
1# =============================================================================2# SP BOUCHER | INFRASTRUCTURE & AUTOMATION3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : MacLustr Identity7# File : sdk/python/tests/test_examples.py8# Description : The FastAPI and Flask webhook examples really verify9# signatures and deduplicate events.10# Module : sdk11# Repository : https://git.spboucher.ai/maclustr-identity12# Project Website : https://identity.spboucher.ai13# Created : 2026-12-0114# Last Modified : 2026-12-0115#16# Author : Simon-Pierre Boucher17# Position : Professor of Finance18# Institution : Université du Québec en Outaouais (UQO)19# Organization : SP Boucher20# Website : https://spboucher.ai21# Git : https://git.spboucher.ai22#23# Preferred Contact: admin@spboucher.ai24# Technical Contact: dev@spboucher.ai25# Security/Admin : admin@spboucher.ai26#27# Copyright (c) 202628# Simon-Pierre Boucher29# All rights reserved where applicable. This file is governed by the30# applicable project license, copyright notices and relevant law.31# Existing third-party rights and notices remain unaffected.32#33# License : None specified (no license file in this repository)34#35# Do not embed credentials, private keys, access tokens,36# certificates or confidential personal information.37#38# =============================================================================39# END OF SP BOUCHER SOURCE HEADER40# =============================================================================4142"""The FastAPI and Flask webhook examples really verify signatures and deduplicate events."""43import json4445import pytest4647from maclustr_identity import Webhook4849SECRET = "whsec_examples"50VER = {"verification_id": "ver_9", "object": "verification", "session_id": "vs_9", "status": "verified", "assurance_level": "document_check",51 "method": "document_only", "verification_policy": "identity_document_v1", "policy_version": "1.0", "livemode": False, "simulated": False,52 "external_user_id": "user_9", "reasons": [], "dimensions": {}, "decision_engine": "risk-1.0", "created_at": "2026-12-01T09:00:00-05:00",53 "verified_at": "2026-12-01T09:00:00-05:00", "expires_at": "2027-12-01T09:00:00-05:00", "revoked_at": None, "personal_data_deleted_at": None}54BODY = json.dumps({"id": "evt_9", "object": "event", "type": "verification.verified", "api_version": "2026-12-01", "created": 1, "livemode": False,55 "data": {"object": VER}}).encode()56EXPECTED = {"verification_id": "ver_9", "status": "verified", "assurance_level": "document_check", "expires_at": "2027-12-01T09:00:00-05:00"}575859@pytest.fixture(autouse=True)60def secret(monkeypatch):61 monkeypatch.setenv("MLID_WEBHOOK_SECRET", SECRET)626364def test_fastapi_example():65 from fastapi.testclient import TestClient66 import fastapi_webhook as ex67 ex.SEEN_EVENTS.clear(); ex.USER_STATUS.clear()68 client = TestClient(ex.app)69 h = {"MLID-Signature": Webhook.sign(BODY, SECRET), "Content-Type": "application/json"}70 assert client.post("/webhooks/identity", content=BODY, headers={**h, "MLID-Signature": Webhook.sign(BODY, "whsec_wrong")}).status_code == 40071 assert client.post("/webhooks/identity", content=BODY).status_code == 40072 assert ex.USER_STATUS == {}73 assert client.post("/webhooks/identity", content=BODY, headers=h).json() == {"received": True}74 assert ex.USER_STATUS == {"user_9": EXPECTED}75 assert client.post("/webhooks/identity", content=BODY, headers=h).json() == {"received": True, "duplicate": True}767778def test_flask_example():79 import flask_webhook as ex80 ex.SEEN_EVENTS.clear(); ex.USER_STATUS.clear()81 client = ex.app.test_client()82 h = {"MLID-Signature": Webhook.sign(BODY, SECRET), "Content-Type": "application/json"}83 assert client.post("/webhooks/identity", data=BODY + b" ", headers=h).status_code == 40084 assert ex.USER_STATUS == {}85 assert client.post("/webhooks/identity", data=BODY, headers=h).get_json() == {"received": True}86 assert ex.USER_STATUS == {"user_9": EXPECTED}87 assert client.post("/webhooks/identity", data=BODY, headers=h).get_json() == {"received": True, "duplicate": True}88