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

167 lines
4 KiB
Ruby
Raw Normal View History

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
2017-09-10 17:25:29 +05:30
describe ProjectSnippetPolicy do
2017-08-17 22:00:37 +05:30
let(:regular_user) { create(:user) }
let(:external_user) { create(:user, :external) }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public) }
2017-08-17 22:00:37 +05:30
let(:author_permissions) do
[
:update_project_snippet,
:admin_project_snippet
]
end
def abilities(user, snippet_visibility)
snippet = create(:project_snippet, snippet_visibility, project: project)
2017-09-10 17:25:29 +05:30
described_class.new(user, snippet)
end
def expect_allowed(*permissions)
permissions.each { |p| is_expected.to be_allowed(p) }
end
def expect_disallowed(*permissions)
permissions.each { |p| is_expected.not_to be_allowed(p) }
2017-08-17 22:00:37 +05:30
end
context 'public snippet' do
context 'no user' do
subject { abilities(nil, :public) }
it do
2017-09-10 17:25:29 +05:30
expect_allowed(:read_project_snippet)
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'regular user' do
subject { abilities(regular_user, :public) }
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'external user' do
subject { abilities(external_user, :public) }
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
end
context 'internal snippet' do
context 'no user' do
subject { abilities(nil, :internal) }
it do
2017-09-10 17:25:29 +05:30
expect_disallowed(:read_project_snippet)
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'regular user' do
subject { abilities(regular_user, :internal) }
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'external user' do
subject { abilities(external_user, :internal) }
it do
2019-02-13 22:33:31 +05:30
expect_disallowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'project team member external user' do
subject { abilities(external_user, :internal) }
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(external_user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
end
context 'private snippet' do
context 'no user' do
subject { abilities(nil, :private) }
it do
2017-09-10 17:25:29 +05:30
expect_disallowed(:read_project_snippet)
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'regular user' do
subject { abilities(regular_user, :private) }
it do
2019-02-13 22:33:31 +05:30
expect_disallowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'snippet author' do
let(:snippet) { create(:project_snippet, :private, author: regular_user, project: project) }
2017-09-10 17:25:29 +05:30
subject { described_class.new(regular_user, snippet) }
2017-08-17 22:00:37 +05:30
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_allowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'project team member normal user' do
subject { abilities(regular_user, :private) }
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(regular_user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'project team member external user' do
subject { abilities(external_user, :private) }
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(external_user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_disallowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
context 'admin user' do
subject { abilities(create(:admin), :private) }
it do
2019-02-13 22:33:31 +05:30
expect_allowed(:read_project_snippet, :create_note)
2017-09-10 17:25:29 +05:30
expect_allowed(*author_permissions)
2017-08-17 22:00:37 +05:30
end
end
end
end