summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/requests/api/terraform/modules/v1/packages_shared_examples.rb
blob: 70cc9b1e6b53ed0982a908fdf1ee09e073a123d8 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# frozen_string_literal: true

RSpec.shared_examples 'when package feature is disabled' do
  before do
    stub_config(packages: { enabled: false })
  end

  it_behaves_like 'returning response status', :not_found
end

RSpec.shared_examples 'without authentication' do
  it_behaves_like 'returning response status', :unauthorized
end

RSpec.shared_examples 'with authentication' do
  where(:user_role, :token_header, :token_type, :valid_token, :status) do
    :guest     | 'PRIVATE-TOKEN' | :personal_access_token   | true  | :not_found
    :guest     | 'PRIVATE-TOKEN' | :personal_access_token   | false | :unauthorized
    :guest     | 'DEPLOY-TOKEN'  | :deploy_token            | true  | :not_found
    :guest     | 'DEPLOY-TOKEN'  | :deploy_token            | false | :unauthorized
    :guest     | 'JOB-TOKEN'     | :job_token               | true  | :not_found
    :guest     | 'JOB-TOKEN'     | :job_token               | false | :unauthorized
    :reporter  | 'PRIVATE-TOKEN' | :personal_access_token   | true  | :not_found
    :reporter  | 'PRIVATE-TOKEN' | :personal_access_token   | false | :unauthorized
    :reporter  | 'DEPLOY-TOKEN'  | :deploy_token            | true  | :not_found
    :reporter  | 'DEPLOY-TOKEN'  | :deploy_token            | false | :unauthorized
    :reporter  | 'JOB-TOKEN'     | :job_token               | true  | :not_found
    :reporter  | 'JOB-TOKEN'     | :job_token               | false | :unauthorized
    :developer | 'PRIVATE-TOKEN' | :personal_access_token   | true  | :not_found
    :developer | 'PRIVATE-TOKEN' | :personal_access_token   | false | :unauthorized
    :developer | 'DEPLOY-TOKEN'  | :deploy_token            | true  | :not_found
    :developer | 'DEPLOY-TOKEN'  | :deploy_token            | false | :unauthorized
    :developer | 'JOB-TOKEN'     | :job_token               | true  | :not_found
    :developer | 'JOB-TOKEN'     | :job_token               | false | :unauthorized
  end

  with_them do
    before do
      project.send("add_#{user_role}", user) unless user_role == :anonymous
    end

    let(:token) { valid_token ? tokens[token_type] : 'invalid-token123' }
    let(:headers) { { token_header => token } }

    it_behaves_like 'returning response status', params[:status]
  end
end

RSpec.shared_examples 'an unimplemented route' do
  it_behaves_like 'without authentication'
  it_behaves_like 'with authentication'
  it_behaves_like 'when package feature is disabled'
end

RSpec.shared_examples 'grants terraform module download' do |user_type, status, add_member = true|
  context "for user type #{user_type}" do
    before do
      group.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    it_behaves_like 'returning response status', status

    it 'returns a valid response' do
      subject

      expect(response.headers).to include 'X-Terraform-Get'
    end
  end
end

RSpec.shared_examples 'returns terraform module packages' do |user_type, status, add_member = true|
  context "for user type #{user_type}" do
    before do
      group.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    it_behaves_like 'returning response status', status

    it 'returning a valid response' do
      subject

      expect(json_response).to match_schema('public_api/v4/packages/terraform/modules/v1/versions')
    end
  end
end

RSpec.shared_examples 'returns no terraform module packages' do |user_type, status, add_member = true|
  context "for user type #{user_type}" do
    before do
      group.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    it_behaves_like 'returning response status', status

    it 'returns a response with no versions' do
      subject

      expect(json_response['modules'][0]['versions'].size).to eq(0)
    end
  end
end

RSpec.shared_examples 'grants terraform module packages access' do |user_type, status, add_member = true|
  context "for user type #{user_type}" do
    before do
      project.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    it_behaves_like 'returning response status', status
  end
end

RSpec.shared_examples 'grants terraform module package file access' do |user_type, status, add_member = true|
  context "for user type #{user_type}" do
    before do
      project.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    it_behaves_like 'returning response status', status
    it_behaves_like 'a package tracking event', described_class.name, 'pull_package'
  end
end

RSpec.shared_examples 'rejects terraform module packages access' do |user_type, status, add_member = true|
  context "for user type #{user_type}" do
    before do
      project.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    it_behaves_like 'returning response status', status
  end
end

RSpec.shared_examples 'process terraform module workhorse authorization' do |user_type, status, add_member = true|
  context "for user type #{user_type}" do
    before do
      project.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    it_behaves_like 'returning response status', status

    it 'has the proper content type' do
      subject

      expect(response.media_type).to eq(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE)
    end

    context 'with a request that bypassed gitlab-workhorse' do
      let(:headers) do
        { 'HTTP_PRIVATE_TOKEN' => personal_access_token.token }
          .merge(workhorse_headers)
          .tap { |h| h.delete(Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER) }
      end

      before do
        project.add_maintainer(user)
      end

      it_behaves_like 'returning response status', :forbidden
    end
  end
end

RSpec.shared_examples 'process terraform module upload' do |user_type, status, add_member = true|
  RSpec.shared_examples 'creates terraform module package files' do
    it 'creates package files', :aggregate_failures do
      expect { subject }
          .to change { project.packages.count }.by(1)
          .and change { Packages::PackageFile.count }.by(1)
      expect(response).to have_gitlab_http_status(status)

      package_file = project.packages.last.package_files.reload.last
      expect(package_file.file_name).to eq('mymodule-mysystem-1.0.0.tgz')
    end
  end

  context "for user type #{user_type}" do
    before do
      project.send("add_#{user_type}", user) if add_member && user_type != :anonymous
    end

    context 'with object storage disabled' do
      before do
        stub_package_file_object_storage(enabled: false)
      end

      context 'without a file from workhorse' do
        let(:send_rewritten_field) { false }

        it_behaves_like 'returning response status', :bad_request
      end

      context 'with correct params' do
        it_behaves_like 'package workhorse uploads'
        it_behaves_like 'creates terraform module package files'
        it_behaves_like 'a package tracking event', described_class.name, 'push_package'
      end
    end

    context 'with object storage enabled' do
      let(:tmp_object) do
        fog_connection.directories.new(key: 'packages').files.create( # rubocop:disable Rails/SaveBang
          key: "tmp/uploads/#{file_name}",
          body: 'content'
        )
      end

      let(:fog_file) { fog_to_uploaded_file(tmp_object) }
      let(:params) { { file: fog_file, 'file.remote_id' => file_name } }

      context 'and direct upload enabled' do
        let(:fog_connection) do
          stub_package_file_object_storage(direct_upload: true)
        end

        it_behaves_like 'creates terraform module package files'

        ['123123', '../../123123'].each do |remote_id|
          context "with invalid remote_id: #{remote_id}" do
            let(:params) do
              {
                file: fog_file,
                'file.remote_id' => remote_id
              }
            end

            it_behaves_like 'returning response status', :forbidden
          end
        end
      end

      context 'and direct upload disabled' do
        context 'and background upload disabled' do
          let(:fog_connection) do
            stub_package_file_object_storage(direct_upload: false, background_upload: false)
          end

          it_behaves_like 'creates terraform module package files'
        end

        context 'and background upload enabled' do
          let(:fog_connection) do
            stub_package_file_object_storage(direct_upload: false, background_upload: true)
          end

          it_behaves_like 'creates terraform module package files'
        end
      end
    end
  end
end