gitlab-org--gitlab-foss/spec/helpers/gitlab_markdown_helper_spec.rb

486 lines
17 KiB
Ruby
Raw Normal View History

require "spec_helper"
2012-08-08 04:52:09 -04:00
describe GitlabMarkdownHelper do
include ApplicationHelper
2013-01-23 09:13:28 -05:00
include IssuesHelper
let!(:project) { create(:project) }
let(:user) { create(:user, username: 'gfm') }
let(:commit) { project.repository.commit }
let(:issue) { create(:issue, project: project) }
Merge Request on forked projects The good: - You can do a merge request for a forked commit and it will merge properly (i.e. it does work). - Push events take into account merge requests on forked projects - Tests around merge_actions now present, spinach, and other rspec tests - Satellites now clean themselves up rather then recreate The questionable: - Events only know about target projects - Project's merge requests only hold on to MR's where they are the target - All operations performed in the satellite The bad: - Duplication between project's repositories and satellites (e.g. commits_between) (for reference: http://feedback.gitlab.com/forums/176466-general/suggestions/3456722-merge-requests-between-projects-repos) Fixes: Make test repos/satellites only create when needed -Spinach/Rspec now only initialize test directory, and setup stubs (things that are relatively cheap) -project_with_code, source_project_with_code, and target_project_with_code now create/destroy their repos individually -fixed remote removal -How to merge renders properly -Update emails to show project/branches -Edit MR doesn't set target branch -Fix some failures on editing/creating merge requests, added a test -Added back a test around merge request observer -Clean up project_transfer_spec, Remove duplicate enable/disable observers -Ensure satellite lock files are cleaned up, Attempted to add some testing around these as well -Signifant speed ups for tests -Update formatting ordering in notes_on_merge_requests -Remove wiki schema update Fixes for search/search results -Search results was using by_project for a list of projects, updated this to use in_projects -updated search results to reference the correct (target) project -udpated search results to print both sides of the merge request Change-Id: I19407990a0950945cc95d62089cbcc6262dab1a8
2013-04-25 10:15:33 -04:00
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
2013-03-25 03:20:14 -04:00
let(:snippet) { create(:project_snippet, project: project) }
let(:member) { project.users_projects.where(user_id: user).first }
before do
# Helper expects a @project instance variable
@project = project
@repository = project.repository
end
describe "#gfm" do
it "should return unaltered text if project is nil" do
actual = "Testing references: ##{issue.iid}"
gfm(actual).should_not == actual
@project = nil
gfm(actual).should == actual
end
it "should not alter non-references" do
actual = expected = "_Please_ *stop* 'helping' and all the other b*$#%' you do."
gfm(actual).should == expected
end
it "should not touch HTML entities" do
@project.issues.stub(:where).with(id: '39').and_return([issue])
actual = expected = "We'll accept good pull requests."
gfm(actual).should == expected
end
it "should forward HTML options to links" do
gfm("Fixed in #{commit.id}", class: "foo").should have_selector("a.gfm.foo")
end
describe "referencing a commit" do
let(:expected) { project_commit_path(project, commit) }
it "should link using a full id" do
actual = "Reverts #{commit.id}"
gfm(actual).should match(expected)
end
it "should link using a short id" do
actual = "Backported from #{commit.short_id(6)}"
gfm(actual).should match(expected)
end
it "should link with adjacent text" do
actual = "Reverted (see #{commit.id})"
gfm(actual).should match(expected)
end
it "should keep whitespace intact" do
actual = "Changes #{commit.id} dramatically"
expected = /Changes <a.+>#{commit.id}<\/a> dramatically/
gfm(actual).should match(expected)
end
it "should not link with an invalid id" do
actual = expected = "What happened in #{commit.id.reverse}"
gfm(actual).should == expected
end
it "should include a title attribute" do
actual = "Reverts #{commit.id}"
gfm(actual).should match(/title="#{commit.link_title}"/)
end
it "should include standard gfm classes" do
actual = "Reverts #{commit.id}"
gfm(actual).should match(/class="\s?gfm gfm-commit\s?"/)
end
end
describe "referencing a team member" do
let(:actual) { "@#{user.username} you are right." }
2013-03-20 07:41:18 -04:00
let(:expected) { user_path(user) }
before do
2013-01-04 11:50:31 -05:00
project.team << [user, :master]
end
it "should link using a simple name" do
gfm(actual).should match(expected)
end
it "should link using a name with dots" do
user.update_attributes(name: "alphA.Beta")
gfm(actual).should match(expected)
end
it "should link using name with underscores" do
user.update_attributes(name: "ping_pong_king")
gfm(actual).should match(expected)
end
it "should link with adjacent text" do
actual = "Mail the admin (@#{user.username})"
gfm(actual).should match(expected)
end
it "should keep whitespace intact" do
actual = "Yes, @#{user.username} is right."
expected = /Yes, <a.+>@#{user.username}<\/a> is right/
gfm(actual).should match(expected)
end
it "should not link with an invalid id" do
actual = expected = "@#{user.username.reverse} you are right."
gfm(actual).should == expected
end
it "should include standard gfm classes" do
gfm(actual).should match(/class="\s?gfm gfm-team_member\s?"/)
end
end
# Shared examples for referencing an object
#
# Expects the following attributes to be available in the example group:
#
# - object - The object itself
# - reference - The object reference string (e.g., #1234, $1234, !1234)
#
# Currently limited to Snippets, Issues and MergeRequests
shared_examples 'referenced object' do
let(:actual) { "Reference to #{reference}" }
let(:expected) { polymorphic_path([project, object]) }
it "should link using a valid id" do
gfm(actual).should match(expected)
end
it "should link with adjacent text" do
# Wrap the reference in parenthesis
gfm(actual.gsub(reference, "(#{reference})")).should match(expected)
# Append some text to the end of the reference
gfm(actual.gsub(reference, "#{reference}, right?")).should match(expected)
end
it "should keep whitespace intact" do
actual = "Referenced #{reference} already."
expected = /Referenced <a.+>[^\s]+<\/a> already/
gfm(actual).should match(expected)
end
it "should not link with an invalid id" do
# Modify the reference string so it's still parsed, but is invalid
reference.gsub!(/^(.)(\d+)$/, '\1' + ('\2' * 2))
gfm(actual).should == actual
end
it "should include a title attribute" do
title = "#{object.class.to_s.titlecase}: #{object.title}"
gfm(actual).should match(/title="#{title}"/)
end
it "should include standard gfm classes" do
css = object.class.to_s.underscore
gfm(actual).should match(/class="\s?gfm gfm-#{css}\s?"/)
end
end
describe "referencing an issue" do
let(:object) { issue }
let(:reference) { "##{issue.iid}" }
include_examples 'referenced object'
end
describe "referencing a merge request" do
let(:object) { merge_request }
let(:reference) { "!#{merge_request.iid}" }
include_examples 'referenced object'
end
describe "referencing a snippet" do
let(:object) { snippet }
let(:reference) { "$#{snippet.id}" }
2013-03-25 03:20:14 -04:00
let(:actual) { "Reference to #{reference}" }
let(:expected) { project_snippet_path(project, object) }
it "should link using a valid id" do
gfm(actual).should match(expected)
end
it "should link with adjacent text" do
# Wrap the reference in parenthesis
gfm(actual.gsub(reference, "(#{reference})")).should match(expected)
# Append some text to the end of the reference
gfm(actual.gsub(reference, "#{reference}, right?")).should match(expected)
end
it "should keep whitespace intact" do
actual = "Referenced #{reference} already."
expected = /Referenced <a.+>[^\s]+<\/a> already/
gfm(actual).should match(expected)
end
it "should not link with an invalid id" do
# Modify the reference string so it's still parsed, but is invalid
reference.gsub!(/^(.)(\d+)$/, '\1' + ('\2' * 2))
gfm(actual).should == actual
end
it "should include a title attribute" do
title = "Snippet: #{object.title}"
gfm(actual).should match(/title="#{title}"/)
end
it "should include standard gfm classes" do
css = object.class.to_s.underscore
gfm(actual).should match(/class="\s?gfm gfm-snippet\s?"/)
end
end
describe "referencing multiple objects" do
let(:actual) { "!#{merge_request.iid} -> #{commit.id} -> ##{issue.iid}" }
it "should link to the merge request" do
expected = project_merge_request_path(project, merge_request)
gfm(actual).should match(expected)
end
it "should link to the commit" do
expected = project_commit_path(project, commit)
gfm(actual).should match(expected)
end
it "should link to the issue" do
expected = project_issue_path(project, issue)
gfm(actual).should match(expected)
end
end
2012-09-05 16:05:20 -04:00
describe "emoji" do
it "matches at the start of a string" do
gfm(":+1:").should match(/<img/)
end
it "matches at the end of a string" do
gfm("This gets a :-1:").should match(/<img/)
end
it "matches with adjacent text" do
gfm("+1 (:+1:)").should match(/<img/)
end
it "has a title attribute" do
gfm(":-1:").should match(/title=":-1:"/)
end
it "has an alt attribute" do
gfm(":-1:").should match(/alt=":-1:"/)
end
it "has an emoji class" do
gfm(":+1:").should match('class="emoji"')
end
it "sets height and width" do
actual = gfm(":+1:")
actual.should match(/width="20"/)
actual.should match(/height="20"/)
end
2012-09-05 16:05:20 -04:00
it "keeps whitespace intact" do
gfm("This deserves a :+1: big time.").should match(/deserves a <img.+\/> big time/)
end
it "ignores invalid emoji" do
gfm(":invalid-emoji:").should_not match(/<img/)
end
2013-07-29 06:46:00 -04:00
it "should work independent of reference links (i.e. without @project being set)" do
@project = nil
gfm(":+1:").should match(/<img/)
end
2012-09-05 16:05:20 -04:00
end
end
describe "#link_to_gfm" do
let(:commit_path) { project_commit_path(project, commit) }
let(:issues) { create_list(:issue, 2, project: project) }
it "should handle references nested in links with all the text" do
actual = link_to_gfm("This should finally fix ##{issues[0].iid} and ##{issues[1].iid} for real", commit_path)
# Break the result into groups of links with their content, without
# closing tags
groups = actual.split("</a>")
# Leading commit link
groups[0].should match(/href="#{commit_path}"/)
groups[0].should match(/This should finally fix $/)
# First issue link
2013-01-16 16:08:01 -05:00
groups[1].should match(/href="#{project_issue_url(project, issues[0])}"/)
groups[1].should match(/##{issues[0].iid}$/)
2012-08-01 20:28:50 -04:00
# Internal commit link
groups[2].should match(/href="#{commit_path}"/)
groups[2].should match(/ and /)
2012-08-01 20:28:50 -04:00
# Second issue link
2013-01-16 16:08:01 -05:00
groups[3].should match(/href="#{project_issue_url(project, issues[1])}"/)
groups[3].should match(/##{issues[1].iid}$/)
# Trailing commit link
groups[4].should match(/href="#{commit_path}"/)
groups[4].should match(/ for real$/)
2012-08-01 20:28:50 -04:00
end
it "should forward HTML options" do
actual = link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo')
actual.should have_selector 'a.gfm.gfm-commit.foo'
2012-08-01 20:28:50 -04:00
end
it "escapes HTML passed in as the body" do
actual = "This is a <h1>test</h1> - see ##{issues[0].iid}"
link_to_gfm(actual, commit_path).should match('&lt;h1&gt;test&lt;/h1&gt;')
end
2012-08-01 20:28:50 -04:00
end
describe "#markdown" do
it "should handle references in paragraphs" do
actual = "\n\nLorem ipsum dolor sit amet. #{commit.id} Nam pulvinar sapien eget.\n"
expected = project_commit_path(project, commit)
markdown(actual).should match(expected)
end
it "should handle references in headers" do
actual = "\n# Working around ##{issue.iid}\n## Apply !#{merge_request.iid}"
markdown(actual, {no_header_anchors:true}).should match(%r{<h1[^<]*>Working around <a.+>##{issue.iid}</a></h1>})
markdown(actual, {no_header_anchors:true}).should 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..'
id = 'ab_c-d-e'
markdown("# #{text}").should match(%r{<h1 id="#{id}">#{text}<a href="[^"]*##{id}"></a></h1>})
markdown("# #{text}", {no_header_anchors:true}).should == "<h1>#{text}</h1>"
id = 'link-text'
markdown("# [link text](url) ![img alt](url)").should match(
%r{<h1 id="#{id}"><a href="[^"]*url">link text</a> <img[^>]*><a href="[^"]*##{id}"></a></h1>}
)
end
it "should handle references in lists" do
2013-01-04 11:50:31 -05:00
project.team << [user, :master]
actual = "\n* dark: ##{issue.iid}\n* light by @#{member.user.username}"
markdown(actual).should match(%r{<li>dark: <a.+>##{issue.iid}</a></li>})
markdown(actual).should match(%r{<li>light by <a.+>@#{member.user.username}</a></li>})
end
it "should handle references in <em>" do
actual = "Apply _!#{merge_request.iid}_ ASAP"
markdown(actual).should match(%r{Apply <em><a.+>!#{merge_request.iid}</a></em>})
end
2013-08-26 08:07:07 -04:00
it "should handle tables" do
actual = %Q{| header 1 | header 2 |
| -------- | -------- |
| cell 1 | cell 2 |
| cell 3 | cell 4 |}
markdown(actual).should match(/\A<table/)
end
it "should leave code blocks untouched" do
2012-11-28 10:05:11 -05:00
helper.stub(:user_color_scheme_class).and_return(:white)
target_html = "\n<div class=\"highlighted-data white\">\n <div class=\"highlight\">\n <pre><code class=\"\">some code from $#{snippet.id}\nhere too\n</code></pre>\n </div>\n</div>\n\n"
2012-11-28 10:05:11 -05:00
helper.markdown("\n some code from $#{snippet.id}\n here too\n").should == target_html
helper.markdown("\n```\nsome code from $#{snippet.id}\nhere too\n```\n").should == target_html
end
it "should leave inline code untouched" do
markdown("\nDon't use `$#{snippet.id}` here.\n").should == "<p>Don&#39;t use <code>$#{snippet.id}</code> here.</p>\n"
end
2013-01-16 16:08:01 -05:00
it "should leave ref-like autolinks untouched" do
markdown("look at http://example.tld/#!#{merge_request.iid}").should == "<p>look at <a href=\"http://example.tld/#!#{merge_request.iid}\">http://example.tld/#!#{merge_request.iid}</a></p>\n"
end
it "should leave ref-like href of 'manual' links untouched" do
markdown("why not [inspect !#{merge_request.iid}](http://example.tld/#!#{merge_request.iid})").should == "<p>why not <a href=\"http://example.tld/#!#{merge_request.iid}\">inspect </a><a class=\"gfm gfm-merge_request \" href=\"#{project_merge_request_url(project, merge_request)}\" title=\"Merge Request: #{merge_request.title}\">!#{merge_request.iid}</a><a href=\"http://example.tld/#!#{merge_request.iid}\"></a></p>\n"
end
it "should leave ref-like src of images untouched" do
markdown("screen shot: ![some image](http://example.tld/#!#{merge_request.iid})").should == "<p>screen shot: <img src=\"http://example.tld/#!#{merge_request.iid}\" alt=\"some image\"></p>\n"
end
2013-01-16 16:08:01 -05:00
it "should generate absolute urls for refs" do
markdown("##{issue.iid}").should include(project_issue_url(project, issue))
2013-01-16 16:08:01 -05:00
end
it "should generate absolute urls for emoji" do
markdown(":smile:").should include("src=\"#{url_to_image("emoji/smile")}")
end
2013-10-09 09:01:02 -04:00
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/master/doc/api/README.md\">GitLab API doc</a></p>\n"
markdown(actual).should 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/master/doc/api\">GitLab API doc</a></p>\n"
markdown(actual).should 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"
markdown(actual).should 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/master/doc/api/README.md\">GitLab API doc</a></p>\n"
markdown(actual).should 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/master/doc/api\">GitLab API doc directory</a></p>\n"
markdown(actual).should 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 = ""
markdown(actual).should match(expected)
end
end
describe "#render_wiki_content" do
before do
2013-12-14 08:43:48 -05:00
@wiki = double('WikiPage')
@wiki.stub(:content).and_return('wiki content')
end
2013-05-19 05:13:39 -04:00
it "should use GitLab Flavored Markdown for markdown files" do
@wiki.stub(:format).and_return(:markdown)
helper.should_receive(:markdown).with('wiki content')
helper.render_wiki_content(@wiki)
end
it "should use the Gollum renderer for all other file types" do
@wiki.stub(:format).and_return(:rdoc)
2013-12-14 08:43:48 -05:00
formatted_content_stub = double('formatted_content')
formatted_content_stub.should_receive(:html_safe)
@wiki.stub(:formatted_content).and_return(formatted_content_stub)
helper.render_wiki_content(@wiki)
end
end
end