@@ -301,6 +301,8 @@ def test_flags_conditions_rule_match_multiple_actions_multiple_rules_multiple_co
301301
302302
303303# check a case where the feature exists but the rule doesn't match so we revert to the default value of the feature
304+
305+ # Check IN/NOT_IN/KEY_IN_VALUE/KEY_NOT_IN_VALUE/VALUE_IN_KEY/VALUE_NOT_IN_KEY conditions
304306def test_flags_match_rule_with_in_action (mocker , config ):
305307 expected_value = True
306308 mocked_app_config_schema = {
@@ -397,6 +399,207 @@ def test_flags_no_match_rule_with_not_in_action(mocker, config):
397399 assert toggle == expected_value
398400
399401
402+ def test_flags_match_rule_with_key_in_value_action (mocker , config ):
403+ expected_value = True
404+ mocked_app_config_schema = {
405+ "my_feature" : {
406+ "default" : False ,
407+ "rules" : {
408+ "tenant id is contained in [6, 2]" : {
409+ "when_match" : expected_value ,
410+ "conditions" : [
411+ {
412+ "action" : RuleAction .KEY_IN_VALUE .value ,
413+ "key" : "tenant_id" ,
414+ "value" : ["6" , "2" ],
415+ }
416+ ],
417+ }
418+ },
419+ }
420+ }
421+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
422+ toggle = feature_flags .evaluate (name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" }, default = False )
423+ assert toggle == expected_value
424+
425+
426+ def test_flags_no_match_rule_with_key_in_value_action (mocker , config ):
427+ expected_value = False
428+ mocked_app_config_schema = {
429+ "my_feature" : {
430+ "default" : expected_value ,
431+ "rules" : {
432+ "tenant id is contained in [8, 2]" : {
433+ "when_match" : True ,
434+ "conditions" : [
435+ {
436+ "action" : RuleAction .KEY_IN_VALUE .value ,
437+ "key" : "tenant_id" ,
438+ "value" : ["8" , "2" ],
439+ }
440+ ],
441+ }
442+ },
443+ }
444+ }
445+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
446+ toggle = feature_flags .evaluate (name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" }, default = False )
447+ assert toggle == expected_value
448+
449+
450+ def test_flags_match_rule_with_key_not_in_value_action (mocker , config ):
451+ expected_value = True
452+ mocked_app_config_schema = {
453+ "my_feature" : {
454+ "default" : False ,
455+ "rules" : {
456+ "tenant id is contained in [8, 2]" : {
457+ "when_match" : expected_value ,
458+ "conditions" : [
459+ {
460+ "action" : RuleAction .KEY_NOT_IN_VALUE .value ,
461+ "key" : "tenant_id" ,
462+ "value" : ["10" , "4" ],
463+ }
464+ ],
465+ }
466+ },
467+ }
468+ }
469+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
470+ toggle = feature_flags .evaluate (name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" }, default = False )
471+ assert toggle == expected_value
472+
473+
474+ def test_flags_no_match_rule_with_key_not_in_value_action (mocker , config ):
475+ expected_value = False
476+ mocked_app_config_schema = {
477+ "my_feature" : {
478+ "default" : expected_value ,
479+ "rules" : {
480+ "tenant id is contained in [8, 2]" : {
481+ "when_match" : True ,
482+ "conditions" : [
483+ {
484+ "action" : RuleAction .KEY_NOT_IN_VALUE .value ,
485+ "key" : "tenant_id" ,
486+ "value" : ["6" , "4" ],
487+ }
488+ ],
489+ }
490+ },
491+ }
492+ }
493+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
494+ toggle = feature_flags .evaluate (name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" }, default = False )
495+ assert toggle == expected_value
496+
497+
498+ def test_flags_match_rule_with_value_in_key_action (mocker , config ):
499+ expected_value = True
500+ mocked_app_config_schema = {
501+ "my_feature" : {
502+ "default" : False ,
503+ "rules" : {
504+ "user is in the SYSADMIN group" : {
505+ "when_match" : expected_value ,
506+ "conditions" : [
507+ {
508+ "action" : RuleAction .VALUE_IN_KEY .value ,
509+ "key" : "groups" ,
510+ "value" : "SYSADMIN" ,
511+ }
512+ ],
513+ }
514+ },
515+ }
516+ }
517+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
518+ toggle = feature_flags .evaluate (
519+ name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" , "groups" : ["SYSADMIN" , "IT" ]}, default = False
520+ )
521+ assert toggle == expected_value
522+
523+
524+ def test_flags_no_match_rule_with_value_in_key_action (mocker , config ):
525+ expected_value = False
526+ mocked_app_config_schema = {
527+ "my_feature" : {
528+ "default" : expected_value ,
529+ "rules" : {
530+ "tenant id is contained in [8, 2]" : {
531+ "when_match" : True ,
532+ "conditions" : [
533+ {
534+ "action" : RuleAction .VALUE_IN_KEY .value ,
535+ "key" : "groups" ,
536+ "value" : "GUEST" ,
537+ }
538+ ],
539+ }
540+ },
541+ }
542+ }
543+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
544+ toggle = feature_flags .evaluate (
545+ name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" , "groups" : ["SYSADMIN" , "IT" ]}, default = False
546+ )
547+ assert toggle == expected_value
548+
549+
550+ def test_flags_match_rule_with_value_not_in_key_action (mocker , config ):
551+ expected_value = True
552+ mocked_app_config_schema = {
553+ "my_feature" : {
554+ "default" : False ,
555+ "rules" : {
556+ "user is in the GUEST group" : {
557+ "when_match" : expected_value ,
558+ "conditions" : [
559+ {
560+ "action" : RuleAction .VALUE_NOT_IN_KEY .value ,
561+ "key" : "groups" ,
562+ "value" : "GUEST" ,
563+ }
564+ ],
565+ }
566+ },
567+ }
568+ }
569+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
570+ toggle = feature_flags .evaluate (
571+ name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" , "groups" : ["SYSADMIN" , "IT" ]}, default = False
572+ )
573+ assert toggle == expected_value
574+
575+
576+ def test_flags_no_match_rule_with_value_not_in_key_action (mocker , config ):
577+ expected_value = False
578+ mocked_app_config_schema = {
579+ "my_feature" : {
580+ "default" : expected_value ,
581+ "rules" : {
582+ "user is in the SYSADMIN group" : {
583+ "when_match" : True ,
584+ "conditions" : [
585+ {
586+ "action" : RuleAction .VALUE_NOT_IN_KEY .value ,
587+ "key" : "groups" ,
588+ "value" : "SYSADMIN" ,
589+ }
590+ ],
591+ }
592+ },
593+ }
594+ }
595+ feature_flags = init_feature_flags (mocker , mocked_app_config_schema , config )
596+ toggle = feature_flags .evaluate (
597+ name = "my_feature" , context = {"tenant_id" : "6" , "username" : "a" , "groups" : ["SYSADMIN" , "IT" ]}, default = False
598+ )
599+ assert toggle == expected_value
600+
601+
602+ # Check multiple features
400603def test_multiple_features_enabled (mocker , config ):
401604 expected_value = ["my_feature" , "my_feature2" ]
402605 mocked_app_config_schema = {
0 commit comments