Commit 7fc9242c authored by Jarka Košanová's avatar Jarka Košanová

Merge branch 'al-214420-pass-hard-delete-on-snippets-destroy' into 'master'

Pass hard delete option to snippets bulk destroy

See merge request gitlab-org/gitlab!33520
parents 951b1aaf 8c8728cf
...@@ -14,10 +14,10 @@ module Snippets ...@@ -14,10 +14,10 @@ module Snippets
@snippets = snippets @snippets = snippets
end end
def execute def execute(options = {})
return ServiceResponse.success(message: 'No snippets found.') if snippets.empty? return ServiceResponse.success(message: 'No snippets found.') if snippets.empty?
user_can_delete_snippets! user_can_delete_snippets! unless options[:hard_delete]
attempt_delete_repositories! attempt_delete_repositories!
snippets.destroy_all # rubocop: disable Cop/DestroyAll snippets.destroy_all # rubocop: disable Cop/DestroyAll
......
...@@ -56,7 +56,7 @@ module Users ...@@ -56,7 +56,7 @@ module Users
MigrateToGhostUserService.new(user).execute unless options[:hard_delete] MigrateToGhostUserService.new(user).execute unless options[:hard_delete]
response = Snippets::BulkDestroyService.new(current_user, user.snippets).execute response = Snippets::BulkDestroyService.new(current_user, user.snippets).execute(options)
raise DestroyError, response.message if response.error? raise DestroyError, response.message if response.error?
# Rails attempts to load all related records into memory before # Rails attempts to load all related records into memory before
......
---
title: Pass hard delete option to snippets bulk destroy
merge_request: 33520
author:
type: fixed
...@@ -69,6 +69,18 @@ describe Snippets::BulkDestroyService do ...@@ -69,6 +69,18 @@ describe Snippets::BulkDestroyService do
it_behaves_like 'error is raised' do it_behaves_like 'error is raised' do
let(:error_message) { "You don't have access to delete these snippets." } let(:error_message) { "You don't have access to delete these snippets." }
end end
context 'when hard_delete option is passed' do
subject { described_class.new(service_user, snippets).execute(hard_delete: true) }
it 'returns a ServiceResponse success response' do
expect(subject).to be_success
end
it 'deletes all the snippets that belong to the user' do
expect { subject }.to change(Snippet, :count).by(-2)
end
end
end end
context 'when an error is raised deleting the repository' do context 'when an error is raised deleting the repository' do
......
...@@ -67,6 +67,18 @@ describe Users::DestroyService do ...@@ -67,6 +67,18 @@ describe Users::DestroyService do
end end
end end
it 'calls the bulk snippet destroy service with hard delete option if it is present' do
# this avoids getting into Projects::DestroyService as it would
# call Snippets::BulkDestroyService first!
allow(user).to receive(:personal_projects).and_return([])
expect_next_instance_of(Snippets::BulkDestroyService) do |bulk_destroy_service|
expect(bulk_destroy_service).to receive(:execute).with(hard_delete: true).and_call_original
end
service.execute(user, hard_delete: true)
end
it 'does not delete project snippets that the user is the author of' do it 'does not delete project snippets that the user is the author of' do
repo = create(:project_snippet, :repository, author: user).snippet_repository repo = create(:project_snippet, :repository, author: user).snippet_repository
service.execute(user) service.execute(user)
......
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