summaryrefslogtreecommitdiff
path: root/spec/services/packages/debian/sign_distribution_service_spec.rb
blob: 2aec0e506362f411a7f7a8f43a46237beee22e83 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Debian::SignDistributionService do
  let_it_be(:group) { create(:group, :public) }

  let(:content) { FFaker::Lorem.paragraph }
  let(:service) { described_class.new(distribution, content, detach: detach) }

  shared_examples 'Sign Distribution' do |container_type, detach: false|
    context "for #{container_type} detach=#{detach}" do
      let(:detach) { detach }

      if container_type == :group
        let_it_be(:distribution) { create('debian_group_distribution', container: group) }
      else
        let_it_be(:project) { create(:project, group: group) }
        let_it_be(:distribution) { create('debian_project_distribution', container: project) }
      end

      describe '#execute' do
        subject { service.execute }

        context 'without an existing key' do
          it 'raises ArgumentError', :aggregate_failures do
            expect { subject }
              .to raise_error(ArgumentError, 'distribution key is missing')
          end
        end

        context 'with an existing key' do
          let!(:key) { create("debian_#{container_type}_distribution_key", distribution: distribution)}

          it 'returns the content signed', :aggregate_failures do
            expect(Packages::Debian::GenerateDistributionKeyService).not_to receive(:new)

            key_class = "Packages::Debian::#{container_type.capitalize}DistributionKey".constantize

            expect { subject }
              .to not_change { key_class.count }

            if detach
              expect(subject).to start_with("-----BEGIN PGP SIGNATURE-----\n")
            else
              expect(subject).to start_with("-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\n#{content}\n-----BEGIN PGP SIGNATURE-----\n")
            end

            expect(subject).to end_with("\n-----END PGP SIGNATURE-----\n")
          end
        end
      end
    end
  end

  it_behaves_like 'Sign Distribution', :project
  it_behaves_like 'Sign Distribution', :project, detach: true
  it_behaves_like 'Sign Distribution', :group
  it_behaves_like 'Sign Distribution', :group, detach: true
end