Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-10-23 06:06:01 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2019-10-23 06:06:01 +0300
commit8c7eab92cd0009f55cb999bbade43e0f969c137e (patch)
tree180cac6632448a211ddbe555191574c98e8dc385 /spec/lib
parentdffeff5520e861dc6e7319b690c573186bbbd22e (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/banzai/filter/wiki_link_filter_spec.rb13
-rw-r--r--spec/lib/banzai/pipeline/wiki_pipeline_spec.rb209
-rw-r--r--spec/lib/gitlab/url_builder_spec.rb2
-rw-r--r--spec/lib/gitlab/utils_spec.rb37
4 files changed, 166 insertions, 95 deletions
diff --git a/spec/lib/banzai/filter/wiki_link_filter_spec.rb b/spec/lib/banzai/filter/wiki_link_filter_spec.rb
index 4587bd85939..7a83dc3ec1d 100644
--- a/spec/lib/banzai/filter/wiki_link_filter_spec.rb
+++ b/spec/lib/banzai/filter/wiki_link_filter_spec.rb
@@ -11,6 +11,10 @@ describe Banzai::Filter::WikiLinkFilter do
let(:wiki) { ProjectWiki.new(project, user) }
let(:repository_upload_folder) { Wikis::CreateAttachmentService::ATTACHMENT_PATH }
+ def upload_href(file_name)
+ ::File.join(wiki.wiki_page_path, repository_upload_folder, file_name)
+ end
+
it "doesn't rewrite absolute links" do
filtered_link = filter("<a href='http://example.com:8000/'>Link</a>", project_wiki: wiki).children[0]
@@ -28,12 +32,12 @@ describe Banzai::Filter::WikiLinkFilter do
it 'rewrites links' do
filtered_link = filter("<a href='#{repository_upload_folder}/a.test'>Link</a>", project_wiki: wiki).children[0]
- expect(filtered_link.attribute('href').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.test")
+ expect(filtered_link.attribute('href').value).to eq(upload_href "a.test")
end
end
context 'with "img" html tag' do
- let(:path) { "#{wiki.wiki_base_path}/#{repository_upload_folder}/a.jpg" }
+ let(:path) { upload_href "a.jpg" }
context 'inside an "a" html tag' do
it 'rewrites links' do
@@ -57,7 +61,7 @@ describe Banzai::Filter::WikiLinkFilter do
it 'rewrites links' do
filtered_link = filter("<video src='#{repository_upload_folder}/a.mp4'></video>", project_wiki: wiki).children[0]
- expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.mp4")
+ expect(filtered_link.attribute('src').value).to eq(upload_href "a.mp4")
end
end
@@ -65,7 +69,8 @@ describe Banzai::Filter::WikiLinkFilter do
it 'rewrites links' do
filtered_link = filter("<audio src='#{repository_upload_folder}/a.wav'></audio>", project_wiki: wiki).children[0]
- expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.wav")
+ # expect(filtered_link.attribute('src').value).to eq("#{wiki.wiki_base_path}/#{repository_upload_folder}/a.wav")
+ expect(filtered_link.attribute('src').value).to eq(upload_href "a.wav")
end
end
end
diff --git a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb
index 26f2b0b0acf..7f60106cdca 100644
--- a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb
+++ b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb
@@ -3,6 +3,12 @@
require 'spec_helper'
describe Banzai::Pipeline::WikiPipeline do
+ let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") }
+ let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) }
+ let_it_be(:project_wiki) { ProjectWiki.new(project, double(:user)) }
+ let_it_be(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) }
+ let(:prefix) { project_wiki.wiki_page_path }
+
describe 'TableOfContents' do
it 'replaces the tag with the TableOfContentsFilter result' do
markdown = <<-MD.strip_heredoc
@@ -54,132 +60,138 @@ describe Banzai::Pipeline::WikiPipeline do
end
describe "Links" do
- let(:namespace) { create(:namespace, name: "wiki_link_ns") }
- let(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) }
- let(:project_wiki) { ProjectWiki.new(project, double(:user)) }
- let(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) }
-
- { "when GitLab is hosted at a root URL" => '/',
- "when GitLab is hosted at a relative URL" => '/nested/relative/gitlab' }.each do |test_name, relative_url_root|
- context test_name do
+ shared_examples 'a correct link rewrite' do
+ it 'rewrites links correctly' do
+ output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
+
+ expect(output).to include("href=\"#{page_href}\"")
+ end
+ end
+
+ shared_examples 'link examples' do |test_name|
+ let(:page_href) { "#{prefix}/#{expected_page_path}" }
+
+ context "when GitLab is hosted at a #{test_name} URL" do
before do
allow(Gitlab.config.gitlab).to receive(:relative_url_root).and_return(relative_url_root)
end
describe "linking to pages within the wiki" do
- context "when creating hierarchical links to the current directory" do
- it "rewrites non-file links to be at the scope of the current directory" do
- markdown = "[Page](./page)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
+ let(:markdown) { "[Page](#{nesting}page#{extension})" }
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/nested/twice/page\"")
+ context "when creating hierarchical links to the current directory" do
+ let(:nesting) { './' }
+ context 'non file links' do
+ let(:extension) { '' }
+ let(:expected_page_path) { 'nested/twice/page' }
+ it_behaves_like 'a correct link rewrite'
end
- it "rewrites file links to be at the scope of the current directory" do
- markdown = "[Link to Page](./page.md)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/nested/twice/page.md\"")
+ context 'file-like links' do
+ let(:extension) { '.md' }
+ let(:expected_page_path) { 'nested/twice/page.md' }
+ it_behaves_like 'a correct link rewrite'
end
end
context "when creating hierarchical links to the parent directory" do
- it "rewrites non-file links to be at the scope of the parent directory" do
- markdown = "[Link to Page](../page)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/nested/page\"")
+ let(:nesting) { '../' }
+ context "non file links" do
+ let(:extension) { '' }
+ let(:expected_page_path) { 'nested/page' }
+ it_behaves_like 'a correct link rewrite'
end
- it "rewrites file links to be at the scope of the parent directory" do
- markdown = "[Link to Page](../page.md)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/nested/page.md\"")
+ context "file-like links" do
+ let(:extension) { '.md' }
+ let(:expected_page_path) { 'nested/page.md' }
+ it_behaves_like 'a correct link rewrite'
end
end
context "when creating hierarchical links to a sub-directory" do
- it "rewrites non-file links to be at the scope of the sub-directory" do
- markdown = "[Link to Page](./subdirectory/page)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
+ let(:nesting) { './subdirectory/' }
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/nested/twice/subdirectory/page\"")
+ context "non file links" do
+ let(:extension) { '' }
+ let(:expected_page_path) { 'nested/twice/subdirectory/page' }
+ it_behaves_like 'a correct link rewrite'
end
- it "rewrites file links to be at the scope of the sub-directory" do
- markdown = "[Link to Page](./subdirectory/page.md)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/nested/twice/subdirectory/page.md\"")
+ context 'file-like links' do
+ let(:extension) { '.md' }
+ let(:expected_page_path) { 'nested/twice/subdirectory/page.md' }
+ it_behaves_like 'a correct link rewrite'
end
end
describe "when creating non-hierarchical links" do
- it 'rewrites non-file links to be at the scope of the wiki root' do
- markdown = "[Link to Page](page)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
+ let(:nesting) { '' }
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/page\"")
+ context 'non-file links' do
+ let(:extension) { '' }
+ let(:expected_page_path) { 'page' }
+ it_behaves_like 'a correct link rewrite'
end
- it 'rewrites non-file links (with spaces) to be at the scope of the wiki root' do
- markdown = "[Link to Page](page slug)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/page%20slug\"")
+ context 'non-file links (with spaces)' do
+ let(:extension) { ' slug' }
+ let(:expected_page_path) { 'page%20slug' }
+ it_behaves_like 'a correct link rewrite'
end
- it "rewrites file links to be at the scope of the current directory" do
- markdown = "[Link to Page](page.md)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/nested/twice/page.md\"")
+ context "file links" do
+ let(:extension) { '.md' }
+ let(:expected_page_path) { 'nested/twice/page.md' }
+ it_behaves_like 'a correct link rewrite'
end
- it 'rewrites links with anchor' do
- markdown = '[Link to Header](start-page#title)'
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/start-page#title\"")
+ context 'links with anchor' do
+ let(:extension) { '#title' }
+ let(:expected_page_path) { 'page#title' }
+ it_behaves_like 'a correct link rewrite'
end
- it 'rewrites links (with spaces) with anchor' do
- markdown = '[Link to Header](start page#title)'
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/start%20page#title\"")
+ context 'links (with spaces) with anchor' do
+ let(:extension) { ' two#title' }
+ let(:expected_page_path) { 'page%20two#title' }
+ it_behaves_like 'a correct link rewrite'
end
end
describe "when creating root links" do
- it 'rewrites non-file links to be at the scope of the wiki root' do
- markdown = "[Link to Page](/page)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
+ let(:nesting) { '/' }
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/page\"")
+ context 'non-file links' do
+ let(:extension) { '' }
+ let(:expected_page_path) { 'page' }
+ it_behaves_like 'a correct link rewrite'
end
- it 'rewrites file links to be at the scope of the wiki root' do
- markdown = "[Link to Page](/page.md)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include("href=\"#{relative_url_root}/wiki_link_ns/wiki_link_project/wikis/page.md\"")
+ context 'file links' do
+ let(:extension) { '.md' }
+ let(:expected_page_path) { 'page.md' }
+ it_behaves_like 'a correct link rewrite'
end
end
end
describe "linking to pages outside the wiki (absolute)" do
- it "doesn't rewrite links" do
- markdown = "[Link to Page](http://example.com/page)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
-
- expect(output).to include('href="http://example.com/page"')
- end
+ let(:markdown) { "[Link to Page](http://example.com/page)" }
+ let(:page_href) { 'http://example.com/page' }
+ it_behaves_like 'a correct link rewrite'
end
end
end
+ include_examples 'link examples', :root do
+ let(:relative_url_root) { '/' }
+ end
+
+ include_examples 'link examples', :relative do
+ let(:relative_url_root) { '/nested/relative/gitlab' }
+ end
+
describe "checking slug validity when assembling links" do
context "with a valid slug" do
let(:valid_slug) { "http://example.com" }
@@ -261,37 +273,54 @@ describe Banzai::Pipeline::WikiPipeline do
end
describe 'videos and audio' do
- let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") }
- let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) }
- let_it_be(:project_wiki) { ProjectWiki.new(project, double(:user)) }
- let_it_be(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) }
+ def src(file_name)
+ "#{prefix}/nested/twice/#{file_name}"
+ end
- it 'generates video html structure' do
- markdown = "![video_file](video_file_name.mp4)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
+ shared_examples 'correct video rewrite' do
+ let(:markdown) { "![video_file](#{file_name})" }
+ let(:video_fragment) { "<video src=\"#{prefix}/#{expected_file_path}\"" }
+ let(:options) do
+ {
+ project: project,
+ project_wiki: project_wiki,
+ page_slug: page.slug
+ }
+ end
+
+ it 'generates video html structure' do
+ output = described_class.to_html(markdown, options)
- expect(output).to include('<video src="/wiki_link_ns/wiki_link_project/wikis/nested/twice/video_file_name.mp4"')
+ expect(output).to include(video_fragment)
+ end
end
- it 'rewrites and replaces video links names with white spaces to %20' do
- markdown = "![video file](video file name.mp4)"
- output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
+ context 'underscores' do
+ let(:file_name) { 'video_file_name.mp4' }
+ let(:expected_file_path) { 'nested/twice/video_file_name.mp4' }
+ it_behaves_like 'correct video rewrite'
+ end
- expect(output).to include('<video src="/wiki_link_ns/wiki_link_project/wikis/nested/twice/video%20file%20name.mp4"')
+ context 'spaces' do
+ let(:file_name) { 'video file name.mp4' }
+ let(:expected_file_path) { 'nested/twice/video%20file%20name.mp4' }
+ it_behaves_like 'correct video rewrite'
end
it 'generates audio html structure' do
markdown = "![audio_file](audio_file_name.wav)"
+ safe_name = "audio_file_name.wav"
output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
- expect(output).to include('<audio src="/wiki_link_ns/wiki_link_project/wikis/nested/twice/audio_file_name.wav"')
+ expect(output).to include(%Q'<audio src="#{src(safe_name)}"')
end
it 'rewrites and replaces audio links names with white spaces to %20' do
markdown = "![audio file](audio file name.wav)"
+ safe_name = "audio%20file%20name.wav"
output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
- expect(output).to include('<audio src="/wiki_link_ns/wiki_link_project/wikis/nested/twice/audio%20file%20name.wav"')
+ expect(output).to include(%Q'<audio src="#{src(safe_name)}"')
end
end
end
diff --git a/spec/lib/gitlab/url_builder_spec.rb b/spec/lib/gitlab/url_builder_spec.rb
index 08d3c638f9e..4ed2c232f97 100644
--- a/spec/lib/gitlab/url_builder_spec.rb
+++ b/spec/lib/gitlab/url_builder_spec.rb
@@ -150,7 +150,7 @@ describe Gitlab::UrlBuilder do
wiki_page = build(:wiki_page)
url = described_class.build(wiki_page)
- expect(url).to eq "#{Gitlab.config.gitlab.url}#{wiki_page.wiki.wiki_base_path}/#{wiki_page.slug}"
+ expect(url).to eq "#{Gitlab.config.gitlab.url}#{wiki_page.wiki.wiki_page_path}/#{wiki_page.slug}"
end
end
end
diff --git a/spec/lib/gitlab/utils_spec.rb b/spec/lib/gitlab/utils_spec.rb
index 890918d4a7c..a68434c8c66 100644
--- a/spec/lib/gitlab/utils_spec.rb
+++ b/spec/lib/gitlab/utils_spec.rb
@@ -252,4 +252,41 @@ describe Gitlab::Utils do
expect(described_class.string_to_ip_object('1:0:0:0:0:0:0:0/124')).to eq(IPAddr.new('1:0:0:0:0:0:0:0/124'))
end
end
+
+ describe '.allow_hash_values' do
+ it 'removes keys that do not pass the inclusion filters' do
+ symbols = %i[x y z]
+ ints = (0..100)
+ strings = %w[foo bar baz].to_set
+
+ hash = {
+ a: :x,
+ b: 100,
+ c: 'foo',
+ d: :irrelevant,
+ aa: :w,
+ bb: 200,
+ cc: 'food',
+ dd: :totally_irrelevant
+ }
+ allowed = {
+ a: symbols,
+ b: ints,
+ c: strings,
+ aa: symbols,
+ bb: ints,
+ cc: strings
+ }
+
+ described_class.allow_hash_values(hash, allowed)
+
+ expect(hash).to eq({
+ a: :x,
+ b: 100,
+ c: 'foo',
+ d: :irrelevant,
+ dd: :totally_irrelevant
+ })
+ end
+ end
end