2020-08-26 02:10:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
RSpec.describe RedisTracking do
|
2021-08-05 23:08:43 -04:00
|
|
|
include TrackingHelpers
|
|
|
|
|
2020-08-26 02:10:34 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
controller(ApplicationController) do
|
|
|
|
include RedisTracking
|
|
|
|
|
|
|
|
skip_before_action :authenticate_user!, only: :show
|
2022-08-31 05:13:12 -04:00
|
|
|
track_redis_hll_event(:index, :show,
|
|
|
|
name: 'g_compliance_approval_rules',
|
2021-03-17 20:08:58 -04:00
|
|
|
if: [:custom_condition_one?, :custom_condition_two?]) { |controller| controller.get_custom_id }
|
2020-08-26 02:10:34 -04:00
|
|
|
|
|
|
|
def index
|
|
|
|
render html: 'index'
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
render html: 'new'
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render html: 'show'
|
|
|
|
end
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-03-17 20:08:58 -04:00
|
|
|
def get_custom_id
|
|
|
|
'some_custom_id'
|
|
|
|
end
|
|
|
|
|
2020-10-14 20:08:42 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def custom_condition_one?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_condition_two?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_tracking
|
|
|
|
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
|
2021-01-07 19:10:44 -05:00
|
|
|
.with('g_compliance_approval_rules', values: instance_of(String))
|
2020-10-14 20:08:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def expect_no_tracking
|
|
|
|
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
|
2020-08-26 02:10:34 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
context 'when user is logged in' do
|
2020-08-26 02:10:34 -04:00
|
|
|
before do
|
2021-02-11 10:09:11 -05:00
|
|
|
sign_in(user)
|
2020-08-26 02:10:34 -04:00
|
|
|
end
|
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
it 'tracks the event' do
|
|
|
|
expect_tracking
|
2020-09-08 11:08:41 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
get :index
|
|
|
|
end
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
it 'tracks the event if DNT is not enabled' do
|
2021-08-05 23:08:43 -04:00
|
|
|
stub_do_not_track('0')
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
expect_tracking
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
get :index
|
|
|
|
end
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
it 'does not track the event if DNT is enabled' do
|
2021-08-05 23:08:43 -04:00
|
|
|
stub_do_not_track('1')
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
expect_no_tracking
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
get :index
|
|
|
|
end
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
it 'does not track the event if the format is not HTML' do
|
|
|
|
expect_no_tracking
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
get :index, format: :json
|
|
|
|
end
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
it 'does not track the event if a custom condition returns false' do
|
|
|
|
expect(controller).to receive(:custom_condition_two?).and_return(false)
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
expect_no_tracking
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
get :index
|
|
|
|
end
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
it 'does not track the event for untracked actions' do
|
|
|
|
expect_no_tracking
|
2020-10-14 20:08:42 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
get :new
|
2020-08-26 02:10:34 -04:00
|
|
|
end
|
2021-02-11 10:09:11 -05:00
|
|
|
end
|
2020-08-26 02:10:34 -04:00
|
|
|
|
2021-03-17 20:08:58 -04:00
|
|
|
context 'when user is not logged in' do
|
2021-02-11 10:09:11 -05:00
|
|
|
let(:visitor_id) { SecureRandom.uuid }
|
2020-08-26 02:10:34 -04:00
|
|
|
|
2021-03-17 20:08:58 -04:00
|
|
|
it 'tracks the event when there is a visitor id' do
|
2021-02-11 10:09:11 -05:00
|
|
|
cookies[:visitor_id] = { value: visitor_id, expires: 24.months }
|
2020-08-26 02:10:34 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
expect_tracking
|
2020-08-26 02:10:34 -04:00
|
|
|
|
2021-03-17 20:08:58 -04:00
|
|
|
get :show, params: { id: 1 }
|
2020-08-26 02:10:34 -04:00
|
|
|
end
|
2021-02-11 10:09:11 -05:00
|
|
|
end
|
2020-08-26 02:10:34 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
context 'when user is not logged in and there is no visitor_id' do
|
|
|
|
it 'does not track the event' do
|
|
|
|
expect_no_tracking
|
2020-08-26 02:10:34 -04:00
|
|
|
|
2021-02-11 10:09:11 -05:00
|
|
|
get :index
|
2020-08-26 02:10:34 -04:00
|
|
|
end
|
2021-03-17 20:08:58 -04:00
|
|
|
|
|
|
|
it 'tracks the event when there is custom id' do
|
|
|
|
expect_tracking
|
|
|
|
|
|
|
|
get :show, params: { id: 1 }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not track the event when there is no custom id' do
|
|
|
|
expect(controller).to receive(:get_custom_id).and_return(nil)
|
|
|
|
|
|
|
|
expect_no_tracking
|
|
|
|
|
|
|
|
get :show, params: { id: 2 }
|
|
|
|
end
|
2020-08-26 02:10:34 -04:00
|
|
|
end
|
|
|
|
end
|