Commit acf8e0c7 authored by Josianne Hyson's avatar Josianne Hyson

Refactor Import/Export permission error

The permission error is shared between Group and project  import/export
so move this to the error class so that the functionality can be shared
by the different call sites.
parent 6412234c
...@@ -24,12 +24,7 @@ module Groups ...@@ -24,12 +24,7 @@ module Groups
def validate_user_permissions def validate_user_permissions
unless @current_user.can?(:admin_group, @group) unless @current_user.can?(:admin_group, @group)
@shared.error( @shared.error(::Gitlab::ImportExport::Error.permission_error(@current_user, @group))
::Gitlab::ImportExport::Error.new(
"User with ID: %s does not have permission to Group %s with ID: %s." %
[@current_user.id, @group.name, @group.id]
)
)
notify_error! notify_error!
end end
......
...@@ -52,12 +52,7 @@ module Groups ...@@ -52,12 +52,7 @@ module Groups
if current_user.can?(:admin_group, group) if current_user.can?(:admin_group, group)
true true
else else
@shared.error( @shared.error(::Gitlab::ImportExport::Error.permission_error(current_user, group))
Gitlab::ImportExport::Error.new(
"User with ID: %s does not have permission to Group %s with ID: %s." %
[current_user.id, group.name, group.id]
)
)
false false
end end
......
...@@ -5,9 +5,7 @@ module Projects ...@@ -5,9 +5,7 @@ module Projects
class ExportService < BaseService class ExportService < BaseService
def execute(after_export_strategy = nil, options = {}) def execute(after_export_strategy = nil, options = {})
unless project.template_source? || can?(current_user, :admin_project, project) unless project.template_source? || can?(current_user, :admin_project, project)
raise ::Gitlab::ImportExport::Error.new( raise ::Gitlab::ImportExport::Error.permission_error(current_user, project)
"User with ID: %s does not have permission to Project %s with ID: %s." %
[current_user.id, project.name, project.id])
end end
@shared = project.import_export_shared @shared = project.import_export_shared
......
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
module Gitlab module Gitlab
module ImportExport module ImportExport
Error = Class.new(StandardError) class Error < StandardError
def self.permission_error(user, importable)
self.new(
"User with ID: %s does not have required permissions for %s: %s with ID: %s" %
[user.id, importable.class.name, importable.name, importable.id]
)
end
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::ImportExport::Error do
describe '.permission_error' do
subject(:error) do
described_class.permission_error(user, importable)
end
let(:user) { build(:user, id: 1) }
context 'when supplied a project' do
let(:importable) { build(:project, id: 1, name: 'project1') }
it 'returns an error with the correct message' do
expect(error.message)
.to eq 'User with ID: 1 does not have required permissions for Project: project1 with ID: 1'
end
end
context 'when supplied a group' do
let(:importable) { build(:group, id: 1, name: 'group1') }
it 'returns an error with the correct message' do
expect(error.message)
.to eq 'User with ID: 1 does not have required permissions for Group: group1 with ID: 1'
end
end
end
end
...@@ -39,7 +39,7 @@ describe Groups::ImportExport::ExportService do ...@@ -39,7 +39,7 @@ describe Groups::ImportExport::ExportService do
let(:service) { described_class.new(group: group, user: another_user, params: { shared: shared }) } let(:service) { described_class.new(group: group, user: another_user, params: { shared: shared }) }
let(:expected_message) do let(:expected_message) do
"User with ID: %s does not have permission to Group %s with ID: %s." % "User with ID: %s does not have required permissions for Group: %s with ID: %s" %
[another_user.id, group.name, group.id] [another_user.id, group.name, group.id]
end end
......
...@@ -61,7 +61,7 @@ describe Groups::ImportExport::ImportService do ...@@ -61,7 +61,7 @@ describe Groups::ImportExport::ImportService do
allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared) allow(Gitlab::ImportExport::Shared).to receive(:new).and_return(shared)
expect(shared).to receive(:error) do |param| expect(shared).to receive(:error) do |param|
expect(param.message).to include 'does not have permission to' expect(param.message).to include 'does not have required permissions for'
end end
expect { subject }.to raise_error(Gitlab::ImportExport::Error) expect { subject }.to raise_error(Gitlab::ImportExport::Error)
......
...@@ -164,7 +164,7 @@ describe Projects::ImportExport::ExportService do ...@@ -164,7 +164,7 @@ describe Projects::ImportExport::ExportService do
it 'fails' do it 'fails' do
expected_message = expected_message =
"User with ID: %s does not have permission to Project %s with ID: %s." % "User with ID: %s does not have required permissions for Project: %s with ID: %s" %
[another_user.id, project.name, project.id] [another_user.id, project.name, project.id]
expect { service.execute }.to raise_error(Gitlab::ImportExport::Error).with_message(expected_message) expect { service.execute }.to raise_error(Gitlab::ImportExport::Error).with_message(expected_message)
end end
......
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