2017-02-06 18:19:37 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-01-18 11:07:06 -05:00
|
|
|
# Snippet visibility scenarios are included in more details in spec/support/snippet_visibility.rb
|
2017-07-10 10:24:02 -04:00
|
|
|
describe ProjectSnippetPolicy do
|
2017-04-28 18:06:27 -04:00
|
|
|
let(:regular_user) { create(:user) }
|
|
|
|
let(:external_user) { create(:user, :external) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet) { create(:project_snippet, snippet_visibility, project: project) }
|
2017-02-06 18:19:37 -05:00
|
|
|
let(:author_permissions) do
|
|
|
|
[
|
|
|
|
:update_project_snippet,
|
|
|
|
:admin_project_snippet
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
subject { described_class.new(current_user, snippet) }
|
2017-04-06 17:09:58 -04:00
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
context 'public snippet' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :public }
|
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
context 'no user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { nil }
|
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
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { regular_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
it do
|
2019-01-15 13:53:24 -05:00
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { external_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
it do
|
2019-01-15 13:53:24 -05:00
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'internal snippet' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :internal }
|
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
context 'no user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { nil }
|
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
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { regular_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
it do
|
2019-01-15 13:53:24 -05:00
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { external_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
it do
|
2019-01-15 13:53:24 -05:00
|
|
|
expect_disallowed(:read_project_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
context 'project team member' do
|
|
|
|
before do
|
|
|
|
project.add_developer(external_user)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
it do
|
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
|
|
|
expect_disallowed(*author_permissions)
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'private snippet' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :private }
|
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
context 'no user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { nil }
|
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
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:current_user) { regular_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2017-02-06 18:19:37 -05:00
|
|
|
it do
|
2019-01-15 13:53:24 -05:00
|
|
|
expect_disallowed(:read_project_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_disallowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
context 'snippet author' do
|
|
|
|
let(:snippet) { create(:project_snippet, :private, author: regular_user, project: project) }
|
2017-02-06 18:19:37 -05:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
it do
|
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
|
|
|
expect_allowed(*author_permissions)
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
context 'project team member normal user' do
|
|
|
|
before do
|
|
|
|
project.add_developer(regular_user)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
it do
|
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
|
|
|
expect_disallowed(*author_permissions)
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
context 'external user' do
|
|
|
|
context 'project team member' do
|
|
|
|
let(:current_user) { external_user }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
before do
|
|
|
|
project.add_developer(external_user)
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
|
2019-03-26 07:31:47 -04:00
|
|
|
it do
|
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
|
|
|
expect_disallowed(*author_permissions)
|
|
|
|
end
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'admin user' do
|
2019-03-26 07:31:47 -04:00
|
|
|
let(:snippet_visibility) { :private }
|
|
|
|
let(:current_user) { create(:admin) }
|
2017-02-06 18:19:37 -05:00
|
|
|
|
|
|
|
it do
|
2019-01-15 13:53:24 -05:00
|
|
|
expect_allowed(:read_project_snippet, :create_note)
|
2017-04-06 17:09:58 -04:00
|
|
|
expect_allowed(*author_permissions)
|
2017-02-06 18:19:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|