summaryrefslogtreecommitdiff
path: root/spec/services/terraform/states/trigger_destroy_service_spec.rb
blob: 2e96331779cac37380d584b6623f834a6453210c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Terraform::States::TriggerDestroyService do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user, maintainer_projects: [project]) }

  describe '#execute', :aggregate_failures do
    let_it_be(:state) { create(:terraform_state, project: project) }

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

    it 'marks the state as deleted and schedules a cleanup worker' do
      expect(Terraform::States::DestroyWorker).to receive(:perform_async).with(state.id).once

      expect(subject).to be_success
      expect(state.deleted_at).to be_like_time(Time.current)
    end

    shared_examples 'unable to delete state' do
      it 'does not modify the state' do
        expect(Terraform::States::DestroyWorker).not_to receive(:perform_async)

        expect { subject }.not_to change(state, :deleted_at)
        expect(subject).to be_error
        expect(subject.message).to eq(message)
      end
    end

    context 'user does not have permission' do
      let(:user) { create(:user, developer_projects: [project]) }
      let(:message) { 'You have insufficient permissions to delete this state' }

      include_examples 'unable to delete state'
    end

    context 'state is locked' do
      let(:state) { create(:terraform_state, :locked, project: project) }
      let(:message) { 'Cannot remove a locked state' }

      include_examples 'unable to delete state'
    end
  end
end