summaryrefslogtreecommitdiff
path: root/spec/helpers/blob_helper_spec.rb
blob: cafe4c4275eddcba29f431e0a7f3bd7c77dda381 (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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BlobHelper do
  include TreeHelper

  describe "#sanitize_svg_data" do
    let(:input_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'unsanitized.svg') }
    let(:data) { File.read(input_svg_path) }
    let(:expected_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'sanitized.svg') }
    let(:expected) { File.read(expected_svg_path) }

    it 'retains essential elements' do
      expect(sanitize_svg_data(data)).to eq(expected)
    end
  end

  describe "#edit_blob_link" do
    let(:namespace) { create(:namespace, name: 'gitlab') }
    let(:project) { create(:project, :repository, namespace: namespace) }

    subject(:link) { helper.edit_blob_button(project, 'master', 'README.md') }

    before do
      allow(helper).to receive(:current_user).and_return(nil)
      allow(helper).to receive(:can?).and_return(true)
      allow(helper).to receive(:can_collaborate_with_project?).and_return(true)
    end

    it 'verifies blob is text' do
      expect(helper).not_to receive(:blob_text_viewable?)

      button = helper.edit_blob_button(project, 'refs/heads/master', 'README.md')

      expect(button).to start_with('<button')
    end

    it 'uses the passed blob instead retrieve from repository' do
      blob = project.repository.blob_at('refs/heads/master', 'README.md')

      expect(project.repository).not_to receive(:blob_at)

      helper.edit_blob_button(project, 'refs/heads/master', 'README.md', blob: blob)
    end

    it 'returns a link with the proper route' do
      expect(Capybara.string(link).find_link('Edit')[:href]).to eq("/#{project.full_path}/-/edit/master/README.md")
    end

    it 'returns a link with the passed link_opts on the expected route' do
      link_with_mr = helper.edit_blob_button(project, 'master', 'README.md', link_opts: { mr_id: 10 })

      expect(Capybara.string(link_with_mr).find_link('Edit')[:href]).to eq("/#{project.full_path}/-/edit/master/README.md?mr_id=10")
    end

    context 'when edit is the primary button' do
      before do
        stub_feature_flags(web_ide_primary_edit: false)
      end

      it 'is rendered as primary' do
        expect(link).not_to match(/btn-inverted/)
      end

      it 'passes on primary tracking attributes' do
        parsed_link = Capybara.string(link).find_link('Edit')

        expect(parsed_link[:'data-track-event']).to eq("click_edit")
        expect(parsed_link[:'data-track-label']).to eq("Edit")
        expect(parsed_link[:'data-track-property']).to eq(nil)
      end
    end

    context 'when Web IDE is the primary button' do
      before do
        stub_feature_flags(web_ide_primary_edit: true)
      end

      it 'is rendered as inverted' do
        expect(link).to match(/btn-inverted/)
      end

      it 'passes on secondary tracking attributes' do
        parsed_link = Capybara.string(link).find_link('Edit')

        expect(parsed_link[:'data-track-event']).to eq("click_edit")
        expect(parsed_link[:'data-track-label']).to eq("Edit")
        expect(parsed_link[:'data-track-property']).to eq("secondary")
      end
    end
  end

  context 'viewer related' do
    include FakeBlobHelpers

    let(:project) { build(:project, lfs_enabled: true) }

    before do
      allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
    end

    let(:viewer_class) do
      Class.new(BlobViewer::Base) do
        include BlobViewer::ServerSide

        self.collapse_limit = 1.megabyte
        self.size_limit = 5.megabytes
        self.type = :rich
      end
    end

    let(:viewer) { viewer_class.new(blob) }
    let(:blob) { fake_blob }

    describe '#blob_render_error_reason' do
      context 'for error :too_large' do
        context 'when the blob size is larger than the absolute size limit' do
          let(:blob) { fake_blob(size: 10.megabytes) }

          it 'returns an error message' do
            expect(helper.blob_render_error_reason(viewer)).to eq('it is larger than 5 MB')
          end
        end

        context 'when the blob size is larger than the size limit' do
          let(:blob) { fake_blob(size: 2.megabytes) }

          it 'returns an error message' do
            expect(helper.blob_render_error_reason(viewer)).to eq('it is larger than 1 MB')
          end
        end
      end

      context 'for error :server_side_but_stored_externally' do
        let(:blob) { fake_blob(lfs: true) }

        it 'returns an error message' do
          expect(helper.blob_render_error_reason(viewer)).to eq('it is stored in LFS')
        end
      end
    end

    describe '#blob_render_error_options' do
      before do
        assign(:project, project)
        assign(:blob, blob)
        assign(:id, File.join('master', blob.path))

        controller.params[:controller] = 'projects/blob'
        controller.params[:action] = 'show'
        controller.params[:namespace_id] = project.namespace.to_param
        controller.params[:project_id] = project.to_param
        controller.params[:id] = File.join('master', blob.path)
      end

      context 'for error :collapsed' do
        let(:blob) { fake_blob(size: 2.megabytes) }

        it 'includes a "load it anyway" link' do
          expect(helper.blob_render_error_options(viewer)).to include(/load it anyway/)
        end
      end

      context 'for error :too_large' do
        let(:blob) { fake_blob(size: 10.megabytes) }

        it 'does not include a "load it anyway" link' do
          expect(helper.blob_render_error_options(viewer)).not_to include(/load it anyway/)
        end

        context 'when the viewer is rich' do
          context 'the blob is rendered as text' do
            let(:blob) { fake_blob(path: 'file.md', size: 2.megabytes) }

            it 'includes a "view the source" link' do
              expect(helper.blob_render_error_options(viewer)).to include(/view the source/)
            end
          end

          context 'the blob is not rendered as text' do
            let(:blob) { fake_blob(path: 'file.pdf', binary: true, size: 2.megabytes) }

            it 'does not include a "view the source" link' do
              expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
            end
          end
        end

        context 'when the viewer is not rich' do
          before do
            viewer_class.type = :simple
          end

          let(:blob) { fake_blob(path: 'file.md', size: 2.megabytes) }

          it 'does not include a "view the source" link' do
            expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
          end
        end

        it 'includes a "download it" link' do
          expect(helper.blob_render_error_options(viewer)).to include(/download it/)
        end
      end

      context 'for error :server_side_but_stored_externally' do
        let(:blob) { fake_blob(path: 'file.md', lfs: true) }

        it 'does not include a "load it anyway" link' do
          expect(helper.blob_render_error_options(viewer)).not_to include(/load it anyway/)
        end

        it 'does not include a "view the source" link' do
          expect(helper.blob_render_error_options(viewer)).not_to include(/view the source/)
        end

        it 'includes a "download it" link' do
          expect(helper.blob_render_error_options(viewer)).to include(/download it/)
        end
      end
    end

    describe '#show_suggest_pipeline_creation_celebration?' do
      let(:current_user) { create(:user) }

      before do
        assign(:project, project)
        assign(:blob, blob)
        assign(:commit, double('Commit', sha: 'whatever'))
        helper.request.cookies["suggest_gitlab_ci_yml_commit_#{project.id}"] = 'true'
        allow(helper).to receive(:current_user).and_return(current_user)
      end

      context 'when file is a pipeline config file' do
        let(:data) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) }
        let(:blob) { fake_blob(path: Gitlab::FileDetector::PATTERNS[:gitlab_ci], data: data) }

        context 'feature enabled' do
          it 'is true' do
            expect(helper.show_suggest_pipeline_creation_celebration?).to be_truthy
          end

          context 'file is invalid format' do
            let(:data) { 'foo' }

            it 'is false' do
              expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey
            end
          end

          context 'does not use the default ci config' do
            before do
              project.ci_config_path = 'something_bad'
            end

            it 'is false' do
              expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey
            end
          end

          context 'does not have the needed cookie' do
            before do
              helper.request.cookies.delete "suggest_gitlab_ci_yml_commit_#{project.id}"
            end

            it 'is false' do
              expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey
            end
          end

          context 'blob does not have auxiliary view' do
            before do
              allow(blob).to receive(:auxiliary_viewer).and_return(nil)
            end

            it 'is false' do
              expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey
            end
          end
        end

        context 'feature disabled' do
          before do
            stub_feature_flags(suggest_pipeline: false)
          end

          it 'is false' do
            expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey
          end
        end
      end

      context 'when file is not a pipeline config file' do
        let(:blob) { fake_blob(path: 'LICENSE') }

        context 'feature enabled' do
          it 'is false' do
            expect(helper.show_suggest_pipeline_creation_celebration?).to be_falsey
          end
        end
      end
    end
  end

  describe 'suggest_pipeline_commit_cookie_name' do
    let(:project) { create(:project) }

    it 'uses project id to make up the cookie name' do
      assign(:project, project)

      expect(helper.suggest_pipeline_commit_cookie_name).to eq "suggest_gitlab_ci_yml_commit_#{project.id}"
    end
  end

  describe `#ide_edit_button` do
    let_it_be(:namespace) { create(:namespace, name: 'gitlab') }
    let_it_be(:project) { create(:project, :repository, namespace: namespace) }
    let_it_be(:current_user) { create(:user) }
    let(:can_push_code) { true }
    let(:blob) { project.repository.blob_at('refs/heads/master', 'README.md') }

    subject(:link) { helper.ide_edit_button(project, 'master', 'README.md', blob: blob) }

    before do
      allow(helper).to receive(:current_user).and_return(current_user)
      allow(helper).to receive(:can?).with(current_user, :push_code, project).and_return(can_push_code)
      allow(helper).to receive(:can_collaborate_with_project?).and_return(true)
    end

    it 'returns a link with a Web IDE route' do
      expect(Capybara.string(link).find_link('Web IDE')[:href]).to eq("/-/ide/project/#{project.full_path}/edit/master/-/README.md")
    end

    context 'when edit is the primary button' do
      before do
        stub_feature_flags(web_ide_primary_edit: false)
      end

      it 'is rendered as inverted' do
        expect(link).to match(/btn-inverted/)
      end

      it 'passes on secondary tracking attributes' do
        parsed_link = Capybara.string(link).find_link('Web IDE')

        expect(parsed_link[:'data-track-event']).to eq("click_edit_ide")
        expect(parsed_link[:'data-track-label']).to eq("Web IDE")
        expect(parsed_link[:'data-track-property']).to eq("secondary")
      end
    end

    context 'when Web IDE is the primary button' do
      before do
        stub_feature_flags(web_ide_primary_edit: true)
      end

      it 'is rendered as primary' do
        expect(link).not_to match(/btn-inverted/)
      end

      it 'passes on primary tracking attributes' do
        parsed_link = Capybara.string(link).find_link('Web IDE')

        expect(parsed_link[:'data-track-event']).to eq("click_edit_ide")
        expect(parsed_link[:'data-track-label']).to eq("Web IDE")
        expect(parsed_link[:'data-track-property']).to eq(nil)
      end
    end
  end

  describe '#ide_edit_path' do
    let(:project) { create(:project) }
    let(:current_user) { create(:user) }
    let(:can_push_code) { true }

    before do
      allow(helper).to receive(:current_user).and_return(current_user)
      allow(helper).to receive(:can?).and_return(can_push_code)
    end

    around do |example|
      old_script_name = Rails.application.routes.default_url_options[:script_name]
      begin
        example.run
      ensure
        Rails.application.routes.default_url_options[:script_name] = old_script_name
      end
    end

    it 'returns full IDE path' do
      Rails.application.routes.default_url_options[:script_name] = nil

      expect(helper.ide_edit_path(project, "master", "")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/master")
    end

    it 'returns full IDE path with second -' do
      Rails.application.routes.default_url_options[:script_name] = nil

      expect(helper.ide_edit_path(project, "testing/slashes", "readme.md")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/testing/slashes/-/readme.md")
    end

    it 'returns IDE path without relative_url_root' do
      Rails.application.routes.default_url_options[:script_name] = "/gitlab"

      expect(helper.ide_edit_path(project, "master", "")).to eq("/gitlab/-/ide/project/#{project.namespace.path}/#{project.path}/edit/master")
    end

    it 'escapes special characters' do
      Rails.application.routes.default_url_options[:script_name] = nil

      expect(helper.ide_edit_path(project, "testing/#hashes", "readme.md#test")).to eq("/-/ide/project/#{project.full_path}/edit/testing/%23hashes/-/readme.md%23test")
      expect(helper.ide_edit_path(project, "testing/#hashes", "src#/readme.md#test")).to eq("/-/ide/project/#{project.full_path}/edit/testing/%23hashes/-/src%23/readme.md%23test")
    end

    it 'does not escape "/" character' do
      Rails.application.routes.default_url_options[:script_name] = nil

      expect(helper.ide_edit_path(project, "testing/slashes", "readme.md/")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/testing/slashes/-/readme.md/")
    end

    context 'when user is not logged in' do
      let(:current_user) { nil }

      it 'returns IDE path inside the project' do
        expect(helper.ide_edit_path(project, "master", "")).to eq("/-/ide/project/#{project.namespace.path}/#{project.path}/edit/master")
      end
    end

    context 'when user cannot push to the project' do
      let(:can_push_code) { false }

      it "returns IDE path with the user's fork" do
        expect(helper.ide_edit_path(project, "master", "")).to eq("/-/ide/project/#{current_user.namespace.full_path}/#{project.path}/edit/master")
      end
    end
  end

  describe '#ide_merge_request_path' do
    let_it_be(:project) { create(:project, :repository) }
    let_it_be(:merge_request) { create(:merge_request, source_project: project)}

    it 'returns IDE path for the given MR if MR is not merged' do
      expect(helper.ide_merge_request_path(merge_request)).to eq("/-/ide/project/#{project.full_path}/merge_requests/#{merge_request.iid}")
    end

    context 'when the MR comes from a fork' do
      include ProjectForksHelper

      let(:forked_project) { fork_project(project, nil, repository: true) }
      let(:merge_request) { create(:merge_request, source_project: forked_project, target_project: project) }

      it 'returns IDE path for MR in the forked repo with target project included as param' do
        expect(helper.ide_merge_request_path(merge_request)).to eq("/-/ide/project/#{forked_project.full_path}/merge_requests/#{merge_request.iid}?target_project=#{CGI.escape(project.full_path)}")
      end
    end

    context 'when the MR is merged' do
      let(:current_user) { build(:user) }

      let_it_be(:merge_request) { create(:merge_request, :merged, source_project: project, source_branch: 'testing-1', target_branch: 'feature-1') }

      before do
        allow(helper).to receive(:current_user).and_return(current_user)
        allow(helper).to receive(:can?).and_return(true)
      end

      it 'returns default IDE url with master branch' do
        expect(helper.ide_merge_request_path(merge_request)).to eq("/-/ide/project/#{project.full_path}/edit/master")
      end

      it 'includes file path passed' do
        expect(helper.ide_merge_request_path(merge_request, 'README.md')).to eq("/-/ide/project/#{project.full_path}/edit/master/-/README.md")
      end

      context 'when target branch exists' do
        before do
          allow(merge_request).to receive(:target_branch_exists?).and_return(true)
        end

        it 'returns IDE edit url with the target branch' do
          expect(helper.ide_merge_request_path(merge_request)).to eq("/-/ide/project/#{project.full_path}/edit/feature-1")
        end
      end
    end
  end

  describe '#ide_fork_and_edit_path' do
    let_it_be(:project) { create(:project) }
    let_it_be(:user) { create(:user) }

    let(:current_user) { user }

    before do
      allow(helper).to receive(:current_user).and_return(current_user)
      allow(helper).to receive(:can?).and_return(true)
    end

    it 'returns path to fork the repo with a redirect param to the full IDE path' do
      uri = URI(helper.ide_fork_and_edit_path(project, "master", ""))
      params = CGI.unescape(uri.query)

      expect(uri.path).to eq("/#{project.namespace.path}/#{project.path}/-/forks")
      expect(params).to include("continue[to]=/-/ide/project/#{project.namespace.path}/#{project.path}/edit/master")
      expect(params).to include("namespace_key=#{current_user.namespace.id}")
    end

    context 'when user is not logged in' do
      let(:current_user) { nil }

      it 'returns nil' do
        expect(helper.ide_fork_and_edit_path(project, "master", "")).to be_nil
      end
    end
  end

  describe '#fork_and_edit_path' do
    let_it_be(:project) { create(:project) }
    let_it_be(:user) { create(:user) }

    let(:current_user) { user }

    before do
      allow(helper).to receive(:current_user).and_return(current_user)
      allow(helper).to receive(:can?).and_return(true)
    end

    it 'returns path to fork the repo with a redirect param to the full edit path' do
      uri = URI(helper.fork_and_edit_path(project, "master", ""))
      params = CGI.unescape(uri.query)

      expect(uri.path).to eq("/#{project.namespace.path}/#{project.path}/-/forks")
      expect(params).to include("continue[to]=/#{project.namespace.path}/#{project.path}/-/edit/master/")
      expect(params).to include("namespace_key=#{current_user.namespace.id}")
    end

    context 'when user is not logged in' do
      let(:current_user) { nil }

      it 'returns nil' do
        expect(helper.ide_fork_and_edit_path(project, "master", "")).to be_nil
      end
    end
  end

  describe '#editing_ci_config?' do
    let(:project) { build(:project) }

    subject { helper.editing_ci_config? }

    before do
      assign(:project, project)
      assign(:path, path)
    end

    context 'when path is nil' do
      let(:path) { nil }

      it { is_expected.to be_falsey }
    end

    context 'when path is not a ci file' do
      let(:path) { 'some-file.txt' }

      it { is_expected.to be_falsey }
    end

    context 'when path ends is gitlab-ci.yml' do
      let(:path) { '.gitlab-ci.yml' }

      it { is_expected.to be_truthy }
    end

    context 'when path ends with gitlab-ci.yml' do
      let(:path) { 'template.gitlab-ci.yml' }

      it { is_expected.to be_truthy }
    end

    context 'with custom ci paths' do
      let(:path) { 'path/to/ci.yaml' }

      before do
        project.ci_config_path = 'path/to/ci.yaml'
      end

      it { is_expected.to be_truthy }
    end

    context 'with custom ci config and path' do
      let(:path) { 'path/to/template.gitlab-ci.yml' }

      before do
        project.ci_config_path = 'ci/path/.gitlab-ci.yml@another-group/another-project'
      end

      it { is_expected.to be_truthy }
    end
  end
end