2015-11-19 15:18:13 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
describe Banzai::Filter::UploadLinkFilter, lib: true do
|
2015-11-19 15:18:13 +00:00
|
|
|
def filter(doc, contexts = {})
|
|
|
|
contexts.reverse_merge!({
|
|
|
|
project: project
|
|
|
|
})
|
|
|
|
|
2016-05-07 19:28:04 +00:00
|
|
|
raw_filter(doc, contexts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_filter(doc, contexts = {})
|
2015-11-19 15:18:13 +00:00
|
|
|
described_class.call(doc, contexts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def image(path)
|
|
|
|
%(<img src="#{path}" />)
|
|
|
|
end
|
|
|
|
|
|
|
|
def link(path)
|
|
|
|
%(<a href="#{path}">#{path}</a>)
|
|
|
|
end
|
|
|
|
|
2016-06-15 17:00:50 +00:00
|
|
|
def nested_image(path)
|
|
|
|
%(<div><img src="#{path}" /></div>)
|
|
|
|
end
|
|
|
|
|
|
|
|
def nested_link(path)
|
|
|
|
%(<div><a href="#{path}">#{path}</a></div>)
|
|
|
|
end
|
|
|
|
|
2015-11-19 15:18:13 +00:00
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
|
|
|
shared_examples :preserve_unchanged do
|
|
|
|
it 'does not modify any relative URL in anchor' do
|
|
|
|
doc = filter(link('README.md'))
|
|
|
|
expect(doc.at_css('a')['href']).to eq 'README.md'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not modify any relative URL in image' do
|
|
|
|
doc = filter(image('files/images/logo-black.png'))
|
|
|
|
expect(doc.at_css('img')['src']).to eq 'files/images/logo-black.png'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an exception on invalid URIs' do
|
|
|
|
act = link("://foo")
|
|
|
|
expect { filter(act) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a valid repository' do
|
|
|
|
it 'rebuilds relative URL for a link' do
|
|
|
|
doc = filter(link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))
|
|
|
|
expect(doc.at_css('a')['href']).
|
|
|
|
to eq "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg"
|
2016-06-15 17:00:50 +00:00
|
|
|
|
|
|
|
doc = filter(nested_link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))
|
|
|
|
expect(doc.at_css('a')['href']).
|
|
|
|
to eq "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg"
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'rebuilds relative URL for an image' do
|
2016-06-15 17:00:50 +00:00
|
|
|
doc = filter(image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))
|
|
|
|
expect(doc.at_css('img')['src']).
|
|
|
|
to eq "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg"
|
|
|
|
|
|
|
|
doc = filter(nested_image('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg'))
|
|
|
|
expect(doc.at_css('img')['src']).
|
2015-11-19 15:18:13 +00:00
|
|
|
to eq "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not modify absolute URL' do
|
|
|
|
doc = filter(link('http://example.com'))
|
|
|
|
expect(doc.at_css('a')['href']).to eq 'http://example.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports Unicode filenames' do
|
|
|
|
path = '/uploads/한글.png'
|
|
|
|
escaped = Addressable::URI.escape(path)
|
|
|
|
|
|
|
|
# Stub these methods so the file doesn't actually need to be in the repo
|
|
|
|
allow_any_instance_of(described_class).
|
|
|
|
to receive(:file_exists?).and_return(true)
|
|
|
|
allow_any_instance_of(described_class).
|
|
|
|
to receive(:image?).with(path).and_return(true)
|
|
|
|
|
|
|
|
doc = filter(image(escaped))
|
|
|
|
expect(doc.at_css('img')['src']).to match "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/%ED%95%9C%EA%B8%80.png"
|
|
|
|
end
|
|
|
|
end
|
2016-05-07 19:28:04 +00:00
|
|
|
|
|
|
|
context 'when project context does not exist' do
|
|
|
|
let(:upload_link) { link('/uploads/e90decf88d8f96fe9e1389afc2e4a91f/test.jpg') }
|
|
|
|
|
|
|
|
it 'does not raise error' do
|
2016-05-10 11:46:04 +00:00
|
|
|
expect { raw_filter(upload_link, project: nil) }.not_to raise_error
|
2016-05-07 19:28:04 +00:00
|
|
|
end
|
|
|
|
|
2016-05-10 11:46:04 +00:00
|
|
|
it 'does not rewrite link' do
|
2016-05-07 19:28:04 +00:00
|
|
|
doc = raw_filter(upload_link, project: nil)
|
|
|
|
|
2016-05-10 11:46:04 +00:00
|
|
|
expect(doc.to_html).to eq upload_link
|
2016-05-07 19:28:04 +00:00
|
|
|
end
|
|
|
|
end
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|