2015-11-19 15:18:13 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Banzai::Filter::SnippetReferenceFilter do
|
2015-11-19 15:18:13 +00:00
|
|
|
include FilterSpecHelper
|
|
|
|
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :public) }
|
2015-11-19 15:18:13 +00:00
|
|
|
let(:snippet) { create(:project_snippet, project: project) }
|
|
|
|
let(:reference) { snippet.to_reference }
|
|
|
|
|
|
|
|
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}>Snippet #{reference}</#{elem}>"
|
2015-12-07 13:48:53 +00:00
|
|
|
expect(reference_filter(act).to_html).to eq exp
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'internal reference' do
|
|
|
|
it 'links to a valid reference' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("See #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.css('a').first.attr('href')).to eq urls
|
2017-06-29 17:06:35 +00:00
|
|
|
.project_snippet_url(project, snippet)
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'links with adjacent text' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Snippet (#{reference}.)")
|
2018-01-27 05:35:53 +00:00
|
|
|
expect(doc.to_html).to match(%r{\(<a.+>#{Regexp.escape(reference)}</a>\.\)})
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores invalid snippet IDs' do
|
|
|
|
exp = act = "Snippet #{invalidate_reference(reference)}"
|
|
|
|
|
2015-12-07 13:48:53 +00:00
|
|
|
expect(reference_filter(act).to_html).to eq exp
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a title attribute' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Snippet #{reference}")
|
2016-07-17 00:03:56 +00:00
|
|
|
expect(doc.css('a').first.attr('title')).to eq snippet.title
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'escapes the title attribute' do
|
|
|
|
snippet.update_attribute(:title, %{"></a>whatever<a title="})
|
|
|
|
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Snippet #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
expect(doc.text).to eq "Snippet #{reference}"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes default classes' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Snippet #{reference}")
|
2016-07-17 00:03:56 +00:00
|
|
|
expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-snippet has-tooltip'
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a data-project attribute' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Snippet #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
link = doc.css('a').first
|
|
|
|
|
|
|
|
expect(link).to have_attribute('data-project')
|
|
|
|
expect(link.attr('data-project')).to eq project.id.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a data-snippet attribute' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("See #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
link = doc.css('a').first
|
|
|
|
|
|
|
|
expect(link).to have_attribute('data-snippet')
|
|
|
|
expect(link.attr('data-snippet')).to eq snippet.id.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports an :only_path context' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Snippet #{reference}", only_path: true)
|
2015-11-19 15:18:13 +00:00
|
|
|
link = doc.css('a').first.attr('href')
|
|
|
|
|
|
|
|
expect(link).not_to match %r(https?://)
|
2017-06-29 17:06:35 +00:00
|
|
|
expect(link).to eq urls.project_snippet_url(project, snippet, only_path: true)
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-02 23:49:13 +00:00
|
|
|
context 'cross-project / cross-namespace complete reference' do
|
|
|
|
let(:namespace) { create(:namespace) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project2) { create(:project, :public, namespace: namespace) }
|
2016-11-02 23:49:13 +00:00
|
|
|
let!(:snippet) { create(:project_snippet, project: project2) }
|
2017-07-20 09:34:09 +00:00
|
|
|
let(:reference) { "#{project2.full_path}$#{snippet.id}" }
|
2015-11-19 15:18:13 +00:00
|
|
|
|
|
|
|
it 'links to a valid reference' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("See #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.css('a').first.attr('href'))
|
2017-06-29 17:06:35 +00:00
|
|
|
.to eq urls.project_snippet_url(project2, snippet)
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
2016-11-02 23:49:13 +00:00
|
|
|
it 'link has valid text' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("See (#{reference}.)")
|
2016-11-02 23:49:13 +00:00
|
|
|
|
|
|
|
expect(doc.css('a').first.text).to eql(reference)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has valid text' do
|
|
|
|
doc = reference_filter("See (#{reference}.)")
|
|
|
|
|
|
|
|
expect(doc.text).to eql("See (#{reference}.)")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores invalid snippet IDs on the referenced project' do
|
|
|
|
exp = act = "See #{invalidate_reference(reference)}"
|
|
|
|
|
|
|
|
expect(reference_filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cross-project / same-namespace complete reference' do
|
|
|
|
let(:namespace) { create(:namespace) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :public, namespace: namespace) }
|
|
|
|
let(:project2) { create(:project, :public, namespace: namespace) }
|
2016-11-02 23:49:13 +00:00
|
|
|
let!(:snippet) { create(:project_snippet, project: project2) }
|
2017-07-20 09:34:09 +00:00
|
|
|
let(:reference) { "#{project2.full_path}$#{snippet.id}" }
|
2016-11-02 23:49:13 +00:00
|
|
|
|
|
|
|
it 'links to a valid reference' do
|
|
|
|
doc = reference_filter("See #{reference}")
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.css('a').first.attr('href'))
|
2017-06-29 17:06:35 +00:00
|
|
|
.to eq urls.project_snippet_url(project2, snippet)
|
2016-11-02 23:49:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'link has valid text' do
|
|
|
|
doc = reference_filter("See (#{project2.path}$#{snippet.id}.)")
|
|
|
|
|
|
|
|
expect(doc.css('a').first.text).to eql("#{project2.path}$#{snippet.id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has valid text' do
|
|
|
|
doc = reference_filter("See (#{project2.path}$#{snippet.id}.)")
|
|
|
|
|
|
|
|
expect(doc.text).to eql("See (#{project2.path}$#{snippet.id}.)")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores invalid snippet IDs on the referenced project' do
|
|
|
|
exp = act = "See #{invalidate_reference(reference)}"
|
|
|
|
|
|
|
|
expect(reference_filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cross-project shorthand reference' do
|
|
|
|
let(:namespace) { create(:namespace) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :public, namespace: namespace) }
|
|
|
|
let(:project2) { create(:project, :public, namespace: namespace) }
|
2016-11-02 23:49:13 +00:00
|
|
|
let!(:snippet) { create(:project_snippet, project: project2) }
|
|
|
|
let(:reference) { "#{project2.path}$#{snippet.id}" }
|
|
|
|
|
|
|
|
it 'links to a valid reference' do
|
|
|
|
doc = reference_filter("See #{reference}")
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.css('a').first.attr('href'))
|
2017-06-29 17:06:35 +00:00
|
|
|
.to eq urls.project_snippet_url(project2, snippet)
|
2016-11-02 23:49:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'link has valid text' do
|
|
|
|
doc = reference_filter("See (#{project2.path}$#{snippet.id}.)")
|
|
|
|
|
|
|
|
expect(doc.css('a').first.text).to eql("#{project2.path}$#{snippet.id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has valid text' do
|
|
|
|
doc = reference_filter("See (#{project2.path}$#{snippet.id}.)")
|
|
|
|
|
|
|
|
expect(doc.text).to eql("See (#{project2.path}$#{snippet.id}.)")
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores invalid snippet IDs on the referenced project' do
|
|
|
|
exp = act = "See #{invalidate_reference(reference)}"
|
|
|
|
|
2015-12-07 13:48:53 +00:00
|
|
|
expect(reference_filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cross-project URL reference' do
|
|
|
|
let(:namespace) { create(:namespace, name: 'cross-reference') }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project2) { create(:project, :public, namespace: namespace) }
|
2015-12-07 13:48:53 +00:00
|
|
|
let(:snippet) { create(:project_snippet, project: project2) }
|
2017-06-29 17:06:35 +00:00
|
|
|
let(:reference) { urls.project_snippet_url(project2, snippet) }
|
2015-12-07 13:48:53 +00:00
|
|
|
|
|
|
|
it 'links to a valid reference' do
|
|
|
|
doc = reference_filter("See #{reference}")
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.css('a').first.attr('href'))
|
2017-06-29 17:06:35 +00:00
|
|
|
.to eq urls.project_snippet_url(project2, snippet)
|
2015-12-07 13:48:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'links with adjacent text' do
|
|
|
|
doc = reference_filter("See (#{reference}.)")
|
2018-01-27 05:35:53 +00:00
|
|
|
expect(doc.to_html).to match(%r{\(<a.+>#{Regexp.escape(snippet.to_reference(project))}</a>\.\)})
|
2015-12-07 13:48:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores invalid snippet IDs on the referenced project' do
|
|
|
|
act = "See #{invalidate_reference(reference)}"
|
|
|
|
|
2018-01-27 05:35:53 +00:00
|
|
|
expect(reference_filter(act).to_html).to match(%r{<a.+>#{Regexp.escape(invalidate_reference(reference))}</a>})
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
end
|
2017-11-01 17:35:14 +00:00
|
|
|
|
|
|
|
context 'group context' do
|
|
|
|
it 'links to a valid reference' do
|
|
|
|
reference = "#{project.full_path}$#{snippet.id}"
|
|
|
|
|
|
|
|
result = reference_filter("See #{reference}", { project: nil, group: create(:group) } )
|
|
|
|
|
|
|
|
expect(result.css('a').first.attr('href')).to eq(urls.project_snippet_url(project, snippet))
|
|
|
|
end
|
2018-04-12 09:37:01 +00:00
|
|
|
|
|
|
|
it 'ignores internal references' do
|
|
|
|
exp = act = "See $#{snippet.id}"
|
|
|
|
|
|
|
|
expect(reference_filter(act, project: nil, group: create(:group)).to_html).to eq exp
|
|
|
|
end
|
2017-11-01 17:35:14 +00:00
|
|
|
end
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|