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