2019-10-24 20:06:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-01 09:14:35 -04: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
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe PersonalSnippetPolicy do
|
2017-05-01 09:14:35 -04:00
|
|
|
let(:regular_user) { create(:user) }
|
|
|
|
let(:external_user) { create(:user, :external) }
|
|
|
|
let(:admin_user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
let(:author_permissions) do
|
|
|
|
[
|
2020-01-23 07:08:38 -05:00
|
|
|
:update_snippet,
|
|
|
|
:admin_snippet
|
2017-05-01 09:14:35 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def permissions(user)
|
2017-04-06 17:09:58 -04:00
|
|
|
described_class.new(user, snippet)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
shared_examples 'admin access with admin mode' do
|
|
|
|
context 'admin user', :enable_admin_mode do
|
2019-11-07 01:06:12 -05:00
|
|
|
subject { permissions(admin_user) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_allowed(:read_snippet)
|
2019-11-07 01:06:12 -05:00
|
|
|
is_expected.to be_allowed(:create_note)
|
|
|
|
is_expected.to be_allowed(:award_emoji)
|
|
|
|
is_expected.to be_allowed(*author_permissions)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
context 'public snippet' do
|
|
|
|
let(:snippet) { create(:personal_snippet, :public) }
|
|
|
|
|
|
|
|
context 'no user' do
|
|
|
|
subject { permissions(nil) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_allowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_disallowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_disallowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
|
|
|
subject { permissions(regular_user) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_allowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_allowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_allowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'author' do
|
|
|
|
subject { permissions(snippet.author) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_allowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_allowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_allowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_allowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
2019-11-07 01:06:12 -05:00
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
it_behaves_like 'admin access with admin mode'
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'internal snippet' do
|
|
|
|
let(:snippet) { create(:personal_snippet, :internal) }
|
|
|
|
|
|
|
|
context 'no user' do
|
|
|
|
subject { permissions(nil) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_disallowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_disallowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_disallowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
|
|
|
subject { permissions(regular_user) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_allowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_allowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_allowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
|
|
|
subject { permissions(external_user) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_disallowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_disallowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_disallowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'snippet author' do
|
|
|
|
subject { permissions(snippet.author) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_allowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_allowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_allowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_allowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
2019-11-07 01:06:12 -05:00
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
it_behaves_like 'admin access with admin mode'
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'private snippet' do
|
|
|
|
let(:snippet) { create(:project_snippet, :private) }
|
|
|
|
|
|
|
|
context 'no user' do
|
|
|
|
subject { permissions(nil) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_disallowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_disallowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_disallowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular user' do
|
|
|
|
subject { permissions(regular_user) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_disallowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_disallowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_disallowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'external user' do
|
|
|
|
subject { permissions(external_user) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_disallowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_disallowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_disallowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_disallowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'snippet author' do
|
|
|
|
subject { permissions(snippet.author) }
|
|
|
|
|
|
|
|
it do
|
2020-01-23 07:08:38 -05:00
|
|
|
is_expected.to be_allowed(:read_snippet)
|
2019-05-02 06:13:42 -04:00
|
|
|
is_expected.to be_allowed(:create_note)
|
2018-04-06 14:19:37 -04:00
|
|
|
is_expected.to be_allowed(:award_emoji)
|
2017-04-06 17:09:58 -04:00
|
|
|
is_expected.to be_allowed(*author_permissions)
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|
2019-11-07 01:06:12 -05:00
|
|
|
|
2020-05-15 11:08:04 -04:00
|
|
|
it_behaves_like 'admin access with admin mode'
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|
|
|
|
end
|