diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-05 16:54:15 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-05 16:54:15 +0300 |
commit | be834a25982746ffd85252ff502df42bb88cb9d5 (patch) | |
tree | b4d6a8ba0931e12fac08f05abea33a3b8ec2c8a2 /spec/lib/gitlab/experimentation_spec.rb | |
parent | ee925a3597f27e92f83a50937a64068109675b3d (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc32
Diffstat (limited to 'spec/lib/gitlab/experimentation_spec.rb')
-rw-r--r-- | spec/lib/gitlab/experimentation_spec.rb | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/spec/lib/gitlab/experimentation_spec.rb b/spec/lib/gitlab/experimentation_spec.rb index 9bc865f4d29..aecbc8734db 100644 --- a/spec/lib/gitlab/experimentation_spec.rb +++ b/spec/lib/gitlab/experimentation_spec.rb @@ -69,12 +69,26 @@ RSpec.describe Gitlab::Experimentation do end end + describe '#push_frontend_experiment' do + it 'pushes an experiment to the frontend' do + gon = instance_double('gon') + experiments = { experiments: { 'myExperiment' => true } } + + stub_experiment_for_user(my_experiment: true) + allow(controller).to receive(:gon).and_return(gon) + + expect(gon).to receive(:push).with(experiments, true) + + controller.push_frontend_experiment(:my_experiment) + end + end + describe '#experiment_enabled?' do subject { controller.experiment_enabled?(:test_experiment) } context 'cookie is not present' do - it 'calls Gitlab::Experimentation.enabled_for_user? with the name of the experiment and an experimentation_subject_index of nil' do - expect(Gitlab::Experimentation).to receive(:enabled_for_user?).with(:test_experiment, nil) + it 'calls Gitlab::Experimentation.enabled_for_value? with the name of the experiment and an experimentation_subject_index of nil' do + expect(Gitlab::Experimentation).to receive(:enabled_for_value?).with(:test_experiment, nil) controller.experiment_enabled?(:test_experiment) end end @@ -85,22 +99,22 @@ RSpec.describe Gitlab::Experimentation do get :index end - it 'calls Gitlab::Experimentation.enabled_for_user? with the name of the experiment and an experimentation_subject_index of the modulo 100 of the hex value of the uuid' do + it 'calls Gitlab::Experimentation.enabled_for_value? with the name of the experiment and an experimentation_subject_index of the modulo 100 of the hex value of the uuid' do # 'abcd1234'.hex % 100 = 76 - expect(Gitlab::Experimentation).to receive(:enabled_for_user?).with(:test_experiment, 76) + expect(Gitlab::Experimentation).to receive(:enabled_for_value?).with(:test_experiment, 76) controller.experiment_enabled?(:test_experiment) end end it 'returns true when DNT: 0 is set in the request' do - allow(Gitlab::Experimentation).to receive(:enabled_for_user?) { true } + allow(Gitlab::Experimentation).to receive(:enabled_for_value?) { true } controller.request.headers['DNT'] = '0' is_expected.to be_truthy end it 'returns false when DNT: 1 is set in the request' do - allow(Gitlab::Experimentation).to receive(:enabled_for_user?) { true } + allow(Gitlab::Experimentation).to receive(:enabled_for_value?) { true } controller.request.headers['DNT'] = '1' is_expected.to be_falsy @@ -336,8 +350,8 @@ RSpec.describe Gitlab::Experimentation do end end - describe '.enabled_for_user?' do - subject { described_class.enabled_for_user?(:test_experiment, experimentation_subject_index) } + describe '.enabled_for_value?' do + subject { described_class.enabled_for_value?(:test_experiment, experimentation_subject_index) } let(:experimentation_subject_index) { 9 } @@ -377,4 +391,32 @@ RSpec.describe Gitlab::Experimentation do end end end + + describe '.enabled_for_attribute?' do + subject { described_class.enabled_for_attribute?(:test_experiment, attribute) } + + let(:attribute) { 'abcd' } # Digest::SHA1.hexdigest('abcd').hex % 100 = 7 + + context 'experiment is disabled' do + before do + allow(described_class).to receive(:enabled?).and_return(false) + end + + it { is_expected.to be false } + end + + context 'experiment is enabled' do + before do + allow(described_class).to receive(:enabled?).and_return(true) + end + + it { is_expected.to be true } + + context 'outside enabled ratio' do + let(:attribute) { 'abc' } # Digest::SHA1.hexdigest('abc').hex % 100 = 17 + + it { is_expected.to be false } + end + end + end end |