summaryrefslogtreecommitdiff
path: root/spec/services/ci/job_artifacts/bulk_delete_by_project_service_spec.rb
blob: a180837f9a9961d41625cb2425656447b8be78bc (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Ci::JobArtifacts::BulkDeleteByProjectService, "#execute", feature_category: :build_artifacts do
  subject(:execute) do
    described_class.new(
      job_artifact_ids: job_artifact_ids,
      current_user: current_user,
      project: project).execute
  end

  let_it_be(:current_user) { create(:user) }
  let_it_be(:build, reload: true) do
    create(:ci_build, :artifacts, :trace_artifact, user: current_user)
  end

  let_it_be(:project) { build.project }
  let_it_be(:job_artifact_ids) { build.job_artifacts.map(&:id) }

  describe '#execute' do
    context 'when number of artifacts exceeds limits to delete' do
      let_it_be(:second_build, reload: true) do
        create(:ci_build, :artifacts, :trace_artifact, user: current_user, project: project)
      end

      let_it_be(:job_artifact_ids) { ::Ci::JobArtifact.all.map(&:id) }

      before do
        project.add_maintainer(current_user)
        stub_const("#{described_class}::JOB_ARTIFACTS_COUNT_LIMIT", 1)
      end

      it 'fails to destroy' do
        result = execute

        expect(result).to be_error
        expect(result[:message]).to eq('Can only delete up to 1 job artifacts per call')
      end
    end

    context 'when requested not existing artifacts do delete' do
      let_it_be(:deleted_build, reload: true) do
        create(:ci_build, :artifacts, :trace_artifact, user: current_user, project: project)
      end

      let_it_be(:deleted_job_artifacts) { deleted_build.job_artifacts }
      let_it_be(:job_artifact_ids) { ::Ci::JobArtifact.all.map(&:id) }

      before do
        project.add_maintainer(current_user)
        deleted_job_artifacts.each(&:destroy!)
      end

      it 'fails to destroy' do
        result = execute

        expect(result).to be_error
        expect(result[:message]).to eq("Artifacts (#{deleted_job_artifacts.map(&:id).join(',')}) not found")
      end
    end

    context 'when maintainer has access to the project' do
      before do
        project.add_maintainer(current_user)
      end

      it 'is successful' do
        result = execute

        expect(result).to be_success
        expect(result.payload).to eq(
          {
            destroyed_count: job_artifact_ids.count,
            destroyed_ids: job_artifact_ids,
            errors: []
          }
        )
        expect(::Ci::JobArtifact.where(id: job_artifact_ids).count).to eq(0)
      end

      context 'and partially owns artifacts' do
        let_it_be(:orphan_artifact) { create(:ci_job_artifact, :archive) }
        let_it_be(:orphan_artifact_id) { orphan_artifact.id }
        let_it_be(:owned_artifacts_ids) { build.job_artifacts.erasable.map(&:id) }
        let_it_be(:job_artifact_ids) { [orphan_artifact_id] + owned_artifacts_ids }

        it 'fails to destroy' do
          result = execute

          expect(result).to be_error
          expect(result[:message]).to be('Not all artifacts belong to requested project')
          expect(::Ci::JobArtifact.where(id: job_artifact_ids).count).to eq(3)
        end
      end

      context 'and request all artifacts from a different project' do
        let_it_be(:different_project_artifact) { create(:ci_job_artifact, :archive) }
        let_it_be(:job_artifact_ids) { [different_project_artifact] }

        let_it_be(:different_build, reload: true) do
          create(:ci_build, :artifacts, :trace_artifact, user: current_user)
        end

        let_it_be(:different_project) { different_build.project }

        before do
          different_project.add_maintainer(current_user)
        end

        it 'returns a error' do
          result = execute

          expect(result).to be_error
          expect(result[:message]).to be('Not all artifacts belong to requested project')
          expect(::Ci::JobArtifact.where(id: job_artifact_ids).count).to eq(job_artifact_ids.count)
        end
      end
    end
  end
end