1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.shared_examples 'routable resource' do
shared_examples_for '.find_by_full_path' do
it 'finds records by their full path' do
expect(described_class.find_by_full_path(record.full_path)).to eq(record)
expect(described_class.find_by_full_path(record.full_path.upcase)).to eq(record)
end
it 'returns nil for unknown paths' do
expect(described_class.find_by_full_path('unknown')).to be_nil
end
context 'when path is a negative number' do
it 'returns nil' do
expect(described_class.find_by_full_path(-1)).to be_nil
end
end
context 'with redirect routes' do
let_it_be(:redirect_route) { create(:redirect_route, source: record) }
context 'without follow_redirects option' do
it 'does not find records by their redirected path' do
expect(described_class.find_by_full_path(redirect_route.path)).to be_nil
expect(described_class.find_by_full_path(redirect_route.path.upcase)).to be_nil
end
end
context 'with follow_redirects option set to true' do
it 'finds records by their canonical path' do
expect(described_class.find_by_full_path(record.full_path, follow_redirects: true)).to eq(record)
expect(described_class.find_by_full_path(record.full_path.upcase, follow_redirects: true)).to eq(record)
end
it 'finds records by their redirected path' do
expect(described_class.find_by_full_path(redirect_route.path, follow_redirects: true)).to eq(record)
expect(described_class.find_by_full_path(redirect_route.path.upcase, follow_redirects: true)).to eq(record)
end
it 'returns nil for unknown paths' do
expect(described_class.find_by_full_path('unknown', follow_redirects: true)).to be_nil
end
end
end
it 'does not have cross-join' do
expect(Gitlab::Database).not_to receive(:allow_cross_joins_across_databases)
described_class.find_by_full_path(record.full_path)
end
end
it_behaves_like '.find_by_full_path', :aggregate_failures
shared_examples_for '.where_full_path_in' do
context 'without any paths' do
it 'returns an empty relation' do
expect(described_class.where_full_path_in([])).to eq([])
end
end
context 'without any valid paths' do
it 'returns an empty relation' do
expect(described_class.where_full_path_in(%w[unknown])).to eq([])
end
end
context 'with valid paths' do
it 'returns the entities matching the paths' do
result = described_class.where_full_path_in([record.full_path, record_2.full_path])
expect(result).to contain_exactly(record, record_2)
end
it 'returns entities regardless of the casing of paths' do
result = described_class.where_full_path_in([record.full_path.upcase, record_2.full_path.upcase])
expect(result).to contain_exactly(record, record_2)
end
end
context 'on the usage of `preload_routes` parameter' do
let_it_be(:klass) { record.class.to_s.downcase }
let_it_be(:record_3) { create(:"#{klass}") }
let_it_be(:record_4) { create(:"#{klass}") }
context 'when preload_routes: true' do
it 'includes route information when loading records' do
control = ActiveRecord::QueryRecorder.new do
described_class.where_full_path_in([record.full_path, record_2.full_path], preload_routes: true)
.map(&:route)
end
expect do
described_class.where_full_path_in(
[
record.full_path,
record_2.full_path,
record_3.full_path,
record_4.full_path
], preload_routes: true)
.map(&:route)
end.to issue_same_number_of_queries_as(control)
end
end
context 'when preload_routes: false' do
it 'does not include route information when loading records' do
control_count = ActiveRecord::QueryRecorder.new do
described_class.where_full_path_in([record.full_path, record_2.full_path], preload_routes: false)
.map(&:route)
end
expect do
described_class.where_full_path_in(
[
record.full_path,
record_2.full_path,
record_3.full_path,
record_4.full_path
], preload_routes: false)
.map(&:route)
end.not_to issue_same_number_of_queries_as(control_count)
end
end
end
end
it_behaves_like '.where_full_path_in', :aggregate_failures
end
RSpec.shared_examples 'routable resource with parent' do
it_behaves_like 'routable resource'
describe '#full_path' do
it { expect(record.full_path).to eq "#{record.parent.full_path}/#{record.path}" }
it 'hits the cache when not preloaded' do
forcibly_hit_cached_lookup(record, :full_path)
expect(record.full_path).to eq("#{record.parent.full_path}/#{record.path}")
end
end
describe '#full_name' do
it { expect(record.full_name).to eq "#{record.parent.human_name} / #{record.name}" }
context 'without route name' do
before do
stub_feature_flags(cached_route_lookups: true)
record.route.update_attribute(:name, nil)
end
it 'builds full name' do
expect(record.full_name).to eq("#{record.parent.human_name} / #{record.name}")
end
end
it 'hits the cache when not preloaded' do
forcibly_hit_cached_lookup(record, :full_name)
expect(record.full_name).to eq("#{record.parent.human_name} / #{record.name}")
end
end
end
RSpec.describe Group, 'Routable', :with_clean_rails_cache, feature_category: :groups_and_projects do
let_it_be_with_reload(:group) { create(:group, name: 'foo') }
let_it_be(:nested_group) { create(:group, parent: group) }
it_behaves_like 'routable resource' do
let_it_be(:record) { group }
let_it_be(:record_2) { nested_group }
end
it_behaves_like 'routable resource with parent' do
let_it_be(:record) { nested_group }
let_it_be(:record_2) { group }
end
describe 'Validations' do
it { is_expected.to validate_presence_of(:route) }
end
describe 'Associations' do
it { is_expected.to have_one(:route).dependent(:destroy) }
it { is_expected.to have_many(:redirect_routes).dependent(:destroy) }
end
describe 'Callbacks' do
context 'for a group' do
it 'creates route record on create' do
expect(group.route.path).to eq(group.path)
expect(group.route.name).to eq(group.name)
end
it 'updates route record on path change' do
group.update!(path: 'wow', name: 'much')
expect(group.route.path).to eq('wow')
expect(group.route.name).to eq('much')
end
it 'ensure route path uniqueness across different objects' do
create(:group, parent: group, path: 'xyz')
duplicate = build(:project, namespace: group, path: 'xyz')
expect { duplicate.save! }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Path has already been taken')
end
end
context 'for a user' do
let(:user) { create(:user, username: 'jane', name: "Jane Doe") }
it 'creates the route for a record on create' do
expect(user.namespace.name).to eq('Jane Doe')
expect(user.namespace.path).to eq('jane')
end
it 'updates routes and nested routes on name change' do
project = create(:project, path: 'work-stuff', name: 'Work stuff', namespace: user.namespace)
user.update!(username: 'jaen', name: 'Jaen Did')
project.reload
expect(user.namespace.name).to eq('Jaen Did')
expect(user.namespace.path).to eq('jaen')
expect(project.full_name).to eq('Jaen Did / Work stuff')
expect(project.full_path).to eq('jaen/work-stuff')
end
end
end
it 'creates route with namespace referencing group' do
expect(group.route).not_to be_nil
expect(group.route.namespace).to eq(group)
end
describe '#parent_loaded?' do
before do
group.parent = create(:group)
group.save!
group.reload
end
it 'is false when the parent is not loaded' do
expect(group.parent_loaded?).to be_falsey
end
it 'is true when the parent is loaded' do
group.parent
expect(group.parent_loaded?).to be_truthy
end
end
describe '#route_loaded?' do
it 'is false when the route is not loaded' do
expect(group.route_loaded?).to be_falsey
end
it 'is true when the route is loaded' do
group.route
expect(group.route_loaded?).to be_truthy
end
end
end
RSpec.describe Project, 'Routable', :with_clean_rails_cache, feature_category: :groups_and_projects do
let_it_be(:namespace) { create(:namespace) }
let_it_be(:project) { create(:project, namespace: namespace) }
let_it_be(:project_2) { create(:project) }
it_behaves_like 'routable resource with parent' do
let_it_be(:record) { project }
let_it_be(:record_2) { project_2 }
end
it 'creates route with namespace referencing project namespace' do
expect(project.route).not_to be_nil
expect(project.route.namespace).to eq(project.project_namespace)
end
describe '.find_by_full_path' do
it 'does not return a record if the sources are different, but the IDs match', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/451914' do
group = create(:group, id: 1992)
project = create(:project, id: 1992)
record = described_class.where(id: project.id).find_by_full_path(group.full_path)
expect(record).to be_nil
end
end
describe '.where_full_path_in' do
it 'does not return records if the sources are different, but the IDs match', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/457548' do
group = create(:group, id: 1992)
project = create(:project, id: 1992)
records = described_class.where(id: project.id).where_full_path_in([group.full_path])
expect(records).to be_empty
end
end
end
RSpec.describe Namespaces::ProjectNamespace, 'Routable', :with_clean_rails_cache, feature_category: :groups_and_projects do
let_it_be(:group) { create(:group) }
it 'skips route creation for the resource' do
expect do
described_class.create!(project: nil, organization: group.organization, parent: group, visibility_level: Gitlab::VisibilityLevel::PUBLIC, path: 'foo', name: 'foo')
end.not_to change { Route.count }
end
end
def forcibly_hit_cached_lookup(record, method)
stub_feature_flags(cached_route_lookups: true)
expect(record).to receive(:persisted?).and_return(true)
expect(record).to receive(:route_loaded?).and_return(false)
expect(record).to receive(:parent_loaded?).and_return(false)
expect(Gitlab::Cache).to receive(:fetch_once).with([record.cache_key, method]).and_call_original
end
|