Skip to content

Commit

Permalink
fix: custom context properties in root are now respected during evalu…
Browse files Browse the repository at this point in the history
…ation (#333)
  • Loading branch information
sighphyre authored Nov 22, 2024
1 parent 6dfcec2 commit d2afb34
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
19 changes: 18 additions & 1 deletion UnleashClient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
from .utils import LOGGER, InstanceAllowType, InstanceCounter

INSTANCES = InstanceCounter()
_BASE_CONTEXT_FIELDS = [
"userId",
"sessionId",
"environment",
"appName",
"currentTime",
"remoteAddress",
"properties",
]


# pylint: disable=dangerous-default-value
Expand Down Expand Up @@ -438,7 +447,7 @@ def _safe_context(self, context) -> dict:
if "currentTime" not in new_context:
new_context["currentTime"] = datetime.now(timezone.utc).isoformat()

safe_properties = new_context.get("properties", {})
safe_properties = self._extract_properties(new_context)
safe_properties = {
k: self._safe_context_value(v) for k, v in safe_properties.items()
}
Expand All @@ -452,6 +461,14 @@ def _safe_context(self, context) -> dict:

return safe_context

def _extract_properties(self, context: dict) -> dict:
properties = context.get("properties", {})
extracted_fields = {
k: v for k, v in context.items() if k not in _BASE_CONTEXT_FIELDS
}
extracted_fields.update(properties)
return extracted_fields

def _safe_context_value(self, value):
if isinstance(value, datetime):
return value.isoformat()
Expand Down
56 changes: 56 additions & 0 deletions tests/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MOCK_FEATURE_ENABLED_NO_VARIANTS_RESPONSE,
MOCK_FEATURE_RESPONSE,
MOCK_FEATURE_RESPONSE_PROJECT,
MOCK_FEATURE_WITH_CUSTOM_CONTEXT_REQUIREMENTS,
MOCK_FEATURE_WITH_DATE_AFTER_CONSTRAINT,
MOCK_FEATURE_WITH_DEPENDENCIES_RESPONSE,
MOCK_FEATURE_WITH_NUMERIC_CONSTRAINT,
Expand Down Expand Up @@ -976,3 +977,58 @@ def test_context_adds_current_time_if_not_set():
)

assert unleash_client.is_enabled("DateConstraint")


def test_context_moves_properties_fields_to_properties():
unleash_client = UnleashClient(
URL,
APP_NAME,
disable_metrics=True,
disable_registration=True,
)

context = {"myContext": "1234"}

assert "myContext" in unleash_client._safe_context(context)["properties"]


def test_existing_properties_are_retained_when_custom_context_properties_are_in_the_root():
unleash_client = UnleashClient(
URL,
APP_NAME,
disable_metrics=True,
disable_registration=True,
)

context = {"myContext": "1234", "properties": {"yourContext": "1234"}}

assert "myContext" in unleash_client._safe_context(context)["properties"]
assert "yourContext" in unleash_client._safe_context(context)["properties"]


def test_base_context_properties_are_retained_in_root():
unleash_client = UnleashClient(
URL,
APP_NAME,
disable_metrics=True,
disable_registration=True,
)

context = {"userId": "1234"}

assert "userId" in unleash_client._safe_context(context)


def test_is_enabled_works_with_properties_field_in_the_context_root():
cache = FileCache("MOCK_CACHE")
cache.bootstrap_from_dict(MOCK_FEATURE_WITH_CUSTOM_CONTEXT_REQUIREMENTS)
unleash_client = UnleashClient(
URL,
APP_NAME,
disable_metrics=True,
cache=cache,
disable_registration=True,
)

context = {"myContext": "1234"}
assert unleash_client.is_enabled("customContextToggle", context)
27 changes: 27 additions & 0 deletions tests/utilities/mocks/mock_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,30 @@
},
],
}

MOCK_FEATURE_WITH_CUSTOM_CONTEXT_REQUIREMENTS = {
"version": 1,
"features": [
{
"name": "customContextToggle",
"description": "Feature toggle with custom context constraint",
"enabled": True,
"strategies": [
{
"name": "default",
"parameters": {},
"constraints": [
{
"contextName": "myContext",
"operator": "IN",
"values": ["1234"],
"inverted": False,
}
],
}
],
"createdAt": "2018-10-09T06:04:05.667Z",
"impressionData": False,
},
],
}

0 comments on commit d2afb34

Please sign in to comment.