debian-mirror-gitlab/spec/lib/gitlab/gfm/uploads_rewriter_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
3.9 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Gfm::UploadsRewriter do
2016-06-02 11:05:42 +05:30
let(:user) { create(:user) }
2017-09-10 17:25:29 +05:30
let(:old_project) { create(:project) }
let(:new_project) { create(:project) }
2022-07-23 23:45:48 +05:30
let(:rewriter) { described_class.new(+text, nil, old_project, user) }
2016-06-02 11:05:42 +05:30
context 'text contains links to uploads' do
let(:image_uploader) do
build(:file_uploader, project: old_project)
end
let(:zip_uploader) do
build(:file_uploader, project: old_project,
fixture: 'ci_build_artifacts.zip')
end
let(:text) do
2022-10-11 01:57:18 +05:30
"Text and #{image_uploader.markdown_link} and #{zip_uploader.markdown_link}".freeze # rubocop:disable Style/RedundantFreeze
2016-06-02 11:05:42 +05:30
end
2022-07-23 23:45:48 +05:30
def referenced_files(text, project)
2022-10-02 17:18:49 +05:30
scanner = FileUploader::MARKDOWN_PATTERN.scan(text)
referenced_files = scanner.map do |match|
UploaderFinder.new(project, match[0], match[1]).execute
2022-07-23 23:45:48 +05:30
end
referenced_files.compact.select(&:exists?)
end
2023-03-04 22:38:38 +05:30
shared_examples 'files are accessible' do
2018-11-08 19:23:39 +05:30
describe '#rewrite' do
2022-10-02 17:18:49 +05:30
subject(:rewrite) { new_text }
let(:new_text) { rewriter.rewrite(new_project) }
2016-06-02 11:05:42 +05:30
2018-11-08 19:23:39 +05:30
let(:old_files) { [image_uploader, zip_uploader] }
let(:new_files) do
2022-07-23 23:45:48 +05:30
referenced_files(new_text, new_project)
2018-11-08 19:23:39 +05:30
end
2016-06-02 11:05:42 +05:30
2018-11-08 19:23:39 +05:30
let(:old_paths) { old_files.map(&:path) }
let(:new_paths) { new_files.map(&:path) }
2016-06-02 11:05:42 +05:30
2018-11-08 19:23:39 +05:30
it 'rewrites content' do
2022-10-02 17:18:49 +05:30
rewrite
2018-11-08 19:23:39 +05:30
expect(new_text).not_to eq text
expect(new_text.length).to eq text.length
end
2016-06-02 11:05:42 +05:30
2018-11-08 19:23:39 +05:30
it 'copies files' do
2022-10-02 17:18:49 +05:30
rewrite
2018-11-08 19:23:39 +05:30
expect(new_files).to all(exist)
expect(old_paths).not_to match_array new_paths
expect(old_paths).to all(include(old_project.disk_path))
expect(new_paths).to all(include(new_project.disk_path))
end
2016-06-02 11:05:42 +05:30
2018-11-08 19:23:39 +05:30
it 'does not remove old files' do
2022-10-02 17:18:49 +05:30
rewrite
2018-11-08 19:23:39 +05:30
expect(old_files).to all(exist)
end
it 'generates a new secret for each file' do
2022-10-02 17:18:49 +05:30
rewrite
2018-11-08 19:23:39 +05:30
expect(new_paths).not_to include image_uploader.secret
expect(new_paths).not_to include zip_uploader.secret
end
2020-06-23 00:09:42 +05:30
it 'skips nil files do' do
allow_next_instance_of(UploaderFinder) do |finder|
allow(finder).to receive(:execute).and_return(nil)
end
2022-10-02 17:18:49 +05:30
rewrite
2020-06-23 00:09:42 +05:30
expect(new_files).to be_empty
2023-03-04 22:38:38 +05:30
expect(new_text).to eq(text)
end
it 'skips non-existant files' do
allow_next_instance_of(FileUploader) do |file|
allow(file).to receive(:exists?).and_return(false)
end
rewrite
expect(new_files).to be_empty
expect(new_text).to eq(text)
2020-06-23 00:09:42 +05:30
end
2016-06-02 11:05:42 +05:30
end
2018-11-08 19:23:39 +05:30
end
2016-06-02 11:05:42 +05:30
2019-10-12 21:52:04 +05:30
it 'does not rewrite plain links as embedded' do
embedded_link = image_uploader.markdown_link
2022-06-21 17:19:12 +05:30
plain_image_link = embedded_link.delete_prefix('!')
2022-07-23 23:45:48 +05:30
text = +"#{plain_image_link} and #{embedded_link}"
2019-10-12 21:52:04 +05:30
2022-07-23 23:45:48 +05:30
moved_text = described_class.new(text, nil, old_project, user).rewrite(new_project)
2019-10-12 21:52:04 +05:30
expect(moved_text.scan(/!\[.*?\]/).count).to eq(1)
expect(moved_text.scan(/\A\[.*?\]/).count).to eq(1)
end
2022-10-02 17:18:49 +05:30
it 'does not casue a timeout on pathological text' do
text = '[!l' * 30000
Timeout.timeout(3) do
moved_text = described_class.new(text, nil, old_project, user).rewrite(new_project)
expect(moved_text).to eq(text)
end
end
2023-03-04 22:38:38 +05:30
context 'file are stored locally' do
include_examples 'files are accessible'
2018-11-08 19:23:39 +05:30
end
2023-03-04 22:38:38 +05:30
context 'files are stored remotely' do
2018-11-08 19:23:39 +05:30
before do
stub_uploads_object_storage(FileUploader)
old_files.each do |file|
file.migrate!(ObjectStorage::Store::REMOTE)
end
2016-06-02 11:05:42 +05:30
end
2018-11-08 19:23:39 +05:30
2023-03-04 22:38:38 +05:30
include_examples 'files are accessible'
2016-06-02 11:05:42 +05:30
end
describe '#needs_rewrite?' do
subject { rewriter.needs_rewrite? }
2019-12-21 20:55:43 +05:30
2016-06-02 11:05:42 +05:30
it { is_expected.to eq true }
end
end
end