@@ -1143,6 +1143,30 @@ def test_track__invalid_object(self):
11431143
11441144 mock_client_logging .error .assert_called_once_with ('Datafile has invalid format. Failing "track".' )
11451145
1146+ def test_track__invalid_experiment_key (self ):
1147+ """ Test that None is returned and expected log messages are logged during track \
1148+ when exp_key is in invalid format. """
1149+
1150+ with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging , \
1151+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , return_value = False ) as mock_validator :
1152+ self .assertIsNone (self .optimizely .track (99 , 'test_user' ))
1153+
1154+ mock_validator .assert_any_call (99 )
1155+
1156+ mock_client_logging .error .assert_called_once_with ('Provided "event_key" is in an invalid format.' )
1157+
1158+ def test_track__invalid_user_id (self ):
1159+ """ Test that None is returned and expected log messages are logged during track \
1160+ when user_id is in invalid format. """
1161+
1162+ with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging , \
1163+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , side_effect = [True , False ]) as mock_validator :
1164+ self .assertIsNone (self .optimizely .track ('test_event' , 99 ))
1165+
1166+ mock_validator .assert_any_call (99 )
1167+
1168+ mock_client_logging .error .assert_called_once_with ('Provided "user_id" is in an invalid format.' )
1169+
11461170 def test_get_variation__invalid_object (self ):
11471171 """ Test that get_variation logs error if Optimizely object is not created correctly. """
11481172
@@ -1153,7 +1177,7 @@ def test_get_variation__invalid_object(self):
11531177
11541178 mock_client_logging .error .assert_called_once_with ('Datafile has invalid format. Failing "get_variation".' )
11551179
1156- def test_get_variation_invalid_experiment_key (self ):
1180+ def test_get_variation_unknown_experiment_key (self ):
11571181 """ Test that get_variation retuns None when invalid experiment key is given. """
11581182 with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging :
11591183 self .optimizely .get_variation ('aabbccdd' , 'test_user' , None )
@@ -1162,33 +1186,37 @@ def test_get_variation_invalid_experiment_key(self):
11621186 'Experiment key "aabbccdd" is invalid. Not activating user "test_user".'
11631187 )
11641188
1165- def test_is_feature_enabled__returns_false_for_none_feature_key (self ):
1166- """ Test that is_feature_enabled returns false if the provided feature key is None . """
1189+ def test_is_feature_enabled__returns_false_for_invalid_feature_key (self ):
1190+ """ Test that is_feature_enabled returns false if the provided feature key is invalid . """
11671191
11681192 opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
11691193
1170- with mock .patch .object (opt_obj , 'logger' ) as mock_client_logging :
1194+ with mock .patch .object (opt_obj , 'logger' ) as mock_client_logging ,\
1195+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , return_value = False ) as mock_validator :
11711196 self .assertFalse (opt_obj .is_feature_enabled (None , 'test_user' ))
11721197
1173- mock_client_logging .error .assert_called_once_with (enums .Errors .NONE_FEATURE_KEY_PARAMETER )
1198+ mock_validator .assert_any_call (None )
1199+ mock_client_logging .error .assert_called_with ('Provided "feature_key" is in an invalid format.' )
11741200
1175- def test_is_feature_enabled__returns_false_for_none_user_id (self ):
1176- """ Test that is_feature_enabled returns false if the provided user ID is None . """
1201+ def test_is_feature_enabled__returns_false_for_invalid_user_id (self ):
1202+ """ Test that is_feature_enabled returns false if the provided user ID is invalid . """
11771203
11781204 opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
11791205
1180- with mock .patch .object (opt_obj , 'logger' ) as mock_client_logging :
1181- self .assertFalse (opt_obj .is_feature_enabled ('feature_key' , None ))
1206+ with mock .patch .object (opt_obj , 'logger' ) as mock_client_logging ,\
1207+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , side_effect = [True , False ]) as mock_validator :
1208+ self .assertFalse (opt_obj .is_feature_enabled ('feature_key' , 1.2 ))
11821209
1183- mock_client_logging .error .assert_called_once_with (enums .Errors .NONE_USER_ID_PARAMETER )
1210+ mock_validator .assert_any_call (1.2 )
1211+ mock_client_logging .error .assert_called_with ('Provided "user_id" is in an invalid format.' )
11841212
11851213 def test_is_feature_enabled__returns_false_for_invalid_feature (self ):
11861214 """ Test that the feature is not enabled for the user if the provided feature key is invalid. """
11871215
11881216 opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
11891217
11901218 with mock .patch ('optimizely.decision_service.DecisionService.get_variation_for_feature' ) as mock_decision , \
1191- mock .patch ('optimizely.event_dispatcher.EventDispatcher.dispatch_event' ) as mock_dispatch_event :
1219+ mock .patch ('optimizely.event_dispatcher.EventDispatcher.dispatch_event' ) as mock_dispatch_event :
11921220 self .assertFalse (opt_obj .is_feature_enabled ('invalid_feature' , 'user1' ))
11931221
11941222 self .assertFalse (mock_decision .called )
@@ -1462,6 +1490,14 @@ def side_effect(*args, **kwargs):
14621490 mock_is_feature_enabled .assert_any_call ('test_feature_in_group' , 'user_1' , None )
14631491 mock_is_feature_enabled .assert_any_call ('test_feature_in_experiment_and_rollout' , 'user_1' , None )
14641492
1493+ def test_get_enabled_features_invalid_user_id (self ):
1494+ with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging , \
1495+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , return_value = False ) as mock_validator :
1496+ self .optimizely .get_enabled_features (1.2 )
1497+
1498+ mock_validator .assert_any_call (1.2 )
1499+ mock_client_logging .error .assert_called_once_with ('Provided "user_id" is in an invalid format.' )
1500+
14651501 def test_get_enabled_features__invalid_object (self ):
14661502 """ Test that get_enabled_features returns empty list if Optimizely object is not valid. """
14671503
@@ -2003,6 +2039,52 @@ def test_get_variation__invalid_attributes(self):
20032039
20042040 mock_client_logging .error .assert_called_once_with ('Provided attributes are in an invalid format.' )
20052041
2042+ def test_get_variation__invalid_experiment_key (self ):
2043+ """ Test that None is returned and expected log messages are logged during get_variation \
2044+ when exp_key is in invalid format. """
2045+
2046+ with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging ,\
2047+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , return_value = False ) as mock_validator :
2048+ self .assertIsNone (self .optimizely .get_variation (99 , 'test_user' ))
2049+
2050+ mock_validator .assert_any_call (99 )
2051+ mock_client_logging .error .assert_called_once_with ('Provided "experiment_key" is in an invalid format.' )
2052+
2053+ def test_get_variation__invalid_user_id (self ):
2054+ """ Test that None is returned and expected log messages are logged during get_variation \
2055+ when user_id is in invalid format. """
2056+
2057+ with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging ,\
2058+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , side_effect = [True , False ]) as mock_validator :
2059+ self .assertIsNone (self .optimizely .get_variation ('test_experiment' , 99 ))
2060+
2061+ mock_validator .assert_any_call (99 )
2062+ mock_client_logging .error .assert_called_once_with ('Provided "user_id" is in an invalid format.' )
2063+
2064+ def test_activate__invalid_experiment_key (self ):
2065+ """ Test that None is returned and expected log messages are logged during activate \
2066+ when exp_key is in invalid format. """
2067+
2068+ with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging ,\
2069+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , return_value = False ) as mock_validator :
2070+ self .assertIsNone (self .optimizely .activate (99 , 'test_user' ))
2071+
2072+ mock_validator .assert_any_call (99 )
2073+
2074+ mock_client_logging .error .assert_called_once_with ('Provided "experiment_key" is in an invalid format.' )
2075+
2076+ def test_activate__invalid_user_id (self ):
2077+ """ Test that None is returned and expected log messages are logged during activate \
2078+ when user_id is in invalid format. """
2079+
2080+ with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging ,\
2081+ mock .patch ('optimizely.helpers.validator.is_non_empty_string' , side_effect = [True , False ]) as mock_validator :
2082+ self .assertIsNone (self .optimizely .activate ('test_experiment' , 99 ))
2083+
2084+ mock_validator .assert_any_call (99 )
2085+
2086+ mock_client_logging .error .assert_called_once_with ('Provided "user_id" is in an invalid format.' )
2087+
20062088 def test_activate__invalid_attributes (self ):
20072089 """ Test that expected log messages are logged during activate when attributes are in invalid format. """
20082090 with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging :
0 commit comments