debian-mirror-gitlab/spec/controllers/projects/forks_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

328 lines
8.5 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Projects::ForksController, feature_category: :source_code_management do
2016-06-02 11:05:42 +05:30
let(:user) { create(:user) }
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :public, :repository) }
2019-12-04 20:38:33 +05:30
let(:forked_project) { Projects::ForkService.new(project, user, name: 'Some name').execute }
2018-10-15 14:42:47 +05:30
let(:group) { create(:group) }
before do
group.add_owner(user)
end
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
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
expect(response).to have_gitlab_http_status(:not_found)
end
end
2016-06-02 11:05:42 +05:30
describe 'GET index' do
2019-12-04 20:38:33 +05:30
def get_forks(search: nil)
2016-06-02 11:05:42 +05:30
get :index,
2019-02-15 15:39:39 +05:30
params: {
namespace_id: project.namespace,
2019-12-04 20:38:33 +05:30
project_id: project,
search: search
2019-02-15 15:39:39 +05:30
}
2016-06-02 11:05:42 +05:30
end
context 'when fork is public' do
2017-09-10 17:25:29 +05:30
before do
forked_project.update_attribute(:visibility_level, Project::PUBLIC)
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
it 'is visible for non logged in users' do
2016-06-02 11:05:42 +05:30
get_forks
expect(assigns[:forks]).to be_present
end
2019-12-04 20:38:33 +05:30
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
2022-04-04 11:22:00 +05:30
context 'when unsupported keys are provided' do
it 'ignores them' do
get :index, params: {
namespace_id: project.namespace,
project_id: project,
user: 'unsupported'
}
expect(assigns[:forks]).to be_present
end
end
2019-12-04 20:38:33 +05:30
end
context 'when fork is internal' do
before do
2021-04-29 21:17:54 +05:30
forked_project.update!(visibility_level: Project::INTERNAL, group: group)
2019-12-04 20:38:33 +05:30
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-06-02 11:05:42 +05:30
end
context 'when fork is private' do
before do
2021-04-29 21:17:54 +05:30
forked_project.update!(visibility_level: Project::PRIVATE, group: group)
2016-06-02 11:05:42 +05:30
end
2019-12-04 20:38:33 +05:30
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-06-02 11:05:42 +05:30
get_forks
expect(assigns[:forks]).to be_blank
end
2019-12-04 20:38:33 +05:30
include_examples 'forks counts'
2016-06-02 11:05:42 +05:30
context 'when user is logged in' do
2017-09-10 17:25:29 +05:30
before do
sign_in(project.creator)
end
2016-06-02 11:05:42 +05:30
context 'when user is not a Project member neither a group member' do
2016-09-13 17:45:13 +05:30
it 'does not see the Project listed' do
2016-06-02 11:05:42 +05:30
get_forks
expect(assigns[:forks]).to be_blank
end
end
context 'when user is a member of the Project' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
forked_project.add_developer(project.creator)
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
it 'sees the project listed' do
2016-06-02 11:05:42 +05:30
get_forks
expect(assigns[:forks]).to be_present
end
2019-12-04 20:38:33 +05:30
include_examples 'forks counts'
2016-06-02 11:05:42 +05:30
end
context 'when user is a member of the Group' do
2017-09-10 17:25:29 +05:30
before do
forked_project.group.add_developer(project.creator)
end
2016-06-02 11:05:42 +05:30
2016-09-13 17:45:13 +05:30
it 'sees the project listed' do
2016-06-02 11:05:42 +05:30
get_forks
expect(assigns[:forks]).to be_present
end
2019-12-04 20:38:33 +05:30
include_examples 'forks counts'
2016-06-02 11:05:42 +05:30
end
end
end
end
2017-08-17 22:00:37 +05:30
describe 'GET new' do
2021-04-29 21:17:54 +05:30
let(:format) { :html }
subject(:do_request) do
2023-05-27 22:25:52 +05:30
get :new, format: format, params: { namespace_id: project.namespace, project_id: project }
2017-08-17 22:00:37 +05:30
end
context 'when user is signed in' do
2020-10-24 23:57:45 +05:30
before do
2017-08-17 22:00:37 +05:30
sign_in(user)
2020-10-24 23:57:45 +05:30
end
2021-04-29 21:17:54 +05:30
it 'responds with status 200' do
request
2020-10-24 23:57:45 +05:30
2021-04-29 21:17:54 +05:30
expect(response).to have_gitlab_http_status(:ok)
2020-10-24 23:57:45 +05:30
end
2017-08-17 22:00:37 +05:30
2021-04-29 21:17:54 +05:30
context 'when JSON is requested' do
let(:format) { :json }
2017-08-17 22:00:37 +05:30
2021-04-29 21:17:54 +05:30
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
2021-09-04 01:27:46 +05:30
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)
expect { do_request.call }.not_to exceed_query_limit(control)
end
end
2017-08-17 22:00:37 +05:30
end
end
context 'when user is not signed in' do
it 'redirects to the sign-in page' do
sign_out(user)
2020-03-13 15:44:24 +05:30
subject
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
2020-03-13 15:44:24 +05:30
it_behaves_like 'forking disabled'
2017-08-17 22:00:37 +05:30
end
describe 'POST create' do
2020-03-13 15:44:24 +05:30
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
namespace_key: user.namespace.id
}
end
2021-03-11 19:13:27 +05:30
let(:created_project) do
Namespace
.find_by_id(params[:namespace_key])
.projects
.find_by_path(params.fetch(:path, project.path))
end
2020-03-13 15:44:24 +05:30
subject do
post :create, params: params
2017-08-17 22:00:37 +05:30
end
context 'when user is signed in' do
2019-09-30 21:07:59 +05:30
before do
2017-08-17 22:00:37 +05:30
sign_in(user)
2019-09-30 21:07:59 +05:30
end
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
it 'responds with status 302' do
2020-03-13 15:44:24 +05:30
subject
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(namespace_project_import_path(user.namespace, project))
end
2019-09-30 21:07:59 +05:30
2020-04-08 14:13:33 +05:30
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-03-13 15:44:24 +05:30
context 'continue params' do
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
namespace_key: user.namespace.id,
continue: continue_params
}
end
2020-10-24 23:57:45 +05:30
2020-03-13 15:44:24 +05:30
let(:continue_params) do
{
to: '/-/ide/project/path',
notice: 'message'
}
end
it 'passes continue params to the redirect' do
subject
2019-09-30 21:07:59 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:found)
expect(response).to redirect_to(namespace_project_import_path(user.namespace, project, continue: continue_params))
end
2019-09-30 21:07:59 +05:30
end
2021-03-11 19:13:27 +05:30
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
2017-08-17 22:00:37 +05:30
end
context 'when user is not signed in' do
it 'redirects to the sign-in page' do
sign_out(user)
2020-03-13 15:44:24 +05:30
subject
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
2020-03-13 15:44:24 +05:30
it_behaves_like 'forking disabled'
2017-08-17 22:00:37 +05:30
end
2016-06-02 11:05:42 +05:30
end