2021-09-30 23:02:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe 'Destroying a package', feature_category: :package_registry do
|
2021-09-30 23:02:18 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
let_it_be_with_reload(:package) { create(:package) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
let(:project) { package.project }
|
|
|
|
let(:id) { package.to_global_id.to_s }
|
|
|
|
|
|
|
|
let(:query) do
|
|
|
|
<<~GQL
|
|
|
|
errors
|
|
|
|
GQL
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:params) { { id: id } }
|
|
|
|
let(:mutation) { graphql_mutation(:destroy_package, params, query) }
|
|
|
|
let(:mutation_response) { graphql_mutation_response(:destroyPackage) }
|
|
|
|
|
|
|
|
shared_examples 'destroying the package' do
|
2022-03-02 08:16:31 +05:30
|
|
|
it 'marks the package as pending destruction' do
|
|
|
|
expect(::Packages::MarkPackageForDestructionService)
|
2021-09-30 23:02:18 +05:30
|
|
|
.to receive(:new).with(container: package, current_user: user).and_call_original
|
2022-03-02 08:16:31 +05:30
|
|
|
expect_next_found_instance_of(::Packages::Package) do |package|
|
|
|
|
expect(package).to receive(:mark_package_files_for_destruction)
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect { mutation_request }
|
|
|
|
.to change { ::Packages::Package.pending_destruction.count }.by(1)
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'returning response status', :success
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'denying the mutation request' do
|
2022-03-02 08:16:31 +05:30
|
|
|
it 'does not mark the package as pending destruction' do
|
|
|
|
expect(::Packages::MarkPackageForDestructionService)
|
2021-09-30 23:02:18 +05:30
|
|
|
.not_to receive(:new).with(container: package, current_user: user)
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect { mutation_request }
|
|
|
|
.to not_change { ::Packages::Package.pending_destruction.count }
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
expect(mutation_response).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'returning response status', :success
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'post graphql mutation' do
|
|
|
|
subject(:mutation_request) { post_graphql_mutation(mutation, current_user: user) }
|
|
|
|
|
|
|
|
context 'with valid id' do
|
|
|
|
where(:user_role, :shared_examples_name) do
|
|
|
|
:maintainer | 'destroying the package'
|
|
|
|
:developer | 'denying the mutation request'
|
|
|
|
:reporter | 'denying the mutation request'
|
|
|
|
:guest | 'denying the mutation request'
|
|
|
|
:anonymous | 'denying the mutation request'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
project.send("add_#{user_role}", user) unless user_role == :anonymous
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like params[:shared_examples_name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid id' do
|
|
|
|
let(:params) { { id: 'gid://gitlab/Packages::Package/5555' } }
|
|
|
|
|
|
|
|
it_behaves_like 'denying the mutation request'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an error occures' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the errors in the response' do
|
|
|
|
allow_next_found_instance_of(::Packages::Package) do |package|
|
2022-03-02 08:16:31 +05:30
|
|
|
allow(package).to receive(:pending_destruction!).and_raise(StandardError)
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
mutation_request
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(mutation_response['errors']).to match_array(['Failed to mark the package as pending destruction'])
|
2022-02-05 19:09:49 +05:30
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|