BigW Consortium Gitlab

gitlab_markdown_helper_spec.rb 6.16 KB
Newer Older
1
require 'spec_helper'
Riyad Preukschas committed
2

randx committed
3
describe GitlabMarkdownHelper do
4
  include ApplicationHelper
Douwe Maan committed
5

6
  let!(:project) { create(:project) }
7

8
  let(:user)          { create(:user, username: 'gfm') }
9
  let(:commit)        { project.commit }
10
  let(:issue)         { create(:issue, project: project) }
11
  let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
Andrew8xx8 committed
12
  let(:snippet)       { create(:project_snippet, project: project) }
13

Riyad Preukschas committed
14
  before do
15 16 17
    # Ensure the generated reference links aren't redacted
    project.team << [user, :master]

18
    # Helper expects a @project instance variable
19 20 21 22
    helper.instance_variable_set(:@project, project)

    # Stub the `current_user` helper
    allow(helper).to receive(:current_user).and_return(user)
Riyad Preukschas committed
23 24
  end

25
  describe "#markdown" do
26
    describe "referencing multiple objects" do
27
      let(:actual) { "#{merge_request.to_reference} -> #{commit.to_reference} -> #{issue.to_reference}" }
28

29
      it "links to the merge request" do
Vinnie Okada committed
30
        expected = namespace_project_merge_request_path(project.namespace, project, merge_request)
31
        expect(helper.markdown(actual)).to match(expected)
Riyad Preukschas committed
32 33
      end

34
      it "links to the commit" do
Vinnie Okada committed
35
        expected = namespace_project_commit_path(project.namespace, project, commit)
36
        expect(helper.markdown(actual)).to match(expected)
Riyad Preukschas committed
37 38
      end

39
      it "links to the issue" do
Vinnie Okada committed
40
        expected = namespace_project_issue_path(project.namespace, project, issue)
41
        expect(helper.markdown(actual)).to match(expected)
Riyad Preukschas committed
42 43
      end
    end
44 45 46

    describe "override default project" do
      let(:actual) { issue.to_reference }
47
      let(:second_project) { create(:project, :public) }
48 49
      let(:second_issue) { create(:issue, project: second_project) }

50
      it 'links to the issue' do
51 52 53 54
        expected = namespace_project_issue_path(second_project.namespace, second_project, second_issue)
        expect(markdown(actual, project: second_project)).to match(expected)
      end
    end
55
  end
Riyad Preukschas committed
56

57
  describe '#link_to_gfm' do
Vinnie Okada committed
58
    let(:commit_path) { namespace_project_commit_path(project.namespace, project, commit) }
59
    let(:issues)      { create_list(:issue, 2, project: project) }
Riyad Preukschas committed
60

61
    it 'handles references nested in links with all the text' do
62
      actual = helper.link_to_gfm("This should finally fix #{issues[0].to_reference} and #{issues[1].to_reference} for real", commit_path)
63
      doc = Nokogiri::HTML.parse(actual)
Riyad Preukschas committed
64

65 66 67
      # Make sure we didn't create invalid markup
      expect(doc.errors).to be_empty

68
      # Leading commit link
69 70
      expect(doc.css('a')[0].attr('href')).to eq commit_path
      expect(doc.css('a')[0].text).to eq 'This should finally fix '
Riyad Preukschas committed
71

72
      # First issue link
73 74
      expect(doc.css('a')[1].attr('href')).
        to eq namespace_project_issue_path(project.namespace, project, issues[0])
75
      expect(doc.css('a')[1].text).to eq issues[0].to_reference
76

77
      # Internal commit link
78 79
      expect(doc.css('a')[2].attr('href')).to eq commit_path
      expect(doc.css('a')[2].text).to eq ' and '
80

81
      # Second issue link
82 83
      expect(doc.css('a')[3].attr('href')).
        to eq namespace_project_issue_path(project.namespace, project, issues[1])
84
      expect(doc.css('a')[3].text).to eq issues[1].to_reference
85 86

      # Trailing commit link
87 88
      expect(doc.css('a')[4].attr('href')).to eq commit_path
      expect(doc.css('a')[4].text).to eq ' for real'
89 90
    end

91
    it 'forwards HTML options' do
92
      actual = helper.link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo')
93 94 95 96 97 98 99 100
      doc = Nokogiri::HTML.parse(actual)

      expect(doc.css('a')).to satisfy do |v|
        # 'foo' gets added to all links
        v.all? { |a| a.attr('class').match(/foo$/) }
      end
    end

101
    it "escapes HTML passed in as the body" do
102
      actual = "This is a <h1>test</h1> - see #{issues[0].to_reference}"
103
      expect(helper.link_to_gfm(actual, commit_path)).
104
        to match('&lt;h1&gt;test&lt;/h1&gt;')
105
    end
106 107 108

    it 'ignores reference links when they are the entire body' do
      text = issues[0].to_reference
109
      act = helper.link_to_gfm(text, '/foo')
110 111
      expect(act).to eq %Q(<a href="/foo">#{issues[0].to_reference}</a>)
    end
SAKATA Sinji committed
112

113
    it 'replaces commit message with emoji to link' do
SAKATA Sinji committed
114 115
      actual = link_to_gfm(':book:Book', '/foo')
      expect(actual).
116
        to eq %Q(<img class="emoji" title=":book:" alt=":book:" src="http://localhost/assets/1F4D6.png" height="20" width="20" align="absmiddle"><a href="/foo">Book</a>)
SAKATA Sinji committed
117
    end
118
  end
119

120
  describe '#render_wiki_content' do
121
    before do
skv committed
122
      @wiki = double('WikiPage')
123
      allow(@wiki).to receive(:content).and_return('wiki content')
124
      allow(@wiki).to receive(:slug).and_return('nested/page')
125
      helper.instance_variable_set(:@project_wiki, @wiki)
126 127
    end

128
    it "uses Wiki pipeline for markdown files" do
129
      allow(@wiki).to receive(:format).and_return(:markdown)
130

131
      expect(helper).to receive(:markdown).with('wiki content', pipeline: :wiki, project_wiki: @wiki, page_slug: "nested/page")
132 133 134 135

      helper.render_wiki_content(@wiki)
    end

136
    it "uses Asciidoctor for asciidoc files" do
137 138 139 140 141 142 143
      allow(@wiki).to receive(:format).and_return(:asciidoc)

      expect(helper).to receive(:asciidoc).with('wiki content')

      helper.render_wiki_content(@wiki)
    end

144
    it "uses the Gollum renderer for all other file types" do
145
      allow(@wiki).to receive(:format).and_return(:rdoc)
skv committed
146
      formatted_content_stub = double('formatted_content')
147 148
      expect(formatted_content_stub).to receive(:html_safe)
      allow(@wiki).to receive(:formatted_content).and_return(formatted_content_stub)
149 150 151 152

      helper.render_wiki_content(@wiki)
    end
  end
153

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  describe '#first_line_in_markdown' do
    let(:text) { "@#{user.username}, can you look at this?\nHello world\n"}

    it 'truncates Markdown properly' do
      actual = first_line_in_markdown(text, 100, project: project)

      doc = Nokogiri::HTML.parse(actual)

      # Make sure we didn't create invalid markup
      expect(doc.errors).to be_empty

      # Leading user link
      expect(doc.css('a').length).to eq(1)
      expect(doc.css('a')[0].attr('href')).to eq user_path(user)
      expect(doc.css('a')[0].text).to eq "@#{user.username}"

      expect(doc.content).to eq "@#{user.username}, can you look at this?..."
    end
  end
Riyad Preukschas committed
173
end