2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
RSpec.describe RedisTracking do
|
2021-01-03 14:25:43 +05:30
|
|
|
let(:feature) { 'approval_rule' }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
before do
|
|
|
|
skip_feature_flags_yaml_validation
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
controller(ApplicationController) do
|
|
|
|
include RedisTracking
|
|
|
|
|
|
|
|
skip_before_action :authenticate_user!, only: :show
|
2021-01-03 14:25:43 +05:30
|
|
|
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?]
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def index
|
|
|
|
render html: 'index'
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
render html: 'new'
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render html: 'show'
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
private
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def custom_condition_one?
|
|
|
|
true
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def custom_condition_two?
|
|
|
|
true
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def expect_tracking
|
|
|
|
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
|
2021-03-08 18:12:59 +05:30
|
|
|
.with('g_compliance_approval_rules', values: instance_of(String))
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def expect_no_tracking
|
|
|
|
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with feature disabled' do
|
2020-11-24 15:15:51 +05:30
|
|
|
it 'does not track the event' do
|
2021-01-03 14:25:43 +05:30
|
|
|
stub_feature_flags(feature => false)
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect_no_tracking
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
get :index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'with feature enabled' do
|
2020-11-24 15:15:51 +05:30
|
|
|
before do
|
|
|
|
stub_feature_flags(feature => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is logged in' do
|
2021-01-03 14:25:43 +05:30
|
|
|
before do
|
2020-11-24 15:15:51 +05:30
|
|
|
sign_in(user)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'tracks the event' do
|
|
|
|
expect_tracking
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
get :index
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes default_enabled flag' do
|
|
|
|
expect(controller).to receive(:metric_feature_enabled?).with(feature.to_sym, true)
|
|
|
|
|
|
|
|
get :index
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
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
|
2020-11-24 15:15:51 +05:30
|
|
|
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 }
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
expect_tracking
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
get :show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not logged in and there is no visitor_id' do
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'does not track the event' do
|
|
|
|
expect_no_tracking
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
get :index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|