2021-09-04 01:27:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Mutations::Ci::Runner::Delete do
|
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
let_it_be(:runner) { create(:ci_runner) }
|
2022-05-07 20:08:51 +05:30
|
|
|
let_it_be(:admin_user) { create(:user, :admin) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
let(:current_ctx) { { current_user: user } }
|
|
|
|
|
|
|
|
let(:mutation_params) do
|
2022-04-04 11:22:00 +05:30
|
|
|
{ id: runner.to_global_id }
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
specify { expect(described_class).to require_graphql_authorizations(:delete_runner) }
|
|
|
|
|
|
|
|
describe '#resolve' do
|
|
|
|
subject do
|
|
|
|
sync(resolve(described_class, args: mutation_params, ctx: current_ctx))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user cannot admin the runner' do
|
2022-05-07 20:08:51 +05:30
|
|
|
it 'generates an error' do
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with more than one associated project' do
|
|
|
|
let!(:project) { create(:project, creator_id: user.id) }
|
|
|
|
let!(:project2) { create(:project, creator_id: user.id) }
|
|
|
|
let!(:two_projects_runner) { create(:ci_runner, :project, description: 'Two projects runner', projects: [project, project2]) }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
mutation_params[:id] = two_projects_runner.to_global_id
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
|
|
|
|
subject
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when required arguments are missing' do
|
|
|
|
let(:mutation_params) { {} }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
2021-10-27 15:23:28 +05:30
|
|
|
expect { subject }.to raise_error(ArgumentError, "Arguments must be provided: id")
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user can delete owned runner' do
|
2022-05-07 20:08:51 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
let!(:project) { create(:project, creator_id: user.id) }
|
|
|
|
let!(:project_runner) { create(:ci_runner, :project, description: 'Project runner', projects: [project]) }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
before do
|
2021-09-04 01:27:46 +05:30
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with one associated project' do
|
2022-05-07 20:08:51 +05:30
|
|
|
let(:mutation_params) do
|
|
|
|
{ id: project_runner.to_global_id }
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
it 'deletes runner' do
|
|
|
|
expect_next_instance_of(::Ci::Runners::UnregisterRunnerService, project_runner, current_ctx[:current_user]) do |service|
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(service).to receive(:execute).once.and_call_original
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
expect { subject }.to change { Ci::Runner.count }.by(-1)
|
|
|
|
expect(subject[:errors]).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with more than one associated project' do
|
2022-03-02 08:16:31 +05:30
|
|
|
let!(:project2) { create(:project, creator_id: user.id) }
|
|
|
|
let!(:two_projects_runner) { create(:ci_runner, :project, description: 'Two projects runner', projects: [project, project2]) }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
let(:mutation_params) do
|
|
|
|
{ id: two_projects_runner.to_global_id }
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
context 'with user as admin', :enable_admin_mode do
|
|
|
|
let(:current_ctx) { { current_user: admin_user } }
|
|
|
|
|
|
|
|
it 'deletes runner' do
|
|
|
|
expect_next_instance_of(::Ci::Runners::UnregisterRunnerService, two_projects_runner, current_ctx[:current_user]) do |service|
|
|
|
|
expect(service).to receive(:execute).once.and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject }.to change { Ci::Runner.count }.by(-1)
|
|
|
|
expect(subject[:errors]).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with user as project maintainer' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project2.add_maintainer(user)
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
it 'raises error' do
|
|
|
|
allow_next_instance_of(::Ci::Runners::UnregisterRunnerService) do |service|
|
|
|
|
expect(service).not_to receive(:execute)
|
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
|
|
|
|
subject
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when admin can delete runner', :enable_admin_mode do
|
|
|
|
let(:current_ctx) { { current_user: admin_user } }
|
|
|
|
|
|
|
|
it 'deletes runner' do
|
2022-05-07 20:08:51 +05:30
|
|
|
expect_next_instance_of(::Ci::Runners::UnregisterRunnerService, runner, current_ctx[:current_user]) do |service|
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(service).to receive(:execute).once.and_call_original
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
expect { subject }.to change { Ci::Runner.count }.by(-1)
|
|
|
|
expect(subject[:errors]).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|