2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'spec_helper'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Banzai::Pipeline::GfmPipeline do
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'integration between parsing regular and external issue references' do
|
2022-11-25 23:54:43 +05:30
|
|
|
let(:project) { create(:project, :with_redmine_integration, :public) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when internal issue tracker is enabled' do
|
2020-07-28 23:09:34 +05:30
|
|
|
context 'when shorthand pattern #ISSUE_ID is used' do
|
|
|
|
it 'links an internal issues and keep updated nodes in result[:reference_filter_nodes]', :aggregate_failures do
|
|
|
|
issue = create(:issue, project: project)
|
|
|
|
markdown = "text #{issue.to_reference(project, full: true)}"
|
|
|
|
|
|
|
|
result = described_class.call(markdown, project: project)
|
|
|
|
link = result[:output].css('a').first
|
|
|
|
text = result[:output].children.first
|
|
|
|
|
|
|
|
expect(link['href']).to eq(Gitlab::Routing.url_helpers.project_issue_path(project, issue))
|
|
|
|
expect(result[:reference_filter_nodes]).to eq([text])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes :each_node only once for first reference filter', :aggregate_failures do
|
|
|
|
issue = create(:issue, project: project)
|
|
|
|
markdown = "text #{issue.to_reference(project, full: true)}"
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
expect_any_instance_of(Banzai::Filter::References::ReferenceFilter).to receive(:each_node).once
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
described_class.call(markdown, project: project)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when shorthand pattern #ISSUE_ID is used' do
|
|
|
|
it 'links an internal issue if it exists' do
|
|
|
|
issue = create(:issue, project: project)
|
|
|
|
markdown = issue.to_reference(project, full: true)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
result = described_class.call(markdown, project: project)[:output]
|
|
|
|
link = result.css('a').first
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(link['href']).to eq(
|
|
|
|
Gitlab::Routing.url_helpers.project_issue_path(project, issue)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not link any issue if it does not exist on GitLab' do
|
|
|
|
markdown = '#12'
|
|
|
|
|
|
|
|
result = described_class.call(markdown, project: project)[:output]
|
|
|
|
expect(result.css('a')).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to use long external reference syntax for Redmine' do
|
|
|
|
markdown = 'API_32-12'
|
|
|
|
|
|
|
|
result = described_class.call(markdown, project: project)[:output]
|
|
|
|
link = result.css('a').first
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(link['href']).to eq 'http://issues.example.com/issues/12'
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses cross-project references to regular issues' do
|
|
|
|
other_project = create(:project, :public)
|
|
|
|
issue = create(:issue, project: other_project)
|
|
|
|
markdown = issue.to_reference(project, full: true)
|
|
|
|
|
|
|
|
result = described_class.call(markdown, project: project)[:output]
|
|
|
|
link = result.css('a').first
|
|
|
|
|
|
|
|
expect(link['href']).to eq(
|
|
|
|
Gitlab::Routing.url_helpers.project_issue_path(other_project, issue)
|
|
|
|
)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when internal issue tracker is disabled' do
|
|
|
|
before do
|
|
|
|
project.issues_enabled = false
|
|
|
|
project.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to use shorthand external reference syntax for Redmine' do
|
|
|
|
markdown = '#12'
|
|
|
|
|
|
|
|
result = described_class.call(markdown, project: project)[:output]
|
|
|
|
link = result.css('a').first
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(link['href']).to eq 'http://issues.example.com/issues/12'
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to use long external reference syntax for Redmine' do
|
|
|
|
markdown = 'API_32-12'
|
|
|
|
|
|
|
|
result = described_class.call(markdown, project: project)[:output]
|
|
|
|
link = result.css('a').first
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(link['href']).to eq 'http://issues.example.com/issues/12'
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses cross-project references to regular issues' do
|
|
|
|
other_project = create(:project, :public)
|
|
|
|
issue = create(:issue, project: other_project)
|
|
|
|
markdown = issue.to_reference(project, full: true)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
result = described_class.call(markdown, project: project)[:output]
|
|
|
|
link = result.css('a').first
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(link['href']).to eq(
|
|
|
|
Gitlab::Routing.url_helpers.project_issue_path(other_project, issue)
|
2017-08-17 22:00:37 +05:30
|
|
|
)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
describe 'markdown link or image urls having spaces' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
|
|
|
|
it 'rewrites links with spaces in url' do
|
|
|
|
markdown = "[Link to Page](page slug)"
|
|
|
|
output = described_class.to_html(markdown, project: project)
|
|
|
|
|
|
|
|
expect(output).to include("href=\"page%20slug\"")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rewrites images with spaces in url' do
|
|
|
|
markdown = "![My Image](test image.png)"
|
|
|
|
output = described_class.to_html(markdown, project: project)
|
|
|
|
|
|
|
|
expect(output).to include("src=\"test%20image.png\"")
|
|
|
|
end
|
2018-11-29 20:51:05 +05:30
|
|
|
|
|
|
|
it 'sanitizes the fixed link' do
|
|
|
|
markdown_xss = "[xss](javascript: alert%28document.domain%29)"
|
|
|
|
output = described_class.to_html(markdown_xss, project: project)
|
|
|
|
|
|
|
|
expect(output).not_to include("javascript")
|
|
|
|
|
|
|
|
markdown_xss = "<invalidtag>\n[xss](javascript:alert%28document.domain%29)"
|
|
|
|
output = described_class.to_html(markdown_xss, project: project)
|
|
|
|
|
|
|
|
expect(output).not_to include("javascript")
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
describe 'emoji in references' do
|
2020-04-08 14:13:33 +05:30
|
|
|
let_it_be(:project) { create(:project, :public) }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:emoji) { '💯' }
|
|
|
|
|
|
|
|
it 'renders a label reference with emoji inside' do
|
|
|
|
create(:label, project: project, name: emoji)
|
|
|
|
|
|
|
|
output = described_class.to_html("#{Label.reference_prefix}\"#{emoji}\"", project: project)
|
|
|
|
|
|
|
|
expect(output).to include(emoji)
|
|
|
|
expect(output).to include(Gitlab::Routing.url_helpers.project_issues_path(project, label_name: emoji))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders a milestone reference with emoji inside' do
|
|
|
|
milestone = create(:milestone, project: project, title: emoji)
|
|
|
|
|
|
|
|
output = described_class.to_html("#{Milestone.reference_prefix}\"#{emoji}\"", project: project)
|
|
|
|
|
|
|
|
expect(output).to include(emoji)
|
|
|
|
expect(output).to include(Gitlab::Routing.url_helpers.milestone_path(milestone))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'asset proxy' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:image) { '![proxy](http://example.com/test.png)' }
|
|
|
|
let(:proxy) { 'https://assets.example.com/08df250eeeef1a8cf2c761475ac74c5065105612/687474703a2f2f6578616d706c652e636f6d2f746573742e706e67' }
|
|
|
|
let(:version) { Gitlab::CurrentSettings.current_application_settings.local_markdown_version }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_asset_proxy_setting(enabled: true)
|
|
|
|
stub_asset_proxy_setting(secret_key: 'shared-secret')
|
|
|
|
stub_asset_proxy_setting(url: 'https://assets.example.com')
|
2021-03-08 18:12:59 +05:30
|
|
|
stub_asset_proxy_setting(allowlist: %W(gitlab.com *.mydomain.com #{Gitlab.config.gitlab.host}))
|
|
|
|
stub_asset_proxy_setting(domain_regexp: Banzai::Filter::AssetProxyFilter.compile_allowlist(Gitlab.config.asset_proxy.allowlist))
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces a lazy loaded img src' do
|
|
|
|
output = described_class.to_html(image, project: project)
|
|
|
|
doc = Nokogiri::HTML.fragment(output)
|
|
|
|
result = doc.css('img').first
|
|
|
|
|
|
|
|
expect(result['data-src']).to eq(proxy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'autolinks images to the proxy' do
|
|
|
|
output = described_class.to_html(image, project: project)
|
|
|
|
doc = Nokogiri::HTML.fragment(output)
|
|
|
|
result = doc.css('a').first
|
|
|
|
|
|
|
|
expect(result['href']).to eq(proxy)
|
|
|
|
expect(result['data-canonical-src']).to eq('http://example.com/test.png')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'properly adds tooltips to link for IDN images' do
|
|
|
|
image = '![proxy](http://exa😄mple.com/test.png)'
|
|
|
|
proxy = 'https://assets.example.com/6d8b634c412a23c6bfe1b2963f174febf5635ddd/687474703a2f2f6578612546302539462539382538346d706c652e636f6d2f746573742e706e67'
|
|
|
|
output = described_class.to_html(image, project: project)
|
|
|
|
doc = Nokogiri::HTML.fragment(output)
|
|
|
|
result = doc.css('a').first
|
|
|
|
|
|
|
|
expect(result['href']).to eq(proxy)
|
|
|
|
expect(result['data-canonical-src']).to eq('http://exa%F0%9F%98%84mple.com/test.png')
|
|
|
|
expect(result['title']).to eq 'http://xn--example-6p25f.com/test.png'
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|