debian-mirror-gitlab/spec/lib/gitlab/url_builder_spec.rb

120 lines
5.3 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Gitlab::UrlBuilder do
2020-04-22 19:07:51 +05:30
subject { described_class }
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
describe '#build' do
it 'delegates to the class method' do
expect(subject).to receive(:build).with(:foo, bar: :baz)
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
subject.instance.build(:foo, bar: :baz)
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
end
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
describe '.build' do
using RSpec::Parameterized::TableSyntax
where(:factory, :path_generator) do
:project | ->(project) { "/#{project.full_path}" }
: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}" }
:project_snippet | ->(snippet) { "/#{snippet.project.full_path}/snippets/#{snippet.id}" }
:project_wiki | ->(wiki) { "/#{wiki.project.full_path}/-/wikis/home" }
:ci_build | ->(build) { "/#{build.project.full_path}/-/jobs/#{build.id}" }
:group | ->(group) { "/groups/#{group.full_path}" }
:group_milestone | ->(milestone) { "/groups/#{milestone.group.full_path}/-/milestones/#{milestone.iid}" }
:user | ->(user) { "/#{user.full_path}" }
:personal_snippet | ->(snippet) { "/snippets/#{snippet.id}" }
: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}" }
: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-04-26 12:48:37 +05:30
end
2020-04-22 19:07:51 +05:30
with_them do
let(:object) { build_stubbed(factory) }
let(:path) { path_generator.call(object) }
2018-11-08 19:23:39 +05:30
2020-04-22 19:07:51 +05:30
it 'returns the full URL' do
expect(subject.build(object)).to eq("#{Gitlab.config.gitlab.url}#{path}")
2018-11-08 19:23:39 +05:30
end
2020-04-22 19:07:51 +05:30
it 'returns only the path if only_path is given' do
expect(subject.build(object, only_path: true)).to eq(path)
2018-11-08 19:23:39 +05:30
end
end
2020-04-22 19:07:51 +05:30
context 'when passing a commit without a project' do
let(:commit) { build_stubbed(:commit) }
2016-06-02 11:05:42 +05:30
2020-04-22 19:07:51 +05:30
it 'returns an empty string' do
allow(commit).to receive(:project).and_return(nil)
2015-04-26 12:48:37 +05:30
2020-04-22 19:07:51 +05:30
expect(subject.build(commit)).to eq('')
2016-06-02 11:05:42 +05:30
end
2015-04-26 12:48:37 +05:30
end
2020-04-22 19:07:51 +05:30
context 'when passing a commit note without a project' do
let(:note) { build_stubbed(:note_on_commit) }
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
it 'returns an empty string' do
allow(note).to receive(:project).and_return(nil)
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
expect(subject.build(note)).to eq('')
2020-01-01 13:55:28 +05:30
end
end
2020-04-22 19:07:51 +05:30
context 'when passing a Snippet' do
let(:snippet) { build_stubbed(:personal_snippet) }
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
it 'returns a raw snippet URL if requested' do
url = subject.build(snippet, raw: true)
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
expect(url).to eq "#{Gitlab.config.gitlab.url}/snippets/#{snippet.id}/raw"
2020-01-01 13:55:28 +05:30
end
end
2020-04-22 19:07:51 +05:30
context 'when passing an unsupported class' do
let(:object) { Object.new }
2016-06-02 11:05:42 +05:30
2020-04-22 19:07:51 +05:30
it 'raises an exception' do
expect { subject.build(object) }.to raise_error(NotImplementedError)
2016-06-02 11:05:42 +05:30
end
2020-04-22 19:07:51 +05:30
end
2015-04-26 12:48:37 +05:30
2020-04-22 19:07:51 +05:30
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-06-02 11:05:42 +05:30
end
end
2015-04-26 12:48:37 +05:30
2020-04-22 19:07:51 +05:30
it 'returns the URL for the real object' do
expect(subject.build(object, only_path: true)).to eq("/#{project.full_path}")
2016-06-02 11:05:42 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
end