2019-10-24 20:06:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-08 03:34:20 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe MergeRequestPolicy do
|
2019-04-09 11:38:58 -04:00
|
|
|
include ExternalAuthorizationServiceHelpers
|
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
let_it_be(:guest) { create(:user) }
|
|
|
|
let_it_be(:author) { create(:user) }
|
|
|
|
let_it_be(:developer) { create(:user) }
|
|
|
|
let_it_be(:non_team_member) { create(:user) }
|
|
|
|
|
2019-03-08 03:34:20 -05:00
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
|
|
|
|
def permissions(user, merge_request)
|
|
|
|
described_class.new(user, merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_guest(guest)
|
|
|
|
project.add_guest(author)
|
|
|
|
project.add_developer(developer)
|
|
|
|
end
|
|
|
|
|
2020-05-19 11:08:04 -04:00
|
|
|
mr_perms = %i[create_merge_request_in
|
2019-06-04 23:30:54 -04:00
|
|
|
create_merge_request_from
|
|
|
|
read_merge_request
|
2020-10-30 14:08:56 -04:00
|
|
|
create_todo
|
2020-07-10 14:09:45 -04:00
|
|
|
approve_merge_request
|
2021-05-21 08:10:27 -04:00
|
|
|
create_note
|
|
|
|
update_subscription].freeze
|
2019-06-04 23:30:54 -04:00
|
|
|
|
|
|
|
shared_examples_for 'a denied user' do
|
|
|
|
let(:perms) { permissions(subject, merge_request) }
|
|
|
|
|
2020-05-19 11:08:04 -04:00
|
|
|
mr_perms.each do |thing|
|
2019-06-04 23:30:54 -04:00
|
|
|
it "cannot #{thing}" do
|
|
|
|
expect(perms).to be_disallowed(thing)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'a user with access' do
|
|
|
|
let(:perms) { permissions(subject, merge_request) }
|
|
|
|
|
2020-05-19 11:08:04 -04:00
|
|
|
mr_perms.each do |thing|
|
2019-06-04 23:30:54 -04:00
|
|
|
it "can #{thing}" do
|
|
|
|
expect(perms).to be_allowed(thing)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-30 14:08:56 -04:00
|
|
|
context 'when merge request is public' do
|
2021-12-06 22:12:22 -05:00
|
|
|
let(:merge_request) { create(:merge_request, source_project: project, target_project: project, author: author) }
|
2020-10-30 14:08:56 -04:00
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
context 'and user is anonymous' do
|
2020-10-30 14:08:56 -04:00
|
|
|
subject { permissions(nil, merge_request) }
|
|
|
|
|
|
|
|
it do
|
2021-05-21 08:10:27 -04:00
|
|
|
is_expected.to be_disallowed(:create_todo, :update_subscription)
|
2020-10-30 14:08:56 -04:00
|
|
|
end
|
|
|
|
end
|
2021-12-06 22:12:22 -05:00
|
|
|
|
|
|
|
describe 'the author, who became a guest' do
|
|
|
|
subject { permissions(author, merge_request) }
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to be_allowed(:update_merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to be_allowed(:reopen_merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to be_allowed(:approve_merge_request)
|
|
|
|
end
|
|
|
|
end
|
2020-10-30 14:08:56 -04:00
|
|
|
end
|
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
context 'when merge requests have been disabled' do
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, target_project: project, author: author) }
|
|
|
|
|
|
|
|
before do
|
2020-07-29 08:09:45 -04:00
|
|
|
project.project_feature.update!(merge_requests_access_level: ProjectFeature::DISABLED)
|
2019-06-04 23:30:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'the author' do
|
|
|
|
subject { author }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
it_behaves_like 'a denied user'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'a guest' do
|
|
|
|
subject { guest }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
it_behaves_like 'a denied user'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'a developer' do
|
|
|
|
subject { developer }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
it_behaves_like 'a denied user'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'any other user' do
|
|
|
|
subject { non_team_member }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
it_behaves_like 'a denied user'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when merge requests are private' do
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, target_project: project, author: author) }
|
|
|
|
|
|
|
|
before do
|
2020-07-29 08:09:45 -04:00
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
project.project_feature.update!(merge_requests_access_level: ProjectFeature::PRIVATE)
|
2019-06-04 23:30:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'a non-team-member' do
|
|
|
|
subject { non_team_member }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
it_behaves_like 'a denied user'
|
|
|
|
end
|
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
describe 'the author' do
|
|
|
|
subject { author }
|
|
|
|
|
|
|
|
it_behaves_like 'a denied user'
|
|
|
|
end
|
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
describe 'a developer' do
|
|
|
|
subject { developer }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-06-04 23:30:54 -04:00
|
|
|
it_behaves_like 'a user with access'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 03:34:20 -05:00
|
|
|
context 'when merge request is unlocked' do
|
|
|
|
let(:merge_request) { create(:merge_request, :closed, source_project: project, target_project: project, author: author) }
|
|
|
|
|
|
|
|
it 'allows author to reopen merge request' do
|
|
|
|
expect(permissions(author, merge_request)).to be_allowed(:reopen_merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows developer to reopen merge request' do
|
|
|
|
expect(permissions(developer, merge_request)).to be_allowed(:reopen_merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents guest from reopening merge request' do
|
|
|
|
expect(permissions(guest, merge_request)).to be_disallowed(:reopen_merge_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when merge request is locked' do
|
|
|
|
let(:merge_request_locked) { create(:merge_request, :closed, discussion_locked: true, source_project: project, target_project: project, author: author) }
|
|
|
|
|
|
|
|
it 'prevents author from reopening merge request' do
|
|
|
|
expect(permissions(author, merge_request_locked)).to be_disallowed(:reopen_merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents developer from reopening merge request' do
|
|
|
|
expect(permissions(developer, merge_request_locked)).to be_disallowed(:reopen_merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents guests from reopening merge request' do
|
|
|
|
expect(permissions(guest, merge_request_locked)).to be_disallowed(:reopen_merge_request)
|
|
|
|
end
|
2019-06-04 23:30:54 -04:00
|
|
|
|
|
|
|
context 'when the user is not a project member' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'cannot create a note' do
|
|
|
|
expect(permissions(user, merge_request_locked)).to be_disallowed(:create_note)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user is project member, with at least guest access' do
|
|
|
|
let(:user) { guest }
|
|
|
|
|
|
|
|
it 'can create a note' do
|
|
|
|
expect(permissions(user, merge_request_locked)).to be_allowed(:create_note)
|
|
|
|
end
|
|
|
|
end
|
2019-03-08 03:34:20 -05:00
|
|
|
end
|
2019-04-09 11:38:58 -04:00
|
|
|
|
|
|
|
context 'with external authorization enabled' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project) }
|
|
|
|
let(:policies) { described_class.new(user, merge_request) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
enable_external_authorization_service_check
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can read the issue iid without accessing the external service' do
|
|
|
|
expect(::Gitlab::ExternalAuthorization).not_to receive(:access_allowed?)
|
|
|
|
|
|
|
|
expect(policies).to be_allowed(:read_merge_request_iid)
|
|
|
|
end
|
|
|
|
end
|
2019-03-08 03:34:20 -05:00
|
|
|
end
|