Commit 870969a1 authored by Robert Speicher's avatar Robert Speicher

More cleanup of gitlab_markdown_helper_spec

parent f9c0f078
...@@ -2,12 +2,6 @@ require 'spec_helper' ...@@ -2,12 +2,6 @@ require 'spec_helper'
describe GitlabMarkdownHelper do describe GitlabMarkdownHelper do
include ApplicationHelper include ApplicationHelper
include IssuesHelper
# TODO: Properly test this
def can?(*)
true
end
let!(:project) { create(:project) } let!(:project) { create(:project) }
...@@ -20,22 +14,15 @@ describe GitlabMarkdownHelper do ...@@ -20,22 +14,15 @@ describe GitlabMarkdownHelper do
# Helper expects a current_user method. # Helper expects a current_user method.
let(:current_user) { user } let(:current_user) { user }
def url_helper(image_name)
File.join(root_url, 'assets', image_name)
end
before do before do
# Helper expects a @project instance variable # Helper expects a @project instance variable
@project = project @project = project
@ref = 'markdown'
@repository = project.repository
@request.host = Gitlab.config.gitlab.host
end end
describe "#gfm" do describe "#gfm" do
it "should forward HTML options to links" do it "should forward HTML options to links" do
expect(gfm("Fixed in #{commit.id}", @project, class: 'foo')). expect(gfm("Fixed in #{commit.id}", @project, class: 'foo')).
to have_selector('a.gfm.foo') to have_selector('a.gfm.foo')
end end
describe "referencing multiple objects" do describe "referencing multiple objects" do
...@@ -176,6 +163,9 @@ describe GitlabMarkdownHelper 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) actual = link_to_gfm("This should finally fix ##{issues[0].iid} and ##{issues[1].iid} for real", commit_path)
doc = Nokogiri::HTML.parse(actual) doc = Nokogiri::HTML.parse(actual)
# Make sure we didn't create invalid markup
expect(doc.errors).to be_empty
# Leading commit link # Leading commit link
expect(doc.css('a')[0].attr('href')).to eq commit_path expect(doc.css('a')[0].attr('href')).to eq commit_path
expect(doc.css('a')[0].text).to eq 'This should finally fix ' expect(doc.css('a')[0].text).to eq 'This should finally fix '
...@@ -219,17 +209,6 @@ describe GitlabMarkdownHelper do ...@@ -219,17 +209,6 @@ describe GitlabMarkdownHelper do
describe "#markdown" do describe "#markdown" do
# TODO (rspeicher) - This block tests multiple different contexts. Break this up! # 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 it "should add ids and links to headers" do
# Test every rule except nested tags. # Test every rule except nested tags.
text = '..Ab_c-d. e..' text = '..Ab_c-d. e..'
...@@ -245,6 +224,17 @@ describe GitlabMarkdownHelper do ...@@ -245,6 +224,17 @@ describe GitlabMarkdownHelper do
) )
end 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 it "should handle references in <em>" do
actual = "Apply _!#{merge_request.iid}_ ASAP" actual = "Apply _!#{merge_request.iid}_ ASAP"
...@@ -260,16 +250,15 @@ describe GitlabMarkdownHelper do ...@@ -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" 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) 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) to eq(target_html)
end end
it "should leave inline code untouched" do it "should leave inline code untouched" do
expect(markdown("\nDon't use `$#{snippet.id}` here.\n")).to eq( expect(markdown("Don't use `$#{snippet.id}` here.")).
"<p>Don't use <code>$#{snippet.id}</code> here.</p>\n" to eq "<p>Don't use <code>$#{snippet.id}</code> here.</p>\n"
)
end end
# REF-LIKE AUTOLINKS? ----------------------------------------------------- # REF-LIKE AUTOLINKS? -----------------------------------------------------
...@@ -287,67 +276,86 @@ describe GitlabMarkdownHelper do ...@@ -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") 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 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 ----------------------------------------------------------- # RELATIVE URLS -----------------------------------------------------------
# TODO (rspeicher): These belong in a relative link filter spec # TODO (rspeicher): These belong in a relative link filter spec
it "should handle relative urls for a file in master" do context 'relative links' do
actual = "[GitLab API doc](doc/api/README.md)\n" context 'with a valid repository' do
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n" before do
expect(markdown(actual)).to match(expected) @repository = project.repository
end @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" it "should handle relative urls for a file in master" do
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md#section\">GitLab API doc</a></p>\n" actual = "[GitLab API doc](doc/api/README.md)\n"
expect(markdown(actual)).to match(expected) expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n"
end 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" it "should handle relative urls for a file in master with an anchor" do
expected = "<p><a href=\"#section\">GitLab API doc</a></p>\n" actual = "[GitLab API doc](doc/api/README.md#section)\n"
expect(markdown(actual)).to match(expected) expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md#section\">GitLab API doc</a></p>\n"
end 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" it "should not handle relative urls for the current file with an anchor" do
expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc</a></p>\n" actual = "[GitLab API doc](#section)\n"
expect(markdown(actual)).to match(expected) expected = "<p><a href=\"#section\">GitLab API doc</a></p>\n"
end expect(markdown(actual)).to match(expected)
end
it "should handle absolute urls" do
actual = "[GitLab](https://www.gitlab.com)\n" it "should handle relative urls for a directory in master" do
expected = "<p><a href=\"https://www.gitlab.com\">GitLab</a></p>\n" actual = "[GitLab API doc](doc/api)\n"
expect(markdown(actual)).to match(expected) expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc</a></p>\n"
end 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" it "should handle absolute urls" do
expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n" actual = "[GitLab](https://www.gitlab.com)\n"
expect(markdown(actual)).to match(expected) expected = "<p><a href=\"https://www.gitlab.com\">GitLab</a></p>\n"
end 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" it "should handle relative urls in reference links for a file in master" do
expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc directory</a></p>\n" actual = "[GitLab API doc][GitLab readme]\n [GitLab readme]: doc/api/README.md\n"
expect(markdown(actual)).to match(expected) expected = "<p><a href=\"/#{project.path_with_namespace}/blob/#{@ref}/doc/api/README.md\">GitLab API doc</a></p>\n"
end 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" it "should handle relative urls in reference links for a directory in master" do
expected = "" actual = "[GitLab API doc directory][GitLab readmes]\n [GitLab readmes]: doc/api/\n"
expect(markdown(actual)).to match(expected) expected = "<p><a href=\"/#{project.path_with_namespace}/tree/#{@ref}/doc/api\">GitLab API doc directory</a></p>\n"
end 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 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
it 'should allow whitelisted HTML tags from the user' do context 'with an empty repository' do
actual = '<dl><dt>Term</dt><dd>Definition</dd></dl>' before do
expect(markdown(actual)).to match(actual) @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 end
# SANITIZATION ------------------------------------------------------------ # SANITIZATION ------------------------------------------------------------
# TODO (rspeicher): These are testing SanitizationFilter, not `markdown`
it 'should sanitize tags that are not whitelisted' do it 'should sanitize tags that are not whitelisted' do
actual = '<textarea>no inputs allowed</textarea> <blink>no blinks</blink>' actual = '<textarea>no inputs allowed</textarea> <blink>no blinks</blink>'
...@@ -375,20 +383,6 @@ describe GitlabMarkdownHelper do ...@@ -375,20 +383,6 @@ describe GitlabMarkdownHelper do
end end
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 describe '#render_wiki_content' do
before do before do
@wiki = double('WikiPage') @wiki = double('WikiPage')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment