Skip to content

Commit 06cd8da

Browse files
fix(feature-flags): bug handling multiple conditions (#599)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent 0b0b38b commit 06cd8da

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

aws_lambda_powertools/utilities/feature_flags/feature_flags.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ def _evaluate_conditions(
6464
rule_match_value = rule.get(schema.RULE_MATCH_VALUE)
6565
conditions = cast(List[Dict], rule.get(schema.CONDITIONS_KEY))
6666

67+
if not conditions:
68+
logger.debug(
69+
f"rule did not match, no conditions to match, rule_name={rule_name}, rule_value={rule_match_value}, "
70+
f"name={feature_name} "
71+
)
72+
return False
73+
6774
for condition in conditions:
6875
context_value = context.get(str(condition.get(schema.CONDITION_KEY)))
6976
cond_action = condition.get(schema.CONDITION_ACTION, "")
@@ -76,9 +83,8 @@ def _evaluate_conditions(
7683
)
7784
return False # context doesn't match condition
7885

79-
logger.debug(f"rule matched, rule_name={rule_name}, rule_value={rule_match_value}, name={feature_name}")
80-
return True
81-
return False
86+
logger.debug(f"rule matched, rule_name={rule_name}, rule_value={rule_match_value}, name={feature_name}")
87+
return True
8288

8389
def _evaluate_rules(
8490
self, *, feature_name: str, context: Dict[str, Any], feat_default: bool, rules: Dict[str, Any]

tests/functional/feature_flags/test_feature_flags.py

+38
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,44 @@ def test_flags_conditions_no_match(mocker, config):
121121

122122

123123
# check that a rule can match when it has multiple conditions, see rule name for further explanation
124+
def test_flags_conditions_rule_not_match_multiple_conditions_match_only_one_condition(mocker, config):
125+
expected_value = False
126+
tenant_id_val = "6"
127+
username_val = "a"
128+
mocked_app_config_schema = {
129+
"my_feature": {
130+
"default": expected_value,
131+
"rules": {
132+
"tenant id equals 6 and username is a": {
133+
"when_match": True,
134+
"conditions": [
135+
{
136+
"action": RuleAction.EQUALS.value, # this condition matches
137+
"key": "tenant_id",
138+
"value": tenant_id_val,
139+
},
140+
{
141+
"action": RuleAction.EQUALS.value, # this condition does not
142+
"key": "username",
143+
"value": "bbb",
144+
},
145+
],
146+
}
147+
},
148+
}
149+
}
150+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
151+
toggle = feature_flags.evaluate(
152+
name="my_feature",
153+
context={
154+
"tenant_id": tenant_id_val,
155+
"username": username_val,
156+
},
157+
default=True,
158+
)
159+
assert toggle == expected_value
160+
161+
124162
def test_flags_conditions_rule_match_equal_multiple_conditions(mocker, config):
125163
expected_value = False
126164
tenant_id_val = "6"

0 commit comments

Comments
 (0)