gitlab-org--gitlab-foss/spec/policies/project_snippet_policy_spec.rb

245 lines
5.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-02-06 18:19:37 -05:00
require 'spec_helper'
# Snippet visibility scenarios are included in more details in spec/support/snippet_visibility.rb
describe ProjectSnippetPolicy do
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
[
:update_snippet,
:admin_snippet
2017-02-06 18:19:37 -05:00
]
end
subject { described_class.new(current_user, snippet) }
shared_examples 'regular user access rights' do
context 'not snippet author' do
context 'project team member (non guest)' do
before do
project.add_developer(current_user)
end
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'project team member (guest)' do
before do
project.add_guest(current_user)
end
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(:admin_snippet)
end
end
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
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
expect_allowed(:read_snippet, :create_note)
expect_allowed(*author_permissions)
end
end
context 'project member (guest)' do
before do
project.add_guest(current_user)
end
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(:admin_snippet)
end
end
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
context 'not a project member' do
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(:admin_snippet)
end
end
end
end
context 'public snippet' do
let(:snippet_visibility) { :public }
2017-02-06 18:19:37 -05:00
context 'no user' do
let(:current_user) { nil }
2017-02-06 18:19:37 -05:00
it do
expect_allowed(:read_snippet)
expect_disallowed(*author_permissions)
2017-02-06 18:19:37 -05:00
end
end
context 'regular user' do
let(:current_user) { regular_user }
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
it_behaves_like 'regular user access rights'
end
context 'external user' do
let(:current_user) { external_user }
2017-02-06 18:19:37 -05:00
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
2017-02-06 18:19:37 -05:00
end
context 'project team 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-02-06 18:19:37 -05:00
end
end
context 'internal snippet' do
let(:snippet_visibility) { :internal }
2017-02-06 18:19:37 -05:00
context 'no user' do
let(:current_user) { nil }
2017-02-06 18:19:37 -05:00
it do
expect_disallowed(:read_snippet)
expect_disallowed(*author_permissions)
2017-02-06 18:19:37 -05:00
end
end
context 'regular user' do
let(:current_user) { regular_user }
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
it_behaves_like 'regular user access rights'
end
context 'external user' do
let(:current_user) { external_user }
it do
expect_disallowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
context 'project team member' do
before do
project.add_developer(external_user)
end
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
2017-02-06 18:19:37 -05:00
end
end
end
context 'private snippet' do
let(:snippet_visibility) { :private }
2017-02-06 18:19:37 -05:00
context 'no user' do
let(:current_user) { nil }
2017-02-06 18:19:37 -05:00
it do
expect_disallowed(:read_snippet)
expect_disallowed(*author_permissions)
2017-02-06 18:19:37 -05:00
end
end
context 'regular user' do
let(:current_user) { regular_user }
2017-02-06 18:19:37 -05:00
it do
expect_disallowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
2017-02-06 18:19:37 -05:00
end
it_behaves_like 'regular user access rights'
end
2017-02-06 18:19:37 -05:00
context 'external user' do
let(:current_user) { external_user }
it do
expect_disallowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
context 'project team member' do
before do
project.add_developer(current_user)
end
2017-02-06 18:19:37 -05:00
it do
expect_allowed(:read_snippet, :create_note)
expect_disallowed(*author_permissions)
end
2017-02-06 18:19:37 -05:00
end
end
context 'admin user' do
let(:snippet_visibility) { :private }
let(:current_user) { create(:admin) }
2017-02-06 18:19:37 -05:00
it do
expect_allowed(:read_snippet, :create_note)
expect_allowed(*author_permissions)
2017-02-06 18:19:37 -05:00
end
end
end
end