diff options
Diffstat (limited to 'spec/controllers/concerns')
-rw-r--r-- | spec/controllers/concerns/autocomplete_sources/expires_in_spec.rb | 69 | ||||
-rw-r--r-- | spec/controllers/concerns/product_analytics_tracking_spec.rb | 27 |
2 files changed, 91 insertions, 5 deletions
diff --git a/spec/controllers/concerns/autocomplete_sources/expires_in_spec.rb b/spec/controllers/concerns/autocomplete_sources/expires_in_spec.rb new file mode 100644 index 00000000000..e1ebe9bcd80 --- /dev/null +++ b/spec/controllers/concerns/autocomplete_sources/expires_in_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AutocompleteSources::ExpiresIn, feature_category: :global_search do + controller(ActionController::Base) do + include AutocompleteSources::ExpiresIn + + def members + render json: [] + end + + def commands + render json: [] + end + + def labels + render json: [] + end + + def not_cached + render json: [] + end + end + + before do + routes.draw do + get "members" => "anonymous#members" + get "commands" => "anonymous#commands" + get "labels" => "anonymous#labels" + get "not_cached" => "anonymous#not_cached" + end + end + + let(:expected_cache_control) { "max-age=#{described_class::AUTOCOMPLETE_EXPIRES_IN}, private" } + + described_class::AUTOCOMPLETE_CACHED_ACTIONS.each do |action| + context "when action is #{action} with feature flag enabled" do + it "sets correct cache-control" do + get action + + expect(response).to have_gitlab_http_status(:ok) + expect(response.headers['Cache-Control']).to eq(expected_cache_control) + end + end + + context "when action is #{action} with feature flag disabled" do + before do + stub_feature_flags("cache_autocomplete_sources_#{action}" => false) + end + + it 'does not set cache-control' do + get action + + expect(response).to have_gitlab_http_status(:ok) + expect(response.headers['Cache-Control']).to be_nil + end + end + end + + context 'when action is not in AUTOCOMPLETE_CACHED_ACTIONS' do + it 'does not set cache-control' do + get :not_cached + + expect(response).to have_gitlab_http_status(:ok) + expect(response.headers['Cache-Control']).to be_nil + end + end +end diff --git a/spec/controllers/concerns/product_analytics_tracking_spec.rb b/spec/controllers/concerns/product_analytics_tracking_spec.rb index 5c69af48e65..7b48782be98 100644 --- a/spec/controllers/concerns/product_analytics_tracking_spec.rb +++ b/spec/controllers/concerns/product_analytics_tracking_spec.rb @@ -21,7 +21,7 @@ RSpec.describe ProductAnalyticsTracking, :snowplow, feature_category: :product_a include ProductAnalyticsTracking skip_before_action :authenticate_user!, only: [:index] - track_internal_event :index, :show, name: 'g_compliance_dashboard', conditions: [:custom_condition?] + track_internal_event :index, :show, name: 'an_event', conditions: [:custom_condition?] def index render html: 'index' @@ -47,7 +47,7 @@ RSpec.describe ProductAnalyticsTracking, :snowplow, feature_category: :product_a end def expect_internal_tracking(tracked_user: user) - expect(Gitlab::InternalEvents).to receive(:track_event).with('g_compliance_dashboard', + expect(Gitlab::InternalEvents).to receive(:track_event).with(event_name, user: tracked_user, project: project, namespace: project.namespace).once @@ -57,6 +57,10 @@ RSpec.describe ProductAnalyticsTracking, :snowplow, feature_category: :product_a expect(Gitlab::InternalEvents).not_to receive(:track_event) end + before do + allow(Gitlab::InternalEvents::EventDefinitions).to receive(:known_event?).with('an_event').and_return(true) + end + context 'when user is logged in' do let(:namespace) { project.namespace } @@ -96,8 +100,22 @@ RSpec.describe ProductAnalyticsTracking, :snowplow, feature_category: :product_a end context 'when user is not logged in' do - it 'does not track the event' do - expect_no_internal_tracking + let(:user) { nil } + + it 'tracks internal event' do + expect_internal_tracking + + get :index + end + + it 'tracks total Redis counters' do + expect(Gitlab::Usage::Metrics::Instrumentations::TotalCountMetric).to receive(:redis_key).twice # total and 7d + + get :index + end + + it 'does not update unique counter' do + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) get :index end @@ -107,7 +125,6 @@ RSpec.describe ProductAnalyticsTracking, :snowplow, feature_category: :product_a describe '.track_event' do before do allow(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) - stub_const("#{described_class}::MIGRATED_EVENTS", ['an_event']) end controller(ApplicationController) do |