debian-mirror-gitlab/spec/policies/issue_policy_spec.rb

283 lines
14 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe IssuePolicy do
2019-07-07 11:18:12 +05:30
include ExternalAuthorizationServiceHelpers
2016-11-03 12:29:30 +05:30
let(:guest) { create(:user) }
let(:author) { create(:user) }
let(:assignee) { create(:user) }
let(:reporter) { create(:user) }
let(:group) { create(:group, :public) }
let(:reporter_from_group_link) { create(:user) }
def permissions(user, issue)
2017-09-10 17:25:29 +05:30
described_class.new(user, issue)
2016-11-03 12:29:30 +05:30
end
context 'a private project' do
let(:non_member) { create(:user) }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :private) }
2017-08-17 22:00:37 +05:30
let(:issue) { create(:issue, project: project, assignees: [assignee], author: author) }
2016-11-03 12:29:30 +05:30
let(:issue_no_assignee) { create(:issue, project: project) }
before do
2018-03-17 18:26:18 +05:30
project.add_guest(guest)
project.add_guest(author)
project.add_guest(assignee)
project.add_reporter(reporter)
2016-11-03 12:29:30 +05:30
group.add_reporter(reporter_from_group_link)
create(:project_group_link, group: group, project: project)
end
it 'does not allow non-members to read issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(non_member, issue)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(non_member, issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows guests to read issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(guest, issue)).to be_allowed(:read_issue, :read_issue_iid)
2017-09-10 17:25:29 +05:30
expect(permissions(guest, issue)).to be_disallowed(:update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(guest, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid)
2017-09-10 17:25:29 +05:30
expect(permissions(guest, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows reporters to read, update, and admin issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(reporter, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows reporters from group links to read, update, and admin issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(reporter_from_group_link, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter_from_group_link, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows issue authors to read and update their issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(author, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(author, issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(author, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid)
2017-09-10 17:25:29 +05:30
expect(permissions(author, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows issue assignees to read and update their issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(assignee, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(assignee, issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(assignee, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid)
2017-09-10 17:25:29 +05:30
expect(permissions(assignee, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
context 'with confidential issues' do
2017-08-17 22:00:37 +05:30
let(:confidential_issue) { create(:issue, :confidential, project: project, assignees: [assignee], author: author) }
2016-11-03 12:29:30 +05:30
let(:confidential_issue_no_assignee) { create(:issue, :confidential, project: project) }
it 'does not allow non-members to read confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(non_member, confidential_issue)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(non_member, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'does not allow guests to read confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(guest, confidential_issue)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(guest, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows reporters to read, update, and admin confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(reporter, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter, confidential_issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows reporters from group links to read, update, and admin confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(reporter_from_group_link, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter_from_group_link, confidential_issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows issue authors to read and update their confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(author, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(author, confidential_issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(author, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
2020-03-28 13:19:24 +05:30
it 'does not allow issue author to read or update confidential issue moved to an private project' do
confidential_issue.project = build(:project, :private)
expect(permissions(author, confidential_issue)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue)
end
2016-11-03 12:29:30 +05:30
it 'allows issue assignees to read and update their confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(assignee, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(assignee, confidential_issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(assignee, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
2020-03-28 13:19:24 +05:30
it 'does not allow issue assignees to read or update confidential issue moved to an private project' do
confidential_issue.project = build(:project, :private)
expect(permissions(assignee, confidential_issue)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue)
end
2016-11-03 12:29:30 +05:30
end
end
context 'a public project' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
2017-08-17 22:00:37 +05:30
let(:issue) { create(:issue, project: project, assignees: [assignee], author: author) }
2016-11-03 12:29:30 +05:30
let(:issue_no_assignee) { create(:issue, project: project) }
2018-12-05 23:21:45 +05:30
let(:issue_locked) { create(:issue, :locked, project: project, author: author, assignees: [assignee]) }
2016-11-03 12:29:30 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_guest(guest)
project.add_reporter(reporter)
2016-11-03 12:29:30 +05:30
group.add_reporter(reporter_from_group_link)
create(:project_group_link, group: group, project: project)
end
it 'allows guests to read issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(guest, issue)).to be_allowed(:read_issue, :read_issue_iid)
2018-11-20 20:47:30 +05:30
expect(permissions(guest, issue)).to be_disallowed(:update_issue, :admin_issue, :reopen_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(guest, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid)
2018-11-20 20:47:30 +05:30
expect(permissions(guest, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue, :reopen_issue)
expect(permissions(guest, issue_locked)).to be_allowed(:read_issue, :read_issue_iid)
expect(permissions(guest, issue_locked)).to be_disallowed(:update_issue, :admin_issue, :reopen_issue)
2016-11-03 12:29:30 +05:30
end
2018-11-20 20:47:30 +05:30
it 'allows reporters to read, update, reopen, and admin issues' do
expect(permissions(reporter, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue, :reopen_issue)
expect(permissions(reporter, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue, :reopen_issue)
expect(permissions(reporter, issue_locked)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter, issue_locked)).to be_disallowed(:reopen_issue)
2016-11-03 12:29:30 +05:30
end
2018-11-20 20:47:30 +05:30
it 'allows reporters from group links to read, update, reopen and admin issues' do
expect(permissions(reporter_from_group_link, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue, :reopen_issue)
expect(permissions(reporter_from_group_link, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue, :reopen_issue)
expect(permissions(reporter_from_group_link, issue_locked)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter_from_group_link, issue_locked)).to be_disallowed(:reopen_issue)
2016-11-03 12:29:30 +05:30
end
2018-11-20 20:47:30 +05:30
it 'allows issue authors to read, reopen and update their issues' do
expect(permissions(author, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :reopen_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(author, issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(author, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid)
2018-11-20 20:47:30 +05:30
expect(permissions(author, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue, :reopen_issue)
expect(permissions(author, issue_locked)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
expect(permissions(author, issue_locked)).to be_disallowed(:admin_issue, :reopen_issue)
2016-11-03 12:29:30 +05:30
end
2018-11-20 20:47:30 +05:30
it 'allows issue assignees to read, reopen and update their issues' do
expect(permissions(assignee, issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :reopen_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(assignee, issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(assignee, issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid)
2018-11-20 20:47:30 +05:30
expect(permissions(assignee, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue, :reopen_issue)
expect(permissions(assignee, issue_locked)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
expect(permissions(assignee, issue_locked)).to be_disallowed(:admin_issue, :reopen_issue)
2016-11-03 12:29:30 +05:30
end
2019-09-04 21:01:54 +05:30
context 'when issues are private' do
before do
project.project_feature.update(issues_access_level: ProjectFeature::PRIVATE)
end
let(:issue) { create(:issue, project: project, author: author) }
let(:visitor) { create(:user) }
let(:admin) { create(:user, :admin) }
it 'forbids visitors from viewing issues' do
expect(permissions(visitor, issue)).to be_disallowed(:read_issue)
end
it 'forbids visitors from commenting' do
expect(permissions(visitor, issue)).to be_disallowed(:create_note)
end
it 'allows guests to view' do
expect(permissions(guest, issue)).to be_allowed(:read_issue)
end
it 'allows guests to comment' do
expect(permissions(guest, issue)).to be_allowed(:create_note)
end
2020-05-24 23:13:21 +05:30
context 'when admin mode is enabled', :enable_admin_mode do
it 'allows admins to view' do
expect(permissions(admin, issue)).to be_allowed(:read_issue)
end
it 'allows admins to comment' do
expect(permissions(admin, issue)).to be_allowed(:create_note)
end
2019-09-04 21:01:54 +05:30
end
2020-05-24 23:13:21 +05:30
context 'when admin mode is disabled' do
it 'forbids admins to view' do
expect(permissions(admin, issue)).to be_disallowed(:read_issue)
end
it 'forbids admins to comment' do
expect(permissions(admin, issue)).to be_disallowed(:create_note)
end
2019-09-04 21:01:54 +05:30
end
end
2016-11-03 12:29:30 +05:30
context 'with confidential issues' do
2017-08-17 22:00:37 +05:30
let(:confidential_issue) { create(:issue, :confidential, project: project, assignees: [assignee], author: author) }
2016-11-03 12:29:30 +05:30
let(:confidential_issue_no_assignee) { create(:issue, :confidential, project: project) }
it 'does not allow guests to read confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(guest, confidential_issue)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(guest, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows reporters to read, update, and admin confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(reporter, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter, confidential_issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows reporter from group links to read, update, and admin confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(reporter_from_group_link, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
expect(permissions(reporter_from_group_link, confidential_issue_no_assignee)).to be_allowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows issue authors to read and update their confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(author, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(author, confidential_issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(author, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
it 'allows issue assignees to read and update their confidential issues' do
2018-03-27 19:54:05 +05:30
expect(permissions(assignee, confidential_issue)).to be_allowed(:read_issue, :read_issue_iid, :update_issue)
2017-09-10 17:25:29 +05:30
expect(permissions(assignee, confidential_issue)).to be_disallowed(:admin_issue)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
expect(permissions(assignee, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :read_issue_iid, :update_issue, :admin_issue)
2016-11-03 12:29:30 +05:30
end
end
end
2019-07-07 11:18:12 +05:30
context 'with external authorization enabled' do
let(:user) { create(:user) }
let(:project) { create(:project, :public) }
let(:issue) { create(:issue, project: project) }
let(:policies) { described_class.new(user, issue) }
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_issue_iid)
end
end
2016-11-03 12:29:30 +05:30
end