2019-10-24 20:06:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-01-18 11:07:06 -05:00
|
|
|
# Snippet visibility scenarios are included in more details in spec/support/snippet_visibility.rb
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe ProjectSnippetPolicy do
|
2019-12-16 10:07:39 -05:00
|
|
|
let_it_be(:regular_user) { create(:user) }
|
|
|
|
let_it_be(:other_user) { create(:user) }
|
|
|
|
let_it_be(:external_user) { create(:user, :external) }
|
|
|
|
let_it_be(:project) { create(:project, :public) }
|
|
|
|
let(:snippet) { create(:project_snippet, snippet_visibility, project: project, author: author) }
|
|
|
|
let(:author) { other_user }
|
2017-02-06 18:19:37 -05:00
|
|
|
let(:author_permissions) do
|
|
|
|
[
|
2020-01-23 07:08:38 -05:00
|
|
|
:update_snippet,
|
|
|
|
:admin_snippet
|
2017-02-06 18:19:37 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
subject { described_class.new(current_user, snippet) }
|
2017-04-06 17:09:58 -04:00
|
|
|
|
2019-12-16 10:07:39 -05:00
|
|
|
shared_examples 'regular user access rights' do
|
2020-02-24 19:09:12 -05:00
|
|
|
context 'not snippet author' do
|
|
|
|
context 'project team member (non guest)' do
|
|
|
|
before do
|
|
|
|
project.add_developer(current_user)
|
|
|
|
end
|
2019-12-16 10:07:39 -05:00
|
|
|
|
2020-02-24 19:09:12 -05:00
|
|
|
it do
|
|
|
|
expect_allowed(:read_snippet, :create_note)
|
|
|
|
expect_disallowed(*author_permissions)
|
|
|
|
end
|
2019-12-16 10:07:39 -05:00
|
|
|
end
|
|
|
|
|
2020-02-24 19:09:12 -05:00
|
|
|
context 'project team member (guest)' do
|
|
|
|
before do
|
|
|
|
project.add_guest(current_user)
|
|
|
|
end
|
2019-12-16 10:07:39 -05:00
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
|
|
|
expect_disallowed(:admin_snippet)
|
2019-12-16 10:07:39 -05:00
|
|
|
end
|
|
|
|
end
|
2020-02-24 19:09:12 -05:00
|
|
|
|
|
|
|
context 'project team member (maintainer)' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect_allowed(:read_snippet, :create_note)
|
|
|
|
expect_allowed(*author_permissions)
|
|
|
|
end
|
|
|
|
end
|
2019-12-16 10:07:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'snippet author' do
|
|
|
|
let(:author) { current_user }
|
|
|
|
|
|
|
|
context 'project member (non guest)' do
|
|
|
|
before do
|
|
|
|
project.add_developer(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
2019-12-16 10:07:39 -05:00
|
|
|
expect_allowed(*author_permissions)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project member (guest)' do
|
|
|
|
before do
|
|
|
|
project.add_guest(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
|
|
|
expect_disallowed(:admin_snippet)
|
2019-12-16 10:07:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-24 19:09:12 -05:00
|
|
|
context 'project team member (maintainer)' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect_allowed(:read_snippet, :create_note)
|
|
|
|
expect_allowed(*author_permissions)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-16 10:07:39 -05:00
|
|
|
context 'not a project member' do
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
|
|
|
expect_disallowed(:admin_snippet)
|
2019-12-16 10:07:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
context 'public snippet' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :public }
|
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
context 'no user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { nil }
|
2017-02-06 18:19:37 -05:00
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { regular_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
2019-12-16 10:07:39 -05:00
|
|
|
|
|
|
|
it_behaves_like 'regular user access rights'
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { external_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
2019-12-16 10:07:39 -05:00
|
|
|
|
|
|
|
context 'project team member' do
|
|
|
|
before do
|
|
|
|
project.add_developer(external_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
2019-12-16 10:07:39 -05:00
|
|
|
expect_disallowed(*author_permissions)
|
|
|
|
end
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'internal snippet' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :internal }
|
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
context 'no user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { nil }
|
2017-02-06 18:19:37 -05:00
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_disallowed(:read_snippet)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { regular_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
2019-12-16 10:07:39 -05:00
|
|
|
|
|
|
|
it_behaves_like 'regular user access rights'
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { external_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_disallowed(:read_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
context 'project team member' do
|
|
|
|
before do
|
|
|
|
project.add_developer(external_user)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
2019-03-26 07:31:47 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'private snippet' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :private }
|
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
context 'no user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { nil }
|
2017-02-06 18:19:37 -05:00
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_disallowed(:read_snippet)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { regular_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_disallowed(:read_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-12-16 10:07:39 -05:00
|
|
|
it_behaves_like 'regular user access rights'
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
|
2019-12-16 10:07:39 -05:00
|
|
|
context 'external user' do
|
|
|
|
let(:current_user) { external_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-12-16 10:07:39 -05:00
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_disallowed(:read_snippet, :create_note)
|
2019-12-16 10:07:39 -05:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
context 'project team member' do
|
|
|
|
before do
|
2019-12-16 10:07:39 -05:00
|
|
|
project.add_developer(current_user)
|
2019-03-26 07:31:47 -04:00
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
expect_allowed(:read_snippet, :create_note)
|
2019-03-26 07:31:47 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'admin user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :private }
|
|
|
|
let(:current_user) { create(:admin) }
|
2017-02-06 18:19:37 -05:00
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
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
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|