File: mirrors_controller_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 (315 lines) | stat: -rw-r--r-- 10,727 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::MirrorsController, feature_category: :source_code_management do
  include ReactiveCachingHelpers

  shared_examples 'only admin is allowed when mirroring is disabled' do
    let(:subject_action) { raise 'subject_action is required' }
    let(:user) { project.first_owner }
    let(:project_settings_path) { project_settings_repository_path(project, anchor: 'js-push-remote-settings') }

    context 'when project mirroring is enabled' do
      it 'allows requests from a maintainer' do
        sign_in(user)

        subject_action
        expect(response).to redirect_to(project_settings_path)
      end

      it 'allows requests from an admin user' do
        user.update!(admin: true)
        sign_in(user)

        subject_action
        expect(response).to redirect_to(project_settings_path)
      end
    end

    context 'when project mirroring is disabled' do
      before do
        stub_application_setting(mirror_available: false)
      end

      it 'disallows requests from a maintainer' do
        sign_in(user)

        subject_action
        expect(response).to have_gitlab_http_status(:not_found)
      end

      context 'when admin mode is enabled', :enable_admin_mode do
        it 'allows requests from an admin user' do
          user.update!(admin: true)
          sign_in(user)

          subject_action
          expect(response).to redirect_to(project_settings_path)
        end
      end

      context 'when admin mode is disabled' do
        it 'disallows requests from an admin user' do
          user.update!(admin: true)
          sign_in(user)

          subject_action
          expect(response).to have_gitlab_http_status(:not_found)
        end
      end
    end
  end

  describe 'Access control' do
    let(:project) { create(:project, :repository) }

    describe '#update' do
      include_examples 'only admin is allowed when mirroring is disabled' do
        let(:subject_action) do
          do_put(project, remote_mirrors_attributes: { '0' => { 'enabled' => 1, 'url' => 'http://foo.com' } })
        end
      end
    end

    describe '#update_now' do
      include_examples 'only admin is allowed when mirroring is disabled' do
        let(:options) { { namespace_id: project.namespace, project_id: project } }

        let(:subject_action) do
          get :update_now, params: options.merge(sync_remote: true)
        end
      end
    end
  end

  describe 'setting up a remote mirror' do
    let_it_be(:project) { create(:project, :repository) }

    context 'when the current project is not a mirror' do
      it 'allows to create a remote mirror' do
        sign_in(project.first_owner)

        expect do
          do_put(project, remote_mirrors_attributes: { '0' => { 'enabled' => 1, 'url' => 'http://foo.com' } })
        end.to change { RemoteMirror.count }.to(1)
      end
    end

    context 'setting up SSH public-key authentication' do
      let(:ssh_mirror_attributes) do
        {
          'auth_method' => 'ssh_public_key',
          'url' => 'ssh://git@example.com',
          'ssh_known_hosts' => 'test'
        }
      end

      it 'processes a successful update' do
        sign_in(project.first_owner)
        do_put(project, remote_mirrors_attributes: { '0' => ssh_mirror_attributes })

        expect(response).to redirect_to(project_settings_repository_path(project, anchor: 'js-push-remote-settings'))

        expect(RemoteMirror.count).to eq(1)
        expect(RemoteMirror.first).to have_attributes(
          auth_method: 'ssh_public_key',
          url: 'ssh://git@example.com',
          ssh_public_key: match(/\Assh-rsa /),
          ssh_known_hosts: 'test'
        )
      end
    end
  end

  describe '#update' do
    let(:project) { create(:project, :repository, :remote_mirror) }

    before do
      sign_in(project.first_owner)
    end

    context 'With valid URL for a push' do
      let(:remote_mirror_attributes) do
        { "0" => { "enabled" => "0", url: 'https://updated.example.com' } }
      end

      it 'processes a successful update' do
        do_put(project, remote_mirrors_attributes: remote_mirror_attributes)

        expect(response).to redirect_to(project_settings_repository_path(project, anchor: 'js-push-remote-settings'))
        expect(flash[:notice]).to match(/successfully updated/)
      end

      it 'creates a RemoteMirror object' do
        expect { do_put(project, remote_mirrors_attributes: remote_mirror_attributes) }.to change(RemoteMirror, :count).by(1)
      end

      context 'with json format' do
        it 'processes a successful update' do
          do_put(project, { remote_mirrors_attributes: remote_mirror_attributes }, { format: :json })

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to include(
            'id' => project.id,
            'remote_mirrors_attributes' => a_kind_of(Array)
          )
        end
      end
    end

    context 'With invalid URL for a push' do
      let(:remote_mirror_attributes) do
        { "0" => { "enabled" => "0", url: 'ftp://invalid.invalid' } }
      end

      it 'processes an unsuccessful update' do
        do_put(project, remote_mirrors_attributes: remote_mirror_attributes)

        expect(response).to redirect_to(project_settings_repository_path(project, anchor: 'js-push-remote-settings'))
        expect(flash[:alert]).to match(/Only allowed schemes are/)
      end

      it 'does not create a RemoteMirror object' do
        expect { do_put(project, remote_mirrors_attributes: remote_mirror_attributes) }.not_to change(RemoteMirror, :count)
      end

      context 'when service returns an error' do
        before do
          allow(::RemoteMirrors::CreateService).to receive(:new).and_return(
            instance_double('RemoteMirrors::CreateService', execute: ServiceResponse.error(message: 'ServiceError'))
          )
        end

        it 'processes an unsuccessful update' do
          do_put(project, remote_mirrors_attributes: remote_mirror_attributes)

          expect(response).to redirect_to(project_settings_repository_path(project, anchor: 'js-push-remote-settings'))
          expect(flash[:alert]).to eq('ServiceError')
        end
      end

      context 'with json format' do
        it 'processes an unsuccessful update' do
          do_put(project, { remote_mirrors_attributes: remote_mirror_attributes }, { format: :json })

          expect(response).to have_gitlab_http_status(:unprocessable_entity)
          expect(json_response['url']).to include(/Only allowed schemes are/)
        end
      end
    end

    context 'when user deletes the remote mirror' do
      let(:remote_mirror_attributes) { { id: mirror_id, _destroy: 1 } }
      let(:mirror_id) { project.remote_mirrors.first.id }

      it 'processes a successful delete' do
        expect { do_put(project, remote_mirrors_attributes: remote_mirror_attributes) }.to change(RemoteMirror, :count).by(-1)

        expect(response).to redirect_to(project_settings_repository_path(project, anchor: 'js-push-remote-settings'))
        expect(flash[:notice]).to match(/successfully updated/)
      end

      context 'when mirror id is not found' do
        let(:mirror_id) { non_existing_record_id }

        it 'returns a 404 error' do
          expect { do_put(project, remote_mirrors_attributes: remote_mirror_attributes) }.not_to change(RemoteMirror, :count)

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end
    end

    context 'when request is invalid' do
      context 'with html format' do
        it 'returns an error' do
          do_put(project, { wrong: :params })

          expect(response).to redirect_to(project_settings_repository_path(project, anchor: 'js-push-remote-settings'))
          expect(flash[:alert]).to match(/Invalid mirror update request/)
        end
      end

      context 'with json format' do
        it 'processes an unsuccessful update' do
          do_put(project, { wrong: :params }, { format: :json })

          expect(response).to have_gitlab_http_status(:bad_request)
          expect(json_response).to eq('error' => 'Invalid mirror update request')
        end
      end
    end
  end

  describe '#ssh_host_keys', :use_clean_rails_memory_store_caching do
    let(:project) { create(:project) }
    let(:cache) { SshHostKey.new(project: project, url: "ssh://example.com:22") }

    before do
      sign_in(project.first_owner)
    end

    context 'invalid URLs' do
      %w[
        INVALID
        git@example.com:foo/bar.git
        ssh://git@example.com:foo/bar.git
        ssh://127.0.0.1/foo/bar.git
      ].each do |url|
        it "returns an error with a 400 response for URL #{url.inspect}" do
          do_get(project, url)

          expect(response).to have_gitlab_http_status(:bad_request)
          expect(json_response).to eq('message' => 'Invalid URL')
        end
      end
    end

    context 'no data in cache' do
      it 'requests the cache to be filled and returns a 204 response' do
        expect(ExternalServiceReactiveCachingWorker).to receive(:perform_async).with(cache.class, cache.id).at_least(:once)

        do_get(project)

        expect(response).to have_gitlab_http_status(:no_content)
      end
    end

    context 'error in the cache' do
      it 'returns the error with a 400 response' do
        stub_reactive_cache(cache, error: 'An error')

        do_get(project)

        expect(response).to have_gitlab_http_status(:bad_request)
        expect(json_response).to eq('message' => 'An error')
      end
    end

    context 'data in the cache' do
      let(:ssh_key) { 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf' }
      let(:ssh_fp) { { type: 'ed25519', bits: 256, fingerprint: '2e:65:6a:c8:cf:bf:b2:8b:9a:bd:6d:9f:11:5c:12:16', fingerprint_sha256: 'SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8', index: 0 } }

      it 'returns the data with a 200 response' do
        stub_reactive_cache(cache, known_hosts: ssh_key)

        do_get(project)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to eq('known_hosts' => ssh_key, 'fingerprints' => [ssh_fp.stringify_keys], 'host_keys_changed' => true)
      end
    end

    def do_get(project, url = 'ssh://example.com')
      get :ssh_host_keys, params: { namespace_id: project.namespace, project_id: project, ssh_url: url }
    end
  end

  def do_put(project, options, extra_attrs = {})
    attrs = extra_attrs.merge(namespace_id: project.namespace.to_param, project_id: project.to_param)
    attrs[:project] = options

    put :update, params: attrs
  end
end