2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Banzai::Filter::ExternalIssueReferenceFilter do
|
2015-12-23 02:04:40 +05:30
|
|
|
include FilterSpecHelper
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
shared_examples_for "external issue tracker" do
|
2017-08-17 22:00:37 +05:30
|
|
|
it_behaves_like 'a reference containing an element node'
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
it 'requires project context' do
|
|
|
|
expect { described_class.call('') }.to raise_error(ArgumentError, /:project/)
|
|
|
|
end
|
|
|
|
|
|
|
|
%w(pre code a style).each do |elem|
|
|
|
|
it "ignores valid references contained inside '#{elem}' element" do
|
|
|
|
exp = act = "<#{elem}>Issue #{reference}</#{elem}>"
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores valid references when using default tracker' do
|
|
|
|
expect(project).to receive(:default_issues_tracker?).and_return(true)
|
|
|
|
|
|
|
|
exp = act = "Issue #{reference}"
|
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'links to a valid reference' do
|
|
|
|
doc = filter("Issue #{reference}")
|
2016-11-03 12:29:30 +05:30
|
|
|
issue_id = doc.css('a').first.attr("data-external-issue")
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
expect(doc.css('a').first.attr('href'))
|
2020-06-23 00:09:42 +05:30
|
|
|
.to eq project.external_issue_tracker.issue_url(issue_id)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'links to the external tracker' do
|
|
|
|
doc = filter("Issue #{reference}")
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
link = doc.css('a').first.attr('href')
|
2016-11-03 12:29:30 +05:30
|
|
|
issue_id = doc.css('a').first.attr("data-external-issue")
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(link).to eq(project.external_issue_tracker.issue_url(issue_id))
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'links with adjacent text' do
|
|
|
|
doc = filter("Issue (#{reference}.)")
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(doc.to_html).to match(%r{\(<a.+>#{reference}</a>\.\)})
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a title attribute' do
|
|
|
|
doc = filter("Issue #{reference}")
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(doc.css('a').first.attr('title')).to include("Issue in #{project.external_issue_tracker.title}")
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'escapes the title attribute' do
|
2017-09-10 17:25:29 +05:30
|
|
|
allow(project.external_issue_tracker).to receive(:title)
|
|
|
|
.and_return(%{"></a>whatever<a title="})
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
doc = filter("Issue #{reference}")
|
|
|
|
expect(doc.text).to eq "Issue #{reference}"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes default classes' do
|
|
|
|
doc = filter("Issue #{reference}")
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-issue has-tooltip'
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports an :only_path context' do
|
|
|
|
doc = filter("Issue #{reference}", only_path: true)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
link = doc.css('a').first.attr('href')
|
2016-11-03 12:29:30 +05:30
|
|
|
issue_id = doc.css('a').first["data-external-issue"]
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(link).to eq project.external_issue_tracker.issue_path(issue_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has an empty link if issue_url is invalid' do
|
|
|
|
expect_any_instance_of(project.external_issue_tracker.class).to receive(:issue_url) { 'javascript:alert("foo");' }
|
|
|
|
|
|
|
|
doc = filter("Issue #{reference}")
|
|
|
|
link = doc.css('a').first.attr('href')
|
|
|
|
|
|
|
|
expect(link).to eq ''
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has an empty link if issue_path is invalid' do
|
|
|
|
expect_any_instance_of(project.external_issue_tracker.class).to receive(:issue_path) { 'javascript:alert("foo");' }
|
|
|
|
|
|
|
|
doc = filter("Issue #{reference}", only_path: true)
|
|
|
|
link = doc.css('a').first.attr('href')
|
|
|
|
|
|
|
|
expect(link).to eq ''
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
context 'with RequestStore enabled', :request_store do
|
2016-11-03 12:29:30 +05:30
|
|
|
let(:reference_filter) { HTML::Pipeline.new([described_class]) }
|
|
|
|
|
|
|
|
it 'queries the collection on the first call' do
|
|
|
|
expect_any_instance_of(Project).to receive(:default_issues_tracker?).once.and_call_original
|
2017-08-17 22:00:37 +05:30
|
|
|
expect_any_instance_of(Project).to receive(:external_issue_reference_pattern).once.and_call_original
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
not_cached = reference_filter.call("look for #{reference}", { project: project })
|
|
|
|
|
|
|
|
expect_any_instance_of(Project).not_to receive(:default_issues_tracker?)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect_any_instance_of(Project).not_to receive(:external_issue_reference_pattern)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
cached = reference_filter.call("look for #{reference}", { project: project })
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
# Links must be the same
|
|
|
|
expect(cached[:output].css('a').first[:href]).to eq(not_cached[:output].css('a').first[:href])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "redmine project" do
|
|
|
|
let(:project) { create(:redmine_project) }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
2018-12-13 13:39:08 +05:30
|
|
|
project.update!(issues_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a hash prefix" do
|
|
|
|
let(:issue) { ExternalIssue.new("#123", project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a single-letter prefix" do
|
|
|
|
let(:issue) { ExternalIssue.new("T-123", project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "youtrack project" do
|
|
|
|
let(:project) { create(:youtrack_project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.update!(issues_enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with right markdown" do
|
|
|
|
let(:issue) { ExternalIssue.new("YT-123", project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with underscores in the prefix" do
|
|
|
|
let(:issue) { ExternalIssue.new("PRJ_1-123", project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with lowercase letters in the prefix" do
|
|
|
|
let(:issue) { ExternalIssue.new("YTkPrj-123", project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
context "with a single-letter prefix" do
|
|
|
|
let(:issue) { ExternalIssue.new("T-123", project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
context "with a lowercase prefix" do
|
|
|
|
let(:issue) { ExternalIssue.new("gl-030", project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context "jira project" do
|
|
|
|
let(:project) { create(:jira_project) }
|
|
|
|
let(:reference) { issue.to_reference }
|
|
|
|
|
|
|
|
context "with right markdown" do
|
|
|
|
let(:issue) { ExternalIssue.new("JIRA-123", project) }
|
|
|
|
|
|
|
|
it_behaves_like "external issue tracker"
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
context "with a single-letter prefix" do
|
|
|
|
let(:issue) { ExternalIssue.new("J-123", project) }
|
|
|
|
|
|
|
|
it "ignores reference" do
|
|
|
|
exp = act = "Issue #{reference}"
|
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
context "with wrong markdown" do
|
|
|
|
let(:issue) { ExternalIssue.new("#123", project) }
|
|
|
|
|
|
|
|
it "ignores reference" do
|
|
|
|
exp = act = "Issue #{reference}"
|
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|