2020-08-11 11:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe MarkdownContentRewriterService do
|
2020-08-19 23:10:04 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:source_parent) { create(:project, :public) }
|
|
|
|
let_it_be(:target_parent) { create(:project, :public) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2020-08-19 23:10:04 -04:00
|
|
|
let(:content) { 'My content' }
|
2022-06-02 17:09:25 -04:00
|
|
|
let(:issue) { create(:issue, project: source_parent, description: content)}
|
2020-08-19 23:10:04 -04:00
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
it 'raises an error if source_parent is not a Project' do
|
|
|
|
expect do
|
2022-06-02 17:09:25 -04:00
|
|
|
described_class.new(user, issue, :description, create(:group), target_parent)
|
2020-08-19 23:10:04 -04:00
|
|
|
end.to raise_error(ArgumentError, 'The rewriter classes require that `source_parent` is a `Project`')
|
|
|
|
end
|
2022-06-02 17:09:25 -04:00
|
|
|
|
|
|
|
it 'raises an error if field does not have cached markdown' do
|
|
|
|
expect do
|
|
|
|
described_class.new(user, issue, :author, source_parent, target_parent)
|
|
|
|
end.to raise_error(ArgumentError, 'The `field` attribute does not contain cached markdown')
|
|
|
|
end
|
2020-08-19 23:10:04 -04:00
|
|
|
end
|
2020-08-11 11:10:08 -04:00
|
|
|
|
2020-08-19 23:10:04 -04:00
|
|
|
describe '#execute' do
|
2022-06-02 17:09:25 -04:00
|
|
|
subject { described_class.new(user, issue, :description, source_parent, target_parent).execute }
|
2020-08-11 11:10:08 -04:00
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
context 'when content does not need a rewrite' do
|
|
|
|
it 'returns original content and cached html' do
|
|
|
|
expect(subject).to eq({
|
|
|
|
'description' => issue.description,
|
|
|
|
'description_html' => issue.description_html,
|
|
|
|
'skip_markdown_cache_validation' => true
|
|
|
|
})
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|
2022-06-02 17:09:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when content needs a rewrite' do
|
|
|
|
it 'calls the rewriter classes successfully', :aggregate_failures do
|
|
|
|
described_class::REWRITERS.each do |rewriter_class|
|
|
|
|
service = double
|
2020-08-11 11:10:08 -04:00
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
allow(service).to receive(:needs_rewrite?).and_return(true)
|
|
|
|
|
|
|
|
expect(service).to receive(:rewrite).with(target_parent)
|
|
|
|
expect(rewriter_class).to receive(:new).and_return(service)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Perform simple integration-style tests for each rewriter class.
|
|
|
|
# to prove they run correctly.
|
2022-06-02 17:09:25 -04:00
|
|
|
context 'when content has references' do
|
|
|
|
let_it_be(:issue_to_reference) { create(:issue, project: source_parent) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
let(:content) { "See ##{issue_to_reference.iid}" }
|
2020-08-11 11:10:08 -04:00
|
|
|
|
|
|
|
it 'rewrites content' do
|
2022-06-02 17:09:25 -04:00
|
|
|
expect(subject).to eq({
|
|
|
|
'description' => "See #{source_parent.full_path}##{issue_to_reference.iid}",
|
|
|
|
'description_html' => nil,
|
|
|
|
'skip_markdown_cache_validation' => false
|
|
|
|
})
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when content contains an upload' do
|
|
|
|
let(:image_uploader) { build(:file_uploader, project: source_parent) }
|
|
|
|
let(:content) { "Text and #{image_uploader.markdown_link}" }
|
|
|
|
|
|
|
|
it 'rewrites content' do
|
|
|
|
new_content = subject
|
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
expect(new_content[:description]).not_to eq(content)
|
|
|
|
expect(new_content[:description].length).to eq(content.length)
|
|
|
|
expect(new_content[1]).to eq(nil)
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-06-02 17:09:25 -04:00
|
|
|
|
|
|
|
describe '#safe_to_copy_markdown?' do
|
|
|
|
subject do
|
|
|
|
rewriter = described_class.new(user, issue, :description, source_parent, target_parent)
|
|
|
|
rewriter.safe_to_copy_markdown?
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when content has references' do
|
|
|
|
let(:milestone) { create(:milestone, project: source_parent) }
|
|
|
|
let(:content) { "Description that references #{milestone.to_reference}" }
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when content has uploaded file references' do
|
|
|
|
let(:image_uploader) { build(:file_uploader, project: source_parent) }
|
|
|
|
let(:content) { "Text and #{image_uploader.markdown_link}" }
|
|
|
|
|
|
|
|
it { is_expected.to eq(false) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when content does not have references or uploads' do
|
|
|
|
let(:content) { "simples text with ```code```" }
|
|
|
|
|
|
|
|
it { is_expected.to eq(true) }
|
|
|
|
end
|
|
|
|
end
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|