2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Explore::ProjectsController do
|
2019-12-04 20:38:33 +05:30
|
|
|
shared_examples 'explore projects' do
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:expected_default_sort) { 'latest_activity_desc' }
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
describe 'GET #index.json' do
|
|
|
|
render_views
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
before do
|
|
|
|
get :index, format: :json
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to respond_with(:success) }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
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
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
describe 'GET #trending.json' do
|
|
|
|
render_views
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
before do
|
|
|
|
get :trending, format: :json
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it { is_expected.to respond_with(:success) }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
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
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #starred.json' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
before do
|
|
|
|
get :starred, format: :json
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to respond_with(:success) }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
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
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
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
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
shared_examples "blocks high page numbers" do
|
2021-03-08 18:12:59 +05:30
|
|
|
let(:page_limit) { described_class::PAGE_LIMIT }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
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' 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
|
|
|
|
|
|
|
|
control = ActiveRecord::QueryRecorder.new { get endpoint }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
expect { get endpoint }.not_to exceed_query_limit(control).with_threshold(8)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when user is signed in' do
|
|
|
|
let(:user) { create(:user) }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
sign_in(user)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
include_examples 'explore projects'
|
2020-03-13 15:44:24 +05:30
|
|
|
include_examples "blocks high page numbers"
|
2020-10-24 23:57:45 +05:30
|
|
|
include_examples 'avoids N+1 queries'
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'user preference sorting' do
|
|
|
|
let(:project) { create(:project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it_behaves_like 'set sort order from user preference' do
|
|
|
|
let(:sorting_param) { 'created_asc' }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'when user is not signed in' do
|
|
|
|
include_examples 'explore projects'
|
2020-03-13 15:44:24 +05:30
|
|
|
include_examples "blocks high page numbers"
|
2020-10-24 23:57:45 +05:30
|
|
|
include_examples 'avoids N+1 queries'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
context 'user preference sorting' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:sorting_param) { 'created_asc' }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it 'does not set sort order from user preference' do
|
|
|
|
expect_any_instance_of(UserPreference).not_to receive(:update)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
get :index, params: { sort: sorting_param }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|