diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-09 15:06:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-09 15:06:13 +0300 |
commit | 0a850868dfb85086cba8320cee9dac4657dcae6c (patch) | |
tree | 40d17228fe23d9db7b861fe2a20d024d64c50323 /spec/lib/banzai | |
parent | 3744bcc0d10d24104e39985b6833a0ec51791c0a (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/banzai')
-rw-r--r-- | spec/lib/banzai/filter/audio_link_filter_spec.rb | 120 | ||||
-rw-r--r-- | spec/lib/banzai/filter/relative_link_filter_spec.rb | 17 | ||||
-rw-r--r-- | spec/lib/banzai/filter/wiki_link_filter_spec.rb | 8 | ||||
-rw-r--r-- | spec/lib/banzai/pipeline/wiki_pipeline_spec.rb | 24 |
4 files changed, 164 insertions, 5 deletions
diff --git a/spec/lib/banzai/filter/audio_link_filter_spec.rb b/spec/lib/banzai/filter/audio_link_filter_spec.rb new file mode 100644 index 00000000000..a8459137169 --- /dev/null +++ b/spec/lib/banzai/filter/audio_link_filter_spec.rb @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Banzai::Filter::AudioLinkFilter do + def filter(doc, contexts = {}) + contexts.reverse_merge!({ + project: project + }) + + described_class.call(doc, contexts) + end + + def link_to_image(path) + return '<img/>' if path.nil? + + %(<img src="#{path}"/>) + end + + let(:project) { create(:project, :repository) } + + shared_examples 'an audio element' do + let(:image) { link_to_image(src) } + + it 'replaces the image tag with an audio tag' do + container = filter(image).children.first + + expect(container.name).to eq 'div' + expect(container['class']).to eq 'audio-container' + + audio, paragraph = container.children + + expect(audio.name).to eq 'audio' + expect(audio['src']).to eq src + + expect(paragraph.name).to eq 'p' + + link = paragraph.children.first + + expect(link.name).to eq 'a' + expect(link['href']).to eq src + expect(link['target']).to eq '_blank' + end + end + + shared_examples 'an unchanged element' do |ext| + it 'leaves the document unchanged' do + element = filter(link_to_image(src)).children.first + + expect(element.name).to eq 'img' + expect(element['src']).to eq src + end + end + + context 'when the element src has an audio extension' do + Gitlab::FileTypeDetection::SAFE_AUDIO_EXT.each do |ext| + it_behaves_like 'an audio element' do + let(:src) { "/path/audio.#{ext}" } + end + + it_behaves_like 'an audio element' do + let(:src) { "/path/audio.#{ext.upcase}" } + end + end + end + + context 'when the element has no src attribute' do + let(:src) { nil } + + it_behaves_like 'an unchanged element' + end + + context 'when the element src is an image' do + let(:src) { '/path/my_image.jpg' } + + it_behaves_like 'an unchanged element' + end + + context 'when the element src has an invalid file extension' do + let(:src) { '/path/my_audio.somewav' } + + it_behaves_like 'an unchanged element' + end + + context 'when data-canonical-src is empty' do + let(:image) { %(<img src="#{src}" data-canonical-src=""/>) } + + context 'and src is audio' do + let(:src) { '/path/audio.wav' } + + it_behaves_like 'an audio element' + end + + context 'and src is an image' do + let(:src) { '/path/my_image.jpg' } + + it_behaves_like 'an unchanged element' + end + end + + context 'when data-canonical-src is set' do + it 'uses the correct src' do + proxy_src = 'https://assets.example.com/6d8b63' + canonical_src = 'http://example.com/test.wav' + image = %(<img src="#{proxy_src}" data-canonical-src="#{canonical_src}"/>) + container = filter(image).children.first + + expect(container['class']).to eq 'audio-container' + + audio, paragraph = container.children + + expect(audio['src']).to eq proxy_src + expect(audio['data-canonical-src']).to eq canonical_src + + link = paragraph.children.first + + expect(link['href']).to eq proxy_src + end + end +end diff --git a/spec/lib/banzai/filter/relative_link_filter_spec.rb b/spec/lib/banzai/filter/relative_link_filter_spec.rb index f983265ce71..046c346a7ac 100644 --- a/spec/lib/banzai/filter/relative_link_filter_spec.rb +++ b/spec/lib/banzai/filter/relative_link_filter_spec.rb @@ -29,6 +29,10 @@ describe Banzai::Filter::RelativeLinkFilter do %(<video src="#{path}"></video>) end + def audio(path) + %(<audio src="#{path}"></audio>) + end + def link(path) %(<a href="#{path}">#{path}</a>) end @@ -82,6 +86,12 @@ describe Banzai::Filter::RelativeLinkFilter do expect(doc.at_css('video')['src']).to eq 'files/videos/intro.mp4' end + + it 'does not modify any relative URL in audio' do + doc = filter(audio('files/audio/sample.wav'), commit: project.commit('audio'), ref: 'audio') + + expect(doc.at_css('audio')['src']).to eq 'files/audio/sample.wav' + end end context 'with a project_wiki' do @@ -218,6 +228,13 @@ describe Banzai::Filter::RelativeLinkFilter do .to eq "/#{project_path}/raw/video/files/videos/intro.mp4" end + it 'rebuilds relative URL for audio in the repo' do + doc = filter(audio('files/audio/sample.wav'), commit: project.commit('audio'), ref: 'audio') + + expect(doc.at_css('audio')['src']) + .to eq "/#{project_path}/raw/audio/files/audio/sample.wav" + end + it 'does not modify relative URL with an anchor only' do doc = filter(link('#section-1')) expect(doc.at_css('a')['href']).to eq '#section-1' diff --git a/spec/lib/banzai/filter/wiki_link_filter_spec.rb b/spec/lib/banzai/filter/wiki_link_filter_spec.rb index d2d539a62fc..4587bd85939 100644 --- a/spec/lib/banzai/filter/wiki_link_filter_spec.rb +++ b/spec/lib/banzai/filter/wiki_link_filter_spec.rb @@ -60,6 +60,14 @@ describe Banzai::Filter::WikiLinkFilter do expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.mp4") end end + + context 'with "audio" html tag' do + it 'rewrites links' do + filtered_link = filter("<audio src='#{repository_upload_folder}/a.wav'></audio>", project_wiki: wiki).children[0] + + expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.wav") + end + end end describe "invalid links" do diff --git a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb index 4a485fbc2bd..26f2b0b0acf 100644 --- a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb @@ -260,11 +260,11 @@ describe Banzai::Pipeline::WikiPipeline do end end - describe 'videos' do - let(:namespace) { create(:namespace, name: "wiki_link_ns") } - let(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) } - let(:project_wiki) { ProjectWiki.new(project, double(:user)) } - let(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) } + describe 'videos and audio' do + let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") } + let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) } + let_it_be(:project_wiki) { ProjectWiki.new(project, double(:user)) } + let_it_be(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) } it 'generates video html structure' do markdown = "![video_file](video_file_name.mp4)" @@ -279,5 +279,19 @@ describe Banzai::Pipeline::WikiPipeline do expect(output).to include('<video src="/wiki_link_ns/wiki_link_project/wikis/nested/twice/video%20file%20name.mp4"') end + + it 'generates audio html structure' do + markdown = "![audio_file](audio_file_name.wav)" + output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug) + + expect(output).to include('<audio src="/wiki_link_ns/wiki_link_project/wikis/nested/twice/audio_file_name.wav"') + end + + it 'rewrites and replaces audio links names with white spaces to %20' do + markdown = "![audio file](audio file name.wav)" + output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug) + + expect(output).to include('<audio src="/wiki_link_ns/wiki_link_project/wikis/nested/twice/audio%20file%20name.wav"') + end end end |