summaryrefslogtreecommitdiff
path: root/spec/services/packages/composer/create_package_service_spec.rb
blob: 2ffd0a269f20cf38d45c3fc4fa333b6eafbddc0e (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Packages::Composer::CreatePackageService do
  include PackagesManagerApiSpecHelpers

  let_it_be(:package_name) { 'composer-package-name' }
  let_it_be(:json) { { name: package_name }.to_json }
  let_it_be(:project) { create(:project, :custom_repo, files: { 'composer.json' => json }) }
  let_it_be(:user) { create(:user) }

  let(:params) do
    {
      branch: branch,
      tag: tag
    }
  end

  describe '#execute' do
    let(:tag) { nil }
    let(:branch) { nil }

    subject { described_class.new(project, user, params).execute }

    let(:created_package) { Packages::Package.composer.last }

    shared_examples 'using the cache update worker' do
      context 'with remove_composer_v1_cache_code enabled' do
        it 'does not enqueue a cache update job' do
          expect(::Packages::Composer::CacheUpdateWorker).not_to receive(:perform_async)

          subject
        end
      end

      context 'with remove_composer_v1_cache_code disabled' do
        it 'enqueues a cache update job' do
          stub_feature_flags(remove_composer_v1_cache_code: true)
          expect(::Packages::Composer::CacheUpdateWorker).not_to receive(:perform_async)

          subject
        end
      end
    end

    context 'without an existing package' do
      context 'with a branch' do
        let(:branch) { project.repository.find_branch('master') }

        it 'creates the package' do
          expect { subject }
            .to change { Packages::Package.composer.count }.by(1)
            .and change { Packages::Composer::Metadatum.count }.by(1)

          expect(created_package.name).to eq package_name
          expect(created_package.version).to eq 'dev-master'
          expect(created_package.composer_metadatum.target_sha).to eq branch.target
          expect(created_package.composer_metadatum.composer_json.to_json).to eq json
        end

        it_behaves_like 'assigns the package creator' do
          let(:package) { created_package }
        end

        it_behaves_like 'assigns build to package'
        it_behaves_like 'assigns status to package'
        it_behaves_like 'using the cache update worker'
      end

      context 'with a tag' do
        let(:tag) { project.repository.find_tag('v1.2.3') }

        before(:all) do
          project.repository.add_tag(user, 'v1.2.3', 'master')
        end

        it 'creates the package' do
          expect { subject }
            .to change { Packages::Package.composer.count }.by(1)
            .and change { Packages::Composer::Metadatum.count }.by(1)

          expect(created_package.name).to eq package_name
          expect(created_package.version).to eq '1.2.3'
        end

        it_behaves_like 'assigns the package creator' do
          let(:package) { created_package }
        end

        it_behaves_like 'assigns build to package'
        it_behaves_like 'assigns status to package'
        it_behaves_like 'using the cache update worker'
      end
    end

    context 'with an existing package' do
      let(:branch) { project.repository.find_branch('master') }

      context 'belonging to the same project' do
        before do
          described_class.new(project, user, params).execute
        end

        it 'does not create a new package' do
          expect { subject }
            .to change { Packages::Package.composer.count }.by(0)
            .and change { Packages::Composer::Metadatum.count }.by(0)
        end

        it_behaves_like 'using the cache update worker'
      end

      context 'belonging to another project' do
        let(:other_project) { create(:project) }
        let!(:other_package) { create(:composer_package, name: package_name, version: 'dev-master', project: other_project) }

        it 'fails with an error' do
          expect { subject }
            .to raise_error(/is already taken/)
        end
      end

      context 'same name but of different type' do
        let(:other_project) { create(:project) }
        let!(:other_package) { create(:package, name: package_name, version: 'dev-master', project: other_project) }

        it 'creates the package' do
          expect { subject }
            .to change { Packages::Package.composer.count }.by(1)
            .and change { Packages::Composer::Metadatum.count }.by(1)
        end

        it_behaves_like 'using the cache update worker'
      end
    end
  end
end