2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-02 12:35:56 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Projects::ForksController do
|
2016-03-02 12:35:56 -05:00
|
|
|
let(:user) { create(:user) }
|
2017-01-25 16:44:33 -05:00
|
|
|
let(:project) { create(:project, :public, :repository) }
|
2019-08-17 05:14:51 -04:00
|
|
|
let(:forked_project) { Projects::ForkService.new(project, user, name: 'Some name').execute }
|
2018-04-23 11:48:26 -04:00
|
|
|
let(:group) { create(:group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
2016-03-02 12:35:56 -05:00
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
shared_examples 'forking disabled' do
|
|
|
|
let(:project) { create(:project, :private, :repository, :forking_disabled) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns with 404' do
|
|
|
|
subject
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2020-01-17 13:08:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-02 12:35:56 -05:00
|
|
|
describe 'GET index' do
|
2019-08-17 05:14:51 -04:00
|
|
|
def get_forks(search: nil)
|
2016-03-02 12:35:56 -05:00
|
|
|
get :index,
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace,
|
2019-08-17 05:14:51 -04:00
|
|
|
project_id: project,
|
|
|
|
search: search
|
2018-12-17 17:52:17 -05:00
|
|
|
}
|
2016-03-02 12:35:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when fork is public' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
forked_project.update_attribute(:visibility_level, Project::PUBLIC)
|
|
|
|
end
|
2016-03-02 12:35:56 -05:00
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'is visible for non logged in users' do
|
2016-03-02 12:35:56 -05:00
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:forks]).to be_present
|
|
|
|
end
|
2019-08-17 05:14:51 -04:00
|
|
|
|
|
|
|
it 'forks counts are correct' do
|
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:total_forks_count]).to eq(1)
|
|
|
|
expect(assigns[:public_forks_count]).to eq(1)
|
|
|
|
expect(assigns[:internal_forks_count]).to eq(0)
|
|
|
|
expect(assigns[:private_forks_count]).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'after search' do
|
|
|
|
it 'forks counts are correct' do
|
|
|
|
get_forks(search: 'Non-matching query')
|
|
|
|
|
|
|
|
expect(assigns[:total_forks_count]).to eq(1)
|
|
|
|
expect(assigns[:public_forks_count]).to eq(1)
|
|
|
|
expect(assigns[:internal_forks_count]).to eq(0)
|
|
|
|
expect(assigns[:private_forks_count]).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when fork is internal' do
|
|
|
|
before do
|
2021-03-30 08:10:51 -04:00
|
|
|
forked_project.update!(visibility_level: Project::INTERNAL, group: group)
|
2019-08-17 05:14:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'forks counts are correct' do
|
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:total_forks_count]).to eq(1)
|
|
|
|
expect(assigns[:public_forks_count]).to eq(0)
|
|
|
|
expect(assigns[:internal_forks_count]).to eq(1)
|
|
|
|
expect(assigns[:private_forks_count]).to eq(0)
|
|
|
|
end
|
2016-03-02 12:35:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when fork is private' do
|
|
|
|
before do
|
2021-03-30 08:10:51 -04:00
|
|
|
forked_project.update!(visibility_level: Project::PRIVATE, group: group)
|
2016-03-02 12:35:56 -05:00
|
|
|
end
|
|
|
|
|
2019-08-17 05:14:51 -04:00
|
|
|
shared_examples 'forks counts' do
|
|
|
|
it 'forks counts are correct' do
|
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:total_forks_count]).to eq(1)
|
|
|
|
expect(assigns[:public_forks_count]).to eq(0)
|
|
|
|
expect(assigns[:internal_forks_count]).to eq(0)
|
|
|
|
expect(assigns[:private_forks_count]).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not visible for non logged in users' do
|
2016-03-02 12:35:56 -05:00
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:forks]).to be_blank
|
|
|
|
end
|
|
|
|
|
2019-08-17 05:14:51 -04:00
|
|
|
include_examples 'forks counts'
|
|
|
|
|
2016-03-02 12:35:56 -05:00
|
|
|
context 'when user is logged in' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
sign_in(project.creator)
|
|
|
|
end
|
2016-03-02 12:35:56 -05:00
|
|
|
|
|
|
|
context 'when user is not a Project member neither a group member' do
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'does not see the Project listed' do
|
2016-03-02 12:35:56 -05:00
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:forks]).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a member of the Project' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-12-22 03:18:28 -05:00
|
|
|
forked_project.add_developer(project.creator)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-03-02 12:35:56 -05:00
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'sees the project listed' do
|
2016-03-02 12:35:56 -05:00
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:forks]).to be_present
|
|
|
|
end
|
2019-08-17 05:14:51 -04:00
|
|
|
|
|
|
|
include_examples 'forks counts'
|
2016-03-02 12:35:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a member of the Group' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
forked_project.group.add_developer(project.creator)
|
|
|
|
end
|
2016-03-02 12:35:56 -05:00
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it 'sees the project listed' do
|
2016-03-02 12:35:56 -05:00
|
|
|
get_forks
|
|
|
|
|
|
|
|
expect(assigns[:forks]).to be_present
|
|
|
|
end
|
2019-08-17 05:14:51 -04:00
|
|
|
|
|
|
|
include_examples 'forks counts'
|
2016-03-02 12:35:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-12 18:59:43 -05:00
|
|
|
|
|
|
|
describe 'GET new' do
|
2021-03-23 14:09:05 -04:00
|
|
|
let(:format) { :html }
|
|
|
|
|
|
|
|
subject(:do_request) do
|
2016-11-14 17:59:11 -05:00
|
|
|
get :new,
|
2021-03-23 14:09:05 -04:00
|
|
|
format: format,
|
2020-01-17 13:08:41 -05:00
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project
|
|
|
|
}
|
2016-11-14 17:59:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is signed in' do
|
2020-07-17 11:09:13 -04:00
|
|
|
before do
|
2016-11-14 17:59:11 -05:00
|
|
|
sign_in(user)
|
2020-07-17 11:09:13 -04:00
|
|
|
end
|
2016-11-14 17:59:11 -05:00
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
it 'responds with status 200' do
|
|
|
|
request
|
2020-07-17 11:09:13 -04:00
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-07-17 11:09:13 -04:00
|
|
|
end
|
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
context 'when JSON is requested' do
|
|
|
|
let(:format) { :json }
|
2016-11-14 17:59:11 -05:00
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
it 'responds with user namespace + groups' do
|
|
|
|
do_request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['namespaces'].length).to eq(2)
|
|
|
|
expect(json_response['namespaces'][0]['id']).to eq(user.namespace.id)
|
|
|
|
expect(json_response['namespaces'][1]['id']).to eq(group.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with group only when fork_project_form feature flag is disabled' do
|
|
|
|
stub_feature_flags(fork_project_form: false)
|
|
|
|
do_request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response['namespaces'].length).to eq(1)
|
|
|
|
expect(json_response['namespaces'][0]['id']).to eq(group.id)
|
|
|
|
end
|
2021-05-31 02:10:40 -04:00
|
|
|
|
|
|
|
context 'N+1 queries' do
|
|
|
|
before do
|
|
|
|
create(:fork_network, root_project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N+1 queries' do
|
|
|
|
do_request = -> { get :new, format: format, params: { namespace_id: project.namespace, project_id: project } }
|
|
|
|
|
|
|
|
# warm up
|
|
|
|
do_request.call
|
|
|
|
|
|
|
|
control = ActiveRecord::QueryRecorder.new { do_request.call }
|
|
|
|
|
|
|
|
create(:group, :public).add_owner(user)
|
|
|
|
|
2021-06-08 11:10:00 -04:00
|
|
|
expect { do_request.call }.not_to exceed_query_limit(control)
|
2021-05-31 02:10:40 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-14 17:59:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not signed in' do
|
|
|
|
it 'redirects to the sign-in page' do
|
|
|
|
sign_out(user)
|
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
subject
|
2016-11-14 17:59:11 -05:00
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
end
|
2020-01-17 13:08:41 -05:00
|
|
|
|
|
|
|
it_behaves_like 'forking disabled'
|
2016-11-14 17:59:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST create' do
|
2020-01-17 13:08:41 -05:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
namespace_key: user.namespace.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-27 10:09:15 -05:00
|
|
|
let(:created_project) do
|
|
|
|
Namespace
|
|
|
|
.find_by_id(params[:namespace_key])
|
|
|
|
.projects
|
|
|
|
.find_by_path(params.fetch(:path, project.path))
|
|
|
|
end
|
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
subject do
|
|
|
|
post :create, params: params
|
2016-11-14 17:59:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is signed in' do
|
2019-06-20 13:45:01 -04:00
|
|
|
before do
|
2016-11-14 17:59:11 -05:00
|
|
|
sign_in(user)
|
2019-06-20 13:45:01 -04:00
|
|
|
end
|
2016-11-14 17:59:11 -05:00
|
|
|
|
2019-06-20 13:45:01 -04:00
|
|
|
it 'responds with status 302' do
|
2020-01-17 13:08:41 -05:00
|
|
|
subject
|
2016-11-14 17:59:11 -05:00
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2016-11-15 13:23:20 -05:00
|
|
|
expect(response).to redirect_to(namespace_project_import_path(user.namespace, project))
|
2016-11-14 17:59:11 -05:00
|
|
|
end
|
2019-06-20 13:45:01 -04:00
|
|
|
|
2020-02-20 04:09:13 -05:00
|
|
|
context 'when target namespace is not valid for forking' do
|
|
|
|
let(:params) { super().merge(namespace_key: another_group.id) }
|
|
|
|
let(:another_group) { create :group }
|
|
|
|
|
|
|
|
it 'responds with :not_found' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
context 'continue params' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
namespace_key: user.namespace.id,
|
|
|
|
continue: continue_params
|
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
let(:continue_params) do
|
|
|
|
{
|
|
|
|
to: '/-/ide/project/path',
|
|
|
|
notice: 'message'
|
|
|
|
}
|
|
|
|
end
|
2019-06-20 13:45:01 -04:00
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
it 'passes continue params to the redirect' do
|
|
|
|
subject
|
|
|
|
|
2020-01-28 07:08:44 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2020-01-17 13:08:41 -05:00
|
|
|
expect(response).to redirect_to(namespace_project_import_path(user.namespace, project, continue: continue_params))
|
|
|
|
end
|
2019-06-20 13:45:01 -04:00
|
|
|
end
|
2021-01-27 10:09:15 -05:00
|
|
|
|
|
|
|
context 'custom attributes set' do
|
|
|
|
let(:params) { super().merge(path: 'something_custom', name: 'Something Custom', description: 'Something Custom', visibility: 'private') }
|
|
|
|
|
|
|
|
it 'creates a project with custom values' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
|
|
|
expect(response).to redirect_to(namespace_project_import_path(user.namespace, params[:path]))
|
|
|
|
expect(created_project.path).to eq(params[:path])
|
|
|
|
expect(created_project.name).to eq(params[:name])
|
|
|
|
expect(created_project.description).to eq(params[:description])
|
|
|
|
expect(created_project.visibility).to eq(params[:visibility])
|
|
|
|
end
|
|
|
|
end
|
2016-11-14 17:59:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not signed in' do
|
2016-11-12 18:59:43 -05:00
|
|
|
it 'redirects to the sign-in page' do
|
2016-11-14 17:59:11 -05:00
|
|
|
sign_out(user)
|
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
subject
|
2016-11-12 18:59:43 -05:00
|
|
|
|
2016-11-14 17:59:11 -05:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
2016-11-12 18:59:43 -05:00
|
|
|
end
|
|
|
|
end
|
2020-01-17 13:08:41 -05:00
|
|
|
|
|
|
|
it_behaves_like 'forking disabled'
|
2016-11-12 18:59:43 -05:00
|
|
|
end
|
2016-03-02 12:35:56 -05:00
|
|
|
end
|