diff options
Diffstat (limited to 'spec/controllers/concerns/redis_tracking_spec.rb')
-rw-r--r-- | spec/controllers/concerns/redis_tracking_spec.rb | 99 |
1 files changed, 69 insertions, 30 deletions
diff --git a/spec/controllers/concerns/redis_tracking_spec.rb b/spec/controllers/concerns/redis_tracking_spec.rb index 3795fca5576..831f5ad7bb1 100644 --- a/spec/controllers/concerns/redis_tracking_spec.rb +++ b/spec/controllers/concerns/redis_tracking_spec.rb @@ -3,15 +3,19 @@ require "spec_helper" RSpec.describe RedisTracking do - let(:event_name) { 'g_compliance_dashboard' } - let(:feature) { 'g_compliance_dashboard_feature' } + let(:feature) { 'approval_rule' } let(:user) { create(:user) } + before do + skip_feature_flags_yaml_validation + end + controller(ApplicationController) do include RedisTracking skip_before_action :authenticate_user!, only: :show - track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score', feature: :g_compliance_dashboard_feature, feature_default_enabled: true + track_redis_hll_event :index, :show, name: 'g_compliance_approval_rules', feature: :approval_rule, feature_default_enabled: true, + if: [:custom_condition_one?, :custom_condition_two?] def index render html: 'index' @@ -24,51 +28,94 @@ RSpec.describe RedisTracking do def show render html: 'show' end - end - context 'with feature disabled' do - it 'does not track the event' do - stub_feature_flags(feature => false) + private - expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + def custom_condition_one? + true + end - get :index + def custom_condition_two? + true end end - context 'with usage ping disabled' do + def expect_tracking + expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) + .with(instance_of(String), 'g_compliance_approval_rules') + end + + def expect_no_tracking + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + end + + context 'with feature disabled' do it 'does not track the event' do - stub_feature_flags(feature => true) - allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(false) + stub_feature_flags(feature => false) - expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + expect_no_tracking get :index end end - context 'with feature enabled and usage ping enabled' do + context 'with feature enabled' do before do stub_feature_flags(feature => true) - allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(true) end context 'when user is logged in' do - it 'tracks the event' do + before do sign_in(user) + end - expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) + it 'tracks the event' do + expect_tracking get :index end it 'passes default_enabled flag' do - sign_in(user) - expect(controller).to receive(:metric_feature_enabled?).with(feature.to_sym, true) get :index end + + it 'tracks the event if DNT is not enabled' do + request.headers['DNT'] = '0' + + expect_tracking + + get :index + end + + it 'does not track the event if DNT is enabled' do + request.headers['DNT'] = '1' + + expect_no_tracking + + get :index + end + + it 'does not track the event if the format is not HTML' do + expect_no_tracking + + get :index, format: :json + end + + it 'does not track the event if a custom condition returns false' do + expect(controller).to receive(:custom_condition_two?).and_return(false) + + expect_no_tracking + + get :index + end + + it 'does not track the event for untracked actions' do + expect_no_tracking + + get :new + end end context 'when user is not logged in and there is a visitor_id' do @@ -81,26 +128,18 @@ RSpec.describe RedisTracking do it 'tracks the event' do cookies[:visitor_id] = { value: visitor_id, expires: 24.months } - expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) + expect_tracking get :show end end context 'when user is not logged in and there is no visitor_id' do - it 'does not tracks the event' do - expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + it 'does not track the event' do + expect_no_tracking get :index end end - - context 'for untracked action' do - it 'does not tracks the event' do - expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) - - get :new - end - end end end |