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