2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-13 10:34:15 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::UrlBuilder do
|
2020-04-06 18:09:37 +00:00
|
|
|
subject { described_class }
|
2020-03-03 18:08:16 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
describe '#build' do
|
|
|
|
it 'delegates to the class method' do
|
|
|
|
expect(subject).to receive(:build).with(:foo, bar: :baz)
|
2020-03-03 18:08:16 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
subject.instance.build(:foo, bar: :baz)
|
2020-03-03 18:08:16 +00:00
|
|
|
end
|
2020-04-06 18:09:37 +00:00
|
|
|
end
|
2020-03-03 18:08:16 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
describe '.build' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:factory, :path_generator) do
|
|
|
|
:project | ->(project) { "/#{project.full_path}" }
|
2021-01-08 00:10:44 +00:00
|
|
|
:board | ->(board) { "/#{board.project.full_path}/-/boards/#{board.id}" }
|
|
|
|
:group_board | ->(board) { "/groups/#{board.group.full_path}/-/boards/#{board.id}" }
|
2020-04-06 18:09:37 +00:00
|
|
|
:commit | ->(commit) { "/#{commit.project.full_path}/-/commit/#{commit.id}" }
|
|
|
|
:issue | ->(issue) { "/#{issue.project.full_path}/-/issues/#{issue.iid}" }
|
|
|
|
:merge_request | ->(merge_request) { "/#{merge_request.project.full_path}/-/merge_requests/#{merge_request.iid}" }
|
|
|
|
:project_milestone | ->(milestone) { "/#{milestone.project.full_path}/-/milestones/#{milestone.iid}" }
|
2020-07-23 15:09:28 +00:00
|
|
|
:project_snippet | ->(snippet) { "/#{snippet.project.full_path}/-/snippets/#{snippet.id}" }
|
2020-04-22 12:09:29 +00:00
|
|
|
:project_wiki | ->(wiki) { "/#{wiki.container.full_path}/-/wikis/home" }
|
2020-11-07 00:08:58 +00:00
|
|
|
:release | ->(release) { "/#{release.project.full_path}/-/releases/#{release.tag}" }
|
2020-04-06 18:09:37 +00:00
|
|
|
:ci_build | ->(build) { "/#{build.project.full_path}/-/jobs/#{build.id}" }
|
2020-05-12 00:10:11 +00:00
|
|
|
:design | ->(design) { "/#{design.project.full_path}/-/design_management/designs/#{design.id}/raw_image" }
|
2020-04-06 18:09:37 +00:00
|
|
|
|
|
|
|
:group | ->(group) { "/groups/#{group.full_path}" }
|
|
|
|
:group_milestone | ->(milestone) { "/groups/#{milestone.group.full_path}/-/milestones/#{milestone.iid}" }
|
|
|
|
|
|
|
|
:user | ->(user) { "/#{user.full_path}" }
|
2020-07-23 15:09:28 +00:00
|
|
|
:personal_snippet | ->(snippet) { "/-/snippets/#{snippet.id}" }
|
2020-04-06 18:09:37 +00:00
|
|
|
:wiki_page | ->(wiki_page) { "#{wiki_page.wiki.wiki_base_path}/#{wiki_page.slug}" }
|
|
|
|
|
|
|
|
:note_on_commit | ->(note) { "/#{note.project.full_path}/-/commit/#{note.commit_id}#note_#{note.id}" }
|
|
|
|
:diff_note_on_commit | ->(note) { "/#{note.project.full_path}/-/commit/#{note.commit_id}#note_#{note.id}" }
|
|
|
|
:discussion_note_on_commit | ->(note) { "/#{note.project.full_path}/-/commit/#{note.commit_id}#note_#{note.id}" }
|
|
|
|
:legacy_diff_note_on_commit | ->(note) { "/#{note.project.full_path}/-/commit/#{note.commit_id}#note_#{note.id}" }
|
|
|
|
|
|
|
|
:note_on_issue | ->(note) { "/#{note.project.full_path}/-/issues/#{note.noteable.iid}#note_#{note.id}" }
|
|
|
|
:discussion_note_on_issue | ->(note) { "/#{note.project.full_path}/-/issues/#{note.noteable.iid}#note_#{note.id}" }
|
|
|
|
|
|
|
|
:note_on_merge_request | ->(note) { "/#{note.project.full_path}/-/merge_requests/#{note.noteable.iid}#note_#{note.id}" }
|
|
|
|
:diff_note_on_merge_request | ->(note) { "/#{note.project.full_path}/-/merge_requests/#{note.noteable.iid}#note_#{note.id}" }
|
|
|
|
:discussion_note_on_merge_request | ->(note) { "/#{note.project.full_path}/-/merge_requests/#{note.noteable.iid}#note_#{note.id}" }
|
|
|
|
:legacy_diff_note_on_merge_request | ->(note) { "/#{note.project.full_path}/-/merge_requests/#{note.noteable.iid}#note_#{note.id}" }
|
|
|
|
|
2020-07-23 15:09:28 +00:00
|
|
|
:note_on_project_snippet | ->(note) { "/#{note.project.full_path}/-/snippets/#{note.noteable_id}#note_#{note.id}" }
|
|
|
|
:discussion_note_on_project_snippet | ->(note) { "/#{note.project.full_path}/-/snippets/#{note.noteable_id}#note_#{note.id}" }
|
|
|
|
:discussion_note_on_personal_snippet | ->(note) { "/-/snippets/#{note.noteable_id}#note_#{note.id}" }
|
|
|
|
:note_on_personal_snippet | ->(note) { "/-/snippets/#{note.noteable_id}#note_#{note.id}" }
|
2015-03-05 18:38:23 +00:00
|
|
|
end
|
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
with_them do
|
|
|
|
let(:object) { build_stubbed(factory) }
|
|
|
|
let(:path) { path_generator.call(object) }
|
2018-06-12 22:40:16 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
it 'returns the full URL' do
|
|
|
|
expect(subject.build(object)).to eq("#{Gitlab.config.gitlab.url}#{path}")
|
2018-06-12 22:40:16 +00:00
|
|
|
end
|
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
it 'returns only the path if only_path is given' do
|
|
|
|
expect(subject.build(object, only_path: true)).to eq(path)
|
2018-06-12 22:40:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
context 'when passing a commit without a project' do
|
|
|
|
let(:commit) { build_stubbed(:commit) }
|
2016-04-13 09:25:42 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
it 'returns an empty string' do
|
|
|
|
allow(commit).to receive(:project).and_return(nil)
|
2015-03-05 18:38:23 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
expect(subject.build(commit)).to eq('')
|
2016-04-13 09:25:42 +00:00
|
|
|
end
|
2015-03-05 18:38:23 +00:00
|
|
|
end
|
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
context 'when passing a commit note without a project' do
|
|
|
|
let(:note) { build_stubbed(:note_on_commit) }
|
2019-11-28 15:06:57 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
it 'returns an empty string' do
|
|
|
|
allow(note).to receive(:project).and_return(nil)
|
2019-11-28 15:06:57 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
expect(subject.build(note)).to eq('')
|
2019-11-28 15:06:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
context 'when passing a Snippet' do
|
2020-07-01 15:08:45 +00:00
|
|
|
let_it_be(:personal_snippet) { create(:personal_snippet, :repository) }
|
|
|
|
let_it_be(:project_snippet) { create(:project_snippet, :repository) }
|
|
|
|
let(:blob) { snippet.blobs.first }
|
|
|
|
let(:ref) { blob.repository.root_ref }
|
2019-11-28 15:06:57 +00:00
|
|
|
|
2020-07-01 15:08:45 +00:00
|
|
|
context 'for a PersonalSnippet' do
|
|
|
|
let(:snippet) { personal_snippet }
|
2019-11-28 15:06:57 +00:00
|
|
|
|
2020-07-01 15:08:45 +00:00
|
|
|
it 'returns a raw snippet URL if requested' do
|
|
|
|
url = subject.build(snippet, raw: true)
|
|
|
|
|
2020-07-23 15:09:28 +00:00
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/-/snippets/#{snippet.id}/raw"
|
2020-07-01 15:08:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a raw snippet blob URL if requested' do
|
|
|
|
url = subject.build(snippet, file: blob.path, ref: ref)
|
|
|
|
|
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a ProjectSnippet' do
|
|
|
|
let(:snippet) { project_snippet }
|
|
|
|
|
|
|
|
it 'returns a raw snippet URL if requested' do
|
|
|
|
url = subject.build(snippet, raw: true)
|
|
|
|
|
2020-07-23 15:09:28 +00:00
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/#{snippet.project.full_path}/-/snippets/#{snippet.id}/raw"
|
2020-07-01 15:08:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a raw snippet blob URL if requested' do
|
|
|
|
url = subject.build(snippet, file: blob.path, ref: ref)
|
|
|
|
|
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/#{snippet.project.full_path}/-/snippets/#{snippet.id}/raw/#{ref}/#{blob.path}"
|
|
|
|
end
|
2019-11-28 15:06:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 15:08:36 +00:00
|
|
|
context 'when passing a Wiki' do
|
|
|
|
let(:wiki) { build_stubbed(:project_wiki) }
|
|
|
|
|
|
|
|
describe '#wiki_url' do
|
|
|
|
it 'uses the default collection action' do
|
|
|
|
url = subject.wiki_url(wiki)
|
|
|
|
|
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/#{wiki.project.full_path}/-/wikis/home"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a custom collection action' do
|
|
|
|
url = subject.wiki_url(wiki, action: :pages)
|
|
|
|
|
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/#{wiki.project.full_path}/-/wikis/pages"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#wiki_page_url' do
|
|
|
|
it 'uses the default member action' do
|
|
|
|
url = subject.wiki_page_url(wiki, 'foo')
|
|
|
|
|
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/#{wiki.project.full_path}/-/wikis/foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a custom member action' do
|
|
|
|
url = subject.wiki_page_url(wiki, 'foo', action: :edit)
|
|
|
|
|
|
|
|
expect(url).to eq "#{Gitlab.config.gitlab.url}/#{wiki.project.full_path}/-/wikis/foo/edit"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-12 00:10:11 +00:00
|
|
|
context 'when passing a DesignManagement::Design' do
|
|
|
|
let(:design) { build_stubbed(:design) }
|
|
|
|
|
|
|
|
it 'uses the given ref and size in the URL' do
|
|
|
|
url = subject.build(design, ref: 'feature', size: 'small')
|
|
|
|
|
|
|
|
expect(url).to eq "#{Settings.gitlab['url']}/#{design.project.full_path}/-/design_management/designs/#{design.id}/feature/resized_image/small"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
context 'when passing an unsupported class' do
|
|
|
|
let(:object) { Object.new }
|
2015-03-05 18:38:23 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
it 'raises an exception' do
|
|
|
|
expect { subject.build(object) }.to raise_error(NotImplementedError)
|
2016-04-13 09:25:42 +00:00
|
|
|
end
|
2020-04-06 18:09:37 +00:00
|
|
|
end
|
2016-04-13 09:25:42 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
context 'when passing a batch loaded model' do
|
|
|
|
let(:project) { build_stubbed(:project) }
|
|
|
|
let(:object) do
|
|
|
|
BatchLoader.for(:project).batch do |batch, loader|
|
|
|
|
batch.each { |_| loader.call(:project, project) }
|
2016-04-13 09:25:42 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-15 04:47:09 +00:00
|
|
|
|
2020-04-06 18:09:37 +00:00
|
|
|
it 'returns the URL for the real object' do
|
|
|
|
expect(subject.build(object, only_path: true)).to eq("/#{project.full_path}")
|
2016-04-15 04:47:09 +00:00
|
|
|
end
|
|
|
|
end
|
2015-03-05 18:38:23 +00:00
|
|
|
end
|
2014-06-13 10:34:15 +00:00
|
|
|
end
|