Commit 79c4e389 authored by Robert Speicher's avatar Robert Speicher

Rename ReferenceFilterSpecHelper to FilterSpecHelper

And make it more generalized for all filter specs.
parent 442a0663
...@@ -2,11 +2,9 @@ require 'spec_helper' ...@@ -2,11 +2,9 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe AutolinkFilter do describe AutolinkFilter do
let(:link) { 'http://about.gitlab.com/' } include FilterSpecHelper
def filter(html, options = {}) let(:link) { 'http://about.gitlab.com/' }
described_class.call(html, options)
end
it 'does nothing when :autolink is false' do it 'does nothing when :autolink is false' do
exp = act = link exp = act = link
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe CommitRangeReferenceFilter do describe CommitRangeReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
let(:project) { create(:project) } let(:project) { create(:project) }
let(:commit1) { project.commit } let(:commit1) { project.commit }
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe CommitReferenceFilter do describe CommitReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
let(:project) { create(:project) } let(:project) { create(:project) }
let(:commit) { project.commit } let(:commit) { project.commit }
......
...@@ -2,9 +2,7 @@ require 'spec_helper' ...@@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe EmojiFilter do describe EmojiFilter do
def filter(html, contexts = {}) include FilterSpecHelper
described_class.call(html, contexts)
end
before do before do
ActionController::Base.asset_host = 'https://foo.com' ActionController::Base.asset_host = 'https://foo.com'
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe ExternalIssueReferenceFilter do describe ExternalIssueReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
def helper def helper
IssuesHelper IssuesHelper
......
...@@ -2,9 +2,7 @@ require 'spec_helper' ...@@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe ExternalLinkFilter do describe ExternalLinkFilter do
def filter(html, options = {}) include FilterSpecHelper
described_class.call(html, options)
end
it 'ignores elements without an href attribute' do it 'ignores elements without an href attribute' do
exp = act = %q(<a id="ignored">Ignore Me</a>) exp = act = %q(<a id="ignored">Ignore Me</a>)
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe IssueReferenceFilter do describe IssueReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
def helper def helper
IssuesHelper IssuesHelper
......
...@@ -3,7 +3,7 @@ require 'html/pipeline' ...@@ -3,7 +3,7 @@ require 'html/pipeline'
module Gitlab::Markdown module Gitlab::Markdown
describe LabelReferenceFilter do describe LabelReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:label) { create(:label, project: project) } let(:label) { create(:label, project: project) }
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe MergeRequestReferenceFilter do describe MergeRequestReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
let(:project) { create(:project) } let(:project) { create(:project) }
let(:merge) { create(:merge_request, source_project: project) } let(:merge) { create(:merge_request, source_project: project) }
......
...@@ -2,9 +2,7 @@ require 'spec_helper' ...@@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe SanitizationFilter do describe SanitizationFilter do
def filter(html, options = {}) include FilterSpecHelper
described_class.call(html, options)
end
describe 'default whitelist' do describe 'default whitelist' do
it 'sanitizes tags that are not whitelisted' do it 'sanitizes tags that are not whitelisted' do
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe SnippetReferenceFilter do describe SnippetReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:snippet) { create(:project_snippet, project: project) } let(:snippet) { create(:project_snippet, project: project) }
......
...@@ -4,9 +4,7 @@ require 'spec_helper' ...@@ -4,9 +4,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe TableOfContentsFilter do describe TableOfContentsFilter do
def filter(html, options = {}) include FilterSpecHelper
described_class.call(html, options)
end
def header(level, text) def header(level, text)
"<h#{level}>#{text}</h#{level}>\n" "<h#{level}>#{text}</h#{level}>\n"
......
...@@ -2,9 +2,7 @@ require 'spec_helper' ...@@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe TaskListFilter do describe TaskListFilter do
def filter(html, options = {}) include FilterSpecHelper
described_class.call(html, options)
end
it 'does not apply `task-list` class to non-task lists' do it 'does not apply `task-list` class to non-task lists' do
exp = act = %(<ul><li>Item</li></ul>) exp = act = %(<ul><li>Item</li></ul>)
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown module Gitlab::Markdown
describe UserReferenceFilter do describe UserReferenceFilter do
include ReferenceFilterSpecHelper include FilterSpecHelper
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:user) { create(:user) } let(:user) { create(:user) }
......
# Common methods and setup for Gitlab::Markdown reference filter specs # Helper methods for Gitlab::Markdown filter specs
# #
# Must be included into specs manually # Must be included into specs manually
module ReferenceFilterSpecHelper module FilterSpecHelper
extend ActiveSupport::Concern extend ActiveSupport::Concern
# Shortcut to Rails' auto-generated routes helpers, to avoid including the
# module
def urls
Rails.application.routes.url_helpers
end
# Modify a String reference to make it invalid
#
# Commit SHAs get reversed, IDs get incremented by 1, all other Strings get
# their word characters reversed.
#
# reference - String reference to modify
#
# Returns a String
def invalidate_reference(reference)
if reference =~ /\A(.+)?.\d+\z/
# Integer-based reference with optional project prefix
reference.gsub(/\d+\z/) { |i| i.to_i + 1 }
elsif reference =~ /\A(.+@)?(\h{6,40}\z)/
# SHA-based reference with optional prefix
reference.gsub(/\h{6,40}\z/) { |v| v.reverse }
else
reference.gsub(/\w+\z/) { |v| v.reverse }
end
end
# Perform `call` on the described class # Perform `call` on the described class
# #
# Automatically passes the current `project` value to the context if none is # Automatically passes the current `project` value, if defined, to the context
# provided. # if none is provided.
# #
# html - String text to pass to the filter's `call` method. # html - HTML String to pass to the filter's `call` method.
# contexts - Hash context for the filter. (default: {project: project}) # contexts - Hash context for the filter. (default: {project: project})
# #
# Returns the String text returned by the filter's `call` method. # Returns a Nokogiri::XML::DocumentFragment
def filter(html, contexts = {}) def filter(html, contexts = {})
if defined?(project)
contexts.reverse_merge!(project: project) contexts.reverse_merge!(project: project)
end
described_class.call(html, contexts) described_class.call(html, contexts)
end end
...@@ -50,7 +27,7 @@ module ReferenceFilterSpecHelper ...@@ -50,7 +27,7 @@ module ReferenceFilterSpecHelper
# body - String text to run through the pipeline # body - String text to run through the pipeline
# contexts - Hash context for the filter. (default: {project: project}) # contexts - Hash context for the filter. (default: {project: project})
# #
# Returns the Hash of the pipeline result # Returns the Hash
def pipeline_result(body, contexts = {}) def pipeline_result(body, contexts = {})
contexts.reverse_merge!(project: project) contexts.reverse_merge!(project: project)
...@@ -58,13 +35,43 @@ module ReferenceFilterSpecHelper ...@@ -58,13 +35,43 @@ module ReferenceFilterSpecHelper
pipeline.call(body) pipeline.call(body)
end end
# Modify a String reference to make it invalid
#
# Commit SHAs get reversed, IDs get incremented by 1, all other Strings get
# their word characters reversed.
#
# reference - String reference to modify
#
# Returns a String
def invalidate_reference(reference)
if reference =~ /\A(.+)?.\d+\z/
# Integer-based reference with optional project prefix
reference.gsub(/\d+\z/) { |i| i.to_i + 1 }
elsif reference =~ /\A(.+@)?(\h{6,40}\z)/
# SHA-based reference with optional prefix
reference.gsub(/\h{6,40}\z/) { |v| v.reverse }
else
reference.gsub(/\w+\z/) { |v| v.reverse }
end
end
# Stub CrossProjectReference#user_can_reference_project? to return true for
# the current test
def allow_cross_reference! def allow_cross_reference!
allow_any_instance_of(described_class). allow_any_instance_of(described_class).
to receive(:user_can_reference_project?).and_return(true) to receive(:user_can_reference_project?).and_return(true)
end end
# Stub CrossProjectReference#user_can_reference_project? to return false for
# the current test
def disallow_cross_reference! def disallow_cross_reference!
allow_any_instance_of(described_class). allow_any_instance_of(described_class).
to receive(:user_can_reference_project?).and_return(false) to receive(:user_can_reference_project?).and_return(false)
end end
# Shortcut to Rails' auto-generated routes helpers, to avoid including the
# module
def urls
Rails.application.routes.url_helpers
end
end end
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