debian-mirror-gitlab/spec/helpers/gitlab_markdown_helper_spec.rb

174 lines
6.2 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
2014-09-02 18:07:02 +05:30
describe GitlabMarkdownHelper do
include ApplicationHelper
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
let!(:project) { create(:project) }
let(:user) { create(:user, username: 'gfm') }
2015-09-11 14:41:01 +05:30
let(:commit) { project.commit }
2014-09-02 18:07:02 +05:30
let(:issue) { create(:issue, project: project) }
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
let(:snippet) { create(:project_snippet, project: project) }
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
before do
2015-10-24 18:46:33 +05:30
# Ensure the generated reference links aren't redacted
project.team << [user, :master]
2014-09-02 18:07:02 +05:30
# Helper expects a @project instance variable
2015-10-24 18:46:33 +05:30
helper.instance_variable_set(:@project, project)
# Stub the `current_user` helper
allow(helper).to receive(:current_user).and_return(user)
2014-09-02 18:07:02 +05:30
end
2015-09-25 12:07:36 +05:30
describe "#markdown" do
2014-09-02 18:07:02 +05:30
describe "referencing multiple objects" do
2015-09-11 14:41:01 +05:30
let(:actual) { "#{merge_request.to_reference} -> #{commit.to_reference} -> #{issue.to_reference}" }
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
it "links to the merge request" do
2015-04-26 12:48:37 +05:30
expected = namespace_project_merge_request_path(project.namespace, project, merge_request)
2015-10-24 18:46:33 +05:30
expect(helper.markdown(actual)).to match(expected)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "links to the commit" do
2015-04-26 12:48:37 +05:30
expected = namespace_project_commit_path(project.namespace, project, commit)
2015-10-24 18:46:33 +05:30
expect(helper.markdown(actual)).to match(expected)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "links to the issue" do
2015-04-26 12:48:37 +05:30
expected = namespace_project_issue_path(project.namespace, project, issue)
2015-10-24 18:46:33 +05:30
expect(helper.markdown(actual)).to match(expected)
2015-09-25 12:07:36 +05:30
end
end
describe "override default project" do
let(:actual) { issue.to_reference }
2015-10-24 18:46:33 +05:30
let(:second_project) { create(:project, :public) }
2015-09-25 12:07:36 +05:30
let(:second_issue) { create(:issue, project: second_project) }
2016-09-13 17:45:13 +05:30
it 'links to the issue' do
2015-09-25 12:07:36 +05:30
expected = namespace_project_issue_path(second_project.namespace, second_project, second_issue)
expect(markdown(actual, project: second_project)).to match(expected)
2014-09-02 18:07:02 +05:30
end
end
end
2015-09-11 14:41:01 +05:30
describe '#link_to_gfm' do
2015-04-26 12:48:37 +05:30
let(:commit_path) { namespace_project_commit_path(project.namespace, project, commit) }
2014-09-02 18:07:02 +05:30
let(:issues) { create_list(:issue, 2, project: project) }
2016-09-13 17:45:13 +05:30
it 'handles references nested in links with all the text' do
2015-10-24 18:46:33 +05:30
actual = helper.link_to_gfm("This should finally fix #{issues[0].to_reference} and #{issues[1].to_reference} for real", commit_path)
2015-09-11 14:41:01 +05:30
doc = Nokogiri::HTML.parse(actual)
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
# Make sure we didn't create invalid markup
expect(doc.errors).to be_empty
2014-09-02 18:07:02 +05:30
# Leading commit link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[0].attr('href')).to eq commit_path
expect(doc.css('a')[0].text).to eq 'This should finally fix '
2014-09-02 18:07:02 +05:30
# First issue link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[1].attr('href')).
to eq namespace_project_issue_path(project.namespace, project, issues[0])
expect(doc.css('a')[1].text).to eq issues[0].to_reference
2014-09-02 18:07:02 +05:30
# Internal commit link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[2].attr('href')).to eq commit_path
expect(doc.css('a')[2].text).to eq ' and '
2014-09-02 18:07:02 +05:30
# Second issue link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[3].attr('href')).
to eq namespace_project_issue_path(project.namespace, project, issues[1])
expect(doc.css('a')[3].text).to eq issues[1].to_reference
2014-09-02 18:07:02 +05:30
# Trailing commit link
2015-09-11 14:41:01 +05:30
expect(doc.css('a')[4].attr('href')).to eq commit_path
expect(doc.css('a')[4].text).to eq ' for real'
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it 'forwards HTML options' do
2015-10-24 18:46:33 +05:30
actual = helper.link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo')
2015-09-11 14:41:01 +05:30
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
2014-09-02 18:07:02 +05:30
end
it "escapes HTML passed in as the body" do
2015-09-11 14:41:01 +05:30
actual = "This is a <h1>test</h1> - see #{issues[0].to_reference}"
2015-10-24 18:46:33 +05:30
expect(helper.link_to_gfm(actual, commit_path)).
2015-04-26 12:48:37 +05:30
to match('&lt;h1&gt;test&lt;/h1&gt;')
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
it 'ignores reference links when they are the entire body' do
text = issues[0].to_reference
2015-10-24 18:46:33 +05:30
act = helper.link_to_gfm(text, '/foo')
2015-09-11 14:41:01 +05:30
expect(act).to eq %Q(<a href="/foo">#{issues[0].to_reference}</a>)
2014-09-02 18:07:02 +05:30
end
2015-10-24 18:46:33 +05:30
2016-09-13 17:45:13 +05:30
it 'replaces commit message with emoji to link' do
2015-10-24 18:46:33 +05:30
actual = link_to_gfm(':book:Book', '/foo')
expect(actual).
2016-04-02 18:10:28 +05:30
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>)
2015-10-24 18:46:33 +05:30
end
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
describe '#render_wiki_content' do
2014-09-02 18:07:02 +05:30
before do
@wiki = double('WikiPage')
2015-04-26 12:48:37 +05:30
allow(@wiki).to receive(:content).and_return('wiki content')
allow(@wiki).to receive(:slug).and_return('nested/page')
helper.instance_variable_set(:@project_wiki, @wiki)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "uses Wiki pipeline for markdown files" do
2015-04-26 12:48:37 +05:30
allow(@wiki).to receive(:format).and_return(:markdown)
2014-09-02 18:07:02 +05:30
expect(helper).to receive(:markdown).with('wiki content', pipeline: :wiki, project_wiki: @wiki, page_slug: "nested/page")
2014-09-02 18:07:02 +05:30
helper.render_wiki_content(@wiki)
end
2016-09-13 17:45:13 +05:30
it "uses Asciidoctor for asciidoc files" do
2015-09-11 14:41:01 +05:30
allow(@wiki).to receive(:format).and_return(:asciidoc)
expect(helper).to receive(:asciidoc).with('wiki content')
helper.render_wiki_content(@wiki)
end
2016-09-13 17:45:13 +05:30
it "uses the Gollum renderer for all other file types" do
2015-04-26 12:48:37 +05:30
allow(@wiki).to receive(:format).and_return(:rdoc)
2014-09-02 18:07:02 +05:30
formatted_content_stub = double('formatted_content')
2015-04-26 12:48:37 +05:30
expect(formatted_content_stub).to receive(:html_safe)
allow(@wiki).to receive(:formatted_content).and_return(formatted_content_stub)
2014-09-02 18:07:02 +05:30
helper.render_wiki_content(@wiki)
end
end
2015-04-26 12:48:37 +05:30
2015-10-24 18:46:33 +05:30
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
2014-09-02 18:07:02 +05:30
end