Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.5.2] - 2026-05-22

### Fixed

- `/flags` request field assertions now accept `api_key` as an alias for `token`, matching the endpoint's accepted authentication fields.

## [0.5.1] - 2026-05-04

### Changed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "posthog-sdk-test-harness"
version = "0.5.1"
version = "0.5.2"
description = "Language-agnostic test harness for validating PostHog SDK compliance"
readme = "README.md"
requires-python = "==3.12.12"
Expand Down
8 changes: 7 additions & 1 deletion src/posthog_test_harness/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,19 @@ async def execute(self, params: Dict[str, Any], ctx: "TestContext") -> Any:
# Support dot notation for nested fields (e.g., "person_properties.$device_id")
parts = field.split(".")
current = body
for part in parts:
for index, part in enumerate(parts):
if not isinstance(current, dict):
raise AssertionError(
f"Cannot traverse into non-dict at '{part}' in field path '{field}'. "
f"Value is: {current!r}"
)
if part not in current:
# The /flags endpoint accepts api_key as an alias for token.
# Keep the contract's default assertion on token while allowing
# SDKs that send api_key to satisfy the same check.
if index == 0 and part == "token" and "api_key" in current:
Comment thread
marandaneto marked this conversation as resolved.
current = current["api_key"]
continue
raise AssertionError(
f"Field '{part}' not found in /flags request body at path '{field}'. "
f"Available keys: {list(current.keys())}"
Expand Down
64 changes: 63 additions & 1 deletion tests/test_feature_flag_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
AssertActionResultAction,
AssertEventCountWithNameAction,
AssertEventPropertyInNamedEventAction,
AssertFlagsRequestFieldAction,
AssertFlagsRequestQueryParamAction,
GetFeatureFlagAction,
)
Expand Down Expand Up @@ -179,10 +180,71 @@ def test_feature_flag_contract_asserts_top_level_distinct_id_on_flags_request()
)


@pytest.mark.asyncio
@pytest.mark.parametrize("auth_field", ["token", "api_key"])
async def test_assert_flags_request_field_accepts_token_aliases(auth_field: str) -> None:
ctx = _ctx(
_FakeMockServer(
[
_make_recorded(
"/flags/",
body_decompressed=json.dumps({auth_field: "phc_test_key"}),
)
]
)
)

await AssertFlagsRequestFieldAction().execute(
{"field": "token", "expected": "phc_test_key"},
ctx,
)
Comment thread
marandaneto marked this conversation as resolved.


@pytest.mark.asyncio
async def test_assert_flags_request_field_requires_token_or_api_key() -> None:
ctx = _ctx(
_FakeMockServer(
[
_make_recorded(
"/flags/",
body_decompressed=json.dumps({}),
)
]
)
)

with pytest.raises(AssertionError, match="Field 'token' not found"):
await AssertFlagsRequestFieldAction().execute(
{"field": "token", "expected": "phc_test_key"},
ctx,
)


@pytest.mark.asyncio
async def test_assert_flags_request_field_rejects_mismatched_api_key_alias() -> None:
ctx = _ctx(
_FakeMockServer(
[
_make_recorded(
"/flags/",
body_decompressed=json.dumps({"api_key": "wrong_key"}),
)
]
)
)

with pytest.raises(AssertionError, match="Expected token='phc_test_key', got 'wrong_key'"):
await AssertFlagsRequestFieldAction().execute(
{"field": "token", "expected": "phc_test_key"},
ctx,
)


def _make_recorded(
path: str,
query_params: dict | None = None,
parsed_events: list | None = None,
body_decompressed: str | None = None,
) -> RecordedRequest:
return RecordedRequest(
timestamp_ms=0,
Expand All @@ -191,7 +253,7 @@ def _make_recorded(
headers={},
query_params=query_params or {},
body_raw=b"",
body_decompressed=None,
body_decompressed=body_decompressed,
parsed_events=parsed_events,
response_status=200,
response_headers={},
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading