diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-20 12:40:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-20 12:40:42 +0300 |
commit | ee664acb356f8123f4f6b00b73c1e1cf0866c7fb (patch) | |
tree | f8479f94a28f66654c6a4f6fb99bad6b4e86a40e /spec/lib/banzai | |
parent | 62f7d5c5b69180e82ae8196b7b429eeffc8e7b4f (diff) |
Add latest changes from gitlab-org/gitlab@15-5-stable-eev15.5.0-rc42
Diffstat (limited to 'spec/lib/banzai')
6 files changed, 156 insertions, 47 deletions
diff --git a/spec/lib/banzai/filter/pathological_markdown_filter_spec.rb b/spec/lib/banzai/filter/pathological_markdown_filter_spec.rb deleted file mode 100644 index e0a07d1ea77..00000000000 --- a/spec/lib/banzai/filter/pathological_markdown_filter_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Banzai::Filter::PathologicalMarkdownFilter do - include FilterSpecHelper - - let_it_be(:short_text) { '![a' * 5 } - let_it_be(:long_text) { ([short_text] * 10).join(' ') } - let_it_be(:with_images_text) { "![One ![one](one.jpg) #{'and\n' * 200} ![two ![two](two.jpg)" } - - it 'detects a significat number of unclosed image links' do - msg = <<~TEXT - _Unable to render markdown - too many unclosed markdown image links detected._ - TEXT - - expect(filter(long_text)).to eq(msg.strip) - end - - it 'does nothing when there are only a few unclosed image links' do - expect(filter(short_text)).to eq(short_text) - end - - it 'does nothing when there are only a few unclosed image links and images' do - expect(filter(with_images_text)).to eq(with_images_text) - end -end diff --git a/spec/lib/banzai/filter/references/label_reference_filter_spec.rb b/spec/lib/banzai/filter/references/label_reference_filter_spec.rb index c342a831d62..12cdb5cfb95 100644 --- a/spec/lib/banzai/filter/references/label_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/label_reference_filter_spec.rb @@ -309,11 +309,12 @@ RSpec.describe Banzai::Filter::References::LabelReferenceFilter do it 'links to valid references' do doc = reference_filter("See #{references}") - expect(doc.css('a').map { |a| a.attr('href') }).to match_array([ - urls.project_issues_url(project, label_name: bug.name), - urls.project_issues_url(project, label_name: feature_proposal.name), - urls.project_issues_url(project, label_name: technical_debt.name) - ]) + expect(doc.css('a').map { |a| a.attr('href') }).to match_array( + [ + urls.project_issues_url(project, label_name: bug.name), + urls.project_issues_url(project, label_name: feature_proposal.name), + urls.project_issues_url(project, label_name: technical_debt.name) + ]) expect(doc.text).to eq 'See bug, feature proposal, technical debt' end end @@ -324,11 +325,12 @@ RSpec.describe Banzai::Filter::References::LabelReferenceFilter do it 'links to valid references' do doc = reference_filter("See #{references}") - expect(doc.css('a').map { |a| a.attr('href') }).to match_array([ - urls.project_issues_url(project, label_name: bug.name), - urls.project_issues_url(project, label_name: feature_proposal.name), - urls.project_issues_url(project, label_name: technical_debt.name) - ]) + expect(doc.css('a').map { |a| a.attr('href') }).to match_array( + [ + urls.project_issues_url(project, label_name: bug.name), + urls.project_issues_url(project, label_name: feature_proposal.name), + urls.project_issues_url(project, label_name: technical_debt.name) + ]) expect(doc.text).to eq 'See bug feature proposal technical debt' end end diff --git a/spec/lib/banzai/filter/truncate_visible_filter_spec.rb b/spec/lib/banzai/filter/truncate_visible_filter_spec.rb new file mode 100644 index 00000000000..8daaed05264 --- /dev/null +++ b/spec/lib/banzai/filter/truncate_visible_filter_spec.rb @@ -0,0 +1,128 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Banzai::Filter::TruncateVisibleFilter do + include FilterSpecHelper + + let_it_be(:project) { build(:project, :repository) } + let_it_be(:max_chars) { 100 } + let_it_be(:user) do + user = create(:user, username: 'gfm') + project.add_maintainer(user) + user + end + + # Since we're truncating nodes of an html document, actually use the + # full pipeline to generate full documents. + def convert_markdown(text, context = {}) + Banzai::Pipeline::FullPipeline.to_html(text, { project: project }.merge(context)) + end + + shared_examples_for 'truncates text' do + specify do + html = convert_markdown(markdown) + doc = filter(html, { truncate_visible_max_chars: max_chars }) + + expect(doc.to_html).to match(expected) + end + end + + describe 'displays inline code' do + let(:markdown) { 'Text with `inline code`' } + let(:expected) { 'Text with <code>inline code</code>' } + + it_behaves_like 'truncates text' + end + + describe 'truncates the text with multiple paragraphs' do + let(:markdown) { "Paragraph 1\n\nParagraph 2" } + let(:expected) { 'Paragraph 1...' } + + it_behaves_like 'truncates text' + end + + describe 'truncates the first line of a code block' do + let(:markdown) { "```\nCode block\nwith two lines\n```" } + let(:expected) { "Code block...</span>\n</code>" } + + it_behaves_like 'truncates text' + end + + describe 'preserves code color scheme' do + let(:max_chars) { 150 } + let(:markdown) { "```ruby\ndef test\n 'hello world'\nend\n```" } + let(:expected) do + '<code><span id="LC1" class="line" lang="ruby">' \ + '<span class="k">def</span> <span class="nf">test</span>...</span>' + end + + it_behaves_like 'truncates text' + end + + describe 'truncates a single long line of text' do + let(:max_chars) { 150 } + let(:text) { 'The quick brown fox jumped over the lazy dog twice' } # 50 chars + let(:markdown) { text * 4 } + let(:expected) { (text * 2).sub(/.{3}/, '...') } + + it_behaves_like 'truncates text' + end + + it 'preserves a link href when link text is truncated' do + max_chars = 150 + text = 'The quick brown fox jumped over the lazy dog' # 44 chars + link_url = 'http://example.com/foo/bar/baz' # 30 chars + markdown = "#{text}#{text}#{text} #{link_url}" # 163 chars + expected_link_text = 'http://example...</a>' + + html = convert_markdown(markdown) + doc = filter(html, { truncate_visible_max_chars: max_chars }) + + expect(doc.to_html).to match(link_url) + expect(doc.to_html).to match(expected_link_text) + end + + it 'truncates HTML properly' do + markdown = "@#{user.username}, can you look at this?\nHello world\n" + + html = convert_markdown(markdown) + doc = filter(html, { truncate_visible_max_chars: max_chars }) + + # Make sure we didn't create invalid markup + expect(doc.errors).to be_empty + + # Leading user link + expect(doc.css('a').length).to eq(1) + expect(doc.css('a')[0].attr('href')).to eq urls.user_path(user) + expect(doc.css('a')[0].text).to eq "@#{user.username}" + expect(doc.content).to eq "@#{user.username}, can you look at this?..." + end + + it 'truncates HTML with emoji properly' do + markdown = "foo :wink:\nbar :grinning:" + # actual = first_line_in_markdown(object, attribute, 100, project: project) + + html = convert_markdown(markdown) + doc = filter(html, { truncate_visible_max_chars: max_chars }) + + # Make sure we didn't create invalid markup + # But also account for the 2 errors caused by the unknown `gl-emoji` elements + expect(doc.errors.length).to eq(2) + + expect(doc.css('gl-emoji').length).to eq(2) + expect(doc.css('gl-emoji')[0].attr('data-name')).to eq 'wink' + expect(doc.css('gl-emoji')[1].attr('data-name')).to eq 'grinning' + + expect(doc.content).to eq "foo 😉\nbar 😀" + end + + it 'does not truncate if truncate_visible_max_chars not specified' do + markdown = "@#{user.username}, can you look at this?\nHello world" + + html = convert_markdown(markdown) + doc = filter(html) + + expect(doc.content).to eq markdown + end +end diff --git a/spec/lib/banzai/filter/wiki_link_filter_spec.rb b/spec/lib/banzai/filter/wiki_link_filter_spec.rb index 70c7c3c74fb..9807e385a5a 100644 --- a/spec/lib/banzai/filter/wiki_link_filter_spec.rb +++ b/spec/lib/banzai/filter/wiki_link_filter_spec.rb @@ -47,6 +47,14 @@ RSpec.describe Banzai::Filter::WikiLinkFilter do expect(filtered_link.attribute('href').value).to eq(path) end + + it 'does not rewrite links to old relative wiki path' do + old_wiki_base_path = wiki.wiki_base_path.sub('/-/', '/') + path = "#{old_wiki_base_path}/#{repository_upload_folder}/a.jpg" + filtered_link = filter("<a href='#{path}'>Link</a>", wiki: wiki, page_slug: 'home').children[0] + + expect(filtered_link.attribute('href').value).to eq(path) + end end describe "when links point to the #{Wikis::CreateAttachmentService::ATTACHMENT_PATH} folder" do diff --git a/spec/lib/banzai/pipeline/full_pipeline_spec.rb b/spec/lib/banzai/pipeline/full_pipeline_spec.rb index c07f99dc9fc..1a0f5a53a23 100644 --- a/spec/lib/banzai/pipeline/full_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/full_pipeline_spec.rb @@ -168,15 +168,13 @@ RSpec.describe Banzai::Pipeline::FullPipeline do end end - describe 'unclosed image links' do - it 'detects a significat number of unclosed image links' do - markdown = '![a ' * 30 - msg = <<~TEXT - Unable to render markdown - too many unclosed markdown image links detected. - TEXT - output = described_class.to_html(markdown, project: nil) - - expect(output).to include(msg.strip) + describe 'cmark-gfm and autlolinks' do + it 'does not hang with significant number of unclosed image links' do + markdown = '![a ' * 300000 + + expect do + Timeout.timeout(2.seconds) { described_class.to_html(markdown, project: nil) } + end.not_to raise_error end end end diff --git a/spec/lib/banzai/pipeline/gfm_pipeline_spec.rb b/spec/lib/banzai/pipeline/gfm_pipeline_spec.rb index e24177a7043..f67f13b3862 100644 --- a/spec/lib/banzai/pipeline/gfm_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/gfm_pipeline_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' RSpec.describe Banzai::Pipeline::GfmPipeline do describe 'integration between parsing regular and external issue references' do - let(:project) { create(:redmine_project, :public) } + let(:project) { create(:project, :with_redmine_integration, :public) } context 'when internal issue tracker is enabled' do context 'when shorthand pattern #ISSUE_ID is used' do |