|
1 | 1 | require 'spec_helper'
|
2 | 2 | require 'optimizely'
|
| 3 | +require 'optimizely/audience' |
3 | 4 | require 'optimizely/helpers/validator'
|
4 | 5 | require 'optimizely/exceptions'
|
5 | 6 | require 'optimizely/version'
|
@@ -98,8 +99,8 @@ class InvalidErrorHandler; end
|
98 | 99 | allow(project_instance.bucketer).to receive(:bucket).and_return('111128')
|
99 | 100 | allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
|
100 | 101 | allow(project_instance.config).to receive(:get_audience_ids_for_experiment)
|
101 |
| - .with('test_experiment') |
102 |
| - .and_return([]) |
| 102 | + .with('test_experiment') |
| 103 | + .and_return([]) |
103 | 104 |
|
104 | 105 | stub_request(:get, log_url).with(:query => params)
|
105 | 106 |
|
@@ -506,6 +507,41 @@ class InvalidErrorHandler; end
|
506 | 507 | expect { project_instance.activate('test_experiment', 'test_user', 'invalid') }
|
507 | 508 | .to raise_error(Optimizely::InvalidAttributeFormatError)
|
508 | 509 | end
|
| 510 | + |
| 511 | + it 'should override the audience check if the user is whitelisted to a specific variation' do |
| 512 | + params = { |
| 513 | + 'projectId' => '111001', |
| 514 | + 'accountId' => '12001', |
| 515 | + 'visitorId' => 'forced_audience_user', |
| 516 | + 'userFeatures' => [ |
| 517 | + { |
| 518 | + 'id' => '111094', |
| 519 | + 'name' => 'browser_type', |
| 520 | + 'type' => 'custom', |
| 521 | + 'value' => 'wrong_browser', |
| 522 | + 'shouldIndex' => true, |
| 523 | + }, |
| 524 | + ], |
| 525 | + 'clientEngine' => 'ruby-sdk', |
| 526 | + 'clientVersion' => version, |
| 527 | + 'timestamp' => (time_now.to_f * 1000).to_i, |
| 528 | + 'isGlobalHoldback' => false, |
| 529 | + 'layerId' => '3', |
| 530 | + 'decision' => { |
| 531 | + 'variationId' => '122229', |
| 532 | + 'experimentId' => '122227', |
| 533 | + 'isLayerHoldback' => false, |
| 534 | + } |
| 535 | + } |
| 536 | + |
| 537 | + allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) |
| 538 | + allow(Optimizely::Audience).to receive(:user_in_experiment?) |
| 539 | + |
| 540 | + expect(project_instance.activate('test_experiment_with_audience', 'forced_audience_user', 'browser_type' => 'wrong_browser')) |
| 541 | + .to eq('variation_with_audience') |
| 542 | + expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, params, post_headers)).once |
| 543 | + expect(Optimizely::Audience).to_not have_received(:user_in_experiment?) |
| 544 | + end |
509 | 545 | end
|
510 | 546 |
|
511 | 547 | describe '#track' do
|
@@ -681,6 +717,48 @@ class InvalidErrorHandler; end
|
681 | 717 | expect { project_instance.track('invalid_event', 'test_user') }.to raise_error(Optimizely::InvalidGoalError)
|
682 | 718 | expect(project_instance.event_dispatcher).to_not have_received(:dispatch_event)
|
683 | 719 | end
|
| 720 | + |
| 721 | + it 'should override the audience check if the user is whitelisted to a specific variation' do |
| 722 | + params = { |
| 723 | + 'projectId' => '111001', |
| 724 | + 'accountId' => '12001', |
| 725 | + 'visitorId' => 'forced_audience_user', |
| 726 | + 'userFeatures' => [ |
| 727 | + { |
| 728 | + 'id' => '111094', |
| 729 | + 'name' => 'browser_type', |
| 730 | + 'type' => 'custom', |
| 731 | + 'value' => 'wrong_browser', |
| 732 | + 'shouldIndex' => true, |
| 733 | + } |
| 734 | + ], |
| 735 | + 'clientEngine' => 'ruby-sdk', |
| 736 | + 'clientVersion' => version, |
| 737 | + 'timestamp' => (time_now.to_f * 1000).to_i, |
| 738 | + 'isGlobalHoldback' => false, |
| 739 | + 'eventEntityId' => '111097', |
| 740 | + 'eventFeatures' => [], |
| 741 | + 'eventName' => 'test_event_with_audience', |
| 742 | + 'eventMetrics' => [], |
| 743 | + 'layerStates' => [ |
| 744 | + { |
| 745 | + 'layerId' => '3', |
| 746 | + 'decision' => { |
| 747 | + 'variationId' => '122229', |
| 748 | + 'experimentId' => '122227', |
| 749 | + 'isLayerHoldback' => false, |
| 750 | + }, |
| 751 | + 'actionTriggered' => true, |
| 752 | + } |
| 753 | + ] |
| 754 | + } |
| 755 | + allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) |
| 756 | + allow(Optimizely::Audience).to receive(:user_in_experiment?) |
| 757 | + |
| 758 | + project_instance.track('test_event_with_audience', 'forced_audience_user', 'browser_type' => 'wrong_browser') |
| 759 | + expect(Optimizely::Audience).to_not have_received(:user_in_experiment?) |
| 760 | + expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, conversion_log_url, params, post_headers)).once |
| 761 | + end |
684 | 762 | end
|
685 | 763 |
|
686 | 764 | describe '#get_variation' do
|
@@ -712,5 +790,13 @@ class InvalidErrorHandler; end
|
712 | 790 | expect { project_instance.get_variation('test_experiment', 'test_user', 'invalid') }
|
713 | 791 | .to raise_error(Optimizely::InvalidAttributeFormatError)
|
714 | 792 | end
|
| 793 | + |
| 794 | + it 'should override the audience check if the user is whitelisted to a specific variation' do |
| 795 | + allow(Optimizely::Audience).to receive(:user_in_experiment?) |
| 796 | + |
| 797 | + expect(project_instance.get_variation('test_experiment_with_audience', 'forced_audience_user', 'browser_type' => 'wrong_browser')) |
| 798 | + .to eq('variation_with_audience') |
| 799 | + expect(Optimizely::Audience).to_not have_received(:user_in_experiment?) |
| 800 | + end |
715 | 801 | end
|
716 | 802 | end
|
0 commit comments