2016-07-18 04:16:56 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::UserAccess, lib: true do
|
|
|
|
let(:access) { Gitlab::UserAccess.new(user, project: project) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe 'can_push_to_branch?' do
|
|
|
|
describe 'push to none protected branch' do
|
|
|
|
it 'returns true if user is a master' do
|
|
|
|
project.team << [user, :master]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?('random_branch')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
|
|
|
project.team << [user, :developer]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?('random_branch')).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.team << [user, :reporter]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?('random_branch')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-01 05:06:57 -04:00
|
|
|
describe 'push to empty project' do
|
|
|
|
let(:empty_project) { create(:project_empty_repo) }
|
|
|
|
let(:project_access) { Gitlab::UserAccess.new(user, project: empty_project) }
|
|
|
|
|
|
|
|
it 'returns true if user is master' do
|
|
|
|
empty_project.team << [user, :master]
|
|
|
|
|
|
|
|
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
|
|
|
|
empty_project.team << [user, :developer]
|
|
|
|
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
|
|
|
|
empty_project.team << [user, :developer]
|
|
|
|
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
|
|
|
|
empty_project.team << [user, :developer]
|
|
|
|
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
|
|
|
|
empty_project.team << [user, :developer]
|
|
|
|
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-07-18 04:16:56 -04:00
|
|
|
describe 'push to protected branch' do
|
2017-01-05 06:40:54 -05:00
|
|
|
let(:branch) { create :protected_branch, project: project, name: "test" }
|
|
|
|
let(:not_existing_branch) { create :protected_branch, :developers_can_merge, project: project }
|
2016-07-18 04:16:56 -04:00
|
|
|
|
|
|
|
it 'returns true if user is a master' do
|
|
|
|
project.team << [user, :master]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?(branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a developer' do
|
|
|
|
project.team << [user, :developer]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?(branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.team << [user, :reporter]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?(branch.name)).to be_falsey
|
|
|
|
end
|
2017-01-05 06:40:54 -05:00
|
|
|
|
|
|
|
it 'returns true if branch does not exist and user has permission to merge' do
|
|
|
|
project.team << [user, :developer]
|
|
|
|
|
|
|
|
expect(access.can_push_to_branch?(not_existing_branch.name)).to be_truthy
|
|
|
|
end
|
2016-07-18 04:16:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'push to protected branch if allowed for developers' do
|
|
|
|
before do
|
2016-07-08 02:15:02 -04:00
|
|
|
@branch = create :protected_branch, :developers_can_push, project: project
|
2016-07-18 04:16:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a master' do
|
|
|
|
project.team << [user, :master]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
|
|
|
project.team << [user, :developer]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.team << [user, :reporter]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_push_to_branch?(@branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'merge to protected branch if allowed for developers' do
|
|
|
|
before do
|
2016-07-08 02:15:02 -04:00
|
|
|
@branch = create :protected_branch, :developers_can_merge, project: project
|
2016-07-18 04:16:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a master' do
|
|
|
|
project.team << [user, :master]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_merge_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if user is a developer' do
|
|
|
|
project.team << [user, :developer]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_merge_to_branch?(@branch.name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is a reporter' do
|
|
|
|
project.team << [user, :reporter]
|
2016-08-01 11:48:15 -04:00
|
|
|
|
2016-07-18 04:16:56 -04:00
|
|
|
expect(access.can_merge_to_branch?(@branch.name)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|