summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/video_link_filter_spec.rb
blob: 32d7f6cfefa0f3f3dde48a3e84313a5b6dd58e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Banzai::Filter::VideoLinkFilter, feature_category: :team_planning do
  def filter(doc, contexts = {})
    contexts.reverse_merge!({
      project: project
    })

    described_class.call(doc, contexts)
  end

  def link_to_image(path, height = nil, width = nil)
    return '<img/>' if path.nil?

    attrs = %(src="#{path}")
    attrs += %( width="#{width}") if width
    attrs += %( height="#{height}") if height

    %(<img #{attrs}/>)
  end

  let(:project) { create(:project, :repository) }

  shared_examples 'a video element' do
    let(:image) { link_to_image(src, height, width) }

    it 'replaces the image tag with a video tag' do
      container = filter(image).children.first

      expect(container.name).to eq 'span'
      expect(container['class']).to eq 'media-container video-container'

      video, link = container.children

      expect(video.name).to eq 'video'
      expect(video['src']).to eq src
      expect(video['height']).to eq height if height
      expect(video['width']).to eq width if width
      expect(video['width']).to eq '400' unless width || height
      expect(video['preload']).to eq 'metadata'

      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 a video extension' do
    let(:height) { nil }
    let(:width) { nil }

    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 height or width specified' do
    let(:src) { '/path/video.mp4' }

    it_behaves_like 'a video element' do
      let(:height) { '100%' }
      let(:width) { '50px' }
    end

    it_behaves_like 'a video element' do
      let(:height) { nil }
      let(:width) { '50px' }
    end

    it_behaves_like 'a video element' do
      let(:height) { '50px' }
      let(:width) { nil }
    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=""/>) }

    context 'and src is a video' do
      let(:src) { '/path/video.mp4' }
      let(:height) { nil }
      let(:width) { nil }

      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
      proxy_src = 'https://assets.example.com/6d8b63'
      canonical_src = 'http://example.com/test.mp4'
      image = %(<img src="#{proxy_src}" data-canonical-src="#{canonical_src}"/>)
      container = filter(image).children.first

      expect(container['class']).to eq 'media-container video-container'

      video, link = container.children

      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
end