Read about our upcoming Code of Conduct on this issue

projects_helper_spec.rb 25.3 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
require 'spec_helper'

describe ProjectsHelper do
6
7
  include ProjectForksHelper

Reuben Pereira's avatar
Reuben Pereira committed
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
  describe '#error_tracking_setting_project_json' do
    let(:project) { create(:project) }

    context 'error tracking setting does not exist' do
      before do
        helper.instance_variable_set(:@project, project)
      end

      it 'returns nil' do
        expect(helper.error_tracking_setting_project_json).to be_nil
      end
    end

    context 'error tracking setting exists' do
      let!(:error_tracking_setting) { create(:project_error_tracking_setting, project: project) }

      context 'api_url present' do
        let(:json) do
          {
            name: error_tracking_setting.project_name,
            organization_name: error_tracking_setting.organization_name,
            organization_slug: error_tracking_setting.organization_slug,
            slug: error_tracking_setting.project_slug
          }.to_json
        end

        before do
          helper.instance_variable_set(:@project, project)
        end

        it 'returns error tracking json' do
          expect(helper.error_tracking_setting_project_json).to eq(json)
        end
      end

      context 'api_url not present' do
        before do
          project.error_tracking_setting.api_url = nil
          project.error_tracking_setting.enabled = false

          helper.instance_variable_set(:@project, project)
        end

        it 'returns nil' do
          expect(helper.error_tracking_setting_project_json).to be_nil
        end
      end
    end
  end

Valery Sizov's avatar
Valery Sizov committed
58
59
  describe "#project_status_css_class" do
    it "returns appropriate class" do
60
61
62
      expect(project_status_css_class("started")).to eq("table-active")
      expect(project_status_css_class("failed")).to eq("table-danger")
      expect(project_status_css_class("finished")).to eq("table-success")
Valery Sizov's avatar
Valery Sizov committed
63
64
    end
  end
Valery Sizov's avatar
Valery Sizov committed
65
66

  describe "can_change_visibility_level?" do
67
    let(:project) { create(:project) }
68
    let(:user) { create(:project_member, :reporter, user: create(:user), project: project).user }
69
    let(:forked_project) { fork_project(project, user) }
Valery Sizov's avatar
Valery Sizov committed
70

71
    it "returns false if there are no appropriate permissions" do
Valery Sizov's avatar
Valery Sizov committed
72
73
74
75
76
77
78
79
80
81
82
      allow(helper).to receive(:can?) { false }

      expect(helper.can_change_visibility_level?(project, user)).to be_falsey
    end

    it "returns true if there are permissions and it is not fork" do
      allow(helper).to receive(:can?) { true }

      expect(helper.can_change_visibility_level?(project, user)).to be_truthy
    end

83
84
    it 'allows visibility level to be changed if the project is forked' do
      allow(helper).to receive(:can?).with(user, :change_visibility_level, project) { true }
85
      project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
86
87
88
89
90
      fork_project(project)

      expect(helper.can_change_visibility_level?(project, user)).to be_truthy
    end

Valery Sizov's avatar
Valery Sizov committed
91
92
93
94
    context "forks" do
      it "returns false if there are permissions and origin project is PRIVATE" do
        allow(helper).to receive(:can?) { true }

95
        project.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
Valery Sizov's avatar
Valery Sizov committed
96

97
        expect(helper.can_change_visibility_level?(forked_project, user)).to be_falsey
Valery Sizov's avatar
Valery Sizov committed
98
99
100
101
102
      end

      it "returns true if there are permissions and origin project is INTERNAL" do
        allow(helper).to receive(:can?) { true }

