Skip to content

fix(feature_flags): handle expected falsy values in conditions #2052

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

Merged
merged 5 commits into from
Mar 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def __init__(self, store: StoreProvider, logger: Optional[Union[logging.Logger,
self.logger = logger or logging.getLogger(__name__)

def _match_by_action(self, action: str, condition_value: Any, context_value: Any) -> bool:
if not context_value:
return False
mapping_by_action = {
schema.RuleAction.EQUALS.value: lambda a, b: a == b,
schema.RuleAction.NOT_EQUALS.value: lambda a, b: a != b,
Expand Down
6 changes: 3 additions & 3 deletions aws_lambda_powertools/utilities/feature_flags/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ def validate_condition_key(condition: Dict[str, Any], rule_name: str):

@staticmethod
def validate_condition_value(condition: Dict[str, Any], rule_name: str):
value = condition.get(CONDITION_VALUE, "")
if not value:
raise SchemaValidationError(f"'value' key must not be empty, rule={rule_name}")
value = condition.get(CONDITION_VALUE)
if value is None:
raise SchemaValidationError(f"'value' key must not be null, rule={rule_name}")
action = condition.get(CONDITION_ACTION, "")

# time actions need to be parsed to make sure date and time format is valid and timezone is recognized
Expand Down
39 changes: 38 additions & 1 deletion tests/functional/feature_flags/test_schema_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,47 @@ def test_validate_condition_missing_condition_value():
}

# WHEN calling validate_condition
with pytest.raises(SchemaValidationError, match="'value' key must not be empty"):
with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")


def test_validate_condition_none_condition_value():
# GIVEN a configuration with a missing condition value
condition = {
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": None,
}

# WHEN calling validate_condition
with pytest.raises(SchemaValidationError, match="'value' key must not be null"):
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")


def test_validate_condition_empty_condition_value():
# GIVEN a configuration with a missing condition value
condition = {
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": "",
}

# WHEN calling validate_condition
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")


def test_validate_condition_valid_falsy_condition_value():
# GIVEN a configuration with a missing condition value
condition = {
"action": RuleAction.EQUALS.value,
"key": "tenant_id",
"value": 0,
}

# WHEN calling validate_condition
ConditionsValidator.validate_condition_value(condition=condition, rule_name="dummy")


def test_validate_rule_invalid_rule_type():
# GIVEN an invalid rule type of empty list
# WHEN calling validate_rule
Expand Down