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
3 changes: 2 additions & 1 deletion ld_openfeature/impl/context_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def __build_multi_context(self, context: EvaluationContext) -> Context:
targeting_key = attributes.get('targetingKey')

if targeting_key is not None and not isinstance(targeting_key, str):
continue
logger.warning("A non-string 'targetingKey' attribute was provided.")
targeting_key = None

targeting_key = self.__get_targeting_key(targeting_key, key)
single_context = self.__build_single_context(attributes, kind, targeting_key)
Expand Down
29 changes: 26 additions & 3 deletions tests/impl/test_context_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_invalid_private_attribute_types_are_ignored(context_converter: Evaluati
assert caplog.records[0].message == "'privateAttributes' must be an array of only string values"


def test_create_multi_context_with_invalid_targeting_key(context_converter: EvaluationContextConverter):
def test_create_multi_context_with_invalid_targeting_key(context_converter: EvaluationContextConverter, caplog):
attributes: Mapping[str, EvaluationContextAttribute] = {
'kind': 'multi',
'user': {'targetingKey': False, 'key': 'user-key', 'name': 'User name'},
Expand All @@ -62,8 +62,31 @@ def test_create_multi_context_with_invalid_targeting_key(context_converter: Eval
ld_context = context_converter.to_ld_context(context)

assert ld_context.valid is True
assert ld_context.key == 'org-key'
assert ld_context.kind == 'org'
assert ld_context.multiple is True
user_context = ld_context.get_individual_context('user')
assert user_context is not None
assert user_context.key == 'user-key'
assert user_context.name == 'User name'
assert user_context.get('targetingKey') is None
org_context = ld_context.get_individual_context('org')
assert org_context is not None
assert org_context.key == 'org-key'

assert len(caplog.records) == 1
assert caplog.records[0].message == "A non-string 'targetingKey' attribute was provided."
assert caplog.records[0].levelname == 'WARNING'


def test_multi_context_with_invalid_targeting_key_and_no_key_is_invalid(context_converter: EvaluationContextConverter):
attributes: Mapping[str, EvaluationContextAttribute] = {
'kind': 'multi',
'user': {'targetingKey': False, 'name': 'User name'},
'org': {'key': 'org-key', 'name': 'Org name'},
}
context = EvaluationContext(None, attributes)
ld_context = context_converter.to_ld_context(context)

assert ld_context.valid is False


def test_create_context_with_only_key(context_converter: EvaluationContextConverter):
Expand Down
Loading