2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Gitlab::UserAccess do
|
2018-03-27 19:54:05 +05:30
|
|
|
include ProjectForksHelper
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:access) { described_class.new(user, project: project) }
|
|
|
|
let(:project) { create(:project, :repository) }
|
2016-08-24 12:49:21 +05:30
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#can_push_to_branch?' do
|
2016-08-24 12:49:21 +05:30
|
|
|
describe 'push to none protected branch' do
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_maintainer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?('random_branch')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?('random_branch')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_reporter(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?('random_branch')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
describe 'push to empty project' do
|
|
|
|
let(:empty_project) { create(:project_empty_repo) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project_access) { described_class.new(user, project: empty_project) }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
it 'returns true for admins' do
|
|
|
|
user.update!(admin: true)
|
|
|
|
|
|
|
|
expect(access.can_push_to_branch?('master')).to be_truthy
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is maintainer' do
|
|
|
|
empty_project.add_maintainer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
expect(project_access.can_push_to_branch?('master')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is developer and project is fully protected' do
|
2018-03-17 18:26:18 +05:30
|
|
|
empty_project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_FULL)
|
|
|
|
|
|
|
|
expect(project_access.can_push_to_branch?('master')).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is developer and it is not allowed to push new commits but can merge into branch' do
|
2018-03-17 18:26:18 +05:30
|
|
|
empty_project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_DEV_CAN_MERGE)
|
|
|
|
|
|
|
|
expect(project_access.can_push_to_branch?('master')).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is developer and project is unprotected' do
|
2018-03-17 18:26:18 +05:30
|
|
|
empty_project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_NONE)
|
|
|
|
|
|
|
|
expect(project_access.can_push_to_branch?('master')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is developer and project grants developers permission' do
|
2018-03-17 18:26:18 +05:30
|
|
|
empty_project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_DEV_CAN_PUSH)
|
|
|
|
|
|
|
|
expect(project_access.can_push_to_branch?('master')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
describe 'push to protected branch' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:branch) { create :protected_branch, project: project, name: "test" }
|
|
|
|
let(:not_existing_branch) { create :protected_branch, :developers_can_merge, project: project }
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
it 'returns true for admins' do
|
|
|
|
user.update!(admin: true)
|
|
|
|
|
|
|
|
expect(access.can_push_to_branch?(branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_maintainer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?(branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a developer' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?(branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_reporter(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?(branch.name)).to be_falsey
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns false if branch does not exist' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(access.can_push_to_branch?(not_existing_branch.name)).to be_falsey
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'push to protected branch if allowed for developers' do
|
|
|
|
before do
|
2016-09-13 17:45:13 +05:30
|
|
|
@branch = create :protected_branch, :developers_can_push, project: project
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_maintainer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_reporter(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_push_to_branch?(@branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
describe 'allowing pushes to maintainers of forked projects' do
|
|
|
|
let(:canonical_project) { create(:project, :public, :repository) }
|
|
|
|
let(:project) { fork_project(canonical_project, create(:user), repository: true) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(
|
|
|
|
:merge_request,
|
|
|
|
target_project: canonical_project,
|
|
|
|
source_project: project,
|
|
|
|
source_branch: 'awesome-feature',
|
2018-11-08 19:23:39 +05:30
|
|
|
allow_collaboration: true
|
2018-03-27 19:54:05 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows users that have push access to the canonical project to push to the MR branch' do
|
|
|
|
canonical_project.add_developer(user)
|
|
|
|
|
|
|
|
expect(access.can_push_to_branch?('awesome-feature')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow the user to push to other branches' do
|
|
|
|
canonical_project.add_developer(user)
|
|
|
|
|
|
|
|
expect(access.can_push_to_branch?('master')).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow the user to push if he does not have push access to the canonical project' do
|
|
|
|
canonical_project.add_guest(user)
|
|
|
|
|
|
|
|
expect(access.can_push_to_branch?('awesome-feature')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
describe 'merge to protected branch if allowed for developers' do
|
|
|
|
before do
|
2016-09-13 17:45:13 +05:30
|
|
|
@branch = create :protected_branch, :developers_can_merge, project: project
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_maintainer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_merge_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_developer(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_merge_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
2018-03-17 18:26:18 +05:30
|
|
|
project.add_reporter(user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(access.can_merge_to_branch?(@branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#can_create_tag?' do
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'push to none protected tag' do
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_user(user, :maintainer)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(access.can_create_tag?('random_tag')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
|
|
|
project.add_user(user, :developer)
|
|
|
|
|
|
|
|
expect(access.can_create_tag?('random_tag')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.add_user(user, :reporter)
|
|
|
|
|
|
|
|
expect(access.can_create_tag?('random_tag')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'push to protected tag' do
|
|
|
|
let(:tag) { create(:protected_tag, project: project, name: "test") }
|
|
|
|
let(:not_existing_tag) { create :protected_tag, project: project }
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_user(user, :maintainer)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(access.can_create_tag?(tag.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a developer' do
|
|
|
|
project.add_user(user, :developer)
|
|
|
|
|
|
|
|
expect(access.can_create_tag?(tag.name)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.add_user(user, :reporter)
|
|
|
|
|
|
|
|
expect(access.can_create_tag?(tag.name)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'push to protected tag if allowed for developers' do
|
|
|
|
before do
|
|
|
|
@tag = create(:protected_tag, :developers_can_create, project: project)
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_user(user, :maintainer)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(access.can_create_tag?(@tag.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
|
|
|
project.add_user(user, :developer)
|
|
|
|
|
|
|
|
expect(access.can_create_tag?(@tag.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.add_user(user, :reporter)
|
|
|
|
|
|
|
|
expect(access.can_create_tag?(@tag.name)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
describe '#can_delete_branch?' do
|
|
|
|
describe 'delete unprotected branch' do
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_user(user, :maintainer)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(access.can_delete_branch?('random_branch')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
|
|
|
project.add_user(user, :developer)
|
|
|
|
|
|
|
|
expect(access.can_delete_branch?('random_branch')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.add_user(user, :reporter)
|
|
|
|
|
|
|
|
expect(access.can_delete_branch?('random_branch')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'delete protected branch' do
|
|
|
|
let(:branch) { create(:protected_branch, project: project, name: "test") }
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'returns true if user is a maintainer' do
|
|
|
|
project.add_user(user, :maintainer)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(access.can_delete_branch?(branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a developer' do
|
|
|
|
project.add_user(user, :developer)
|
|
|
|
|
|
|
|
expect(access.can_delete_branch?(branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.add_user(user, :reporter)
|
|
|
|
|
|
|
|
expect(access.can_delete_branch?(branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|