File: catalog_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (138 lines) | stat: -rw-r--r-- 4,326 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::Ci::Catalog, feature_category: :pipeline_composition do
  include HttpBasicAuthHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :public, :catalog_resource_with_components) }
  let_it_be(:catalog_resource) { create(:ci_catalog_resource, project: project) }
  let_it_be(:release) do
    create(:release, project: project, sha: project.repository.root_ref_sha, author: user)
  end

  let(:metadata) do
    {
      components: [
        { name: 'hello-component', spec: { inputs: { hello: nil } }, component_type: 'template' },
        { name: 'world-component', spec: { inputs: { world: { default: 'abc' } } }, component_type: 'template' }
      ]
    }
  end

  describe 'POST /projects/:id/catalog/publish' do
    let(:url) { "/projects/#{project.id}/catalog/publish" }

    subject(:publish) do
      post api(url, user), params: { version: release.tag, metadata: metadata }
    end

    context 'when the project does not exist' do
      let(:url) { "/projects/invalid-path/catalog/publish" }

      it 'returns a 404 response' do
        publish

        expect(response).to have_gitlab_http_status(:not_found)
        expect(json_response['message']).to eq('404 Project Not Found')
      end
    end

    context 'when the user is not authorized to project' do
      it 'returns a 403 response' do
        publish

        expect(response).to have_gitlab_http_status(:forbidden)
        expect(json_response['message']).to eq('403 Forbidden')
      end
    end

    context 'when the user is not authorized to update the release' do
      before_all do
        project.add_guest(user)
      end

      it 'returns a 403 response' do
        publish

        expect(response).to have_gitlab_http_status(:forbidden)
        expect(json_response['message']).to eq('403 Forbidden')
      end
    end

    context 'when the user is authorized as developer' do
      before_all do
        project.add_developer(user)
      end

      it 'returns a success response' do
        publish

        expect(response).to have_gitlab_http_status(:created)

        expect(json_response['catalog_url']).to eq(
          "http://localhost/explore/catalog/#{project.full_path}"
        )
      end

      it 'publishes the release to the catalog' do
        expect do
          expect do
            publish
          end.to change { project.catalog_resource_versions.count }.by(1)
        end.to change { project.ci_components.count }.by(2)

        release = project.releases.last
        version = project.catalog_resource_versions.last
        components = project.ci_components.last(2)

        expect(version.release).to eq(release)
        expect(version.name).to eq(release.tag)
        expect(components.map(&:name)).to match_array(%w[hello-component world-component])
        expect(components.map(&:spec)).to match_array(
          [
            { 'inputs' => { 'hello' => nil } },
            { 'inputs' => { 'world' => { 'default' => 'abc' } } }
          ]
        )
        expect(components.map(&:component_type)).to all(eq('template'))
      end

      context 'when the release was already published' do
        before do
          post api(url, user), params: { version: release.tag, metadata: metadata }
        end

        it 'returns an error response' do
          publish

          expect(response).to have_gitlab_http_status(:unprocessable_entity)
          expect(json_response['message']).to eq('Release has already been published')
        end
      end

      context 'when the release author is different' do
        let(:release) { create(:release, project: project, sha: project.repository.root_ref_sha) }

        it 'returns a 403 response' do
          publish

          expect(response).to have_gitlab_http_status(:unprocessable_entity)
          expect(json_response['message']).to eq('Published by must be the same as the release author')
        end
      end

      context 'when the release does not exist' do
        let(:release) { create(:release) }

        it 'returns a 404 response' do
          publish

          expect(response).to have_gitlab_http_status(:not_found)
          expect(json_response['message']).to eq('404 Not found')
        end
      end
    end
  end
end