103
        project.update(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
Valery Sizov's avatar
Valery Sizov committed
104

105
        expect(helper.can_change_visibility_level?(forked_project, user)).to be_truthy
Valery Sizov's avatar
Valery Sizov committed
106
107
108
      end
    end
  end
109
110

  describe "readme_cache_key" do
111
    let(:project) { create(:project, :repository) }
112
113
114
115
116
117

    before do
      helper.instance_variable_set(:@project, project)
    end

    it "returns a valid cach key" do
118
      expect(helper.send(:readme_cache_key)).to eq("#{project.full_path}-#{project.commit.id}-readme")
119
120
121
122
123
    end

    it "returns a valid cache key if HEAD does not exist" do
      allow(project).to receive(:commit) { nil }

124
      expect(helper.send(:readme_cache_key)).to eq("#{project.full_path}-nil-readme")
125
126
    end
  end
127

128
  describe "#project_list_cache_key", :clean_gitlab_redis_shared_state do
129
    let(:project) { create(:project, :repository) }
130
131
132
133
134
    let(:user) { create(:user) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
      allow(helper).to receive(:can?).with(user, :read_cross_project) { true }
135
      allow(user).to receive(:max_member_access_for_project).and_return(40)
136
    end
137

138
139
    it "includes the route" do
      expect(helper.project_list_cache_key(project)).to include(project.route.cache_key)
140
141
    end

142
143
144
145
    it "includes the project" do
      expect(helper.project_list_cache_key(project)).to include(project.cache_key)
    end

146
147
148
149
    it "includes the last activity date" do
      expect(helper.project_list_cache_key(project)).to include(project.last_activity_date)
    end

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    it "includes the controller name" do
      expect(helper.controller).to receive(:controller_name).and_return("testcontroller")

      expect(helper.project_list_cache_key(project)).to include("testcontroller")
    end

    it "includes the controller action" do
      expect(helper.controller).to receive(:action_name).and_return("testaction")

      expect(helper.project_list_cache_key(project)).to include("testaction")
    end

    it "includes the application settings" do
      settings = Gitlab::CurrentSettings.current_application_settings

      expect(helper.project_list_cache_key(project)).to include(settings.cache_key)
    end

    it "includes a version" do
169
      expect(helper.project_list_cache_key(project).last).to start_with('v')
170
171
    end

172
173
174
175
    it 'includes wether or not the user can read cross project' do
      expect(helper.project_list_cache_key(project)).to include('cross-project:true')
    end

176
177
178
179
180
    it "includes the pipeline status when there is a status" do
      create(:ci_pipeline, :success, project: project, sha: project.commit.sha)

      expect(helper.project_list_cache_key(project)).to include("pipeline-status/#{project.commit.sha}-success")
    end
181
182
183
184

    it "includes the user max member access" do
      expect(helper.project_list_cache_key(project)).to include('access:40')
    end
185
  end
186

187
188
  describe '#load_pipeline_status' do
    it 'loads the pipeline status in batch' do
189
      project = build(:project)
190
191
192
193
194
195
196
197
198

      helper.load_pipeline_status([project])
      # Skip lazy loading of the `pipeline_status` attribute
      pipeline_status = project.instance_variable_get('@pipeline_status')

      expect(pipeline_status).to be_a(Gitlab::Cache::Ci::ProjectPipelineStatus)
    end
  end

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
  describe '#show_no_ssh_key_message?' do
    let(:user) { create(:user) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'user has no keys' do
      it 'returns true' do
        expect(helper.show_no_ssh_key_message?).to be_truthy
      end
    end

    context 'user has an ssh key' do
      it 'returns false' do
        create(:personal_key, user: user)

        expect(helper.show_no_ssh_key_message?).to be_falsey
      end
    end
  end

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

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'user has password set' do
      it 'returns false' do
        expect(helper.show_no_password_message?).to be_falsey
      end
    end

234
235
236
237
238
239
240
    context 'user has hidden the message' do
      it 'returns false' do
        allow(helper).to receive(:cookies).and_return(hide_no_password_message: true)

        expect(helper.show_no_password_message?).to be_falsey
      end
    end
241

242
    context 'user requires a password for Git' do
243
      it 'returns true' do
244
245
        allow(user).to receive(:require_password_creation_for_git?).and_return(true)

246
247
248
249
        expect(helper.show_no_password_message?).to be_truthy
      end
    end

250
    context 'user requires a personal access token for Git' do
251
      it 'returns true' do
252
253
        allow(user).to receive(:require_password_creation_for_git?).and_return(false)
        allow(user).to receive(:require_personal_access_token_creation_for_git_auth?).and_return(true)
254
255
256
257
258
259
260

        expect(helper.show_no_password_message?).to be_truthy
      end
    end
  end

  describe '#link_to_set_password' do
261
262
    let(:user) { create(:user, password_automatically_set: true) }

263
264
265
266
    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

267
    context 'password authentication is enabled for Git' do
268
      it 'returns link to set a password' do
269
270
        stub_application_setting(password_authentication_enabled_for_git?: true)

271
272
273
274
        expect(helper.link_to_set_password).to match %r{<a href="#{edit_profile_password_path}">set a password</a>}
      end
    end

275
    context 'password authentication is disabled for Git' do
276
      it 'returns link to create a personal access token' do
277
        stub_application_setting(password_authentication_enabled_for_git?: false)
278
279
280
281
282
283

        expect(helper.link_to_set_password).to match %r{<a href="#{profile_personal_access_tokens_path}">create a personal access token</a>}
      end
    end
  end

284
285
286
287
288
289
290
291
292
293
294
295
  describe '#link_to_project' do
    let(:group)   { create(:group, name: 'group name with space') }
    let(:project) { create(:project, group: group, name: 'project name with space') }
    subject { link_to_project(project) }

    it 'returns an HTML link to the project' do
      expect(subject).to match(%r{/#{group.full_path}/#{project.path}})
      expect(subject).to include('group name with space /')
      expect(subject).to include('project name with space')
    end
  end

296
  describe '#link_to_member_avatar' do
Maxim Rydkin's avatar
Maxim Rydkin committed
297
    let(:user) { build_stubbed(:user) }
298
    let(:expected) { double }
299

300
    before do
301
      expect(helper).to receive(:avatar_icon_for_user).with(user, 16).and_return(expected)
302
303
304
    end

    it 'returns image tag for member avatar' do
305
      expect(helper).to receive(:image_tag).with(expected, { width: 16, class: ["avatar", "avatar-inline", "s16"], alt: "", "data-src" => anything })
Maxim Rydkin's avatar
Maxim Rydkin committed
306
307

      helper.link_to_member_avatar(user)
308
    end
309
310

    it 'returns image tag with avatar class' do
311
      expect(helper).to receive(:image_tag).with(expected, { width: 16, class: ["avatar", "avatar-inline", "s16", "any-avatar-class"], alt: "", "data-src" => anything })
312
313
314

      helper.link_to_member_avatar(user, avatar_class: "any-avatar-class")
    end
315
316
317
  end

  describe '#link_to_member' do
Maxim Rydkin's avatar
Maxim Rydkin committed
318
319
    let(:group)   { build_stubbed(:group) }
    let(:project) { build_stubbed(:project, group: group) }
320
    let(:user)    { build_stubbed(:user, name: '<h1>Administrator</h1>') }
321
322
323
324
325

    describe 'using the default options' do
      it 'returns an HTML link to the user' do
        link = helper.link_to_member(project, user)

326
        expect(link).to match(%r{/#{user.username}})
327
      end
328
329
330
331
332
333
334

      it 'HTML escapes the name of the user' do
        link = helper.link_to_member(project, user)

        expect(link).to include(ERB::Util.html_escape(user.name))
        expect(link).not_to include(user.name)
      end
335
336
    end
  end
337
338

  describe 'default_clone_protocol' do
339
    context 'when user is not logged in and gitlab protocol is HTTP' do
340
      it 'returns HTTP' do
341
        allow(helper).to receive(:current_user).and_return(nil)
342
343
344
345
346

        expect(helper.send(:default_clone_protocol)).to eq('http')
      end
    end

347
    context 'when user is not logged in and gitlab protocol is HTTPS' do
348
      it 'returns HTTPS' do
349
350
        stub_config_setting(protocol: 'https')
        allow(helper).to receive(:current_user).and_return(nil)
351
352
353
354
355

        expect(helper.send(:default_clone_protocol)).to eq('https')
      end
    end
  end
356

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  describe '#last_push_event' do
    let(:user) { double(:user, fork_of: nil) }
    let(:project) { double(:project, id: 1) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
      helper.instance_variable_set(:@project, project)
    end

    context 'when there is no current_user' do
      let(:user) { nil }

      it 'returns nil' do
        expect(helper.last_push_event).to eq(nil)
      end
    end

    it 'returns recent push on the current project' do
      event = double(:event)
376
      expect(user).to receive(:recent_push).with(project).and_return(event)
377
378
379
380

      expect(helper.last_push_event).to eq(event)
    end
  end
Felipe Artur's avatar
Felipe Artur committed
381

Kamil Trzcinski's avatar
Kamil Trzcinski committed
382
  describe '#get_project_nav_tabs' do
383
    let(:project) { create(:project) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
    let(:user)    { create(:user) }

    before do
      allow(helper).to receive(:can?) { true }
    end

    subject do
      helper.send(:get_project_nav_tabs, project, user)
    end

    context 'when builds feature is enabled' do
      before do
        allow(project).to receive(:builds_enabled?).and_return(true)
      end

      it "does include pipelines tab" do
        is_expected.to include(:pipelines)
      end
    end

    context 'when builds feature is disabled' do
      before do
        allow(project).to receive(:builds_enabled?).and_return(false)
      end

409
410
411
412
413
414
415
416
417
418
419
420
421
422
      context 'when user has access to builds' do
        it "does include pipelines tab" do
          is_expected.to include(:pipelines)
        end
      end

      context 'when user does not have access to builds' do
        before do
          allow(helper).to receive(:can?) { false }
        end

        it "does not include pipelines tab" do
          is_expected.not_to include(:pipelines)
        end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
423
424
      end
    end
425
426
427

    context 'when project has external wiki' do
      it 'includes external wiki tab' do
428
429
        project.create_external_wiki_service(active: true, properties: { 'external_wiki_url' => 'https://gitlab.com' })

430
431
432
433
434
435
        is_expected.to include(:external_wiki)
      end
    end

    context 'when project does not have external wiki' do
      it 'does not include external wiki tab' do
436
        expect(project.external_wiki).to be_nil
437
438
439
        is_expected.not_to include(:external_wiki)
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
440
  end
441

442
  describe '#show_projects' do
443
444
445
446
447
    let(:projects) do
      create(:project)
      Project.all
    end

448
449
450
451
    before do
      stub_feature_flags(project_list_filter_bar: false)
    end

452
    it 'returns true when there are projects' do
453
      expect(helper.show_projects?(projects, {})).to eq(true)
454
455
456
    end

    it 'returns true when there are no projects but a name is given' do
457
458
459
460
461
      expect(helper.show_projects?(Project.none, name: 'foo')).to eq(true)
    end

    it 'returns true when there are no projects but personal is present' do
      expect(helper.show_projects?(Project.none, personal: 'true')).to eq(true)
462
463
464
    end

    it 'returns false when there are no projects and there is no name' do
465
      expect(helper.show_projects?(Project.none, {})).to eq(false)
466
467
468
    end
  end

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
  describe('#push_to_create_project_command') do
    let(:user) { create(:user, username: 'john') }

    it 'returns the command to push to create project over HTTP' do
      allow(Gitlab::CurrentSettings.current_application_settings).to receive(:enabled_git_access_protocol) { 'http' }

      expect(helper.push_to_create_project_command(user)).to eq('git push --set-upstream http://test.host/john/$(git rev-parse --show-toplevel | xargs basename).git $(git rev-parse --abbrev-ref HEAD)')
    end

    it 'returns the command to push to create project over SSH' do
      allow(Gitlab::CurrentSettings.current_application_settings).to receive(:enabled_git_access_protocol) { 'ssh' }

      expect(helper.push_to_create_project_command(user)).to eq('git push --set-upstream git@localhost:john/$(git rev-parse --show-toplevel | xargs basename).git $(git rev-parse --abbrev-ref HEAD)')
    end
  end

485
  describe '#any_projects?' do
486
    let!(:project) { create(:project) }
487
488
489
490
491
492
493
494
495

    it 'returns true when projects will be returned' do
      expect(helper.any_projects?(Project.all)).to eq(true)
    end

    it 'returns false when no projects will be returned' do
      expect(helper.any_projects?(Project.none)).to eq(false)
    end

496
497
498
499
500
501
502
503
    it 'returns true when using a non-empty Array' do
      expect(helper.any_projects?([project])).to eq(true)
    end

    it 'returns false when using an empty Array' do
      expect(helper.any_projects?([])).to eq(false)
    end

504
505
506
507
508
509
510
511
512
513
514
    it 'only executes a single query when a LIMIT is applied' do
      relation = Project.limit(1)
      recorder = ActiveRecord::QueryRecorder.new do
        2.times do
          helper.any_projects?(relation)
        end
      end

      expect(recorder.count).to eq(1)
    end
  end
515
516
517
518
519
520
521
522
523
524
525

  describe '#git_user_name' do
    let(:user) { double(:user, name: 'John "A" Doe53') }
    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    it 'parses quotes in name' do
      expect(helper.send(:git_user_name)).to eq('John \"A\" Doe53')
    end
  end
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

  describe 'show_xcode_link' do
    let!(:project) { create(:project) }
    let(:mac_ua) { 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36' }
    let(:ios_ua) { 'Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3' }

    context 'when the repository is xcode compatible' do
      before do
        allow(project.repository).to receive(:xcode_project?).and_return(true)
      end

      it 'returns false if the visitor is not using macos' do
        allow(helper).to receive(:browser).and_return(Browser.new(ios_ua))

        expect(helper.show_xcode_link?(project)).to eq(false)
      end

      it 'returns true if the visitor is using macos' do
        allow(helper).to receive(:browser).and_return(Browser.new(mac_ua))

        expect(helper.show_xcode_link?(project)).to eq(true)
      end
    end

    context 'when the repository is not xcode compatible' do
      before do
        allow(project.repository).to receive(:xcode_project?).and_return(false)
      end

      it 'returns false if the visitor is not using macos' do
        allow(helper).to receive(:browser).and_return(Browser.new(ios_ua))

        expect(helper.show_xcode_link?(project)).to eq(false)
      end

      it 'returns false if the visitor is using macos' do
        allow(helper).to receive(:browser).and_return(Browser.new(mac_ua))

        expect(helper.show_xcode_link?(project)).to eq(false)
      end
    end
  end
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
  describe 'link_to_bfg' do
    subject { helper.link_to_bfg }

    it 'generates a hardcoded link to the BFG Repo-Cleaner' do
      result = helper.link_to_bfg
      doc = Nokogiri::HTML.fragment(result)

      expect(doc.children.size).to eq(1)

      link = doc.children.first

      aggregate_failures do
        expect(result).to be_html_safe

        expect(link.name).to eq('a')
        expect(link[:target]).to eq('_blank')
        expect(link[:rel]).to eq('noopener noreferrer')
        expect(link[:href]).to eq('https://rtyley.github.io/bfg-repo-cleaner/')
        expect(link.inner_html).to eq('BFG')

        expect(result).to be_html_safe
      end
    end
  end

594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
  describe '#explore_projects_tab?' do
    subject { helper.explore_projects_tab? }

    it 'returns true when on the "All" tab under "Explore projects"' do
      allow(@request).to receive(:path) { explore_projects_path }

      expect(subject).to be_truthy
    end

    it 'returns true when on the "Trending" tab under "Explore projects"' do
      allow(@request).to receive(:path) { trending_explore_projects_path }

      expect(subject).to be_truthy
    end

    it 'returns true when on the "Starred" tab under "Explore projects"' do
      allow(@request).to receive(:path) { starred_explore_projects_path }

      expect(subject).to be_truthy
    end

    it 'returns false when on the "Your projects" tab' do
      allow(@request).to receive(:path) { dashboard_projects_path }

      expect(subject).to be_falsey
    end
  end

  describe '#show_merge_request_count' do
    context 'when the feature flag is enabled' do
      before do
        stub_feature_flags(project_list_show_mr_count: true)
      end

      it 'returns true if compact mode is disabled' do
        expect(helper.show_merge_request_count?).to be_truthy
      end

      it 'returns false if compact mode is enabled' do
        expect(helper.show_merge_request_count?(compact_mode: true)).to be_falsey
      end
    end

    context 'when the feature flag is disabled' do
      before do
        stub_feature_flags(project_list_show_mr_count: false)
      end

      it 'always returns false' do
        expect(helper.show_merge_request_count?(disabled: false)).to be_falsy
        expect(helper.show_merge_request_count?(disabled: true)).to be_falsy
      end
    end

    context 'disabled flag' do
      before do
        stub_feature_flags(project_list_show_mr_count: true)
      end

      it 'returns false if disabled flag is true' do
        expect(helper.show_merge_request_count?(disabled: true)).to be_falsey
      end

      it 'returns true if disabled flag is false' do
        expect(helper.show_merge_request_count?).to be_truthy
      end
    end
  end

  describe '#show_issue_count?' do
    context 'when the feature flag is enabled' do
      before do
        stub_feature_flags(project_list_show_issue_count: true)
      end

      it 'returns true if compact mode is disabled' do
        expect(helper.show_issue_count?).to be_truthy
      end

      it 'returns false if compact mode is enabled' do
        expect(helper.show_issue_count?(compact_mode: true)).to be_falsey
      end
    end

    context 'when the feature flag is disabled' do
      before do
        stub_feature_flags(project_list_show_issue_count: false)
      end

      it 'always returns false' do
        expect(helper.show_issue_count?(disabled: false)).to be_falsy
        expect(helper.show_issue_count?(disabled: true)).to be_falsy
      end
    end

    context 'disabled flag' do
      before do
        stub_feature_flags(project_list_show_issue_count: true)
      end

      it 'returns false if disabled flag is true' do
        expect(helper.show_issue_count?(disabled: true)).to be_falsey
      end

      it 'returns true if disabled flag is false' do
        expect(helper.show_issue_count?).to be_truthy
      end
    end
  end
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801

  describe '#show_auto_devops_implicitly_enabled_banner?' do
    using RSpec::Parameterized::TableSyntax

    let(:user) { create(:user) }

    let(:feature_visibilities) do
      {
        enabled: ProjectFeature::ENABLED,
        disabled: ProjectFeature::DISABLED
      }
    end

    where(:global_setting, :project_setting, :builds_visibility, :gitlab_ci_yml, :user_access, :result) do
      # With ADO implicitly enabled scenarios
      true | nil | :disabled | true  | :developer  | false
      true | nil | :disabled | true  | :maintainer | false
      true | nil | :disabled | true  | :owner      | false

      true | nil | :disabled | false | :developer  | false
      true | nil | :disabled | false | :maintainer | false
      true | nil | :disabled | false | :owner      | false

      true | nil | :enabled  | true  | :developer  | false
      true | nil | :enabled  | true  | :maintainer | false
      true | nil | :enabled  | true  | :owner      | false

      true | nil | :enabled  | false | :developer  | false
      true | nil | :enabled  | false | :maintainer | true
      true | nil | :enabled  | false | :owner      | true

      # With ADO enabled scenarios
      true | true | :disabled | true  | :developer  | false
      true | true | :disabled | true  | :maintainer | false
      true | true | :disabled | true  | :owner      | false

      true | true | :disabled | false | :developer  | false
      true | true | :disabled | false | :maintainer | false
      true | true | :disabled | false | :owner      | false

      true | true | :enabled  | true  | :developer  | false
      true | true | :enabled  | true  | :maintainer | false
      true | true | :enabled  | true  | :owner      | false

      true | true | :enabled  | false | :developer  | false
      true | true | :enabled  | false | :maintainer | false
      true | true | :enabled  | false | :owner      | false

      # With ADO disabled scenarios
      true | false | :disabled | true  | :developer  | false
      true | false | :disabled | true  | :maintainer | false
      true | false | :disabled | true  | :owner      | false

      true | false | :disabled | false | :developer  | false
      true | false | :disabled | false | :maintainer | false
      true | false | :disabled | false | :owner      | false

      true | false | :enabled  | true  | :developer  | false
      true | false | :enabled  | true  | :maintainer | false
      true | false | :enabled  | true  | :owner      | false

      true | false | :enabled  | false | :developer  | false
      true | false | :enabled  | false | :maintainer | false
      true | false | :enabled  | false | :owner      | false
    end

    def grant_user_access(project, user, access)
      case access
      when :developer, :maintainer
        project.add_user(user, access)
      when :owner
        project.namespace.update(owner: user)
      end
    end

    with_them do
      let(:project) do
        if project_setting.nil?
          create(:project, :repository)
        else
          create(:project, :repository, :auto_devops)
        end
      end

      before do
        stub_application_setting(auto_devops_enabled: global_setting)

        allow_any_instance_of(Repository).to receive(:gitlab_ci_yml).and_return(gitlab_ci_yml)

        grant_user_access(project, user, user_access)
        project.project_feature.update_attribute(:builds_access_level, feature_visibilities[builds_visibility])
        project.auto_devops.update_attribute(:enabled, project_setting) unless project_setting.nil?
      end

      subject { helper.show_auto_devops_implicitly_enabled_banner?(project, user) }

      it { is_expected.to eq(result) }
    end
  end
802
end