summaryrefslogtreecommitdiff
path: root/spec/workers/packages/debian/process_changes_worker_spec.rb
blob: b96b75e93b9e91517e3e97bb064a89363023ea27 (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
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Debian::ProcessChangesWorker, type: :worker, feature_category: :package_registry do
  let_it_be(:user) { create(:user) }
  let_it_be_with_reload(:distribution) do
    create(:debian_project_distribution, :with_file, codename: FFaker::Lorem.word, suite: 'unstable')
  end

  let(:incoming) { create(:debian_incoming, project: distribution.project) }
  let(:package_file) { incoming.package_files.with_file_name('sample_1.2.3~alpha2_amd64.changes').first }
  let(:worker) { described_class.new }

  describe '#perform' do
    let(:package_file_id) { package_file.id }
    let(:user_id) { user.id }

    subject { worker.perform(package_file_id, user_id) }

    context 'with mocked service' do
      it 'calls ProcessChangesService' do
        expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
        expect_next_instance_of(::Packages::Debian::ProcessChangesService) do |service|
          expect(service).to receive(:execute)
            .with(no_args)
        end

        subject
      end
    end

    context 'with non existing package file' do
      let(:package_file_id) { non_existing_record_id }

      it 'returns early without error' do
        expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
        expect(::Packages::Debian::ProcessChangesService).not_to receive(:new)

        subject
      end
    end

    context 'with nil package file id' do
      let(:package_file_id) { nil }

      it 'returns early without error' do
        expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
        expect(::Packages::Debian::ProcessChangesService).not_to receive(:new)

        subject
      end
    end

    context 'with non existing user' do
      let(:user_id) { non_existing_record_id }

      it 'returns early without error' do
        expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
        expect(::Packages::Debian::ProcessChangesService).not_to receive(:new)

        subject
      end
    end

    context 'with nil user id' do
      let(:user_id) { nil }

      it 'returns early without error' do
        expect(Gitlab::ErrorTracking).not_to receive(:log_exception)
        expect(::Packages::Debian::ProcessChangesService).not_to receive(:new)

        subject
      end
    end

    context 'without a distribution' do
      before do
        distribution.destroy!
      end

      it 'removes package file and log exception', :aggregate_failures do
        expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
          instance_of(ActiveRecord::RecordNotFound),
          package_file_id: package_file_id,
          user_id: user_id
        )
        expect { subject }
          .to not_change { Packages::Package.count }
          .and change { Packages::PackageFile.count }.by(-1)
          .and change { incoming.package_files.count }.from(7).to(6)
      end
    end

    context 'when the service raises an error' do
      let(:package_file) { incoming.package_files.first }

      it 'removes package file and log exception', :aggregate_failures do
        expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
          instance_of(Packages::Debian::ExtractChangesMetadataService::ExtractionError),
          package_file_id: package_file_id,
          user_id: user_id
        )
        expect { subject }
          .to not_change { Packages::Package.count }
          .and change { Packages::PackageFile.count }.by(-1)
          .and change { incoming.package_files.count }.from(7).to(6)

        expect { package_file.reload }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end

    it_behaves_like 'an idempotent worker' do
      let(:job_args) { [package_file.id, user.id] }

      it 'sets the Debian file type as changes', :aggregate_failures do
        expect(Gitlab::ErrorTracking).not_to receive(:log_exception)

        # Using subject inside this block will process the job multiple times
        expect { subject }
          .to change { Packages::Package.count }.from(1).to(2)
          .and not_change { Packages::PackageFile.count }
          .and change { incoming.package_files.count }.from(7).to(0)
          .and change { package_file&.debian_file_metadatum&.reload&.file_type }.from('unknown').to('changes')

        created_package = Packages::Package.last
        expect(created_package.name).to eq 'sample'
        expect(created_package.version).to eq '1.2.3~alpha2'
        expect(created_package.creator).to eq user
      end
    end
  end
end