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