More cleanup of gitlab_markdown_helper_spec
This commit is contained in:
parent
f9c0f0782b
commit
870969a1b2
1 changed files with 85 additions and 91 deletions
|
@ -2,12 +2,6 @@ require 'spec_helper'
|
|||
|
||||
describe GitlabMarkdownHelper do
|
||||
include ApplicationHelper
|
||||
include IssuesHelper
|
||||
|
||||
# TODO: Properly test this
|
||||
def can?(*)
|
||||
true
|
||||
end
|
||||
|
||||
let!(:project) { create(:project) }
|
||||
|
||||
|
@ -20,22 +14,15 @@ describe GitlabMarkdownHelper do
|
|||
# Helper expects a current_user method.
|
||||
let(:current_user) { user }
|
||||
|
||||
def url_helper(image_name)
|
||||
File.join(root_url, 'assets', image_name)
|
||||
end
|
||||
|
||||
before do
|
||||
# Helper expects a @project instance variable
|
||||
@project = project
|
||||
@ref = 'markdown'
|
||||
@repository = project.repository
|
||||
@request.host = Gitlab.config.gitlab.host
|
||||
end
|
||||
|
||||
describe "#gfm" do
|
||||
it "should forward HTML options to links" do
|
||||
expect(gfm("Fixed in #{commit.id}", @project, class: 'foo')).
|
||||
to have_selector('a.gfm.foo')
|
||||
to have_selector('a.gfm.foo')
|
||||
end
|
||||
|
||||
describe "referencing multiple objects" do
|
||||
|
@ -176,6 +163,9 @@ describe GitlabMarkdownHelper do
|
|||
actual = link_to_gfm("This should finally fix ##{issues[0].iid} and ##{issues[1].iid} for real", commit_path)
|
||||
doc = Nokogiri::HTML.parse(actual)
|
||||
|
||||
# Make sure we didn't create invalid markup
|
||||
expect(doc.errors).to be_empty
|
||||
|
||||
# Leading commit link
|
||||
expect(doc.css('a')[0].attr('href')).to eq commit_path
|
||||
expect(doc.css('a')[0].text).to eq 'This should finally fix '
|
||||
|
@ -219,17 +209,6 @@ describe GitlabMarkdownHelper do
|
|||
describe "#markdown" do
|
||||
# TODO (rspeicher) - This block tests multiple different contexts. Break this up!
|
||||
|
||||
# REFERENCES (PART TWO: THE REVENGE) ---------------------------------------
|
||||
|
||||
it "should handle references in headers" do
|
||||
actual = "\n# Working around ##{issue.iid}\n## Apply !#{merge_request.iid}"
|
||||
|
||||
expect(markdown(actual, no_header_anchors: true)).
|
||||
to match(%r{<h1[^<]*>Working around <a.+>##{issue.iid}</a></h1>})
|
||||
expect(markdown(actual, no_header_anchors: true)).
|
||||
to match(%r{<h2[^<]*>Apply <a.+>!#{merge_request.iid}</a></h2>})
|
||||
end
|
||||
|
||||
it "should add ids and links to headers" do
|
||||
# Test every rule except nested tags.
|
||||
text = '..Ab_c-d. e..'
|
||||
|
@ -245,6 +224,17 @@ describe GitlabMarkdownHelper do
|
|||
)
|
||||
end
|
||||
|
||||
# REFERENCES (PART TWO: THE REVENGE) ---------------------------------------
|
||||
|
||||
it "should handle references in headers" do
|
||||
actual = "\n# Working around ##{issue.iid}\n## Apply !#{merge_request.iid}"
|
||||
|
||||
expect(markdown(actual, no_header_anchors: true)).
|
||||
to match(%r{<h1[^<]*>Working around <a.+>##{issue.iid}</a></h1>})
|
||||
expect(markdown(actual, no_header_anchors: true)).
|
||||
to match(%r{<h2[^<]*>Apply <a.+>!#{merge_request.iid}</a></h2>})
|
||||
end
|
||||
|
||||
it "should handle references in <em>" do
|
||||
actual = "Apply _!#{merge_request.iid}_ ASAP"
|
||||
|
||||
|
@ -260,16 +250,15 @@ describe GitlabMarkdownHelper do
|
|||
|
||||
target_html = "<pre class=\"code highlight white plaintext\"><code>some code from $#{snippet.id}\nhere too\n</code></pre>\n"
|
||||
|
||||
expect(helper.markdown("\n some code from $#{snippet.id}\n here too\n")).
|
||||
expect(markdown("\n some code from $#{snippet.id}\n here too\n")).
|
||||
to eq(target_html)
|
||||
expect(helper.markdown("\n```\nsome code from $#{snippet.id}\nhere too\n```\n")).
|
||||
expect(markdown("\n```\nsome code from $#{snippet.id}\nhere too\n```\n")).
|
||||
to eq(target_html)
|
||||
end
|
||||
|
||||
it "should leave inline code untouched" do
|
||||
expect(markdown("\nDon't use `$#{snippet.id}` here.\n")).to eq(
|
||||
"<p>Don't use <code>$#{snippet.id}</code> here.</p>\n"
|
||||
)
|
||||
expect(markdown("Don't use `$#{snippet.id}` here.")).
|
||||
to eq "<p>Don't use <code>$#{snippet.id}</code> here.</p>\n"
|
||||
end
|
||||
|
||||
# REF-LIKE AUTOLINKS? -----------------------------------------------------
|
||||
|
@ -287,67 +276,86 @@ describe GitlabMarkdownHelper do
|
|||
expect(markdown("screen shot: ![some image](http://example.tld/#!#{merge_request.iid})")).to eq("<p>screen shot: <img src=\"http://example.tld/#!#{merge_request.iid}\" alt=\"some image\"></p>\n")
|
||||
end
|
||||
|
||||
it "should generate absolute urls for refs" do
|
||||
expect(markdown("##{issue.iid}")).to include(namespace_project_issue_path(project.namespace, project, issue))
|
||||
end
|
||||
|
||||
# RELATIVE URLS -----------------------------------------------------------
|
||||
# TODO (rspeicher): These belong in a relative link filter spec
|
||||
|
||||
it "should handle relative urls for a file in master" do
|
||||
actual = "[GitLab API doc](doc/api/README.md)\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
context 'relative links' do
|
||||
context 'with a valid repository' do
|
||||
before do
|
||||
@repository = project.repository
|
||||
@ref = 'markdown'
|
||||
end
|
||||
|
||||
it "should handle relative urls for a file in master with an anchor" do
|
||||
actual = "[GitLab API doc](doc/api/README.md#section)\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md#section\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
it "should handle relative urls for a file in master" do
|
||||
actual = "[GitLab API doc](doc/api/README.md)\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it "should not handle relative urls for the current file with an anchor" do
|
||||
actual = "[GitLab API doc](#section)\n"
|
||||
expected = "<p><a href=\"#section\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
it "should handle relative urls for a file in master with an anchor" do
|
||||
actual = "[GitLab API doc](doc/api/README.md#section)\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md#section\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it "should handle relative urls for a directory in master" do
|
||||
actual = "[GitLab API doc](doc/api)\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
it "should not handle relative urls for the current file with an anchor" do
|
||||
actual = "[GitLab API doc](#section)\n"
|
||||
expected = "<p><a href=\"#section\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it "should handle absolute urls" do
|
||||
actual = "[GitLab](https://www.gitlab.com)\n"
|
||||
expected = "<p><a href=\"https://www.gitlab.com\">GitLab</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
it "should handle relative urls for a directory in master" do
|
||||
actual = "[GitLab API doc](doc/api)\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it "should handle relative urls in reference links for a file in master" do
|
||||
actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
it "should handle absolute urls" do
|
||||
actual = "[GitLab](https://www.gitlab.com)\n"
|
||||
expected = "<p><a href=\"https://www.gitlab.com\">GitLab</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it "should handle relative urls in reference links for a directory in master" do
|
||||
actual = "[GitLab API doc directory][GitLab readmes]\n [GitLab readmes]: doc/api/\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc directory</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
it "should handle relative urls in reference links for a file in master" do
|
||||
actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it "should not handle malformed relative urls in reference links for a file in master" do
|
||||
actual = "[GitLab readme]: doc/api/README.md\n"
|
||||
expected = ""
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
it "should handle relative urls in reference links for a directory in master" do
|
||||
actual = "[GitLab API doc directory][GitLab readmes]\n [GitLab readmes]: doc/api/\n"
|
||||
expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc directory</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it 'should allow whitelisted HTML tags from the user' do
|
||||
actual = '<dl><dt>Term</dt><dd>Definition</dd></dl>'
|
||||
expect(markdown(actual)).to match(actual)
|
||||
it "should not handle malformed relative urls in reference links for a file in master" do
|
||||
actual = "[GitLab readme]: doc/api/README.md\n"
|
||||
expected = ""
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
|
||||
it 'should allow whitelisted HTML tags from the user' do
|
||||
actual = '<dl><dt>Term</dt><dd>Definition</dd></dl>'
|
||||
expect(markdown(actual)).to match(actual)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with an empty repository' do
|
||||
before do
|
||||
@project = create(:empty_project)
|
||||
@repository = @project.repository
|
||||
end
|
||||
|
||||
it "should not touch relative urls" do
|
||||
actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
|
||||
expected = "<p><a href=\"doc/api/README.md\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# SANITIZATION ------------------------------------------------------------
|
||||
# TODO (rspeicher): These are testing SanitizationFilter, not `markdown`
|
||||
|
||||
it 'should sanitize tags that are not whitelisted' do
|
||||
actual = '<textarea>no inputs allowed</textarea> <blink>no blinks</blink>'
|
||||
|
@ -375,20 +383,6 @@ describe GitlabMarkdownHelper do
|
|||
end
|
||||
end
|
||||
|
||||
# TODO (rspeicher): This should be a context of relative link specs, not its own thing
|
||||
describe 'markdown for empty repository' do
|
||||
before do
|
||||
@project = create(:empty_project)
|
||||
@repository = @project.repository
|
||||
end
|
||||
|
||||
it "should not touch relative urls" do
|
||||
actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
|
||||
expected = "<p><a href=\"doc/api/README.md\">GitLab API doc</a></p>\n"
|
||||
expect(markdown(actual)).to match(expected)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#render_wiki_content' do
|
||||
before do
|
||||
@wiki = double('WikiPage')
|
||||
|
|
Loading…
Reference in a new issue