File: bulk_imports_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 (574 lines) | stat: -rw-r--r-- 20,477 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Import::BulkImportsController, feature_category: :importers do
  let_it_be(:user) { create(:user) }

  before do
    stub_application_setting(bulk_import_enabled: true)

    sign_in(user)

    allow(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(false)
  end

  context 'when user is signed in' do
    context 'when importing group and projects by direct transfer is enabled' do
      describe 'POST configure' do
        before do
          allow_next_instance_of(BulkImports::Clients::HTTP) do |instance|
            allow(instance).to receive(:validate_instance_version!).and_return(true)
            allow(instance).to receive(:validate_import_scopes!).and_return(true)
          end
        end

        context 'when no params are passed in' do
          it 'clears out existing session' do
            post :configure

            expect(session[:bulk_import_gitlab_access_token]).to be_nil
            expect(session[:bulk_import_gitlab_url]).to be_nil

            expect(response).to have_gitlab_http_status(:found)
            expect(response).to redirect_to(status_import_bulk_imports_url)
          end
        end

        context 'when URL is invalid' do
          it 'redirects to initial import page' do
            token = 'token'
            url = 'http://192.168.0.1'

            post :configure, params: { bulk_import_gitlab_access_token: token, bulk_import_gitlab_url: url }

            expect(response).to redirect_to new_group_path(anchor: 'import-group-pane')
            expect(flash[:alert]).to include('Specified URL cannot be used')
          end
        end

        context 'when token scope is invalid' do
          before do
            allow_next_instance_of(BulkImports::Clients::HTTP) do |instance|
              allow(instance).to receive(:validate_instance_version!).and_return(true)
              allow(instance).to receive(:validate_import_scopes!).and_raise(BulkImports::Error.new('Error!'))
            end
          end

          it 'redirects to initial import page' do
            token = 'token'
            url = 'https://gitlab.example'

            post :configure, params: { bulk_import_gitlab_access_token: token, bulk_import_gitlab_url: url }

            expect(response).to redirect_to new_group_path(anchor: 'import-group-pane')
            expect(flash[:alert]).to include('Error!')
          end
        end

        context 'when instance version is incompatible' do
          before do
            allow_next_instance_of(BulkImports::Clients::HTTP) do |instance|
              allow(instance).to receive(:validate_instance_version!).and_raise(BulkImports::Error.new('Error!'))
            end
          end

          it 'redirects to initial import page' do
            token = 'token'
            url = 'https://gitlab.example'

            post :configure, params: { bulk_import_gitlab_access_token: token, bulk_import_gitlab_url: url }

            expect(response).to redirect_to new_group_path(anchor: 'import-group-pane')
            expect(flash[:alert]).to include('Error!')
          end
        end

        it 'sets the session variables' do
          token = 'invalid token'
          url = 'https://gitlab.example'

          post :configure, params: { bulk_import_gitlab_access_token: token, bulk_import_gitlab_url: url }

          expect(session[:bulk_import_gitlab_access_token]).to eq(token)
          expect(session[:bulk_import_gitlab_url]).to eq(url)
          expect(response).to have_gitlab_http_status(:found)
          expect(response).to redirect_to(status_import_bulk_imports_url)
        end

        it 'strips access token with spaces' do
          token = 'token'

          post :configure, params: { bulk_import_gitlab_access_token: "  #{token} " }

          expect(session[:bulk_import_gitlab_access_token]).to eq(token)
          expect(controller).to redirect_to(status_import_bulk_imports_url)
        end

        it 'passes namespace_id to status' do
          namespace_id = 5
          token = 'token'
          url = 'https://gitlab.example'

          post :configure, params: { bulk_import_gitlab_access_token: token, bulk_import_gitlab_url: url, namespace_id: namespace_id }

          expect(controller).to redirect_to(status_import_bulk_imports_url(namespace_id: namespace_id))
        end
      end

      describe 'GET status' do
        def get_status(params_override = {}, format = :json)
          params = { page: 1, per_page: 20, filter: '' }.merge(params_override)

          get :status,
            params: params,
            format: format,
            session: {
              bulk_import_gitlab_url: 'https://gitlab.example.com',
              bulk_import_gitlab_access_token: 'demo-pat'
            }
        end

        include_context 'bulk imports requests context', 'https://gitlab.example.com'

        let(:client) { BulkImports::Clients::HTTP.new(url: 'http://gitlab.example', token: 'token') }
        let(:version) { "#{BulkImport::MIN_MAJOR_VERSION}.#{BulkImport::MIN_MINOR_VERSION_FOR_PROJECT}.0" }
        let(:version_response) { double(code: 200, success?: true, parsed_response: { 'version' => version }) }

        describe 'serialized group data' do
          let(:expected_response) do
            double(
              parsed_response: [
                {
                  "full_name" => "Stub",
                  "full_path" => "stub-group",
                  "id" => 2595438,
                  "web_url" => "https://gitlab.com/groups/auto-breakfast"
                }
              ],
              headers: {
                'x-next-page' => '2',
                'x-page' => '1',
                'x-per-page' => '20',
                'x-total' => '42',
                'x-total-pages' => '2'
              }
            )
          end

          let(:source_version) do
            Gitlab::VersionInfo.new(::BulkImport::MIN_MAJOR_VERSION, ::BulkImport::MIN_MINOR_VERSION_FOR_PROJECT)
          end

          before do
            allow_next_instance_of(BulkImports::Clients::HTTP) do |instance|
              allow(instance).to receive(:instance_version).and_return(source_version)
              allow(instance).to receive(:instance_enterprise).and_return(false)
            end
          end

          it 'returns serialized group data' do
            get_status

            version_validation = {
              "features" => {
                "project_migration" => {
                  "available" => true,
                  "min_version" => BulkImport.min_gl_version_for_project_migration.to_s
                },
                "source_instance_version" => version
              }
            }

            expect(json_response).to include("importable_data" => expected_response.parsed_response, "version_validation" => hash_including(version_validation))
          end

          it 'forwards pagination headers' do
            get_status

            expect(response.headers['x-per-page']).to eq expected_response.headers['x-per-page']
            expect(response.headers['x-page']).to eq expected_response.headers['x-page']
            expect(response.headers['x-next-page']).to eq expected_response.headers['x-next-page']
            expect(response.headers['x-prev-page']).to eq expected_response.headers['x-prev-page']
            expect(response.headers['x-total']).to eq expected_response.headers['x-total']
            expect(response.headers['x-total-pages']).to eq expected_response.headers['x-total-pages']
          end

          context 'when filtering' do
            let_it_be(:filter) { 'test' }

            let(:client_params) do
              {
                top_level_only: true,
                min_access_level: Gitlab::Access::OWNER,
                search: filter
              }
            end

            it 'returns filtered result' do
              get_status(filter: filter)

              expect(json_response['importable_data'].first['full_name']).to eq('Test')
            end
          end
        end

        shared_examples 'unacceptable url' do |url, expected_error|
          before do
            stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)

            session[:bulk_import_gitlab_access_token] = 'test'
            session[:bulk_import_gitlab_url] = url
          end

          it 'denies network request' do
            get :status
            expect(controller).to redirect_to(new_group_path(anchor: 'import-group-pane'))
            expect(flash[:alert]).to eq("Specified URL cannot be used: \"#{expected_error}\"")
          end
        end

        context 'when host url is local or not http' do
          include_examples 'unacceptable url', 'https://localhost:3000', "Only allowed schemes are http, https"
          include_examples 'unacceptable url', 'http://192.168.0.1', "Only allowed schemes are http, https"
          include_examples 'unacceptable url', 'ftp://testing', "Only allowed schemes are http, https"

          context 'when local requests are allowed' do
            %w[https://localhost:3000 http://192.168.0.1].each do |url|
              context "with #{url}" do
                before do
                  stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)

                  session[:bulk_import_gitlab_access_token] = 'test'
                  session[:bulk_import_gitlab_url] = url
                end

                it 'allows network request' do
                  get :status

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

          context 'when namespace_id is provided' do
            let_it_be(:group) { create(:group) }

            it 'renders 404 if user does not have access to namespace' do
              get_status({ namespace_id: group.id }, :html)

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

            it 'passes namespace to template' do
              group.add_owner(user)

              get_status({ namespace_id: group.id }, :html)

              expect(response).to have_gitlab_http_status(:ok)
              expect(assigns(:namespace)).to eq(group)
            end
          end
        end

        context 'when connection error occurs' do
          let(:source_version) do
            Gitlab::VersionInfo.new(::BulkImport::MIN_MAJOR_VERSION, ::BulkImport::MIN_MINOR_VERSION_FOR_PROJECT)
          end

          before do
            allow_next_instance_of(BulkImports::Clients::HTTP) do |instance|
              allow(instance).to receive(:instance_version).and_return(source_version)
              allow(instance).to receive(:instance_enterprise).and_return(false)
              allow(instance).to receive(:get).and_raise(BulkImports::Error)
            end
          end

          it 'returns 422' do
            get_status

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

          it 'clears session' do
            get_status

            expect(session[:gitlab_url]).to be_nil
            expect(session[:gitlab_access_token]).to be_nil
          end
        end
      end

      describe 'GET /history' do
        subject(:request) { get :history }

        it 'responds with a 200 and shows the template', :aggregate_failures do
          request

          expect(response).to have_gitlab_http_status(:ok)
          expect(response).to render_template(:history)
        end
      end

      describe 'GET /:id/history' do
        let_it_be(:bulk_import) { create(:bulk_import, user: user) }

        subject(:request) { get :history, params: { id: id } }

        describe 'when bulk_import does not exist' do
          let(:id) { 18231 }

          it 'responds with a 404' do
            request

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

        describe 'when bulk_import exists' do
          let(:id) { bulk_import.id }

          it 'responds with a 200 and shows the template', :aggregate_failures do
            request

            expect(response).to have_gitlab_http_status(:ok)
            expect(response).to render_template(:history)
          end
        end
      end

      describe 'GET failures' do
        let_it_be(:bulk_import) { create(:bulk_import, user: user) }
        let_it_be(:bulk_import_entity) { create(:bulk_import_entity, bulk_import: bulk_import) }
        let(:id) { bulk_import.id }
        let(:entity_id) { bulk_import_entity.id }

        subject(:request) { get :failures, params: { id: id, entity_id: entity_id } }

        describe 'when bulk_import does not exist' do
          let(:id) { 18231 }

          it 'responds with a 404' do
            request

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

        describe 'when bulk_import_entity does not exist' do
          let(:entity_id) { 136485 }

          it 'responds with a 404' do
            request

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

        describe 'when both bulk_import and bulk_import_entity exist' do
          it 'responds with a 200 and shows the template', :aggregate_failures do
            request

            expect(response).to have_gitlab_http_status(:ok)
            expect(response).to render_template(:failures)
          end
        end
      end

      describe 'GET realtime_changes' do
        let_it_be(:bulk_import) { create(:bulk_import, :created, user: user) }

        it 'returns bulk imports created by current user' do
          get :realtime_changes

          expect(json_response).to match_array([{ 'id' => bulk_import.id, 'status_name' => bulk_import.status_name.to_s, 'has_failures' => false }])
        end

        it 'sets a Poll-Interval header' do
          get :realtime_changes

          expect(response.headers['Poll-Interval']).to eq(Import::BulkImportsController::POLLING_INTERVAL.to_s)
        end
      end

      describe 'POST create' do
        let(:instance_url) { "http://fake-instance" }
        let(:bulk_import) { create(:bulk_import) }
        let(:pat) { "fake-pat" }
        let(:bulk_import_params) do
          [{ "source_type" => "group_entity",
             "source_full_path" => "full_path",
             "destination_slug" => "destination_name",
             "destination_namespace" => "root" },
           { "source_type" => "group_entity",
             "source_full_path" => "full_path",
             "destination_slug" => "destination_name",
             "destination_namespace" => "invalid-namespace" }]
        end

        before do
          session[:bulk_import_gitlab_access_token] = pat
          session[:bulk_import_gitlab_url] = instance_url
        end

        it 'executes BulkImports::CreateService' do
          error_response = ServiceResponse.error(message: 'Record invalid', http_status: :unprocessable_entity)

          expect_next_instance_of(
            ::BulkImports::CreateService, user, bulk_import_params[0], { url: instance_url, access_token: pat }) do |service|
            allow(service).to receive(:execute).and_return(ServiceResponse.success(payload: bulk_import))
          end
          expect_next_instance_of(
            ::BulkImports::CreateService, user, bulk_import_params[1], { url: instance_url, access_token: pat }) do |service|
            allow(service).to receive(:execute).and_return(error_response)
          end

          post :create, params: { bulk_import: bulk_import_params }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to eq([{ "success" => true, "id" => bulk_import.id, "message" => nil },
                                       { "success" => false, "id" => nil, "message" => "Record invalid" }])
        end

        context 'when entity destination_name is specified' do
          let(:bulk_import_params) do
            [
              {
                "source_type" => "group_entity",
                "source_full_path" => "full_path",
                "destination_name" => "destination_name",
                "destination_namespace" => "root"
              }
            ]
          end

          it 'replaces destination_name with destination_slug and executes BulkImports::CreateService' do
            entity = {
              "source_type" => "group_entity",
              "source_full_path" => "full_path",
              "destination_slug" => "destination_name",
              "destination_namespace" => "root"
            }

            expect_next_instance_of(
              ::BulkImports::CreateService, user, entity, { url: instance_url, access_token: pat }) do |service|
              allow(service).to receive(:execute).and_return(ServiceResponse.success(payload: bulk_import))
            end

            post :create, params: { bulk_import: bulk_import_params }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to match_array([{ "success" => true, "id" => bulk_import.id, "message" => nil }])
          end
        end

        context 'when source type is project' do
          let(:bulk_import_params) do
            [{ "source_type" => "project_entity",
               "source_full_path" => "full_path",
               "destination_slug" => "destination_name",
               "destination_namespace" => "root" }]
          end

          it 'returns 422' do
            post :create, params: { bulk_import: bulk_import_params }

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

        context 'when request exceeds rate limits' do
          it 'prevents user from starting a new migration' do
            allow(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true)

            post :create, params: { bulk_import: {} }

            request

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

    context 'when importing groups and projects by direct transfer is disabled' do
      before do
        stub_application_setting(bulk_import_enabled: false)
        stub_feature_flags(override_bulk_import_disabled: false)

        allow_next_instance_of(BulkImports::Clients::HTTP) do |instance|
          allow(instance).to receive(:validate_instance_version!).and_return(true)
          allow(instance).to receive(:validate_import_scopes!).and_return(true)
        end
      end

      context 'POST configure' do
        it 'returns 404' do
          post :configure, params: {
            bulk_import_gitlab_access_token: 'token', bulk_import_gitlab_url: 'https://gitlab.example'
          }

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

      context 'GET status' do
        it 'returns 404' do
          get :status

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

      context 'when the override_bulk_import_disabled feature flag is enabled' do
        before do
          stub_feature_flags(override_bulk_import_disabled: true)
        end

        context 'POST configure' do
          it 'does not return 404' do
            post :configure, params: {
              bulk_import_gitlab_access_token: 'token', bulk_import_gitlab_url: 'https://gitlab.example'
            }

            expect(response).to have_gitlab_http_status(:found)
            expect(response).to redirect_to(status_import_bulk_imports_url)
          end
        end

        context 'GET status' do
          it 'does not return 404' do
            get :status

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

  context 'when user is signed out' do
    before do
      sign_out(user)
    end

    context 'POST configure' do
      it 'redirects to sign in page' do
        post :configure

        expect(response).to have_gitlab_http_status(:found)
        expect(response).to redirect_to(new_user_session_path)
      end
    end

    context 'GET status' do
      it 'redirects to sign in page' do
        get :status

        expect(response).to have_gitlab_http_status(:found)
        expect(response).to redirect_to(new_user_session_path)
      end
    end
  end
end