Files
gw/backend/tests/test_analytics_ingest.py
T

42 lines
1.4 KiB
Python
Raw Normal View History

2026-04-18 07:23:55 +12:00
import pytest
from sqlalchemy import select
from app.models.analytics import AnalyticsEvent
from tests.conftest import TestSessionLocal
@pytest.mark.asyncio
async def test_ingest_event_sets_anon_cookie_and_derives_session_server_side(client):
response = await client.post(
"/api/web/event",
json={
"event_type": "cta_click",
"page": "/pack-walks",
"element": "Book Now [pricing-card]",
"metadata": {
"variant": "pricing-card-weekly",
"destination": "/contact",
"screen": "1920x1080",
"nested": {"drop": True},
},
},
headers={"referer": "https://goodwalk.example/pack-walks"},
)
assert response.status_code == 201, response.text
assert "__gw_anon=" in response.headers.get("set-cookie", "")
assert "HttpOnly" in response.headers.get("set-cookie", "")
async with TestSessionLocal() as session:
result = await session.execute(
select(AnalyticsEvent).order_by(AnalyticsEvent.created_at.desc()).limit(1)
)
event = result.scalar_one()
assert event.session_id
assert event.metadata_["variant"] == "pricing-card-weekly"
assert event.metadata_["destination"] == "/contact"
assert event.metadata_["referrer"] == "https://goodwalk.example/pack-walks"
assert "screen" not in event.metadata_
assert "nested" not in event.metadata_