diff --git a/ee/spec/features/subscriptions_spec.rb b/ee/spec/features/subscriptions_spec.rb index fe1bc60168bda4ca176f81575b432dca1681d3c3..d62af1946ac2c3e095ab9249be8da095a7213f23 100644 --- a/ee/spec/features/subscriptions_spec.rb +++ b/ee/spec/features/subscriptions_spec.rb @@ -28,7 +28,7 @@ it { is_expected.to be_blank } end - context 'when a global CSP config exists' do + context 'when a global CSP config exists', :do_not_stub_snowplow_by_default do let(:csp) do ActionDispatch::ContentSecurityPolicy.new do |p| p.script_src(*default_csp_values.split) diff --git a/spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb b/spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb index 2878d72210e2689487a719b0eea2a6bf5c8130ec..68c29bad2879b856fd5aa709868c558ba85402d1 100644 --- a/spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb +++ b/spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb @@ -38,7 +38,11 @@ end end - context 'with application settings and admin users', :do_not_mock_admin_mode_setting do + context( + 'with application settings and admin users', + :do_not_mock_admin_mode_setting, + :do_not_stub_snowplow_by_default + ) do let(:group) { result[:group] } let(:application_setting) { Gitlab::CurrentSettings.current_application_settings } diff --git a/spec/lib/gitlab/tracking/destinations/snowplow_spec.rb b/spec/lib/gitlab/tracking/destinations/snowplow_spec.rb index 06cc2d3800c1471403b91a026755ea566711d6ff..1d4725cf4056253f76e982cf47ccf0c38fa692b9 100644 --- a/spec/lib/gitlab/tracking/destinations/snowplow_spec.rb +++ b/spec/lib/gitlab/tracking/destinations/snowplow_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Tracking::Destinations::Snowplow do +RSpec.describe Gitlab::Tracking::Destinations::Snowplow, :do_not_stub_snowplow_by_default do let(:emitter) { SnowplowTracker::Emitter.new('localhost', buffer_size: 1) } let(:tracker) { SnowplowTracker::Tracker.new(emitter, SnowplowTracker::Subject.new, 'namespace', 'app_id') } diff --git a/spec/lib/gitlab/tracking/incident_management_spec.rb b/spec/lib/gitlab/tracking/incident_management_spec.rb index fbcb9bf3e4cc32b1548d14524f8dc4d57f593da1..ef7816aa0db346c4cc3d5c50f7611e9249ec3d57 100644 --- a/spec/lib/gitlab/tracking/incident_management_spec.rb +++ b/spec/lib/gitlab/tracking/incident_management_spec.rb @@ -20,7 +20,7 @@ described_class.track_from_params(params) end - context 'known params' do + context 'known params', :do_not_stub_snowplow_by_default do known_params = described_class.tracking_keys known_params.each do |key, values| diff --git a/spec/models/application_setting_spec.rb b/spec/models/application_setting_spec.rb index dcb9890c7cd945d1a2a64388a015f365912fff97..afcc3cee913cdf1f60cb19072c88e819143966ce 100644 --- a/spec/models/application_setting_spec.rb +++ b/spec/models/application_setting_spec.rb @@ -305,18 +305,20 @@ def many_usernames(num = 100) end end - context 'when snowplow is enabled' do - before do - setting.snowplow_enabled = true - end + describe 'snowplow settings', :do_not_stub_snowplow_by_default do + context 'when snowplow is enabled' do + before do + setting.snowplow_enabled = true + end - it { is_expected.not_to allow_value(nil).for(:snowplow_collector_hostname) } - it { is_expected.to allow_value("snowplow.gitlab.com").for(:snowplow_collector_hostname) } - it { is_expected.not_to allow_value('/example').for(:snowplow_collector_hostname) } - end + it { is_expected.not_to allow_value(nil).for(:snowplow_collector_hostname) } + it { is_expected.to allow_value("snowplow.gitlab.com").for(:snowplow_collector_hostname) } + it { is_expected.not_to allow_value('/example').for(:snowplow_collector_hostname) } + end - context 'when snowplow is not enabled' do - it { is_expected.to allow_value(nil).for(:snowplow_collector_hostname) } + context 'when snowplow is not enabled' do + it { is_expected.to allow_value(nil).for(:snowplow_collector_hostname) } + end end context 'when mailgun_events_enabled is enabled' do diff --git a/spec/models/concerns/cacheable_attributes_spec.rb b/spec/models/concerns/cacheable_attributes_spec.rb index dc80e30216ac3ac1e3fc36f715eb9108fe346349..0629debda1510a5b6861b2b7aee2d77f898787e6 100644 --- a/spec/models/concerns/cacheable_attributes_spec.rb +++ b/spec/models/concerns/cacheable_attributes_spec.rb @@ -205,7 +205,12 @@ def initialize(attrs = {}, *) end end - it 'uses RequestStore in addition to process memory cache', :request_store, :do_not_mock_admin_mode_setting do + it( + 'uses RequestStore in addition to process memory cache', + :request_store, + :do_not_mock_admin_mode_setting, + :do_not_stub_snowplow_by_default + ) do # Warm up the cache create(:application_setting).cache! diff --git a/spec/requests/api/settings_spec.rb b/spec/requests/api/settings_spec.rb index cfda06da8f31faf0a85545998195a2790c881d52..d4a8e591622789df69b244cffe93ea9ce177114d 100644 --- a/spec/requests/api/settings_spec.rb +++ b/spec/requests/api/settings_spec.rb @@ -373,7 +373,7 @@ end end - context "snowplow tracking settings" do + context "snowplow tracking settings", :do_not_stub_snowplow_by_default do let(:settings) do { snowplow_collector_hostname: "snowplow.example.com", diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 06d6a8d9c0fc5d677ddb790030db527c604eccb3..6f7d3e40901314ff519ecccb9db9284e873a2322 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -201,10 +201,12 @@ config.include SidekiqMiddleware config.include StubActionCableConnection, type: :channel config.include StubSpamServices + config.include SnowplowHelpers config.include RenderedHelpers config.include RSpec::Benchmark::Matchers, type: :benchmark include StubFeatureFlags + include StubSnowplow if ENV['CI'] || ENV['RETRIES'] # This includes the first try, i.e. tests will be run 4 times before failing. @@ -367,6 +369,9 @@ stub_application_setting(admin_mode: true) unless example.metadata[:do_not_mock_admin_mode_setting] allow(Gitlab::CurrentSettings).to receive(:current_application_settings?).and_return(false) + + # Ensure that Snowplow is enabled by default unless forced to the opposite + stub_snowplow unless example.metadata[:do_not_stub_snowplow_by_default] end config.around(:example, :quarantine) do |example| diff --git a/spec/support/gitlab_experiment.rb b/spec/support/gitlab_experiment.rb index 823aab0436ebbe77811704befc9b803c28c4f5d6..4236091ca6c2604ad61ecc3e075bd0d5a3d0fc68 100644 --- a/spec/support/gitlab_experiment.rb +++ b/spec/support/gitlab_experiment.rb @@ -2,7 +2,6 @@ # Require the provided spec helper and matchers. require 'gitlab/experiment/rspec' -require_relative 'stub_snowplow' RSpec.configure do |config| config.include StubSnowplow, :experiment diff --git a/spec/support/stub_snowplow.rb b/spec/support/helpers/stub_snowplow.rb similarity index 88% rename from spec/support/stub_snowplow.rb rename to spec/support/helpers/stub_snowplow.rb index c6e3b40972f6994f41bde8c52c6eb0015b8b492c..85c605efea3ea794351d2d11f50d8b52af7ffc15 100644 --- a/spec/support/stub_snowplow.rb +++ b/spec/support/helpers/stub_snowplow.rb @@ -13,7 +13,7 @@ def stub_snowplow .and_return(SnowplowTracker::Emitter.new(host, buffer_size: buffer_size)) # rubocop:enable RSpec/AnyInstanceOf - stub_application_setting(snowplow_enabled: true) + stub_application_setting(snowplow_enabled: true, snowplow_collector_hostname: host) allow(SnowplowTracker::SelfDescribingJson).to receive(:new).and_call_original allow(Gitlab::Tracking).to receive(:event).and_call_original # rubocop:disable RSpec/ExpectGitlabTracking diff --git a/spec/support/shared_examples/csp.rb b/spec/support/shared_examples/csp.rb index 9143d0f4720bcdc33b53bf3490fc357ad38285ee..91242ae9f376677a4289dc296cd38fc2553ea1c6 100644 --- a/spec/support/shared_examples/csp.rb +++ b/spec/support/shared_examples/csp.rb @@ -15,64 +15,66 @@ end end - context 'when no CSP config' do - include_context 'csp config', nil + context 'csp config and feature toggle', :do_not_stub_snowplow_by_default do + context 'when no CSP config' do + include_context 'csp config', nil - it 'does not add CSP directives' do - is_expected.to be_blank + it 'does not add CSP directives' do + is_expected.to be_blank + end end - end - describe "when a CSP config exists for #{rule_name}" do - include_context 'csp config', rule_name.parameterize.underscore.to_sym + describe "when a CSP config exists for #{rule_name}" do + include_context 'csp config', rule_name.parameterize.underscore.to_sym - context 'when feature is enabled' do - it "appends to #{rule_name}" do - is_expected.to eql("#{rule_name} #{default_csp_values} #{allowlisted_url}") + context 'when feature is enabled' do + it "appends to #{rule_name}" do + is_expected.to eql("#{rule_name} #{default_csp_values} #{allowlisted_url}") + end end - end - context 'when feature is disabled' do - include_context 'disable feature' + context 'when feature is disabled' do + include_context 'disable feature' - it "keeps original #{rule_name}" do - is_expected.to eql("#{rule_name} #{default_csp_values}") + it "keeps original #{rule_name}" do + is_expected.to eql("#{rule_name} #{default_csp_values}") + end end end - end - describe "when a CSP config exists for default-src but not #{rule_name}" do - include_context 'csp config', :default_src + describe "when a CSP config exists for default-src but not #{rule_name}" do + include_context 'csp config', :default_src - context 'when feature is enabled' do - it "uses default-src values in #{rule_name}" do - is_expected.to eql("default-src #{default_csp_values}; #{rule_name} #{default_csp_values} #{allowlisted_url}") + context 'when feature is enabled' do + it "uses default-src values in #{rule_name}" do + is_expected.to eql("default-src #{default_csp_values}; #{rule_name} #{default_csp_values} #{allowlisted_url}") + end end - end - context 'when feature is disabled' do - include_context 'disable feature' + context 'when feature is disabled' do + include_context 'disable feature' - it "does not add #{rule_name}" do - is_expected.to eql("default-src #{default_csp_values}") + it "does not add #{rule_name}" do + is_expected.to eql("default-src #{default_csp_values}") + end end end - end - describe "when a CSP config exists for font-src but not #{rule_name}" do - include_context 'csp config', :font_src + describe "when a CSP config exists for font-src but not #{rule_name}" do + include_context 'csp config', :font_src - context 'when feature is enabled' do - it "uses default-src values in #{rule_name}" do - is_expected.to eql("font-src #{default_csp_values}; #{rule_name} #{allowlisted_url}") + context 'when feature is enabled' do + it "uses default-src values in #{rule_name}" do + is_expected.to eql("font-src #{default_csp_values}; #{rule_name} #{allowlisted_url}") + end end - end - context 'when feature is disabled' do - include_context 'disable feature' + context 'when feature is disabled' do + include_context 'disable feature' - it "does not add #{rule_name}" do - is_expected.to eql("font-src #{default_csp_values}") + it "does not add #{rule_name}" do + is_expected.to eql("font-src #{default_csp_values}") + end end end end diff --git a/spec/support/snowplow.rb b/spec/support/snowplow.rb deleted file mode 100644 index d600bf008b01d6dcaa6252d5845d5f25f3f1c87a..0000000000000000000000000000000000000000 --- a/spec/support/snowplow.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -require_relative 'stub_snowplow' - -RSpec.configure do |config| - config.include SnowplowHelpers, :snowplow - config.include StubSnowplow, :snowplow - - config.before(:each, :snowplow) do - stub_snowplow - end - - config.after(:each, :snowplow) do - Gitlab::Tracking.send(:tracker).send(:tracker).flush - end -end