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
|
2017-04-28 22:06:27 +00:00
|
|
|
let(:regular_user) { create(:user) }
|
|
|
|
let(:external_user) { create(:user, :external) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :public) }
|
2017-02-06 23:19:37 +00:00
|
|
|
|
|
|
|
let(:author_permissions) do
|
|
|
|
[
|
|
|
|
:update_project_snippet,
|
|
|
|
:admin_project_snippet
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2017-04-28 22:06:27 +00:00
|
|
|
def abilities(user, snippet_visibility)
|
|
|
|
snippet = create(:project_snippet, snippet_visibility, project: project)
|
2017-02-06 23:19:37 +00:00
|
|
|
|
2017-04-06 21:09:58 +00:00
|
|
|
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-04-28 22:06:27 +00:00
|
|
|
end
|
2017-02-06 23:19:37 +00:00
|
|
|
|
2017-04-28 22:06:27 +00:00
|
|
|
context 'public snippet' do
|
2017-02-06 23:19:37 +00:00
|
|
|
context 'no user' do
|
2017-04-28 22:06:27 +00:00
|
|
|
subject { abilities(nil, :public) }
|
2017-02-06 23:19:37 +00:00
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
2017-04-28 22:06:27 +00:00
|
|
|
subject { abilities(regular_user, :public) }
|
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 22:06:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
|
|
|
subject { abilities(external_user, :public) }
|
|
|
|
|
2017-02-06 23:19:37 +00:00
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'internal snippet' do
|
|
|
|
context 'no user' do
|
2017-04-28 22:06:27 +00:00
|
|
|
subject { abilities(nil, :internal) }
|
2017-02-06 23:19:37 +00:00
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_disallowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
2017-04-28 22:06:27 +00:00
|
|
|
subject { abilities(regular_user, :internal) }
|
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 22:06:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
|
|
|
subject { abilities(external_user, :internal) }
|
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_disallowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 22:06:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project team member external user' do
|
|
|
|
subject { abilities(external_user, :internal) }
|
|
|
|
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(external_user)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2017-04-28 22:06:27 +00:00
|
|
|
|
2017-02-06 23:19:37 +00:00
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'private snippet' do
|
|
|
|
context 'no user' do
|
2017-04-28 22:06:27 +00:00
|
|
|
subject { abilities(nil, :private) }
|
2017-02-06 23:19:37 +00:00
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_disallowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
2017-04-28 22:06:27 +00:00
|
|
|
subject { abilities(regular_user, :private) }
|
|
|
|
|
2017-02-06 23:19:37 +00:00
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_disallowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'snippet author' do
|
2017-06-08 16:56:39 +00:00
|
|
|
let(:snippet) { create(:project_snippet, :private, author: regular_user, project: project) }
|
2017-04-28 22:06:27 +00:00
|
|
|
|
2017-04-06 21:06:42 +00:00
|
|
|
subject { described_class.new(regular_user, snippet) }
|
2017-02-06 23:19:37 +00:00
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_allowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-28 22:06:27 +00:00
|
|
|
context 'project team member normal user' do
|
|
|
|
subject { abilities(regular_user, :private) }
|
|
|
|
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(regular_user)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2017-04-28 22:06:27 +00:00
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 22:06:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'project team member external user' do
|
|
|
|
subject { abilities(external_user, :private) }
|
|
|
|
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(external_user)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2017-02-06 23:19:37 +00:00
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'admin user' do
|
2017-04-28 22:06:27 +00:00
|
|
|
subject { abilities(create(:admin), :private) }
|
2017-02-06 23:19:37 +00:00
|
|
|
|
|
|
|
it do
|
2017-04-06 21:09:58 +00:00
|
|
|
expect_allowed(:read_project_snippet)
|
|
|
|
expect_allowed(*author_permissions)
|
2017-02-06 23:19:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|