Commit 2175002e authored by Patrick Bajao's avatar Patrick Bajao

Implement stub_do_not_track helper

This helper can be used whenever there's a need to stub the DNT
request header.

The `TrackingHelpers` needs to be included in a spec for it to
work.
parent a960285b
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require "spec_helper" require "spec_helper"
RSpec.describe RedisTracking do RSpec.describe RedisTracking do
include TrackingHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
controller(ApplicationController) do controller(ApplicationController) do
...@@ -60,7 +62,7 @@ RSpec.describe RedisTracking do ...@@ -60,7 +62,7 @@ RSpec.describe RedisTracking do
end end
it 'tracks the event if DNT is not enabled' do it 'tracks the event if DNT is not enabled' do
request.headers['DNT'] = '0' stub_do_not_track('0')
expect_tracking expect_tracking
...@@ -68,7 +70,7 @@ RSpec.describe RedisTracking do ...@@ -68,7 +70,7 @@ RSpec.describe RedisTracking do
end end
it 'does not track the event if DNT is enabled' do it 'does not track the event if DNT is enabled' do
request.headers['DNT'] = '1' stub_do_not_track('1')
expect_no_tracking expect_no_tracking
......
...@@ -4,6 +4,7 @@ require 'spec_helper' ...@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe Projects::MergeRequests::DiffsController do RSpec.describe Projects::MergeRequests::DiffsController do
include ProjectForksHelper include ProjectForksHelper
include TrackingHelpers
shared_examples '404 for unexistent diffable' do shared_examples '404 for unexistent diffable' do
context 'when diffable does not exists' do context 'when diffable does not exists' do
...@@ -447,7 +448,7 @@ RSpec.describe Projects::MergeRequests::DiffsController do ...@@ -447,7 +448,7 @@ RSpec.describe Projects::MergeRequests::DiffsController do
context 'when DNT is enabled' do context 'when DNT is enabled' do
before do before do
request.headers['DNT'] = '1' stub_do_not_track('1')
end end
it 'does not track any mr_diffs event' do it 'does not track any mr_diffs event' do
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
include TrackingHelpers
before do before do
stub_const('Gitlab::Experimentation::EXPERIMENTS', { stub_const('Gitlab::Experimentation::EXPERIMENTS', {
backwards_compatible_test_experiment: { backwards_compatible_test_experiment: {
...@@ -43,7 +45,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do ...@@ -43,7 +45,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
let(:cookie_value) { nil } let(:cookie_value) { nil }
before do before do
request.headers['DNT'] = do_not_track if do_not_track.present? stub_do_not_track(do_not_track) if do_not_track.present?
request.cookies[:experimentation_subject_id] = cookie_value if cookie_value request.cookies[:experimentation_subject_id] = cookie_value if cookie_value
get :index get :index
...@@ -242,7 +244,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do ...@@ -242,7 +244,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
context 'do not track is disabled' do context 'do not track is disabled' do
before do before do
request.headers['DNT'] = '0' stub_do_not_track('0')
end end
it 'does track the event' do it 'does track the event' do
...@@ -260,7 +262,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do ...@@ -260,7 +262,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
context 'do not track enabled' do context 'do not track enabled' do
before do before do
request.headers['DNT'] = '1' stub_do_not_track('1')
end end
it 'does not track the event' do it 'does not track the event' do
...@@ -396,7 +398,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do ...@@ -396,7 +398,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
context 'do not track disabled' do context 'do not track disabled' do
before do before do
request.headers['DNT'] = '0' stub_do_not_track('0')
end end
it 'pushes the right parameters to gon' do it 'pushes the right parameters to gon' do
...@@ -414,7 +416,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do ...@@ -414,7 +416,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
context 'do not track enabled' do context 'do not track enabled' do
before do before do
request.headers['DNT'] = '1' stub_do_not_track('1')
end end
it 'does not push data to gon' do it 'does not push data to gon' do
...@@ -525,7 +527,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do ...@@ -525,7 +527,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
context 'is disabled' do context 'is disabled' do
before do before do
request.headers['DNT'] = '0' stub_do_not_track('0')
stub_experiment_for_subject(test_experiment: false) stub_experiment_for_subject(test_experiment: false)
end end
...@@ -538,7 +540,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do ...@@ -538,7 +540,7 @@ RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
context 'is enabled' do context 'is enabled' do
before do before do
request.headers['DNT'] = '1' stub_do_not_track('1')
end end
it 'does not call add_user on the Experiment model' do it 'does not call add_user on the Experiment model' do
......
# frozen_string_literal: true
module TrackingHelpers
def stub_do_not_track(value)
request.headers['DNT'] = value
end
end
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'tracking unique visits' do |method| RSpec.shared_examples 'tracking unique visits' do |method|
include TrackingHelpers
let(:request_params) { {} } let(:request_params) { {} }
it 'tracks unique visit if the format is HTML' do it 'tracks unique visit if the format is HTML' do
...@@ -14,14 +16,15 @@ RSpec.shared_examples 'tracking unique visits' do |method| ...@@ -14,14 +16,15 @@ RSpec.shared_examples 'tracking unique visits' do |method|
expect(Gitlab::UsageDataCounters::HLLRedisCounter) expect(Gitlab::UsageDataCounters::HLLRedisCounter)
.to receive(:track_event).with(target_id, values: kind_of(String)) .to receive(:track_event).with(target_id, values: kind_of(String))
request.headers['DNT'] = '0' stub_do_not_track('0')
get method, params: request_params, format: :html get method, params: request_params, format: :html
end end
it 'does not track unique visit if DNT is enabled' do it 'does not track unique visit if DNT is enabled' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
request.headers['DNT'] = '1'
stub_do_not_track('1')
get method, params: request_params, format: :html get method, params: request_params, format: :html
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