Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/sdk-compliance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ on:
jobs:
compliance:
name: PostHog SDK compliance tests
uses: PostHog/posthog-sdk-test-harness/.github/workflows/test-sdk-action.yml@85e2901ea3260a28e07a83086d59b4fb4dfc814f
uses: PostHog/posthog-sdk-test-harness/.github/workflows/test-sdk-action.yml@be8b8d5a3f94a249659844e94832e874f049c1e4
with:
adapter-dockerfile: "sdk_compliance_adapter/Dockerfile"
adapter-context: "."
test-harness-version: "main-85e2901@sha256:4c8eac34e7ff66554a2c6947788c0a42b82456bc949c03bd8f6b9a10bef23ef5"
test-harness-version: "0.8.0"
4 changes: 3 additions & 1 deletion posthog/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def post(
gzip: bool = False,
timeout: int = 15,
session: Optional[requests.Session] = None,
api_key_field: str = "api_key",
**kwargs,
) -> requests.Response:
"""Post the `kwargs` to the API"""
Expand All @@ -235,7 +236,7 @@ def post(
body["sent_at"] = datetime.now(tz=timezone.utc).isoformat()
trimmed_host = remove_trailing_slash(normalize_host(host))
url = trimmed_host + cast(str, path)
body["api_key"] = api_key
body[api_key_field] = api_key
data: str | bytes = json.dumps(body, cls=DatetimeSerializer)
log.debug("making request: %s to url: %s", data, url)
headers = {"Content-Type": "application/json", "User-Agent": USER_AGENT}
Expand Down Expand Up @@ -321,6 +322,7 @@ def flags(
gzip,
timeout,
session=_get_flags_session(),
api_key_field="token",
**kwargs,
)
return _process_response(
Expand Down
12 changes: 12 additions & 0 deletions posthog/test/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ def test_post_sends_snake_case_sent_at(key, expected_present):
assert (key in data) is expected_present


def test_flags_request_uses_token_field_for_project_api_key():
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"featureFlags": {}}

with mock.patch.object(request_module, "post", return_value=mock_response) as mock_post:
flags(TEST_API_KEY, host="https://test.posthog.com", distinct_id="user_1")

mock_post.assert_called_once()
assert mock_post.call_args.kwargs["api_key_field"] == "token"


Comment on lines +88 to +99

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Non-parameterised test for a parameterisable case

The team's convention is to prefer parameterised tests. The new test covers one scenario (flags() sending "token"); a natural companion case — that plain post() defaults to "api_key" — is left untested. Expressing both as a @pytest.mark.parametrize block would keep them together, make the contract explicit, and make adding future cases (e.g. a hypothetical batch_post variant) straightforward.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

def test_message_only_debug_logs_include_posthog_prefix():
mock_response = requests.Response()
mock_response.status_code = 200
Expand Down
2 changes: 1 addition & 1 deletion sdk_compliance_adapter/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ docker run -d --name sdk-adapter --network test-network -p 8080:8080 posthog-pyt
docker run --rm \
--name test-harness \
--network test-network \
ghcr.io/posthog/sdk-test-harness:latest \
ghcr.io/posthog/sdk-test-harness:0.8.0 \
run --adapter-url http://sdk-adapter:8080 --mock-url http://test-harness:8081

# Cleanup
Expand Down
6 changes: 3 additions & 3 deletions sdk_compliance_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Configure logging
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
level=logging.WARNING, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -225,7 +225,7 @@ def init():
api_key = data.get("api_key")
host = data.get("host")
flush_at = data.get("flush_at", 100)
flush_interval_ms = data.get("flush_interval_ms", 5000)
flush_interval_ms = data.get("flush_interval_ms", 500)
max_retries = data.get("max_retries", 3)
enable_compression = data.get("enable_compression", False)

Expand All @@ -245,7 +245,7 @@ def init():
flush_interval=flush_interval,
gzip=enable_compression,
max_retries=max_retries,
debug=True,
debug=False,
# Compliance tests assert the request-level default when callers omit
# disable_geoip. Configure the adapter to exercise geoip-enabled
# /flags requests by default while still allowing per-call overrides.
Expand Down
2 changes: 1 addition & 1 deletion sdk_compliance_adapter/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:

# Test harness
test-harness:
image: ghcr.io/posthog/sdk-test-harness:latest
image: ghcr.io/posthog/sdk-test-harness:0.8.0
command: ["run", "--adapter-url", "http://sdk-adapter:8080", "--mock-url", "http://test-harness:8081"]
networks:
- test-network
Expand Down
Loading