Commit 25cca741 authored by alinamihaila's avatar alinamihaila

Move helper in controller concern

parent f73a06a6
# frozen_string_literal: true
module RedisTrackingHelper
# Example:
#
# # In controller include module
# # Track event for index action
#
# include RedisTracking
#
# track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score', feature: :my_feature
module RedisTracking
extend ActiveSupport::Concern
# Example:
#
# # In controller include helper
# # Track event for index action
#
# include RedisTrackingHelper
#
# track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score', feature: :my_feature
#
class_methods do
def track_redis_hll_events(*controller_actions, name:, feature:)
def track_redis_hll_event(*controller_actions, name:, feature:)
after_action only: controller_actions, if: -> { request.format.html? && request.headers['DNT'] != '1' } do
track_unique_redis_hll_event(name, feature)
end
......
# 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) }
let(:visitor_id) { 'b77218e4-eeb1-4e41-ba0e-c1354ea49f7a' }
let(:controller_class) do
Class.new do
include RedisTracking
end
end
let(:controller) { controller_class.new }
before do
allow(controller).to receive(:current_user).and_return(:user)
end
describe '.track_unique_redis_hll_event' do
it 'does not track event if feature flag disabled' do
stub_feature_flags(feature => false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
controller.track_unique_redis_hll_event(event_name, feature)
end
it 'does not track the event when usage ping is disabled' do
stub_feature_flags(feature => true)
allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(false)
allow(controller).to receive(:cookies).and_return({ visitor_id: visitor_id })
allow(controller).to receive(:current_user).and_return(nil)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(visitor_id, event_name)
controller.track_unique_redis_hll_event(event_name, feature)
end
it 'does not track the event when there is no cookie and user is not logged in' do
stub_feature_flags(feature => true)
allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(true)
allow(controller).to receive(:cookies).and_return({})
allow(controller).to receive(:current_user).and_return(nil)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event).with(visitor_id, event_name)
controller.track_unique_redis_hll_event(event_name, feature)
end
it 'tracks the event with visitor_id and no user' do
stub_feature_flags(feature => true)
allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(true)
allow(controller).to receive(:cookies).and_return({ visitor_id: visitor_id })
allow(controller).to receive(:current_user).and_return(nil)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(visitor_id, event_name)
controller.track_unique_redis_hll_event(event_name, feature)
end
it 'tracks the event with visitor_id and user' do
stub_feature_flags(feature => true)
allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(true)
allow(controller).to receive(:cookies).and_return({ visitor_id: visitor_id })
allow(controller).to receive(:current_user).and_return(:user)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(visitor_id, event_name)
controller.track_unique_redis_hll_event(event_name, feature)
end
end
end
# frozen_string_literal: true
require "spec_helper"
RSpec.describe RedisTrackingHelper do
include Devise::Test::ControllerHelpers
describe '.track_unique_redis_hll_event' do
let(:event_name) { 'g_compliance_dashboard' }
let(:feature) { 'g_compliance_dashboard_feature' }
let(:current_user) { create(:user) }
before do
stub_feature_flags(feature => true)
end
it 'does not track event if feature flag disabled' do
stub_feature_flags(feature => false)
sign_in(current_user)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
helper.track_unique_redis_hll_event(event_name, feature)
end
it 'does not track event if usage ping is disabled' do
sign_in(current_user)
expect(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(false)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
helper.track_unique_redis_hll_event(event_name, feature)
end
it 'does not track event if user is not logged in' do
expect_any_instance_of(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
helper.track_unique_redis_hll_event(event_name, feature)
end
it 'tracks event if user is logged in' do
sign_in(current_user)
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
helper.track_unique_redis_hll_event(event_name, feature)
end
it 'tracks event if user is not logged in, but has the cookie already' do
helper.request.cookies[:visitor_id] = { value: SecureRandom.uuid, expires: 24.months }
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
helper.track_unique_redis_hll_event(event_name, feature)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment