2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-31 07:00:53 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Route do
|
2017-05-03 13:14:30 -04:00
|
|
|
let(:group) { create(:group, path: 'git_lab', name: 'git_lab') }
|
|
|
|
let(:route) { group.route }
|
2016-10-31 07:00:53 -04:00
|
|
|
|
|
|
|
describe 'relationships' do
|
|
|
|
it { is_expected.to belong_to(:source) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
expect(route).to be_persisted
|
|
|
|
end
|
|
|
|
|
2016-10-31 07:00:53 -04:00
|
|
|
it { is_expected.to validate_presence_of(:source) }
|
|
|
|
it { is_expected.to validate_presence_of(:path) }
|
2017-07-24 08:33:14 -04:00
|
|
|
it { is_expected.to validate_uniqueness_of(:path).case_insensitive }
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
2017-05-03 13:14:30 -04:00
|
|
|
describe 'callbacks' do
|
2018-01-04 17:16:13 -05:00
|
|
|
context 'before validation' do
|
|
|
|
it 'calls #delete_conflicting_orphaned_routes' do
|
|
|
|
expect(route).to receive(:delete_conflicting_orphaned_routes)
|
|
|
|
route.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-03 13:14:30 -04:00
|
|
|
context 'after update' do
|
|
|
|
it 'calls #create_redirect_for_old_path' do
|
|
|
|
expect(route).to receive(:create_redirect_for_old_path)
|
2018-07-02 06:43:06 -04:00
|
|
|
route.update(path: 'foo')
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls #delete_conflicting_redirects' do
|
|
|
|
expect(route).to receive(:delete_conflicting_redirects)
|
2018-07-02 06:43:06 -04:00
|
|
|
route.update(path: 'foo')
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'after create' do
|
|
|
|
it 'calls #delete_conflicting_redirects' do
|
|
|
|
route.destroy
|
2017-07-25 13:09:00 -04:00
|
|
|
new_route = described_class.new(source: group, path: group.path)
|
2017-05-03 13:14:30 -04:00
|
|
|
expect(new_route).to receive(:delete_conflicting_redirects)
|
|
|
|
new_route.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-21 12:04:12 -04:00
|
|
|
describe '.inside_path' do
|
|
|
|
let!(:nested_group) { create(:group, path: 'test', name: 'test', parent: group) }
|
|
|
|
let!(:deep_nested_group) { create(:group, path: 'foo', name: 'foo', parent: nested_group) }
|
|
|
|
let!(:another_group) { create(:group, path: 'other') }
|
|
|
|
let!(:similar_group) { create(:group, path: 'gitllab') }
|
|
|
|
let!(:another_group_nested) { create(:group, path: 'another', name: 'another', parent: similar_group) }
|
|
|
|
|
|
|
|
it 'returns correct routes' do
|
2017-07-25 13:09:00 -04:00
|
|
|
expect(described_class.inside_path('git_lab')).to match_array([nested_group.route, deep_nested_group.route])
|
2017-03-21 12:04:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-02 10:08:52 -05:00
|
|
|
describe '.for_routable_type' do
|
|
|
|
let!(:nested_group) { create(:group, path: 'foo', name: 'foo', parent: group) }
|
|
|
|
let!(:project) { create(:project, path: 'other-project') }
|
|
|
|
|
|
|
|
it 'returns correct routes' do
|
|
|
|
expect(described_class.for_routable_type(Project.name)).to match_array([project.route])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-05 17:31:33 -04:00
|
|
|
describe '#rename_descendants' do
|
2017-02-04 13:26:11 -05:00
|
|
|
let!(:nested_group) { create(:group, path: 'test', name: 'test', parent: group) }
|
|
|
|
let!(:deep_nested_group) { create(:group, path: 'foo', name: 'foo', parent: nested_group) }
|
|
|
|
let!(:similar_group) { create(:group, path: 'gitlab-org', name: 'gitlab-org') }
|
2017-03-21 12:04:12 -04:00
|
|
|
let!(:another_group) { create(:group, path: 'gittlab', name: 'gitllab') }
|
|
|
|
let!(:another_group_nested) { create(:group, path: 'git_lab', name: 'git_lab', parent: another_group) }
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
context 'path update' do
|
2017-02-08 15:54:33 -05:00
|
|
|
context 'when route name is set' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2018-07-02 06:43:06 -04:00
|
|
|
route.update(path: 'bar')
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-02-08 15:54:33 -05:00
|
|
|
|
2017-05-03 13:14:30 -04:00
|
|
|
it 'updates children routes with new path' do
|
2017-02-08 15:54:33 -05:00
|
|
|
expect(described_class.exists?(path: 'bar')).to be_truthy
|
|
|
|
expect(described_class.exists?(path: 'bar/test')).to be_truthy
|
|
|
|
expect(described_class.exists?(path: 'bar/test/foo')).to be_truthy
|
|
|
|
expect(described_class.exists?(path: 'gitlab-org')).to be_truthy
|
2017-03-21 12:04:12 -04:00
|
|
|
expect(described_class.exists?(path: 'gittlab')).to be_truthy
|
|
|
|
expect(described_class.exists?(path: 'gittlab/git_lab')).to be_truthy
|
2017-02-08 15:54:33 -05:00
|
|
|
end
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2017-02-08 15:54:33 -05:00
|
|
|
context 'when route name is nil' do
|
|
|
|
before do
|
|
|
|
route.update_column(:name, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not fail" do
|
2018-07-02 06:43:06 -04:00
|
|
|
expect(route.update(path: 'bar')).to be_truthy
|
2017-02-08 15:54:33 -05:00
|
|
|
end
|
2017-02-04 13:26:11 -05:00
|
|
|
end
|
2017-05-03 13:14:30 -04:00
|
|
|
|
|
|
|
context 'when conflicting redirects exist' do
|
2017-12-08 12:42:43 -05:00
|
|
|
let(:route) { create(:project).route }
|
2017-05-03 13:14:30 -04:00
|
|
|
let!(:conflicting_redirect1) { route.create_redirect('bar/test') }
|
|
|
|
let!(:conflicting_redirect2) { route.create_redirect('bar/test/foo') }
|
|
|
|
let!(:conflicting_redirect3) { route.create_redirect('gitlab-org') }
|
|
|
|
|
|
|
|
it 'deletes the conflicting redirects' do
|
2018-07-02 06:43:06 -04:00
|
|
|
route.update(path: 'bar')
|
2017-05-03 13:14:30 -04:00
|
|
|
|
|
|
|
expect(RedirectRoute.exists?(path: 'bar/test')).to be_falsey
|
|
|
|
expect(RedirectRoute.exists?(path: 'bar/test/foo')).to be_falsey
|
|
|
|
expect(RedirectRoute.exists?(path: 'gitlab-org')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2017-02-04 13:26:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'name update' do
|
2017-05-03 13:14:30 -04:00
|
|
|
it 'updates children routes with new path' do
|
2018-07-02 06:43:06 -04:00
|
|
|
route.update(name: 'bar')
|
2017-03-20 19:17:14 -04:00
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
expect(described_class.exists?(name: 'bar')).to be_truthy
|
|
|
|
expect(described_class.exists?(name: 'bar / test')).to be_truthy
|
|
|
|
expect(described_class.exists?(name: 'bar / test / foo')).to be_truthy
|
|
|
|
expect(described_class.exists?(name: 'gitlab-org')).to be_truthy
|
|
|
|
end
|
2017-03-20 19:17:14 -04:00
|
|
|
|
|
|
|
it 'handles a rename from nil' do
|
|
|
|
# Note: using `update_columns` to skip all validation and callbacks
|
|
|
|
route.update_columns(name: nil)
|
|
|
|
|
2018-07-02 06:43:06 -04:00
|
|
|
expect { route.update(name: 'bar') }
|
2017-03-20 19:17:14 -04:00
|
|
|
.to change { route.name }.from(nil).to('bar')
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-01 19:48:05 -04:00
|
|
|
|
2017-05-03 13:14:30 -04:00
|
|
|
describe '#create_redirect_for_old_path' do
|
2017-05-01 19:48:05 -04:00
|
|
|
context 'if the path changed' do
|
|
|
|
it 'creates a RedirectRoute for the old path' do
|
2017-05-03 13:14:30 -04:00
|
|
|
redirect_scope = route.source.redirect_routes.where(path: 'git_lab')
|
|
|
|
expect(redirect_scope.exists?).to be_falsey
|
2017-05-01 19:48:05 -04:00
|
|
|
route.path = 'new-path'
|
|
|
|
route.save!
|
2017-05-03 13:14:30 -04:00
|
|
|
expect(redirect_scope.exists?).to be_truthy
|
2017-05-01 19:48:05 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
2017-05-01 19:48:05 -04:00
|
|
|
|
2017-05-03 13:14:30 -04:00
|
|
|
describe '#create_redirect' do
|
|
|
|
it 'creates a RedirectRoute with the same source' do
|
|
|
|
redirect_route = route.create_redirect('foo')
|
|
|
|
expect(redirect_route).to be_a(RedirectRoute)
|
|
|
|
expect(redirect_route).to be_persisted
|
|
|
|
expect(redirect_route.source).to eq(route.source)
|
|
|
|
expect(redirect_route.path).to eq('foo')
|
|
|
|
end
|
2017-12-08 12:42:43 -05:00
|
|
|
|
|
|
|
context 'when the source is a Project' do
|
2018-03-05 07:02:36 -05:00
|
|
|
it 'creates a RedirectRoute' do
|
2017-12-08 12:42:43 -05:00
|
|
|
project = create(:project)
|
|
|
|
route = project.route
|
|
|
|
redirect_route = route.create_redirect('foo')
|
2018-03-05 07:02:36 -05:00
|
|
|
expect(redirect_route).not_to be_nil
|
2017-12-08 12:42:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the source is not a project' do
|
2018-03-05 07:02:36 -05:00
|
|
|
it 'creates a RedirectRoute' do
|
|
|
|
redirect_route = route.create_redirect('foo')
|
|
|
|
expect(redirect_route).not_to be_nil
|
2017-12-08 12:42:43 -05:00
|
|
|
end
|
|
|
|
end
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#delete_conflicting_redirects' do
|
2018-03-05 07:02:36 -05:00
|
|
|
let(:route) { create(:project).route }
|
2017-12-08 12:42:43 -05:00
|
|
|
|
2018-03-05 07:02:36 -05:00
|
|
|
it 'deletes the redirect' do
|
|
|
|
route.create_redirect("#{route.path}/foo")
|
2017-12-08 12:42:43 -05:00
|
|
|
|
2018-03-05 07:02:36 -05:00
|
|
|
expect do
|
|
|
|
route.delete_conflicting_redirects
|
|
|
|
end.to change { RedirectRoute.count }.by(-1)
|
2017-12-08 12:42:43 -05:00
|
|
|
end
|
|
|
|
|
2017-05-03 13:14:30 -04:00
|
|
|
context 'when a redirect route with the same path exists' do
|
2017-08-07 19:41:39 -04:00
|
|
|
context 'when the redirect route has matching case' do
|
2017-12-08 12:42:43 -05:00
|
|
|
let(:route) { create(:project).route }
|
2017-08-07 19:41:39 -04:00
|
|
|
let!(:redirect1) { route.create_redirect(route.path) }
|
2017-05-03 13:14:30 -04:00
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
it 'deletes the redirect' do
|
|
|
|
expect do
|
|
|
|
route.delete_conflicting_redirects
|
2017-08-09 11:11:08 -04:00
|
|
|
end.to change { RedirectRoute.count }.by(-1)
|
2017-08-07 19:41:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when redirect routes with paths descending from the route path exists' do
|
|
|
|
let!(:redirect2) { route.create_redirect("#{route.path}/foo") }
|
|
|
|
let!(:redirect3) { route.create_redirect("#{route.path}/foo/bar") }
|
|
|
|
let!(:redirect4) { route.create_redirect("#{route.path}/baz/quz") }
|
|
|
|
let!(:other_redirect) { route.create_redirect("other") }
|
|
|
|
|
|
|
|
it 'deletes all redirects with paths that descend from the route path' do
|
|
|
|
expect do
|
|
|
|
route.delete_conflicting_redirects
|
2017-08-09 11:11:08 -04:00
|
|
|
end.to change { RedirectRoute.count }.by(-4)
|
2017-08-07 19:41:39 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
context 'when the redirect route is differently cased' do
|
2017-12-08 12:42:43 -05:00
|
|
|
let(:route) { create(:project).route }
|
2017-08-07 19:41:39 -04:00
|
|
|
let!(:redirect1) { route.create_redirect(route.path.upcase) }
|
2017-05-03 13:14:30 -04:00
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
it 'deletes the redirect' do
|
|
|
|
expect do
|
|
|
|
route.delete_conflicting_redirects
|
2017-08-09 11:11:08 -04:00
|
|
|
end.to change { RedirectRoute.count }.by(-1)
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#conflicting_redirects' do
|
2018-03-05 07:02:36 -05:00
|
|
|
let(:route) { create(:project).route }
|
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
it 'returns an ActiveRecord::Relation' do
|
|
|
|
expect(route.conflicting_redirects).to be_an(ActiveRecord::Relation)
|
|
|
|
end
|
|
|
|
|
2018-03-05 07:02:36 -05:00
|
|
|
it 'returns the redirect routes' do
|
|
|
|
redirect1 = route.create_redirect("#{route.path}/foo")
|
|
|
|
redirect2 = route.create_redirect("#{route.path}/foo/bar")
|
|
|
|
redirect3 = route.create_redirect("#{route.path}/baz/quz")
|
2017-12-08 12:42:43 -05:00
|
|
|
|
2018-03-05 07:02:36 -05:00
|
|
|
expect(route.conflicting_redirects).to match_array([redirect1, redirect2, redirect3])
|
2017-12-08 12:42:43 -05:00
|
|
|
end
|
|
|
|
|
2017-05-03 13:14:30 -04:00
|
|
|
context 'when a redirect route with the same path exists' do
|
2017-12-08 12:42:43 -05:00
|
|
|
let(:route) { create(:project).route }
|
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
context 'when the redirect route has matching case' do
|
|
|
|
let!(:redirect1) { route.create_redirect(route.path) }
|
2017-05-03 13:14:30 -04:00
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
it 'returns the redirect route' do
|
|
|
|
expect(route.conflicting_redirects).to match_array([redirect1])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when redirect routes with paths descending from the route path exists' do
|
|
|
|
let!(:redirect2) { route.create_redirect("#{route.path}/foo") }
|
|
|
|
let!(:redirect3) { route.create_redirect("#{route.path}/foo/bar") }
|
|
|
|
let!(:redirect4) { route.create_redirect("#{route.path}/baz/quz") }
|
|
|
|
let!(:other_redirect) { route.create_redirect("other") }
|
|
|
|
|
|
|
|
it 'returns the redirect routes' do
|
|
|
|
expect(route.conflicting_redirects).to match_array([redirect1, redirect2, redirect3, redirect4])
|
|
|
|
end
|
|
|
|
end
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
context 'when the redirect route is differently cased' do
|
|
|
|
let!(:redirect1) { route.create_redirect(route.path.upcase) }
|
2017-05-03 13:14:30 -04:00
|
|
|
|
2017-08-07 19:41:39 -04:00
|
|
|
it 'returns the redirect route' do
|
|
|
|
expect(route.conflicting_redirects).to match_array([redirect1])
|
2017-05-03 13:14:30 -04:00
|
|
|
end
|
2017-05-01 19:48:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-08 12:42:43 -05:00
|
|
|
|
2018-01-04 17:16:13 -05:00
|
|
|
describe '#delete_conflicting_orphaned_routes' do
|
|
|
|
context 'when there is a conflicting route' do
|
|
|
|
let!(:conflicting_group) { create(:group, path: 'foo') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
route.path = conflicting_group.route.path
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the route is orphaned' do
|
|
|
|
let!(:offending_route) { conflicting_group.route }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Group.delete(conflicting_group) # Orphan the route
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the orphaned route' do
|
|
|
|
expect do
|
|
|
|
route.valid?
|
|
|
|
end.to change { described_class.count }.from(2).to(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes validation, as usual' do
|
|
|
|
expect(route.valid?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the route is not orphaned' do
|
|
|
|
it 'does not delete the conflicting route' do
|
|
|
|
expect do
|
|
|
|
route.valid?
|
|
|
|
end.not_to change { described_class.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails validation, as usual' do
|
|
|
|
expect(route.valid?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no conflicting routes' do
|
|
|
|
it 'does not delete any routes' do
|
|
|
|
route
|
|
|
|
|
|
|
|
expect do
|
|
|
|
route.valid?
|
|
|
|
end.not_to change { described_class.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes validation, as usual' do
|
|
|
|
expect(route.valid?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|