summaryrefslogtreecommitdiff
path: root/spec/services/ci/runners/unassign_runner_service_spec.rb
blob: e91d4249473d21e0d59b75d5132b603317a7d2bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Ci::Runners::UnassignRunnerService, '#execute', feature_category: :runner_fleet do
  let_it_be(:project) { create(:project) }
  let_it_be(:runner) { create(:ci_runner, :project, projects: [project]) }

  let(:runner_project) { runner.runner_projects.last }

  subject(:execute) { described_class.new(runner_project, user).execute }

  context 'without user' do
    let(:user) { nil }

    it 'does not destroy runner_project', :aggregate_failures do
      expect(runner_project).not_to receive(:destroy)
      expect { execute }.not_to change { runner.runner_projects.count }.from(1)

      is_expected.to be_error
    end
  end

  context 'with unauthorized user' do
    let(:user) { build(:user) }

    it 'does not call destroy on runner_project' do
      expect(runner).not_to receive(:destroy)

      is_expected.to be_error
    end
  end

  context 'with admin user', :enable_admin_mode do
    let(:user) { create_default(:user, :admin) }

    context 'with destroy returning false' do
      it 'returns error response' do
        expect(runner_project).to receive(:destroy).once.and_return(false)

        is_expected.to be_error
      end
    end

    context 'with destroy returning true' do
      it 'returns success response' do
        expect(runner_project).to receive(:destroy).once.and_return(true)

        is_expected.to be_success
      end
    end
  end
end