summaryrefslogtreecommitdiff
path: root/spec/services/packages/debian/create_package_file_service_spec.rb
blob: 291f6df991cedc2621515e726542a7d8deddd213 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Debian::CreatePackageFileService do
  include WorkhorseHelpers

  let_it_be(:package) { create(:debian_incoming, without_package_files: true) }
  let_it_be(:current_user) { create(:user) }

  describe '#execute' do
    let(:file_name) { 'libsample0_1.2.3~alpha2_amd64.deb' }
    let(:fixture_path) { "spec/fixtures/packages/debian/#{file_name}" }

    let(:params) do
      {
        file: file,
        file_name: file_name,
        file_sha1: '54321',
        file_md5: '12345'
      }.with_indifferent_access
    end

    let(:service) { described_class.new(package: package, current_user: current_user, params: params) }

    subject(:package_file) { service.execute }

    shared_examples 'a valid deb' do
      it 'creates a new package file', :aggregate_failures do
        expect(::Packages::Debian::ProcessChangesWorker).not_to receive(:perform_async)
        expect(package_file).to be_valid
        expect(package_file.file.read).to start_with('!<arch>')
        expect(package_file.size).to eq(1124)
        expect(package_file.file_name).to eq(file_name)
        expect(package_file.file_sha1).to eq('54321')
        expect(package_file.file_sha256).to eq('543212345')
        expect(package_file.file_md5).to eq('12345')
        expect(package_file.debian_file_metadatum).to be_valid
        expect(package_file.debian_file_metadatum.file_type).to eq('unknown')
        expect(package_file.debian_file_metadatum.architecture).to be_nil
        expect(package_file.debian_file_metadatum.fields).to be_nil
      end
    end

    shared_examples 'a valid changes' do
      it 'creates a new package file', :aggregate_failures do
        expect(::Packages::Debian::ProcessChangesWorker).to receive(:perform_async)

        expect(package_file).to be_valid
        expect(package_file.file.read).to start_with('Format: 1.8')
        expect(package_file.size).to eq(2143)
        expect(package_file.file_name).to eq(file_name)
        expect(package_file.file_sha1).to eq('54321')
        expect(package_file.file_sha256).to eq('543212345')
        expect(package_file.file_md5).to eq('12345')
        expect(package_file.debian_file_metadatum).to be_valid
        expect(package_file.debian_file_metadatum.file_type).to eq('unknown')
        expect(package_file.debian_file_metadatum.architecture).to be_nil
        expect(package_file.debian_file_metadatum.fields).to be_nil
      end
    end

    context 'with temp file' do
      let!(:file) do
        upload_path = ::Packages::PackageFileUploader.workhorse_local_upload_path
        file_path = upload_path + '/' + file_name

        FileUtils.mkdir_p(upload_path)
        File.write(file_path, File.read(fixture_path))

        UploadedFile.new(file_path, filename: File.basename(file_path), sha256: '543212345')
      end

      it_behaves_like 'a valid deb'

      context 'with a .changes file' do
        let(:file_name) { 'sample_1.2.3~alpha2_amd64.changes' }
        let(:fixture_path) { "spec/fixtures/packages/debian/#{file_name}" }

        it_behaves_like 'a valid changes'
      end

      context 'when current_user is missing' do
        let(:current_user) { nil }

        it 'raises an error' do
          expect { package_file }.to raise_error(ArgumentError, 'Invalid user')
        end
      end
    end

    context 'with remote file' do
      let!(:fog_connection) do
        stub_package_file_object_storage(direct_upload: true)
      end

      before do
        allow_next_instance_of(UploadedFile) do |uploaded_file|
          allow(uploaded_file).to receive(:sha256).and_return('543212345')
        end
      end

      let(:tmp_object) do
        fog_connection.directories.new(key: 'packages').files.create( # rubocop:disable Rails/SaveBang
          key: "tmp/uploads/#{file_name}",
          body: File.read(fixture_path)
        )
      end

      let!(:file) { fog_to_uploaded_file(tmp_object) }

      it_behaves_like 'a valid deb'
    end

    context 'when package is missing' do
      let(:package) { nil }
      let(:params) { {} }

      it 'raises an error' do
        expect { package_file }.to raise_error(ArgumentError, 'Invalid package')
      end
    end

    context 'when params is empty' do
      let(:params) { {} }

      it 'raises an error' do
        expect { package_file }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end

    context 'when file is missing' do
      let(:file_name) { 'libsample0_1.2.3~alpha2_amd64.deb' }
      let(:file) { nil }

      it 'raises an error' do
        expect { package_file }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end

    context 'when FIPS mode enabled', :fips_mode do
      let(:file) { nil }

      it 'raises an error' do
        expect { package_file }.to raise_error(::Packages::FIPS::DisabledError)
      end
    end
  end
end