2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe UploadService, feature_category: :shared do
|
2015-04-26 12:48:37 +05:30
|
|
|
describe 'File service' do
|
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
@user = create(:user)
|
2017-09-10 17:25:29 +05:30
|
|
|
@project = create(:project, creator_id: @user.id, namespace: @user.namespace)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'for valid gif file' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
gif = fixture_file_upload('spec/fixtures/banana_sample.gif', 'image/gif')
|
2017-08-17 22:00:37 +05:30
|
|
|
@link_to_file = upload_file(@project, gif)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file).to have_key(:alt) }
|
|
|
|
it { expect(@link_to_file).to have_key(:url) }
|
2015-04-26 12:48:37 +05:30
|
|
|
it { expect(@link_to_file).to have_value('banana_sample') }
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file[:url]).to match('banana_sample.gif') }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
context 'for valid png file' do
|
2015-04-26 12:48:37 +05:30
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
png = fixture_file_upload('spec/fixtures/dk.png',
|
2015-04-26 12:48:37 +05:30
|
|
|
'image/png')
|
2017-08-17 22:00:37 +05:30
|
|
|
@link_to_file = upload_file(@project, png)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file).to have_key(:alt) }
|
|
|
|
it { expect(@link_to_file).to have_key(:url) }
|
2015-04-26 12:48:37 +05:30
|
|
|
it { expect(@link_to_file).to have_value('dk') }
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file[:url]).to match('dk.png') }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
context 'for valid jpg file' do
|
2015-04-26 12:48:37 +05:30
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
jpg = fixture_file_upload('spec/fixtures/rails_sample.jpg', 'image/jpg')
|
2017-08-17 22:00:37 +05:30
|
|
|
@link_to_file = upload_file(@project, jpg)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file).to have_key(:alt) }
|
|
|
|
it { expect(@link_to_file).to have_key(:url) }
|
2015-04-26 12:48:37 +05:30
|
|
|
it { expect(@link_to_file).to have_value('rails_sample') }
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file[:url]).to match('rails_sample.jpg') }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'for txt file' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
txt = fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
|
2017-08-17 22:00:37 +05:30
|
|
|
@link_to_file = upload_file(@project, txt)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file).to have_key(:alt) }
|
|
|
|
it { expect(@link_to_file).to have_key(:url) }
|
2015-04-26 12:48:37 +05:30
|
|
|
it { expect(@link_to_file).to have_value('doc_sample.txt') }
|
2015-09-25 12:07:36 +05:30
|
|
|
it { expect(@link_to_file[:url]).to match('doc_sample.txt') }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'for too large a file' do
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
txt = fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain')
|
2015-04-26 12:48:37 +05:30
|
|
|
allow(txt).to receive(:size) { 1000.megabytes.to_i }
|
2017-08-17 22:00:37 +05:30
|
|
|
@link_to_file = upload_file(@project, txt)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
it { expect(@link_to_file).to eq({}) }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
describe '#override_max_attachment_size' do
|
|
|
|
let(:txt) { fixture_file_upload('spec/fixtures/doc_sample.txt', 'text/plain') }
|
|
|
|
let(:service) { described_class.new(@project, txt, FileUploader) }
|
|
|
|
|
|
|
|
subject { service.execute.to_h }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(txt).to receive(:size) { 100.megabytes.to_i }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows the upload' do
|
|
|
|
service.override_max_attachment_size = 101.megabytes
|
|
|
|
|
|
|
|
expect(subject.keys).to eq(%i(alt url markdown))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disallows the upload' do
|
|
|
|
service.override_max_attachment_size = 99.megabytes
|
|
|
|
|
|
|
|
expect(subject).to eq({})
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def upload_file(project, file)
|
2019-02-15 15:39:39 +05:30
|
|
|
described_class.new(project, file, FileUploader).execute.to_h
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|