Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make specific fields in HookContext immutable #266

Merged
merged 1 commit into from
Jan 29, 2024
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
5 changes: 5 additions & 0 deletions openfeature/hook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class HookContext:
client_metadata: typing.Optional[ClientMetadata] = None
provider_metadata: typing.Optional[Metadata] = None

def __setattr__(self, key: str, value: typing.Any) -> None:
if hasattr(self, key) and key in ("flag_key", "flag_type", "default_value"):
federicobond marked this conversation as resolved.
Show resolved Hide resolved
raise AttributeError(f"Attribute {key!r} is immutable")
federicobond marked this conversation as resolved.
Show resolved Hide resolved
super().__setattr__(key, value)


class Hook:
def before(
Expand Down
54 changes: 54 additions & 0 deletions tests/hook/test_hook_support.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from unittest.mock import ANY, MagicMock

import pytest

from openfeature.client import ClientMetadata
from openfeature.evaluation_context import EvaluationContext
from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType
from openfeature.hook import Hook, HookContext
Expand All @@ -10,6 +13,57 @@
error_hooks,
)
from openfeature.immutable_dict.mapping_proxy_type import MappingProxyType
from openfeature.provider.metadata import Metadata


def test_hook_context_has_required_and_optional_fields():
"""Requirement

4.1.1 - Hook context MUST provide: the "flag key", "flag value type", "evaluation context", and the "default value".
4.1.2 - The "hook context" SHOULD provide: access to the "client metadata" and the "provider metadata" fields.
"""

# Given/When
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, EvaluationContext())

# Then
assert hasattr(hook_context, "flag_key")
assert hasattr(hook_context, "flag_type")
assert hasattr(hook_context, "default_value")
assert hasattr(hook_context, "evaluation_context")
assert hasattr(hook_context, "client_metadata")
assert hasattr(hook_context, "provider_metadata")


def test_hook_context_has_immutable_and_mutable_fields():
"""Requirement

4.1.3 - The "flag key", "flag type", and "default value" properties MUST be immutable.
4.1.4.1 - The evaluation context MUST be mutable only within the before hook.
"""

# Given
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, EvaluationContext())

# When
with pytest.raises(AttributeError):
hook_context.flag_key = "new_key"
with pytest.raises(AttributeError):
hook_context.flag_type = FlagType.STRING
with pytest.raises(AttributeError):
hook_context.default_value = "new_value"

hook_context.evaluation_context = EvaluationContext("targeting_key")
hook_context.client_metadata = ClientMetadata("name")
hook_context.provider_metadata = Metadata("name")

# Then
assert hook_context.flag_key == "flag_key"
assert hook_context.flag_type is FlagType.BOOLEAN
assert hook_context.default_value is True
assert hook_context.evaluation_context.targeting_key == "targeting_key"
assert hook_context.client_metadata.name == "name"
assert hook_context.provider_metadata.name == "name"


def test_error_hooks_run_error_method(mock_hook):
Expand Down
Loading