|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from posthog.async_request import async_post, _process_async_response |
| 8 | +from posthog.request import APIError |
| 9 | + |
| 10 | + |
| 11 | +class FakeAsyncClient: |
| 12 | + async def post(self, *args, **kwargs): |
| 13 | + return FakeResponse(200, {"ok": True}) |
| 14 | + |
| 15 | + |
| 16 | +class FakeResponse: |
| 17 | + def __init__(self, status_code, payload): |
| 18 | + self.status_code = status_code |
| 19 | + self._payload = payload |
| 20 | + self.headers = {} |
| 21 | + self.text = str(payload) |
| 22 | + |
| 23 | + def json(self): |
| 24 | + return self._payload |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.asyncio |
| 28 | +async def test_async_post_debug_log_does_not_include_payload_or_api_key(caplog): |
| 29 | + caplog.set_level(logging.DEBUG, logger="posthog") |
| 30 | + |
| 31 | + await async_post( |
| 32 | + "test-secret-key", |
| 33 | + "https://example.com", |
| 34 | + "/batch/", |
| 35 | + client=FakeAsyncClient(), |
| 36 | + batch=[{"properties": {"password": "super-secret"}}], |
| 37 | + ) |
| 38 | + |
| 39 | + logs = caplog.text |
| 40 | + assert "making async request to url: https://example.com/batch/" in logs |
| 41 | + assert "test-secret-key" not in logs |
| 42 | + assert "super-secret" not in logs |
| 43 | + |
| 44 | + |
| 45 | +def test_process_async_response_debug_log_does_not_include_response_payload(caplog): |
| 46 | + caplog.set_level(logging.DEBUG, logger="posthog") |
| 47 | + |
| 48 | + with pytest.raises(APIError): |
| 49 | + _process_async_response(FakeResponse(400, {"detail": "password=secret"}), "ok") |
| 50 | + |
| 51 | + logs = caplog.text |
| 52 | + assert "received response with status: 400" in logs |
| 53 | + assert "password=secret" not in logs |
0 commit comments