diff options
Diffstat (limited to 'spec/controllers/concerns/redis_tracking_spec.rb')
-rw-r--r-- | spec/controllers/concerns/redis_tracking_spec.rb | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/spec/controllers/concerns/redis_tracking_spec.rb b/spec/controllers/concerns/redis_tracking_spec.rb new file mode 100644 index 00000000000..3795fca5576 --- /dev/null +++ b/spec/controllers/concerns/redis_tracking_spec.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe RedisTracking do + let(:event_name) { 'g_compliance_dashboard' } + let(:feature) { 'g_compliance_dashboard_feature' } + let(:user) { create(:user) } + + 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 + + def index + render html: 'index' + end + + def new + render html: 'new' + end + + def show + render html: 'show' + end + end + + context 'with feature disabled' do + it 'does not track the event' do + stub_feature_flags(feature => false) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + get :index + end + end + + context 'with usage ping 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) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + get :index + end + end + + context 'with feature enabled and usage ping 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 + sign_in(user) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) + + 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 + end + + context 'when user is not logged in and there is a visitor_id' do + let(:visitor_id) { SecureRandom.uuid } + + before do + routes.draw { get 'show' => 'anonymous#show' } + end + + it 'tracks the event' do + cookies[:visitor_id] = { value: visitor_id, expires: 24.months } + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) + + 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) + + 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 |