debian-mirror-gitlab/spec/lib/banzai/filter/video_link_filter_spec.rb

117 lines
2.9 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Banzai::Filter::VideoLinkFilter do
2016-08-24 12:49:21 +05:30
def filter(doc, contexts = {})
contexts.reverse_merge!({
project: project
})
described_class.call(doc, contexts)
end
def link_to_image(path)
2019-12-21 20:55:43 +05:30
return '<img/>' if path.nil?
%(<img src="#{path}"/>)
2016-08-24 12:49:21 +05:30
end
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2016-08-24 12:49:21 +05:30
2019-12-21 20:55:43 +05:30
shared_examples 'a video element' do
let(:image) { link_to_image(src) }
2016-08-24 12:49:21 +05:30
2019-12-21 20:55:43 +05:30
it 'replaces the image tag with a video tag' do
container = filter(image).children.first
2016-08-24 12:49:21 +05:30
2021-11-11 11:23:49 +05:30
expect(container.name).to eq 'span'
expect(container['class']).to eq 'media-container video-container'
2016-08-24 12:49:21 +05:30
2021-11-11 11:23:49 +05:30
video, link = container.children
2016-08-24 12:49:21 +05:30
2019-12-21 20:55:43 +05:30
expect(video.name).to eq 'video'
expect(video['src']).to eq src
2019-12-26 22:10:19 +05:30
expect(video['width']).to eq "400"
2021-04-17 20:07:23 +05:30
expect(video['preload']).to eq 'metadata'
2016-08-24 12:49:21 +05:30
2019-12-21 20:55:43 +05:30
expect(link.name).to eq 'a'
expect(link['href']).to eq src
expect(link['target']).to eq '_blank'
2016-08-24 12:49:21 +05:30
end
end
2019-12-21 20:55:43 +05:30
shared_examples 'an unchanged element' do |ext|
2016-08-24 12:49:21 +05:30
it 'leaves the document unchanged' do
2019-12-21 20:55:43 +05:30
element = filter(link_to_image(src)).children.first
2016-08-24 12:49:21 +05:30
expect(element.name).to eq 'img'
2019-12-21 20:55:43 +05:30
expect(element['src']).to eq src
2016-08-24 12:49:21 +05:30
end
end
2019-09-04 21:01:54 +05:30
2019-12-21 20:55:43 +05:30
context 'when the element src has a video extension' do
Gitlab::FileTypeDetection::SAFE_VIDEO_EXT.each do |ext|
it_behaves_like 'a video element' do
let(:src) { "/path/video.#{ext}" }
end
it_behaves_like 'a video element' do
let(:src) { "/path/video.#{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_video.somemp4' }
it_behaves_like 'an unchanged element'
end
context 'when data-canonical-src is empty' do
let(:image) { %(<img src="#{src}" data-canonical-src=""/>) }
2019-09-04 21:01:54 +05:30
2019-12-21 20:55:43 +05:30
context 'and src is a video' do
let(:src) { '/path/video.mp4' }
it_behaves_like 'a video 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
2019-09-04 21:01:54 +05:30
proxy_src = 'https://assets.example.com/6d8b63'
canonical_src = 'http://example.com/test.mp4'
2019-12-21 20:55:43 +05:30
image = %(<img src="#{proxy_src}" data-canonical-src="#{canonical_src}"/>)
container = filter(image).children.first
2019-09-04 21:01:54 +05:30
2021-11-11 11:23:49 +05:30
expect(container['class']).to eq 'media-container video-container'
2019-09-04 21:01:54 +05:30
2021-11-11 11:23:49 +05:30
video, link = container.children
2019-09-04 21:01:54 +05:30
expect(video['src']).to eq proxy_src
expect(video['data-canonical-src']).to eq canonical_src
expect(link['href']).to eq proxy_src
end
end
2016-08-24 12:49:21 +05:30
end