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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Explore::ProjectsController, feature_category: :projects do
shared_examples 'explore projects' do
let(:expected_default_sort) { 'latest_activity_desc' }
describe 'GET #index.json' do
render_views
before do
get :index, format: :json
end
it { is_expected.to respond_with(:success) }
it 'sets a default sort parameter' do
expect(controller.params[:sort]).to eq(expected_default_sort)
expect(assigns[:sort]).to eq(expected_default_sort)
end
end
describe 'GET #trending.json' do
render_views
before do
get :trending, format: :json
end
it { is_expected.to respond_with(:success) }
it 'sets a default sort parameter' do
expect(controller.params[:sort]).to eq(expected_default_sort)
expect(assigns[:sort]).to eq(expected_default_sort)
end
end
describe 'GET #starred.json' do
render_views
before do
get :starred, format: :json
end
it { is_expected.to respond_with(:success) }
it 'sets a default sort parameter' do
expect(controller.params[:sort]).to eq(expected_default_sort)
expect(assigns[:sort]).to eq(expected_default_sort)
end
end
describe 'GET #trending' do
context 'sorting by update date' do
let(:project1) { create(:project, :public, updated_at: 3.days.ago) }
let(:project2) { create(:project, :public, updated_at: 1.day.ago) }
before do
create(:trending_project, project: project1)
create(:trending_project, project: project2)
end
it 'sorts by last updated' do
get :trending, params: { sort: 'updated_desc' }
expect(assigns(:projects)).to eq [project2, project1]
end
it 'sorts by oldest updated' do
get :trending, params: { sort: 'updated_asc' }
expect(assigns(:projects)).to eq [project1, project2]
end
end
context 'projects aimed for deletion' do
let(:project1) { create(:project, :public, updated_at: 3.days.ago) }
let(:project2) { create(:project, :public, updated_at: 1.day.ago) }
let(:aimed_for_deletion_project) { create(:project, :public, :archived, updated_at: 2.days.ago, marked_for_deletion_at: 2.days.ago) }
before do
create(:trending_project, project: project1)
create(:trending_project, project: project2)
create(:trending_project, project: aimed_for_deletion_project)
end
it 'does not list projects aimed for deletion' do
get :trending
expect(assigns(:projects)).to eq [project2, project1]
end
end
end
describe 'GET #topic' do
context 'when topic does not exist' do
it 'renders a 404 error' do
get :topic, params: { topic_name: 'topic1' }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when topic exists' do
before do
create(:topic, name: 'topic1')
end
it 'renders the template' do
get :topic, params: { topic_name: 'topic1' }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('topic')
end
it 'finds topic by case insensitive name' do
get :topic, params: { topic_name: 'TOPIC1' }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template('topic')
end
end
end
end
shared_examples "blocks high page numbers" do
let(:page_limit) { described_class::PAGE_LIMIT }
context "page number is too high" do
[:index, :trending, :starred].each do |endpoint|
describe "GET #{endpoint}" do
render_views
before do
get endpoint, params: { page: page_limit + 1 }
end
it { is_expected.to respond_with(:bad_request) }
it { is_expected.to render_template("explore/projects/page_out_of_bounds") }
it "assigns the page number" do
expect(assigns[:max_page_number]).to eq(page_limit.to_s)
end
end
describe "GET #{endpoint}.json" do
render_views
before do
get endpoint, params: { page: page_limit + 1 }, format: :json
end
it { is_expected.to respond_with(:bad_request) }
end
describe "metrics recording" do
subject { get endpoint, params: { page: page_limit + 1 } }
let(:counter) { double("counter", increment: true) }
before do
allow(Gitlab::Metrics).to receive(:counter) { counter }
end
it "records the interception" do
expect(Gitlab::Metrics).to receive(:counter).with(
:gitlab_page_out_of_bounds,
controller: "explore/projects",
action: endpoint.to_s,
bot: false
)
subject
end
end
end
end
context "page number is acceptable" do
[:index, :trending, :starred].each do |endpoint|
describe "GET #{endpoint}" do
render_views
before do
get endpoint, params: { page: page_limit }
end
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template("explore/projects/#{endpoint}") }
end
describe "GET #{endpoint}.json" do
render_views
before do
get endpoint, params: { page: page_limit }, format: :json
end
it { is_expected.to respond_with(:success) }
end
end
end
end
shared_examples 'avoids N+1 queries' do
[:index, :trending, :starred].each do |endpoint|
describe "GET #{endpoint}" do
render_views
# some N+1 queries still exist
it 'avoids N+1 queries', :request_store do
# Because we enable the request store for this spec, Gitaly may report too many invocations.
# Allow N+1s here and when creating additional objects below because we're just creating test objects.
Gitlab::GitalyClient.allow_n_plus_1_calls do
projects = create_list(:project, 3, :repository, :public)
projects.each do |project|
pipeline = create(:ci_pipeline, :success, project: project, sha: project.commit.id)
create(:commit_status, :success, pipeline: pipeline, ref: pipeline.ref)
end
end
control = ActiveRecord::QueryRecorder.new { get endpoint }
Gitlab::GitalyClient.allow_n_plus_1_calls do
new_projects = create_list(:project, 2, :repository, :public)
new_projects.each do |project|
pipeline = create(:ci_pipeline, :success, project: project, sha: project.commit.id)
create(:commit_status, :success, pipeline: pipeline, ref: pipeline.ref)
end
end
expect { get endpoint }.not_to exceed_query_limit(control).with_threshold(8)
end
end
end
end
context 'when user is signed in' do
let(:user) { create(:user) }
let_it_be(:project) { create(:project, name: 'Project 1') }
let_it_be(:project2) { create(:project, name: 'Project 2') }
before do
sign_in(user)
project.add_developer(user)
project2.add_developer(user)
user.toggle_star(project2)
end
include_examples 'explore projects'
include_examples "blocks high page numbers"
include_examples 'avoids N+1 queries'
context 'user preference sorting' do
let(:project) { create(:project) }
it_behaves_like 'set sort order from user preference' do
let(:sorting_param) { 'created_asc' }
end
end
describe 'GET #index' do
let(:controller_action) { :index }
let(:params_with_name) { { name: 'some project' } }
it 'assigns the correct all_user_projects' do
get :index
all_user_projects = assigns(:all_user_projects)
expect(all_user_projects.count).to eq(2)
end
it 'assigns the correct all_starred_projects' do
get :index
all_starred_projects = assigns(:all_starred_projects)
expect(all_starred_projects.count).to eq(1)
expect(all_starred_projects).to include(project2)
end
context 'when disable_anonymous_project_search is enabled' do
before do
stub_feature_flags(disable_anonymous_project_search: true)
end
it 'does not show a flash message' do
sign_in(create(:user))
get controller_action, params: params_with_name
expect(flash.now[:notice]).to be_nil
end
end
end
end
context 'when user is not signed in' do
include_examples 'explore projects'
include_examples "blocks high page numbers"
include_examples 'avoids N+1 queries'
context 'user preference sorting' do
let(:project) { create(:project) }
let(:sorting_param) { 'created_asc' }
it 'does not set sort order from user preference' do
expect_any_instance_of(UserPreference).not_to receive(:update)
get :index, params: { sort: sorting_param }
end
end
context 'restricted visibility level is public' do
before do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
end
it 'redirects to login page' do
get :index
expect(response).to redirect_to new_user_session_path
end
end
describe 'GET #index' do
let(:controller_action) { :index }
let(:params_with_name) { { name: 'some project' } }
context 'when disable_anonymous_project_search is enabled' do
before do
stub_feature_flags(disable_anonymous_project_search: true)
end
it 'shows a flash message' do
get controller_action, params: params_with_name
expect(flash.now[:notice]).to eq('You must sign in to search for specific projects.')
end
context 'when search param is not given' do
it 'does not show a flash message' do
get controller_action
expect(flash.now[:notice]).to be_nil
end
end
context 'when format is not HTML' do
it 'does not show a flash message' do
get controller_action, params: params_with_name.merge(format: :atom)
expect(flash.now[:notice]).to be_nil
end
end
end
context 'when disable_anonymous_project_search is disabled' do
before do
stub_feature_flags(disable_anonymous_project_search: false)
end
it 'does not show a flash message' do
get controller_action, params: params_with_name
expect(flash.now[:notice]).to be_nil
end
end
end
end
end
|