summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/absolute_link_filter_spec.rb
blob: 50be551cd90f3df6f101604fd6da1214a477c413 (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
require 'spec_helper'

describe Banzai::Filter::AbsoluteLinkFilter do
  def filter(doc, context = {})
    described_class.call(doc, context)
  end

  context 'with html links' do
    context 'if only_path is false' do
      let(:only_path_context) do
        { only_path: false }
      end
      let(:fake_url) { 'http://www.example.com' }

      before do
        allow(Gitlab.config.gitlab).to receive(:url).and_return(fake_url)
      end

      context 'has the .gfm class' do
        it 'converts a relative url into absolute' do
          doc = filter(link('/foo', 'gfm'), only_path_context)
          expect(doc.at_css('a')['href']).to eq "#{fake_url}/foo"
        end

        it 'does not change the url if it already absolute' do
          doc = filter(link("#{fake_url}/foo", 'gfm'), only_path_context)
          expect(doc.at_css('a')['href']).to eq "#{fake_url}/foo"
        end

        context 'if relative_url_root is set' do
          it 'joins the url without doubling the path' do
            allow(Gitlab.config.gitlab).to receive(:url).and_return("#{fake_url}/gitlab/")
            doc = filter(link("/gitlab/foo", 'gfm'), only_path_context)
            expect(doc.at_css('a')['href']).to eq "#{fake_url}/gitlab/foo"
          end
        end
      end

      context 'has not the .gfm class' do
        it 'does not convert a relative url into absolute' do
          doc = filter(link('/foo'), only_path_context)
          expect(doc.at_css('a')['href']).to eq '/foo'
        end
      end
    end

    context 'if only_path is not false' do
      it 'does not convert a relative url into absolute' do
        expect(filter(link('/foo', 'gfm')).at_css('a')['href']).to eq '/foo'
        expect(filter(link('/foo')).at_css('a')['href']).to eq '/foo'
      end
    end
  end

  def link(path, css_class = '')
    %(<a class="#{css_class}" href="#{path}">example</a>)
  end
end