debian-mirror-gitlab/spec/controllers/projects/settings/repository_controller_spec.rb

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

225 lines
7.7 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe Projects::Settings::RepositoryController, feature_category: :source_code_management do
2017-08-17 22:00:37 +05:30
let(:project) { create(:project_empty_repo, :public) }
let(:user) { create(:user) }
2023-01-13 00:05:48 +05:30
let(:base_params) { { namespace_id: project.namespace, project_id: project } }
2017-08-17 22:00:37 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-08-17 22:00:37 +05:30
sign_in(user)
end
describe 'GET show' do
it 'renders show with 200 status code' do
2023-01-13 00:05:48 +05:30
get :show, params: base_params
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to render_template(:show)
end
2023-03-04 22:38:38 +05:30
context 'when feature flag `group_protected_branches` disabled' do
before do
stub_feature_flags(group_protected_branches: false)
end
it 'does not assign instance variable `protected_group_branches`' do
get :show, params: base_params
expect(assigns).not_to include(:protected_group_branches)
end
end
context 'when feature flag `group_protected_branches` enabled' do
context 'when the root namespace is a user' do
it 'assigns empty instance variable `protected_group_branches`' do
get :show, params: base_params
expect(assigns[:protected_group_branches]).to eq([])
end
end
context 'when the root namespace is a group' do
let_it_be(:project) { create(:project_empty_repo, :public, :in_group) }
let(:protected_group_branch) { create(:protected_branch, group: project.root_namespace, project: nil) }
it 'assigns instance variable `protected_group_branches`' do
get :show, params: base_params
expect(assigns[:protected_group_branches]).to include(protected_group_branch)
end
end
end
2017-08-17 22:00:37 +05:30
end
2019-02-15 15:39:39 +05:30
describe 'PUT cleanup' do
let(:object_map) { fixture_file_upload('spec/fixtures/bfg_object_map.txt') }
2021-01-29 00:20:46 +05:30
it 'enqueues a project cleanup' do
expect(Projects::CleanupService)
.to receive(:enqueue)
.with(project, user, anything)
.and_return(status: :success)
2019-02-15 15:39:39 +05:30
2023-01-13 00:05:48 +05:30
put :cleanup, params: base_params.merge({ project: { bfg_object_map: object_map } })
2019-02-15 15:39:39 +05:30
expect(response).to redirect_to project_settings_repository_path(project)
end
2022-04-04 11:22:00 +05:30
context 'when project cleanup returns an error', :aggregate_failures do
it 'shows an error' do
expect(Projects::CleanupService)
.to receive(:enqueue)
.with(project, user, anything)
.and_return(status: :error, message: 'error message')
2023-01-13 00:05:48 +05:30
put :cleanup, params: base_params.merge({ project: { bfg_object_map: object_map } })
2022-04-04 11:22:00 +05:30
expect(controller).to set_flash[:alert].to('error message')
expect(response).to redirect_to project_settings_repository_path(project)
end
end
2019-02-15 15:39:39 +05:30
end
2020-04-25 10:58:03 +05:30
describe 'POST create_deploy_token' do
2023-01-13 00:05:48 +05:30
let(:good_deploy_token_params) do
{
name: 'name',
2023-03-04 22:38:38 +05:30
expires_at: 1.day.from_now.to_datetime.to_s,
2023-01-13 00:05:48 +05:30
username: 'deployer',
read_repository: '1',
deploy_token_type: DeployToken.deploy_token_types[:project_type]
}
end
let(:request_params) { base_params.merge({ deploy_token: deploy_token_params }) }
subject { post :create_deploy_token, params: request_params, format: :json }
context('a good request') do
let(:deploy_token_params) { good_deploy_token_params }
let(:expected_response) do
{
'id' => be_a(Integer),
'name' => deploy_token_params[:name],
'username' => deploy_token_params[:username],
'expires_at' => Time.zone.parse(deploy_token_params[:expires_at]),
'token' => be_a(String),
'expired' => false,
'revoked' => false,
'scopes' => deploy_token_params.inject([]) do |scopes, kv|
key, value = kv
key.to_s.start_with?('read_') && value.to_i != 0 ? scopes << key.to_s : scopes
end
}
2020-04-25 10:58:03 +05:30
end
2023-01-13 00:05:48 +05:30
it 'creates the deploy token' do
subject
expect(response).to have_gitlab_http_status(:created)
expect(response).to match_response_schema('public_api/v4/deploy_token')
expect(json_response).to match(expected_response)
2020-04-25 10:58:03 +05:30
end
end
2023-01-13 00:05:48 +05:30
context('a bad request') do
let(:deploy_token_params) { good_deploy_token_params.except(:read_repository) }
let(:expected_response) { { 'message' => "Scopes can't be blank" } }
it 'does not create the deploy token' do
subject
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response).to match(expected_response)
2020-04-25 10:58:03 +05:30
end
2023-01-13 00:05:48 +05:30
end
2020-10-24 23:57:45 +05:30
2023-01-13 00:05:48 +05:30
context('an invalid request') do
let(:deploy_token_params) { good_deploy_token_params.except(:name) }
it 'raises a validation error' do
expect { subject }.to raise_error(ActiveRecord::StatementInvalid)
2020-04-25 10:58:03 +05:30
end
2023-01-13 00:05:48 +05:30
end
end
2020-04-25 10:58:03 +05:30
2023-01-13 00:05:48 +05:30
describe 'PUT update' do
let(:project) { create(:project, :repository) }
context 'when updating default branch' do
let!(:previous_default_branch) { project.default_branch }
let(:new_default_branch) { 'feature' }
let(:request_params) { base_params.merge({ project: project_params_attributes }) }
subject { put :update, params: request_params }
context('with a good request') do
let(:project_params_attributes) { { default_branch: new_default_branch } }
it "updates default branch and redirect to project_settings_repository_path" do
expect do
subject
end.to change {
Project.find(project.id).default_branch # refind to reset the default branch cache
}.from(previous_default_branch).to(new_default_branch)
expect(response).to redirect_to project_settings_repository_path(project)
expect(controller).to set_flash[:notice].to("Project settings were successfully updated.")
2020-04-25 10:58:03 +05:30
end
2023-01-13 00:05:48 +05:30
end
2020-04-25 10:58:03 +05:30
2023-01-13 00:05:48 +05:30
context('with a bad input') do
let(:project_params_attributes) { { default_branch: 'non_existent_branch' } }
2020-04-25 10:58:03 +05:30
2023-01-13 00:05:48 +05:30
it "does not update default branch and shows an alert" do
expect do
subject
end.not_to change {
Project.find(project.id).default_branch # refind to reset the default branch cache
}
expect(response).to redirect_to project_settings_repository_path(project)
expect(controller).to set_flash[:alert].to("Could not set the default branch")
2020-04-25 10:58:03 +05:30
end
end
2023-01-13 00:05:48 +05:30
end
context 'when updating branch names template from issues' do
let(:branch_name_template) { 'feat/GL-%{id}-%{title}' }
2020-04-25 10:58:03 +05:30
2023-01-13 00:05:48 +05:30
let(:request_params) { base_params.merge({ project: project_params_attributes }) }
2020-04-25 10:58:03 +05:30
2023-01-13 00:05:48 +05:30
subject { put :update, params: request_params }
context('with a good request') do
let(:project_params_attributes) { { issue_branch_template: branch_name_template } }
it "updates issue_branch_template and redirect to project_settings_repository_path" do
2020-04-25 10:58:03 +05:30
subject
2023-01-13 00:05:48 +05:30
expect(response).to redirect_to project_settings_repository_path(project)
expect(controller).to set_flash[:notice].to("Project settings were successfully updated.")
expect(project.reload.issue_branch_template).to eq(branch_name_template)
2020-04-25 10:58:03 +05:30
end
end
2023-01-13 00:05:48 +05:30
context('with a bad input') do
let(:project_params_attributes) { { issue_branch_template: 'a' * 260 } }
it "updates issue_branch_template and redirect to project_settings_repository_path" do
subject
2020-04-25 10:58:03 +05:30
2023-01-13 00:05:48 +05:30
expect(response).to redirect_to project_settings_repository_path(project)
expect(controller).to set_flash[:alert].to("Project setting issue branch template is too long (maximum is 255 characters)")
expect(project.reload.issue_branch_template).to eq(nil)
2020-04-25 10:58:03 +05:30
end
end
end
end
2017-08-17 22:00:37 +05:30
end