@@ -151,12 +151,29 @@ def test_activate__with_attributes__invalid_attributes(self):
151
151
def test_activate__experiment_not_running (self ):
152
152
""" Test that activate returns None when experiment is not Running. """
153
153
154
- with mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = True ),\
154
+ with mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = True ) as mock_audience_check ,\
155
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
156
+ return_value = True ) as mock_whitelist_check ,\
155
157
mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
156
158
return_value = False ) as mock_is_experiment_running :
157
159
self .assertIsNone (self .optimizely .activate ('test_experiment' , 'test_user' ,
158
160
attributes = {'test_attribute' : 'test_value' }))
159
161
mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
162
+ self .assertEqual (0 , mock_whitelist_check .call_count )
163
+ self .assertEqual (0 , mock_audience_check .call_count )
164
+
165
+ def test_activate__whitelisting_overrides_audience_check (self ):
166
+ """ Test that during activate whitelist overrides audience check if user is in the whitelist. """
167
+
168
+ with mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = False ) as mock_audience_check ,\
169
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
170
+ return_value = True ) as mock_whitelist_check ,\
171
+ mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
172
+ return_value = True ) as mock_is_experiment_running :
173
+ self .assertEqual ('control' , self .optimizely .activate ('test_experiment' , 'user_1' ))
174
+ mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
175
+ mock_whitelist_check .assert_called_once_with ({'user_1' : 'control' , 'user_2' : 'control' }, 'user_1' )
176
+ self .assertEqual (0 , mock_audience_check .call_count )
160
177
161
178
def test_activate__bucketer_returns_none (self ):
162
179
""" Test that activate returns None when user is in no variation. """
@@ -253,12 +270,35 @@ def test_track__experiment_not_running(self):
253
270
254
271
with mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
255
272
return_value = False ) as mock_is_experiment_running ,\
273
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
274
+ return_value = True ) as mock_whitelist_check ,\
275
+ mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = True ) as mock_audience_check ,\
256
276
mock .patch ('time.time' , return_value = 42 ),\
257
277
mock .patch ('optimizely.event_dispatcher.EventDispatcher.dispatch_event' ) as mock_dispatch_event :
258
278
self .optimizely .track ('test_event' , 'test_user' )
259
279
260
280
mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
261
281
self .assertEqual (0 , mock_dispatch_event .call_count )
282
+ self .assertEqual (0 , mock_whitelist_check .call_count )
283
+ self .assertEqual (0 , mock_audience_check .call_count )
284
+
285
+ def test_track__whitelisted_user_overrides_audience_check (self ):
286
+ """ Test that track does not check for user in audience when user is in whitelist. """
287
+
288
+ with mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
289
+ return_value = True ) as mock_is_experiment_running ,\
290
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
291
+ return_value = True ) as mock_whitelist_check ,\
292
+ mock .patch ('optimizely.helpers.audience.is_user_in_experiment' ,
293
+ return_value = False ) as mock_audience_check ,\
294
+ mock .patch ('time.time' , return_value = 42 ),\
295
+ mock .patch ('optimizely.event_dispatcher.EventDispatcher.dispatch_event' ) as mock_dispatch_event :
296
+ self .optimizely .track ('test_event' , 'user_1' )
297
+
298
+ mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
299
+ mock_whitelist_check .assert_called_once_with ({'user_1' : 'control' , 'user_2' : 'control' }, 'user_1' )
300
+ self .assertEqual (1 , mock_dispatch_event .call_count )
301
+ self .assertEqual (0 , mock_audience_check .call_count )
262
302
263
303
def test_get_variation__audience_match_and_experiment_running (self ):
264
304
""" Test that get variation retrieves expected variation
@@ -296,13 +336,31 @@ def test_get_variation__experiment_not_running(self):
296
336
return_value = self .project_config .get_variation_from_id (
297
337
'test_experiment' , '111129'
298
338
)) as mock_bucket ,\
339
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
340
+ return_value = True ) as mock_whitelist_check ,\
341
+ mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = True ) as mock_audience_check ,\
299
342
mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
300
343
return_value = False ) as mock_is_experiment_running :
301
344
self .assertIsNone (self .optimizely .get_variation ('test_experiment' , 'test_user' ,
302
345
attributes = {'test_attribute' : 'test_value' }))
303
346
304
347
self .assertEqual (0 , mock_bucket .call_count )
305
348
mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
349
+ self .assertEqual (0 , mock_whitelist_check .call_count )
350
+ self .assertEqual (0 , mock_audience_check .call_count )
351
+
352
+ def test_get_variation__whitelisting_overrides_audience_check (self ):
353
+ """ Test that in get_variation whitelist overrides audience check if user is in the whitelist. """
354
+
355
+ with mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = False ) as mock_audience_check ,\
356
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
357
+ return_value = True ) as mock_whitelist_check ,\
358
+ mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
359
+ return_value = True ) as mock_is_experiment_running :
360
+ self .assertEqual ('control' , self .optimizely .get_variation ('test_experiment' , 'user_1' ))
361
+ mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
362
+ self .assertEqual (1 , mock_whitelist_check .call_count )
363
+ self .assertEqual (0 , mock_audience_check .call_count )
306
364
307
365
def test_custom_logger (self ):
308
366
""" Test creating Optimizely object with a custom logger. """
@@ -443,7 +501,9 @@ def test_activate__with_attributes__invalid_attributes(self):
443
501
def test_activate__experiment_not_running (self ):
444
502
""" Test that activate returns None and does not dispatch event when experiment is not Running. """
445
503
446
- with mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = True ),\
504
+ with mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = True ) as mock_audience_check ,\
505
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
506
+ return_value = True ) as mock_whitelist_check ,\
447
507
mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
448
508
return_value = False ) as mock_is_experiment_running , \
449
509
mock .patch ('optimizely.bucketer.Bucketer.bucket' ) as mock_bucket ,\
@@ -452,9 +512,24 @@ def test_activate__experiment_not_running(self):
452
512
attributes = {'test_attribute' : 'test_value' }))
453
513
454
514
mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
515
+ self .assertEqual (0 , mock_audience_check .call_count )
516
+ self .assertEqual (0 , mock_whitelist_check .call_count )
455
517
self .assertEqual (0 , mock_bucket .call_count )
456
518
self .assertEqual (0 , mock_dispatch_event .call_count )
457
519
520
+ def test_activate__whitelisting_overrides_audience_check (self ):
521
+ """ Test that during activate whitelist overrides audience check if user is in the whitelist. """
522
+
523
+ with mock .patch ('optimizely.helpers.audience.is_user_in_experiment' , return_value = False ) as mock_audience_check ,\
524
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
525
+ return_value = True ) as mock_whitelist_check ,\
526
+ mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
527
+ return_value = True ) as mock_is_experiment_running :
528
+ self .assertEqual ('control' , self .optimizely .activate ('test_experiment' , 'user_1' ))
529
+ mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
530
+ self .assertEqual (1 , mock_whitelist_check .call_count )
531
+ self .assertEqual (0 , mock_audience_check .call_count )
532
+
458
533
def test_activate__bucketer_returns_none (self ):
459
534
""" Test that activate returns None and does not dispatch event when user is in no variation. """
460
535
@@ -595,6 +670,24 @@ def test_track__experiment_not_running(self):
595
670
mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
596
671
self .assertEqual (0 , mock_dispatch_event .call_count )
597
672
673
+ def test_track__whitelisted_user_overrides_audience_check (self ):
674
+ """ Test that track does not check for user in audience when user is in whitelist. """
675
+
676
+ with mock .patch ('optimizely.helpers.experiment.is_experiment_running' ,
677
+ return_value = True ) as mock_is_experiment_running ,\
678
+ mock .patch ('optimizely.helpers.experiment.is_user_in_forced_variation' ,
679
+ return_value = True ) as mock_whitelist_check ,\
680
+ mock .patch ('optimizely.helpers.audience.is_user_in_experiment' ,
681
+ return_value = False ) as mock_audience_check ,\
682
+ mock .patch ('time.time' , return_value = 42 ),\
683
+ mock .patch ('optimizely.event_dispatcher.EventDispatcher.dispatch_event' ) as mock_dispatch_event :
684
+ self .optimizely .track ('test_event' , 'user_1' )
685
+
686
+ mock_is_experiment_running .assert_called_once_with (self .project_config .get_experiment_from_key ('test_experiment' ))
687
+ mock_whitelist_check .assert_called_once_with ({'user_1' : 'control' , 'user_2' : 'control' }, 'user_1' )
688
+ self .assertEqual (1 , mock_dispatch_event .call_count )
689
+ self .assertEqual (0 , mock_audience_check .call_count )
690
+
598
691
599
692
class OptimizelyWithExceptionTest (base .BaseTestV1 ):
600
693
0 commit comments