Fixed some tests and added schemas

This commit is contained in:
Jonas Linter
2025-12-01 10:14:14 +01:00
parent 3e577a499f
commit 2be10ff899
4 changed files with 399 additions and 27 deletions

View File

@@ -7,6 +7,7 @@ This module tests:
"""
import asyncio
import json
import uuid
from datetime import UTC, datetime
from pathlib import Path
@@ -22,6 +23,8 @@ from alpine_bits_python.api import app
from alpine_bits_python.const import WebhookStatus
from alpine_bits_python.db import Base, Reservation, WebhookRequest
from alpine_bits_python.db_setup import reprocess_stuck_webhooks
from alpine_bits_python.schemas import WebhookRequestData
from alpine_bits_python.webhook_processor import initialize_webhook_processors, webhook_registry
@pytest_asyncio.fixture
@@ -165,23 +168,16 @@ class TestWebhookReprocessing:
# Step 1: Process a webhook normally to create a reservation
from alpine_bits_python.webhook_processor import process_wix_form_submission
test_data = {
"data": {
"submissionId": "STUCK-WEBHOOK-TEST-ID",
"submissionTime": "2025-10-07T05:48:41.855Z",
"contact": {
"name": {"first": "Jane", "last": "Smith"},
"email": "jane.smith@example.com",
"phones": [{"e164Phone": "+9876543210"}],
"locale": "en-US",
"contactId": "contact-stuck-test",
},
"field:date_picker_a7c8": "2024-12-25",
"field:date_picker_7e65": "2024-12-31",
"field:number_7cf5": "2",
"field:anzahl_kinder": "0",
}
}
test_form_file = Path(__file__).parent / "test_data" / f"test_form{1}.json"
if not test_form_file.exists():
pytest.skip(f"{test_form_file.name} not found")
# Load test form data
with test_form_file.open() as f:
test_data = json.load(f)
test_data["data"]["submissionId"] = "STUCK-WEBHOOK-TEST-ID" # Fixed ID for duplicate test
async with AsyncSessionLocal() as session:
result = await process_wix_form_submission(
@@ -197,7 +193,7 @@ class TestWebhookReprocessing:
)
result = await session.execute(stmt)
reservation = result.scalar_one_or_none()
assert reservation is not None
assert reservation is not None, "Reservation should exist"
assert reservation.unique_id == "STUCK-WEBHOOK-TEST-ID"
# Step 3: Manually create a webhook request stuck in "processing" status
@@ -230,16 +226,23 @@ class TestWebhookReprocessing:
await session.flush()
# Create stuck webhook request with the SAME payload
stuck_webhook = WebhookRequest(
stuck_webhook_data = WebhookRequestData(
webhook_endpoint_id=endpoint.id,
hotel_id="HOTEL123",
payload_json=test_data,
status=WebhookStatus.PROCESSING, # Stuck in processing!
created_at=datetime.now(UTC),
)
stuck_webhook = WebhookRequest(**stuck_webhook_data.model_dump())
session.add(stuck_webhook)
await session.commit()
# initialize wix_form processor
initialize_webhook_processors()
# Step 4: Run reprocessing (simulates app restart)
await reprocess_stuck_webhooks(AsyncSessionLocal, test_config)
@@ -307,15 +310,14 @@ class TestWebhookReprocessingNeverBlocksStartup:
session.add(endpoint)
await session.flush()
# Create stuck webhook with INVALID data (missing required fields)
stuck_webhook = WebhookRequest(
webhook_endpoint_id=endpoint.id,
webhook_request = WebhookRequestData(
hotel_id="HOTEL123",
payload_json={"data": {"invalid": "data"}}, # Missing required fields
status=WebhookStatus.PROCESSING,
created_at=datetime.now(UTC),
payload_hash="invalidhash" # Add a dummy payload_hash to avoid integrity error
status=WebhookStatus.PROCESSING
)
stuck_webhook = WebhookRequest(**webhook_request.model_dump())
session.add(stuck_webhook) ## Cannot add the stuck webhook. Integrity Error payload_hash is missing
await session.commit()