summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/hook_data/base_builder_spec.rb
blob: e3c5ee3b90516c0d671ec4d6b4a60a1bd20b5214 (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
require 'spec_helper'

describe Gitlab::HookData::BaseBuilder do
  describe '#absolute_image_urls' do
    let(:subclass) do
      Class.new(described_class) do
        public :absolute_image_urls
      end
    end

    using RSpec::Parameterized::TableSyntax

    context 'with an upload prefix specified' do
      let(:project_with_path) { double(full_path: 'baz/bar') }
      let(:object_with_project) { double(project: project_with_path) }
      subject { subclass.new(object_with_project) }

      where do
        {
          'relative image URL' => {
            input: '![an image](foo.png)',
            output: "![an image](#{Gitlab.config.gitlab.url}/foo.png)"
          },
          'absolute upload URL' => {
            input: '![an image](/uploads/foo.png)',
            output: "![an image](#{Gitlab.config.gitlab.url}/baz/bar/uploads/foo.png)"
          },
          'absolute non-upload URL' => {
            input: '![an image](/downloads/foo.png)',
            output: "![an image](#{Gitlab.config.gitlab.url}/downloads/foo.png)"
          }
        }
      end

      with_them do
        it { expect(subject.absolute_image_urls(input)).to eq(output) }
      end
    end

    context 'without an upload prefix specified' do
      subject { subclass.new(nil) }

      where do
        {
          'relative image URL' => {
            input: '![an image](foo.png)',
            output: "![an image](#{Gitlab.config.gitlab.url}/foo.png)"
          },
          'absolute upload URL' => {
            input: '![an image](/uploads/foo.png)',
            output: "![an image](#{Gitlab.config.gitlab.url}/uploads/foo.png)"
          },
          'absolute non-upload URL' => {
            input: '![an image](/downloads/foo.png)',
            output: "![an image](#{Gitlab.config.gitlab.url}/downloads/foo.png)"
          },
          'HTTP URL' => {
            input: '![an image](http://example.com/foo.png)',
            output: '![an image](http://example.com/foo.png)'
          },
          'HTTPS URL' => {
            input: '![an image](https://example.com/foo.png)',
            output: '![an image](https://example.com/foo.png)'
          },
          'protocol-relative URL' => {
            input: '![an image](//example.com/foo.png)',
            output: '![an image](//example.com/foo.png)'
          },
          'URL reference by title' => {
            input: "![foo]\n\n[foo]: foo.png",
            output: "![foo]\n\n[foo]: foo.png"
          },
          'URL reference by label' => {
            input: "![][foo]\n\n[foo]: foo.png",
            output: "![][foo]\n\n[foo]: foo.png"
          },
          'in Markdown inline code block' => {
            input: '`![an image](foo.png)`',
            output: "`![an image](#{Gitlab.config.gitlab.url}/foo.png)`"
          },
          'in HTML tag on the same line' => {
            input: '<p>![an image](foo.png)</p>',
            output: "<p>![an image](#{Gitlab.config.gitlab.url}/foo.png)</p>"
          },
          'in Markdown multi-line code block' => {
            input: "```\n![an image](foo.png)\n```",
            output: "```\n![an image](foo.png)\n```"
          },
          'in HTML tag on different lines' => {
            input: "<p>\n![an image](foo.png)\n</p>",
            output: "<p>\n![an image](foo.png)\n</p>"
          }
        }
      end

      with_them do
        it { expect(subject.absolute_image_urls(input)).to eq(output) }
      end
    end
  end
end