diff options
Diffstat (limited to 'spec/controllers/concerns')
-rw-r--r-- | spec/controllers/concerns/redis_tracking_spec.rb | 106 | ||||
-rw-r--r-- | spec/controllers/concerns/send_file_upload_spec.rb | 140 |
2 files changed, 207 insertions, 39 deletions
diff --git a/spec/controllers/concerns/redis_tracking_spec.rb b/spec/controllers/concerns/redis_tracking_spec.rb new file mode 100644 index 00000000000..3795fca5576 --- /dev/null +++ b/spec/controllers/concerns/redis_tracking_spec.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe RedisTracking do + let(:event_name) { 'g_compliance_dashboard' } + let(:feature) { 'g_compliance_dashboard_feature' } + let(:user) { create(:user) } + + controller(ApplicationController) do + include RedisTracking + + skip_before_action :authenticate_user!, only: :show + track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score', feature: :g_compliance_dashboard_feature, feature_default_enabled: true + + def index + render html: 'index' + end + + def new + render html: 'new' + end + + def show + render html: 'show' + end + end + + context 'with feature disabled' do + it 'does not track the event' do + stub_feature_flags(feature => false) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + get :index + end + end + + context 'with usage ping disabled' do + it 'does not track the event' do + stub_feature_flags(feature => true) + allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(false) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + get :index + end + end + + context 'with feature enabled and usage ping enabled' do + before do + stub_feature_flags(feature => true) + allow(Gitlab::CurrentSettings).to receive(:usage_ping_enabled?).and_return(true) + end + + context 'when user is logged in' do + it 'tracks the event' do + sign_in(user) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) + + get :index + end + + it 'passes default_enabled flag' do + sign_in(user) + + expect(controller).to receive(:metric_feature_enabled?).with(feature.to_sym, true) + + get :index + end + 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 } + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event) + + get :show + end + end + + context 'when user is not logged in and there is no visitor_id' do + it 'does not tracks the event' do + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + get :index + end + end + + context 'for untracked action' do + it 'does not tracks the event' do + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + get :new + end + end + end +end diff --git a/spec/controllers/concerns/send_file_upload_spec.rb b/spec/controllers/concerns/send_file_upload_spec.rb index e24e4cbf5e7..747ccd7ba1b 100644 --- a/spec/controllers/concerns/send_file_upload_spec.rb +++ b/spec/controllers/concerns/send_file_upload_spec.rb @@ -50,9 +50,14 @@ RSpec.describe SendFileUpload do shared_examples 'handles image resize requests' do let(:headers) { double } + let(:image_requester) { build(:user) } + let(:image_owner) { build(:user) } + let(:params) do + { attachment: 'avatar.png' } + end before do - allow(uploader).to receive(:image?).and_return(true) + allow(uploader).to receive(:image_safe_for_scaling?).and_return(true) allow(uploader).to receive(:mounted_as).and_return(:avatar) allow(controller).to receive(:headers).and_return(headers) @@ -60,70 +65,127 @@ RSpec.describe SendFileUpload do # local or remote files allow(controller).to receive(:send_file) allow(controller).to receive(:redirect_to) + + allow(controller).to receive(:current_user).and_return(image_requester) + allow(uploader).to receive(:model).and_return(image_owner) end - context 'when feature is enabled for current user' do - let(:user) { build(:user) } + context 'when boths FFs are enabled' do + before do + stub_feature_flags(dynamic_image_resizing_requester: image_requester) + stub_feature_flags(dynamic_image_resizing_owner: image_owner) + end + + it_behaves_like 'handles image resize requests allowed by FFs' + end + context 'when boths FFs are enabled globally' do before do - stub_feature_flags(dynamic_image_resizing: user) - allow(controller).to receive(:current_user).and_return(user) + stub_feature_flags(dynamic_image_resizing_requester: true) + stub_feature_flags(dynamic_image_resizing_owner: true) end - context 'with valid width parameter' do - it 'renders OK with workhorse command header' do - expect(controller).not_to receive(:send_file) - expect(controller).to receive(:params).at_least(:once).and_return(width: '64') - expect(controller).to receive(:head).with(:ok) - expect(headers).to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + it_behaves_like 'handles image resize requests allowed by FFs' - subject + context 'when current_user is nil' do + before do + allow(controller).to receive(:current_user).and_return(nil) end + + it_behaves_like 'handles image resize requests allowed by FFs' end + end - context 'with missing width parameter' do - it 'does not write workhorse command header' do - expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + context 'when only FF based on content requester is enabled for current user' do + before do + stub_feature_flags(dynamic_image_resizing_requester: image_requester) + stub_feature_flags(dynamic_image_resizing_owner: false) + end - subject - end + it_behaves_like 'bypasses image resize requests not allowed by FFs' + end + + context 'when only FF based on content owner is enabled for requested avatar owner' do + before do + stub_feature_flags(dynamic_image_resizing_requester: false) + stub_feature_flags(dynamic_image_resizing_owner: image_owner) end - context 'with invalid width parameter' do - it 'does not write workhorse command header' do - expect(controller).to receive(:params).at_least(:once).and_return(width: 'not a number') - expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + it_behaves_like 'bypasses image resize requests not allowed by FFs' + end - subject - end + context 'when both FFs are disabled' do + before do + stub_feature_flags(dynamic_image_resizing_requester: false) + stub_feature_flags(dynamic_image_resizing_owner: false) end - context 'with width that is not allowed' do - it 'does not write workhorse command header' do - expect(controller).to receive(:params).at_least(:once).and_return(width: '63') - expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + it_behaves_like 'bypasses image resize requests not allowed by FFs' + end + end - subject - end + shared_examples 'bypasses image resize requests not allowed by FFs' do + it 'does not write workhorse command header' do + expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + + subject + end + end + + shared_examples 'handles image resize requests allowed by FFs' do + context 'with valid width parameter' do + it 'renders OK with workhorse command header' do + expect(controller).not_to receive(:send_file) + expect(controller).to receive(:params).at_least(:once).and_return(width: '64') + expect(controller).to receive(:head).with(:ok) + + expect(Gitlab::Workhorse).to receive(:send_scaled_image).with(a_string_matching('^(/.+|https://.+)'), 64, 'image/png').and_return([ + Gitlab::Workhorse::SEND_DATA_HEADER, "send-scaled-img:faux" + ]) + expect(headers).to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, "send-scaled-img:faux") + + subject end + end - context 'when image file is not an avatar' do - it 'does not write workhorse command header' do - expect(uploader).to receive(:mounted_as).and_return(nil) # FileUploader is not mounted - expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + context 'with missing width parameter' do + it 'does not write workhorse command header' do + expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) - subject - end + subject end end - context 'when feature is disabled' do - before do - stub_feature_flags(dynamic_image_resizing: false) + context 'with invalid width parameter' do + it 'does not write workhorse command header' do + expect(controller).to receive(:params).at_least(:once).and_return(width: 'not a number') + expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + + subject end + end + context 'with width that is not allowed' do it 'does not write workhorse command header' do - expect(controller).to receive(:params).at_least(:once).and_return(width: '64') + expect(controller).to receive(:params).at_least(:once).and_return(width: '63') + expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + + subject + end + end + + context 'when image file is not an avatar' do + it 'does not write workhorse command header' do + expect(uploader).to receive(:mounted_as).and_return(nil) # FileUploader is not mounted + expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) + + subject + end + end + + context 'when image file type is not considered safe for scaling' do + it 'does not write workhorse command header' do + expect(uploader).to receive(:image_safe_for_scaling?).and_return(false) expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/) subject |