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

describe Banzai::Filter::WikiLinkFilter, lib: true do
  include FilterSpecHelper

  let(:namespace) { build_stubbed(:namespace, name: "wiki_link_ns") }
  let(:project)   { build_stubbed(:empty_project, :public, name: "wiki_link_project", namespace: namespace) }
  let(:user) { double }
  let(:project_wiki) { ProjectWiki.new(project, user) }

  describe "links within the wiki (relative)" do
    describe "hierarchical links to the current directory" do
      it "doesn't rewrite non-file links" do
        link = "<a href='./page'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to eq('./page')
      end

      it "doesn't rewrite file links" do
        link = "<a href='./page.md'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to eq('./page.md')
      end
    end

    describe "hierarchical links to the parent directory" do
      it "doesn't rewrite non-file links" do
        link = "<a href='../page'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to eq('../page')
      end

      it "doesn't rewrite file links" do
        link = "<a href='../page.md'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to eq('../page.md')
      end
    end

    describe "hierarchical links to a sub-directory" do
      it "doesn't rewrite non-file links" do
        link = "<a href='./subdirectory/page'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to eq('./subdirectory/page')
      end

      it "doesn't rewrite file links" do
        link = "<a href='./subdirectory/page.md'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to eq('./subdirectory/page.md')
      end
    end

    describe "non-hierarchical links" do
      it 'rewrites non-file links to be at the scope of the wiki root' do
        link = "<a href='page'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to match('/wiki_link_ns/wiki_link_project/wikis/page')
      end

      it "doesn't rewrite file links" do
        link = "<a href='page.md'>Link to Page</a>"
        filtered_link = filter(link, project_wiki: project_wiki).children[0]

        expect(filtered_link.attribute('href').value).to eq('page.md')
      end
    end
  end

  describe "links outside the wiki (absolute)" do
    it "doesn't rewrite links" do
      link = "<a href='http://example.com/page'>Link to Page</a>"
      filtered_link = filter(link, project_wiki: project_wiki).children[0]

      expect(filtered_link.attribute('href').value).to eq('http://example.com/page')
    end
  end
end