diff options
Diffstat (limited to 'spec/controllers/concerns/send_file_upload_spec.rb')
-rw-r--r-- | spec/controllers/concerns/send_file_upload_spec.rb | 140 |
1 files changed, 101 insertions, 39 deletions
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 |