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
329
330
331
332
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::Catalog::Resource, feature_category: :pipeline_composition do
let_it_be(:current_user) { create(:user) }
let_it_be(:project_a) { create(:project, name: 'A') }
let_it_be(:project_b) { create(:project, name: 'B') }
let_it_be(:project_c) { create(:project, name: 'C', description: 'B') }
let_it_be_with_reload(:resource_a) do
create(:ci_catalog_resource, project: project_a, latest_released_at: '2023-02-01T00:00:00Z')
end
let_it_be(:resource_b) do
create(:ci_catalog_resource, project: project_b, latest_released_at: '2023-01-01T00:00:00Z')
end
let_it_be(:resource_c) { create(:ci_catalog_resource, project: project_c) }
it { is_expected.to belong_to(:project) }
it do
is_expected.to(
have_many(:components).class_name('Ci::Catalog::Resources::Component').with_foreign_key(:catalog_resource_id))
end
it do
is_expected.to(
have_many(:versions).class_name('Ci::Catalog::Resources::Version').with_foreign_key(:catalog_resource_id))
end
it do
is_expected.to(
have_many(:sync_events).class_name('Ci::Catalog::Resources::SyncEvent').with_foreign_key(:catalog_resource_id))
end
it { is_expected.to delegate_method(:avatar_path).to(:project) }
it { is_expected.to delegate_method(:star_count).to(:project) }
it { is_expected.to define_enum_for(:state).with_values({ draft: 0, published: 1 }) }
it do
is_expected.to define_enum_for(:verification_level)
.with_values({ unverified: 0, gitlab: 1 })
end
describe '.for_projects' do
it 'returns catalog resources for the given project IDs' do
resources_for_projects = described_class.for_projects(project_a.id)
expect(resources_for_projects).to contain_exactly(resource_a)
end
end
describe '.search' do
it 'returns catalog resources whose name or description match the search term' do
resources = described_class.search('B')
expect(resources).to contain_exactly(resource_b, resource_c)
end
end
describe '.order_by_created_at_desc' do
it 'returns catalog resources sorted by descending created at' do
ordered_resources = described_class.order_by_created_at_desc
expect(ordered_resources.to_a).to eq([resource_c, resource_b, resource_a])
end
end
describe '.order_by_created_at_asc' do
it 'returns catalog resources sorted by ascending created at' do
ordered_resources = described_class.order_by_created_at_asc
expect(ordered_resources.to_a).to eq([resource_a, resource_b, resource_c])
end
end
describe '.order_by_name_desc' do
subject(:ordered_resources) { described_class.order_by_name_desc }
it 'returns catalog resources sorted by descending name' do
expect(ordered_resources.pluck(:name)).to eq(%w[C B A])
end
it 'returns catalog resources sorted by descending name with nulls last' do
resource_a.update!(name: nil)
expect(ordered_resources.pluck(:name)).to eq(['C', 'B', nil])
end
end
describe '.order_by_name_asc' do
subject(:ordered_resources) { described_class.order_by_name_asc }
it 'returns catalog resources sorted by ascending name' do
expect(ordered_resources.pluck(:name)).to eq(%w[A B C])
end
it 'returns catalog resources sorted by ascending name with nulls last' do
resource_a.update!(name: nil)
expect(ordered_resources.pluck(:name)).to eq(['B', 'C', nil])
end
end
describe '.order_by_latest_released_at_desc' do
it 'returns catalog resources sorted by latest_released_at descending with nulls last' do
ordered_resources = described_class.order_by_latest_released_at_desc
expect(ordered_resources).to eq([resource_a, resource_b, resource_c])
end
end
describe '.order_by_latest_released_at_asc' do
it 'returns catalog resources sorted by latest_released_at ascending with nulls last' do
ordered_resources = described_class.order_by_latest_released_at_asc
expect(ordered_resources).to eq([resource_b, resource_a, resource_c])
end
end
describe 'authorized catalog resources' do
let_it_be(:namespace) { create(:group) }
let_it_be(:other_namespace) { create(:group) }
let_it_be(:other_user) { create(:user) }
let_it_be(:public_project) { create(:project, :public) }
let_it_be(:internal_project) { create(:project, :internal) }
let_it_be(:internal_namespace_project) { create(:project, :internal, namespace: namespace) }
let_it_be(:private_namespace_project) { create(:project, namespace: namespace) }
let_it_be(:other_private_namespace_project) { create(:project, namespace: other_namespace) }
let_it_be(:public_resource) { create(:ci_catalog_resource, project: public_project) }
let_it_be(:internal_resource) { create(:ci_catalog_resource, project: internal_project) }
let_it_be(:internal_namespace_resource) { create(:ci_catalog_resource, project: internal_namespace_project) }
let_it_be(:private_namespace_resource) { create(:ci_catalog_resource, project: private_namespace_project) }
let_it_be(:other_private_namespace_resource) do
create(:ci_catalog_resource, project: other_private_namespace_project)
end
before_all do
namespace.add_reporter(current_user)
other_namespace.add_guest(other_user)
end
describe '.public_or_visible_to_user' do
subject(:resources) { described_class.public_or_visible_to_user(current_user) }
it 'returns all resources visible to the user' do
expect(resources).to contain_exactly(
public_resource, internal_resource, internal_namespace_resource, private_namespace_resource)
end
context 'with a different user' do
let(:current_user) { other_user }
it 'returns all resources visible to the user' do
expect(resources).to contain_exactly(
public_resource, internal_resource, internal_namespace_resource, other_private_namespace_resource)
end
end
context 'when the user is nil' do
let(:current_user) { nil }
it 'returns only public resources' do
expect(resources).to contain_exactly(public_resource)
end
end
end
describe '.visible_to_user' do
subject(:resources) { described_class.visible_to_user(current_user) }
it "returns resources belonging to the user's authorized namespaces" do
expect(resources).to contain_exactly(internal_namespace_resource, private_namespace_resource)
end
context 'with a different user' do
let(:current_user) { other_user }
it "returns resources belonging to the user's authorized namespaces" do
expect(resources).to contain_exactly(other_private_namespace_resource)
end
end
context 'when the user is nil' do
let(:current_user) { nil }
it 'does not return any resources' do
expect(resources).to be_empty
end
end
end
end
describe '#state' do
it 'defaults to draft' do
expect(resource_a.state).to eq('draft')
end
end
describe '#publish!' do
context 'when the catalog resource is in draft state' do
it 'updates the state of the catalog resource to published' do
expect(resource_a.state).to eq('draft')
resource_a.publish!
expect(resource_a.reload.state).to eq('published')
end
end
context 'when the catalog resource already has a published state' do
it 'leaves the state as published' do
resource_a.update!(state: :published)
expect(resource_a.state).to eq('published')
resource_a.publish!
expect(resource_a.state).to eq('published')
end
end
end
describe 'synchronizing denormalized columns with `projects` table using SyncEvents processing', :sidekiq_inline do
let_it_be_with_reload(:project) { create(:project, name: 'Test project', description: 'Test description') }
context 'when the catalog resource is created' do
let(:resource) { build(:ci_catalog_resource, project: project) }
it 'updates the catalog resource columns to match the project' do
resource.save!
resource.reload
expect(resource.name).to eq(project.name)
expect(resource.description).to eq(project.description)
expect(resource.visibility_level).to eq(project.visibility_level)
end
end
context 'when the project is updated' do
let_it_be(:resource) { create(:ci_catalog_resource, project: project) }
context 'when project name is updated' do
it 'updates the catalog resource name to match' do
project.update!(name: 'New name')
expect(resource.reload.name).to eq(project.name)
end
end
context 'when project description is updated' do
it 'updates the catalog resource description to match' do
project.update!(description: 'New description')
expect(resource.reload.description).to eq(project.description)
end
end
context 'when project visibility_level is updated' do
it 'updates the catalog resource visibility_level to match' do
project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
expect(resource.reload.visibility_level).to eq(project.visibility_level)
end
end
end
end
describe 'updating latest_released_at using model callbacks' do
let_it_be(:project) { create(:project) }
let_it_be(:resource) { create(:ci_catalog_resource, project: project) }
let_it_be_with_refind(:january_release) do
create(:release, :with_catalog_resource_version, project: project, tag: 'v1', released_at: '2023-01-01T00:00:00Z')
end
let_it_be_with_refind(:february_release) do
create(:release, :with_catalog_resource_version, project: project, tag: 'v2', released_at: '2023-02-01T00:00:00Z')
end
it 'has the expected latest_released_at value' do
expect(resource.reload.latest_released_at).to eq(february_release.released_at)
end
context 'when a new catalog resource version is created' do
it 'updates the latest_released_at value' do
march_release = create(:release, :with_catalog_resource_version, project: project, tag: 'v3',
released_at: '2023-03-01T00:00:00Z')
expect(resource.reload.latest_released_at).to eq(march_release.released_at)
end
end
context 'when a catalog resource version is destroyed' do
it 'updates the latest_released_at value' do
february_release.catalog_resource_version.destroy!
expect(resource.reload.latest_released_at).to eq(january_release.released_at)
end
end
context 'when the released_at value of a release is updated' do
it 'updates the latest_released_at value' do
january_release.update!(released_at: '2024-01-01T00:00:00Z')
expect(resource.reload.latest_released_at).to eq(january_release.released_at)
end
end
context 'when a release is destroyed' do
it 'updates the latest_released_at value' do
february_release.destroy!
expect(resource.reload.latest_released_at).to eq(january_release.released_at)
end
end
context 'when all releases associated with the catalog resource are destroyed' do
it 'updates the latest_released_at value to nil' do
january_release.destroy!
february_release.destroy!
expect(resource.reload.latest_released_at).to be_nil
end
end
end
end
|