Commit 9ee5229b authored by Rémy Coutable's avatar Rémy Coutable

[EE] Reduce the diff with CE in spec/policies/project_snippet_policy_spec.rb

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent d8a5e326
# frozen_string_literal: true
require 'spec_helper'
# Snippet visibility scenarios are included in more details in spec/support/snippet_visibility.rb
describe ProjectSnippetPolicy do
let(:project) { create(:project, :public) }
let(:snippet) { create(:project_snippet, snippet_visibility, project: project) }
let(:author_permissions) do
[
:update_project_snippet,
:admin_project_snippet
]
end
subject { described_class.new(current_user, snippet) }
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) }
end
context 'private snippet' do
let(:snippet_visibility) { :private }
context 'auditor user' do
let(:current_user) { create(:user, :auditor) }
it do
is_expected.to be_allowed(:read_project_snippet)
is_expected.to be_disallowed(*author_permissions)
end
end
end
end
......@@ -5,7 +5,7 @@ describe ProjectSnippetPolicy do
let(:regular_user) { create(:user) }
let(:external_user) { create(:user, :external) }
let(:project) { create(:project, :public) }
let(:snippet) { create(:project_snippet, snippet_visibility, project: project) }
let(:author_permissions) do
[
:update_project_snippet,
......@@ -13,11 +13,7 @@ describe ProjectSnippetPolicy do
]
end
def abilities(user, snippet_visibility)
snippet = create(:project_snippet, snippet_visibility, project: project)
described_class.new(user, snippet)
end
subject { described_class.new(current_user, snippet) }
def expect_allowed(*permissions)
permissions.each { |p| is_expected.to be_allowed(p) }
......@@ -28,8 +24,10 @@ describe ProjectSnippetPolicy do
end
context 'public snippet' do
let(:snippet_visibility) { :public }
context 'no user' do
subject { abilities(nil, :public) }
let(:current_user) { nil }
it do
expect_allowed(:read_project_snippet)
......@@ -38,7 +36,7 @@ describe ProjectSnippetPolicy do
end
context 'regular user' do
subject { abilities(regular_user, :public) }
let(:current_user) { regular_user }
it do
expect_allowed(:read_project_snippet, :create_note)
......@@ -47,7 +45,7 @@ describe ProjectSnippetPolicy do
end
context 'external user' do
subject { abilities(external_user, :public) }
let(:current_user) { external_user }
it do
expect_allowed(:read_project_snippet, :create_note)
......@@ -57,8 +55,10 @@ describe ProjectSnippetPolicy do
end
context 'internal snippet' do
let(:snippet_visibility) { :internal }
context 'no user' do
subject { abilities(nil, :internal) }
let(:current_user) { nil }
it do
expect_disallowed(:read_project_snippet)
......@@ -67,7 +67,7 @@ describe ProjectSnippetPolicy do
end
context 'regular user' do
subject { abilities(regular_user, :internal) }
let(:current_user) { regular_user }
it do
expect_allowed(:read_project_snippet, :create_note)
......@@ -76,41 +76,31 @@ describe ProjectSnippetPolicy do
end
context 'external user' do
subject { abilities(external_user, :internal) }
let(:current_user) { external_user }
it do
expect_disallowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'project team member external user' do
subject { abilities(external_user, :internal) }
before do
project.add_developer(external_user)
end
it do
expect_allowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'external user' do
let(:current_user) { create(:user, :external) }
subject { abilities(current_user, :private) }
context 'project team member' do
before do
project.add_developer(external_user)
end
it do
is_expected.to be_disallowed(:read_project_snippet)
is_expected.to be_disallowed(*author_permissions)
it do
expect_allowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
end
end
context 'private snippet' do
let(:snippet_visibility) { :private }
context 'no user' do
subject { abilities(nil, :private) }
let(:current_user) { nil }
it do
expect_disallowed(:read_project_snippet)
......@@ -119,63 +109,52 @@ describe ProjectSnippetPolicy do
end
context 'regular user' do
subject { abilities(regular_user, :private) }
let(:current_user) { regular_user }
it do
expect_disallowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'snippet author' do
let(:snippet) { create(:project_snippet, :private, author: regular_user, project: project) }
subject { described_class.new(regular_user, snippet) }
context 'snippet author' do
let(:snippet) { create(:project_snippet, :private, author: regular_user, project: project) }
it do
expect_allowed(:read_project_snippet, :create_note)
expect_allowed(*author_permissions)
it do
expect_allowed(:read_project_snippet, :create_note)
expect_allowed(*author_permissions)
end
end
end
context 'project team member normal user' do
subject { abilities(regular_user, :private) }
before do
project.add_developer(regular_user)
end
context 'project team member normal user' do
before do
project.add_developer(regular_user)
end
it do
expect_allowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
it do
expect_allowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
end
context 'project team member external user' do
subject { abilities(external_user, :private) }
before do
project.add_developer(external_user)
end
it do
expect_allowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
context 'external user' do
context 'project team member' do
let(:current_user) { external_user }
context 'auditor user' do
let(:current_user) { create(:user, :auditor) }
subject { abilities(current_user, :private) }
before do
project.add_developer(external_user)
end
it do
is_expected.to be_allowed(:read_project_snippet)
is_expected.to be_disallowed(*author_permissions)
it do
expect_allowed(:read_project_snippet, :create_note)
expect_disallowed(*author_permissions)
end
end
end
context 'admin user' do
subject { abilities(create(:admin), :private) }
let(:snippet_visibility) { :private }
let(:current_user) { create(:admin) }
it do
expect_allowed(:read_project_snippet, :create_note)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment