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

331 lines
8.4 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2022-11-25 23:54:43 +05:30
# Snippet visibility scenarios are included in more details in spec/finders/snippets_finder_spec.rb
2020-07-28 23:09:34 +05:30
RSpec.describe ProjectSnippetPolicy do
2022-11-25 23:54:43 +05:30
let_it_be(:group) { create(:group, :public) }
2020-01-01 13:55:28 +05:30
let_it_be(:regular_user) { create(:user) }
let_it_be(:external_user) { create(:user, :external) }
2022-11-25 23:54:43 +05:30
let_it_be(:author) { create(:user) }
let_it_be(:author_permissions) do
2017-08-17 22:00:37 +05:30
[
2020-03-13 15:44:24 +05:30
:update_snippet,
:admin_snippet
2017-08-17 22:00:37 +05:30
]
end
2022-11-25 23:54:43 +05:30
let(:snippet) { build(:project_snippet, snippet_visibility, project: project, author: author) }
2019-07-07 11:18:12 +05:30
subject { described_class.new(current_user, snippet) }
2019-05-18 00:54:41 +05:30
2022-11-25 23:54:43 +05:30
shared_examples 'regular user member permissions' do
2020-04-08 14:13:33 +05:30
context 'not snippet author' do
2022-11-25 23:54:43 +05:30
context 'member (guest)' do
2020-04-08 14:13:33 +05:30
before do
2022-11-25 23:54:43 +05:30
membership_target.add_guest(current_user)
2020-04-08 14:13:33 +05:30
end
2020-01-01 13:55:28 +05:30
2020-04-08 14:13:33 +05:30
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
2020-01-01 13:55:28 +05:30
end
2022-11-25 23:54:43 +05:30
context 'member (reporter)' do
2020-04-08 14:13:33 +05:30
before do
2022-11-25 23:54:43 +05:30
membership_target.add_reporter(current_user)
2020-04-08 14:13:33 +05:30
end
2020-01-01 13:55:28 +05:30
it do
2020-03-13 15:44:24 +05:30
expect_allowed(:read_snippet, :create_note)
2022-11-25 23:54:43 +05:30
expect_disallowed(*author_permissions)
2020-01-01 13:55:28 +05:30
end
end
2020-04-08 14:13:33 +05:30
2022-11-25 23:54:43 +05:30
context 'member (developer)' do
2020-04-08 14:13:33 +05:30
before do
2022-11-25 23:54:43 +05:30
membership_target.add_developer(current_user)
2020-04-08 14:13:33 +05:30
end
it do
expect_allowed(:read_snippet, :create_note)
2022-11-25 23:54:43 +05:30
expect_disallowed(*author_permissions)
end
end
context 'member (maintainer)' do
before do
membership_target.add_maintainer(current_user)
end
it do
expect_allowed(:read_snippet, :create_note, *author_permissions)
2020-04-08 14:13:33 +05:30
end
end
2020-01-01 13:55:28 +05:30
end
context 'snippet author' do
let(:author) { current_user }
2022-11-25 23:54:43 +05:30
context 'member (guest)' do
2020-01-01 13:55:28 +05:30
before do
2022-11-25 23:54:43 +05:30
membership_target.add_guest(current_user)
2020-01-01 13:55:28 +05:30
end
it do
2022-11-25 23:54:43 +05:30
expect_allowed(:read_snippet, :create_note, :update_snippet)
expect_disallowed(:admin_snippet)
2020-01-01 13:55:28 +05:30
end
end
2022-11-25 23:54:43 +05:30
context 'member (reporter)' do
2020-01-01 13:55:28 +05:30
before do
2022-11-25 23:54:43 +05:30
membership_target.add_reporter(current_user)
2020-01-01 13:55:28 +05:30
end
it do
2022-11-25 23:54:43 +05:30
expect_allowed(:read_snippet, :create_note, *author_permissions)
2020-01-01 13:55:28 +05:30
end
end
2022-11-25 23:54:43 +05:30
context 'member (developer)' do
2020-04-08 14:13:33 +05:30
before do
2022-11-25 23:54:43 +05:30
membership_target.add_developer(current_user)
2020-04-08 14:13:33 +05:30
end
it do
2022-11-25 23:54:43 +05:30
expect_allowed(:read_snippet, :create_note, *author_permissions)
2020-04-08 14:13:33 +05:30
end
end
2022-11-25 23:54:43 +05:30
context 'member (maintainer)' do
before do
membership_target.add_maintainer(current_user)
end
2020-01-01 13:55:28 +05:30
it do
2022-11-25 23:54:43 +05:30
expect_allowed(:read_snippet, :create_note, *author_permissions)
2020-01-01 13:55:28 +05:30
end
end
end
end
2022-11-25 23:54:43 +05:30
shared_examples 'regular user non-member author permissions' do
let(:author) { current_user }
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
it do
expect_allowed(:read_snippet, :create_note, :update_snippet)
expect_disallowed(:admin_snippet)
2017-08-17 22:00:37 +05:30
end
2022-11-25 23:54:43 +05:30
end
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
context 'when project is public' do
let_it_be(:project) { create(:project, :public, group: group) }
2020-01-01 13:55:28 +05:30
2022-11-25 23:54:43 +05:30
context 'with public snippet' do
let(:snippet_visibility) { :public }
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
context 'no user' do
let(:current_user) { nil }
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
it do
expect_allowed(:read_snippet)
expect_disallowed(*author_permissions)
end
2017-08-17 22:00:37 +05:30
end
2020-01-01 13:55:28 +05:30
2022-11-25 23:54:43 +05:30
context 'regular user' do
let(:current_user) { regular_user }
let(:membership_target) { project }
context 'when user is not a member' do
context 'and is not the snippet author' do
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'and is the snippet author' do
it_behaves_like 'regular user non-member author permissions'
end
2020-01-01 13:55:28 +05:30
end
2022-11-25 23:54:43 +05:30
context 'when user is a member' do
it_behaves_like 'regular user member permissions'
end
end
context 'external user' do
let(:current_user) { external_user }
2020-01-01 13:55:28 +05:30
it do
2020-03-13 15:44:24 +05:30
expect_allowed(:read_snippet, :create_note)
2020-01-01 13:55:28 +05:30
expect_disallowed(*author_permissions)
end
2019-07-07 11:18:12 +05:30
2022-11-25 23:54:43 +05:30
context 'when user is a member' do
before do
project.add_developer(external_user)
end
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
2017-08-17 22:00:37 +05:30
end
end
2022-11-25 23:54:43 +05:30
context 'with internal snippet' do
let(:snippet_visibility) { :internal }
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
context 'no user' do
let(:current_user) { nil }
2020-01-01 13:55:28 +05:30
2022-11-25 23:54:43 +05:30
it do
expect_disallowed(:read_snippet)
expect_disallowed(*author_permissions)
end
end
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
context 'regular user' do
let(:current_user) { regular_user }
let(:membership_target) { project }
context 'when user is not a member' do
context 'and is not the snippet author' do
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'and is the snippet author' do
it_behaves_like 'regular user non-member author permissions'
end
end
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
context 'when user is a member' do
it_behaves_like 'regular user member permissions'
end
2017-08-17 22:00:37 +05:30
end
2022-11-25 23:54:43 +05:30
context 'external user' do
let(:current_user) { external_user }
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
it do
2022-11-25 23:54:43 +05:30
expect_disallowed(:read_snippet, :create_note)
2019-07-07 11:18:12 +05:30
expect_disallowed(*author_permissions)
end
2022-11-25 23:54:43 +05:30
context 'when user is a member' do
before do
project.add_developer(external_user)
end
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
2017-08-17 22:00:37 +05:30
end
end
2022-11-25 23:54:43 +05:30
context 'with private snippet' do
let(:snippet_visibility) { :private }
2019-07-07 11:18:12 +05:30
2022-11-25 23:54:43 +05:30
context 'no user' do
let(:current_user) { nil }
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
it do
expect_disallowed(:read_snippet)
expect_disallowed(*author_permissions)
end
2017-08-17 22:00:37 +05:30
end
2022-11-25 23:54:43 +05:30
context 'regular user' do
let(:current_user) { regular_user }
let(:membership_target) { project }
context 'when user is not a member' do
context 'and is not the snippet author' do
it do
expect_disallowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'and is the snippet author' do
it_behaves_like 'regular user non-member author permissions'
end
end
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
context 'when user is a member' do
it_behaves_like 'regular user member permissions'
end
2017-08-17 22:00:37 +05:30
end
2022-11-25 23:54:43 +05:30
context 'inherited user' do
let(:current_user) { regular_user }
let(:membership_target) { group }
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
it_behaves_like 'regular user member permissions'
2017-08-17 22:00:37 +05:30
end
2022-11-25 23:54:43 +05:30
context 'external user' do
let(:current_user) { external_user }
2017-08-17 22:00:37 +05:30
2019-07-07 11:18:12 +05:30
it do
2022-11-25 23:54:43 +05:30
expect_disallowed(:read_snippet, :create_note)
2019-07-07 11:18:12 +05:30
expect_disallowed(*author_permissions)
end
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
context 'when user is a member' do
before do
project.add_developer(current_user)
end
2017-08-17 22:00:37 +05:30
2022-11-25 23:54:43 +05:30
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
2020-05-24 23:13:21 +05:30
end
end
2022-11-25 23:54:43 +05:30
context 'admin user' do
let(:snippet_visibility) { :private }
let(:current_user) { create(:admin) }
context 'when admin mode is enabled', :enable_admin_mode do
it do
expect_allowed(:read_snippet, :create_note)
expect_allowed(*author_permissions)
end
end
context 'when admin mode is disabled' do
it do
expect_disallowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
2020-05-24 23:13:21 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
end
2022-11-25 23:54:43 +05:30
context 'when project is private' do
let_it_be(:project) { create(:project, :private, group: group) }
let(:snippet_visibility) { :private }
context 'inherited user' do
let(:current_user) { regular_user }
let(:membership_target) { group }
it_behaves_like 'regular user member permissions'
end
end
2017-08-17 22:00:37 +05:30
end