2016-10-31 07:00:53 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Group, 'Routable' do
|
2017-03-15 08:45:28 -04:00
|
|
|
let!(:group) { create(:group, name: 'foo') }
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2016-12-07 12:16:02 -05:00
|
|
|
describe 'Validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:route) }
|
|
|
|
end
|
|
|
|
|
2016-10-31 07:00:53 -04:00
|
|
|
describe 'Associations' do
|
|
|
|
it { is_expected.to have_one(:route).dependent(:destroy) }
|
2017-05-01 16:46:30 -04:00
|
|
|
it { is_expected.to have_many(:redirect_routes).dependent(:destroy) }
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
2017-09-19 03:44:58 -04:00
|
|
|
|
2016-10-31 07:00:53 -04:00
|
|
|
describe 'Callbacks' do
|
|
|
|
it 'creates route record on create' do
|
|
|
|
expect(group.route.path).to eq(group.path)
|
2017-02-04 13:26:11 -05:00
|
|
|
expect(group.route.name).to eq(group.name)
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates route record on path change' do
|
2018-07-02 06:43:06 -04:00
|
|
|
group.update(path: 'wow', name: 'much')
|
2016-10-31 07:00:53 -04:00
|
|
|
|
|
|
|
expect(group.route.path).to eq('wow')
|
2017-02-04 13:26:11 -05:00
|
|
|
expect(group.route.name).to eq('much')
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensure route path uniqueness across different objects' do
|
|
|
|
create(:group, parent: group, path: 'xyz')
|
2017-08-02 15:55:11 -04:00
|
|
|
duplicate = build(:project, namespace: group, path: 'xyz')
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2018-01-31 11:52:09 -05:00
|
|
|
expect { duplicate.save! }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Path has already been taken')
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.find_by_full_path' do
|
|
|
|
let!(:nested_group) { create(:group, parent: group) }
|
|
|
|
|
2017-05-01 16:46:30 -04:00
|
|
|
context 'without any redirect routes' do
|
|
|
|
it { expect(described_class.find_by_full_path(group.to_param)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(group.to_param.upcase)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(nested_group.to_param)).to eq(nested_group) }
|
|
|
|
it { expect(described_class.find_by_full_path('unknown')).to eq(nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with redirect routes' do
|
|
|
|
let!(:group_redirect_route) { group.redirect_routes.create!(path: 'bar') }
|
|
|
|
let!(:nested_group_redirect_route) { nested_group.redirect_routes.create!(path: nested_group.path.sub('foo', 'bar')) }
|
|
|
|
|
|
|
|
context 'without follow_redirects option' do
|
|
|
|
context 'with the given path not matching any route' do
|
|
|
|
it { expect(described_class.find_by_full_path('unknown')).to eq(nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the given path matching the canonical route' do
|
|
|
|
it { expect(described_class.find_by_full_path(group.to_param)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(group.to_param.upcase)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(nested_group.to_param)).to eq(nested_group) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the given path matching a redirect route' do
|
|
|
|
it { expect(described_class.find_by_full_path(group_redirect_route.path)).to eq(nil) }
|
|
|
|
it { expect(described_class.find_by_full_path(group_redirect_route.path.upcase)).to eq(nil) }
|
|
|
|
it { expect(described_class.find_by_full_path(nested_group_redirect_route.path)).to eq(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with follow_redirects option set to true' do
|
|
|
|
context 'with the given path not matching any route' do
|
|
|
|
it { expect(described_class.find_by_full_path('unknown', follow_redirects: true)).to eq(nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the given path matching the canonical route' do
|
|
|
|
it { expect(described_class.find_by_full_path(group.to_param, follow_redirects: true)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(group.to_param.upcase, follow_redirects: true)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(nested_group.to_param, follow_redirects: true)).to eq(nested_group) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the given path matching a redirect route' do
|
|
|
|
it { expect(described_class.find_by_full_path(group_redirect_route.path, follow_redirects: true)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(group_redirect_route.path.upcase, follow_redirects: true)).to eq(group) }
|
|
|
|
it { expect(described_class.find_by_full_path(nested_group_redirect_route.path, follow_redirects: true)).to eq(nested_group) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
2016-12-09 10:27:11 -05:00
|
|
|
describe '.where_full_path_in' do
|
2016-10-31 07:00:53 -04:00
|
|
|
context 'without any paths' do
|
|
|
|
it 'returns an empty relation' do
|
2016-12-09 10:27:11 -05:00
|
|
|
expect(described_class.where_full_path_in([])).to eq([])
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without any valid paths' do
|
|
|
|
it 'returns an empty relation' do
|
2016-12-09 10:27:11 -05:00
|
|
|
expect(described_class.where_full_path_in(%w[unknown])).to eq([])
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with valid paths' do
|
|
|
|
let!(:nested_group) { create(:group, parent: group) }
|
|
|
|
|
|
|
|
it 'returns the projects matching the paths' do
|
2016-12-09 10:27:11 -05:00
|
|
|
result = described_class.where_full_path_in([group.to_param, nested_group.to_param])
|
2016-10-31 07:00:53 -04:00
|
|
|
|
|
|
|
expect(result).to contain_exactly(group, nested_group)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns projects regardless of the casing of paths' do
|
2016-12-09 10:27:11 -05:00
|
|
|
result = described_class.where_full_path_in([group.to_param.upcase, nested_group.to_param.upcase])
|
2016-10-31 07:00:53 -04:00
|
|
|
|
|
|
|
expect(result).to contain_exactly(group, nested_group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-05 12:20:12 -05:00
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
describe '#full_path' do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:nested_group) { create(:group, parent: group) }
|
|
|
|
|
|
|
|
it { expect(group.full_path).to eq(group.path) }
|
2017-02-23 18:55:01 -05:00
|
|
|
it { expect(nested_group.full_path).to eq("#{group.full_path}/#{nested_group.path}") }
|
2017-06-29 17:53:32 -04:00
|
|
|
end
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
describe '#full_name' do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:nested_group) { create(:group, parent: group) }
|
|
|
|
|
|
|
|
it { expect(group.full_name).to eq(group.name) }
|
|
|
|
it { expect(nested_group.full_name).to eq("#{group.name} / #{nested_group.name}") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Project, 'Routable' do
|
|
|
|
describe '#full_path' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { build_stubbed(:project) }
|
2017-02-04 13:26:11 -05:00
|
|
|
|
2017-02-23 18:55:01 -05:00
|
|
|
it { expect(project.full_path).to eq "#{project.namespace.full_path}/#{project.path}" }
|
2017-02-04 13:26:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#full_name' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { build_stubbed(:project) }
|
2017-02-04 13:26:11 -05:00
|
|
|
|
|
|
|
it { expect(project.full_name).to eq "#{project.namespace.human_name} / #{project.name}" }
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|