@@ -1143,6 +1143,30 @@ def test_track__invalid_object(self):
1143
1143
1144
1144
mock_client_logging .error .assert_called_once_with ('Datafile has invalid format. Failing "track".' )
1145
1145
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
+
1146
1170
def test_get_variation__invalid_object (self ):
1147
1171
""" Test that get_variation logs error if Optimizely object is not created correctly. """
1148
1172
@@ -1153,7 +1177,7 @@ def test_get_variation__invalid_object(self):
1153
1177
1154
1178
mock_client_logging .error .assert_called_once_with ('Datafile has invalid format. Failing "get_variation".' )
1155
1179
1156
- def test_get_variation_invalid_experiment_key (self ):
1180
+ def test_get_variation_unknown_experiment_key (self ):
1157
1181
""" Test that get_variation retuns None when invalid experiment key is given. """
1158
1182
with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging :
1159
1183
self .optimizely .get_variation ('aabbccdd' , 'test_user' , None )
@@ -1162,33 +1186,37 @@ def test_get_variation_invalid_experiment_key(self):
1162
1186
'Experiment key "aabbccdd" is invalid. Not activating user "test_user".'
1163
1187
)
1164
1188
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 . """
1167
1191
1168
1192
opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
1169
1193
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 :
1171
1196
self .assertFalse (opt_obj .is_feature_enabled (None , 'test_user' ))
1172
1197
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.' )
1174
1200
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 . """
1177
1203
1178
1204
opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
1179
1205
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 ))
1182
1209
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.' )
1184
1212
1185
1213
def test_is_feature_enabled__returns_false_for_invalid_feature (self ):
1186
1214
""" Test that the feature is not enabled for the user if the provided feature key is invalid. """
1187
1215
1188
1216
opt_obj = optimizely .Optimizely (json .dumps (self .config_dict_with_features ))
1189
1217
1190
1218
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 :
1192
1220
self .assertFalse (opt_obj .is_feature_enabled ('invalid_feature' , 'user1' ))
1193
1221
1194
1222
self .assertFalse (mock_decision .called )
@@ -1462,6 +1490,14 @@ def side_effect(*args, **kwargs):
1462
1490
mock_is_feature_enabled .assert_any_call ('test_feature_in_group' , 'user_1' , None )
1463
1491
mock_is_feature_enabled .assert_any_call ('test_feature_in_experiment_and_rollout' , 'user_1' , None )
1464
1492
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
+
1465
1501
def test_get_enabled_features__invalid_object (self ):
1466
1502
""" Test that get_enabled_features returns empty list if Optimizely object is not valid. """
1467
1503
@@ -2003,6 +2039,52 @@ def test_get_variation__invalid_attributes(self):
2003
2039
2004
2040
mock_client_logging .error .assert_called_once_with ('Provided attributes are in an invalid format.' )
2005
2041
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
+
2006
2088
def test_activate__invalid_attributes (self ):
2007
2089
""" Test that expected log messages are logged during activate when attributes are in invalid format. """
2008
2090
with mock .patch .object (self .optimizely , 'logger' ) as mock_client_logging :
0 commit comments