project_spec.rb 74.7 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
require 'spec_helper'

5
describe Project do
6
  include ProjectForksHelper
7
  include ::EE::GeoHelpers
8
9
  using RSpec::Parameterized::TableSyntax

10
11
12
13
14
15
  let(:project) { create(:project) }

  it_behaves_like Vulnerable do
    let(:vulnerable) { project }
  end

16
17
  describe 'associations' do
    it { is_expected.to delegate_method(:shared_runners_minutes).to(:statistics) }
18
19
    it { is_expected.to delegate_method(:shared_runners_seconds).to(:statistics) }
    it { is_expected.to delegate_method(:shared_runners_seconds_last_reset).to(:statistics) }
20

ayufanpl's avatar
ayufanpl committed
21
22
23
    it { is_expected.to delegate_method(:actual_shared_runners_minutes_limit).to(:shared_runners_limit_namespace) }
    it { is_expected.to delegate_method(:shared_runners_minutes_limit_enabled?).to(:shared_runners_limit_namespace) }
    it { is_expected.to delegate_method(:shared_runners_minutes_used?).to(:shared_runners_limit_namespace) }
24

25
26
    it { is_expected.to belong_to(:deleting_user) }

27
    it { is_expected.to have_one(:import_state).class_name('ProjectImportState') }
28
    it { is_expected.to have_one(:repository_state).class_name('ProjectRepositoryState').inverse_of(:project) }
29
    it { is_expected.to have_one(:alerting_setting).class_name('Alerting::ProjectAlertingSetting') }
30

31
    it { is_expected.to have_many(:reviews).inverse_of(:project) }
32
    it { is_expected.to have_many(:path_locks) }
33
    it { is_expected.to have_many(:vulnerability_feedback) }
34
    it { is_expected.to have_many(:audit_events).dependent(false) }
35
    it { is_expected.to have_many(:protected_environments) }
36
37
    it { is_expected.to have_many(:approvers).dependent(:destroy) }
    it { is_expected.to have_many(:approver_users).through(:approvers) }
38
    it { is_expected.to have_many(:approver_groups).dependent(:destroy) }
39
40
    it { is_expected.to have_many(:packages).class_name('Packages::Package') }
    it { is_expected.to have_many(:package_files).class_name('Packages::PackageFile') }
41
42
43
44
    it { is_expected.to have_many(:upstream_project_subscriptions) }
    it { is_expected.to have_many(:upstream_projects) }
    it { is_expected.to have_many(:downstream_project_subscriptions) }
    it { is_expected.to have_many(:downstream_projects) }
45
46

    it { is_expected.to have_one(:github_service) }
47
    it { is_expected.to have_many(:project_aliases) }
48
49
  end

50
51
  context 'scopes' do
    describe '.requiring_code_owner_approval' do
52
53
54
55
      let!(:project) { create(:project) }
      let!(:expected_project) { protected_branch_needing_approval.project }
      let!(:protected_branch_needing_approval) { create(:protected_branch, code_owner_approval_required: true) }

56
      it 'only includes the right projects' do
57
        scoped_query_result = described_class.requiring_code_owner_approval
58

59
60
        expect(described_class.count).to eq(2)
        expect(scoped_query_result).to contain_exactly(expected_project)
61
62
      end
    end
63
64
65
66
67
68
69
70
71
72

    describe '.with_wiki_enabled' do
      it 'returns a project' do
        project = create(:project_empty_repo, wiki_access_level: ProjectFeature::ENABLED)
        project1 = create(:project, wiki_access_level: ProjectFeature::DISABLED)

        expect(described_class.with_wiki_enabled).to include(project)
        expect(described_class.with_wiki_enabled).not_to include(project1)
      end
    end
Ash McKenzie's avatar
Ash McKenzie committed
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

    describe '.with_active_services' do
      it 'returns the correct project' do
        active_service = create(:service, active: true)
        inactive_service = create(:service, active: false)

        expect(described_class.with_active_services).to include(active_service.project)
        expect(described_class.with_active_services).not_to include(inactive_service.project)
      end
    end

    describe '.with_active_jira_services' do
      it 'returns the correct project' do
        active_jira_service = create(:jira_service)
        active_service = create(:service, active: true)

        expect(described_class.with_active_jira_services).to include(active_jira_service.project)
        expect(described_class.with_active_jira_services).not_to include(active_service.project)
      end
    end

    describe '.service_desk_enabled' do
      it 'returns the correct project' do
        project_with_service_desk_enabled = create(:project)
        project_with_service_desk_disabled = create(:project, :service_desk_disabled)

        expect(described_class.service_desk_enabled).to include(project_with_service_desk_enabled)
        expect(described_class.service_desk_enabled).not_to include(project_with_service_desk_disabled)
      end
    end

    describe '.with_jira_dvcs_cloud' do
      it 'returns the correct project' do
        jira_dvcs_cloud_project = create(:project, :jira_dvcs_cloud)
        jira_dvcs_server_project = create(:project, :jira_dvcs_server)

        expect(described_class.with_jira_dvcs_cloud).to include(jira_dvcs_cloud_project)
        expect(described_class.with_jira_dvcs_cloud).not_to include(jira_dvcs_server_project)
      end
    end

    describe '.with_jira_dvcs_server' do
      it 'returns the correct project' do
        jira_dvcs_server_project = create(:project, :jira_dvcs_server)
        jira_dvcs_cloud_project = create(:project, :jira_dvcs_cloud)

        expect(described_class.with_jira_dvcs_server).to include(jira_dvcs_server_project)
        expect(described_class.with_jira_dvcs_server).not_to include(jira_dvcs_cloud_project)
      end
    end
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

    describe '.github_imported' do
      it 'returns the correct project' do
        project_imported_from_github = create(:project, :github_imported)
        project_not_imported_from_github = create(:project)

        expect(described_class.github_imported).to include(project_imported_from_github)
        expect(described_class.github_imported).not_to include(project_not_imported_from_github)
      end
    end

    describe '.with_protected_branches' do
      it 'returns the correct project' do
        project_with_protected_branches = create(:project, protected_branches: [create(:protected_branch)])
        project_without_protected_branches = create(:project)

        expect(described_class.with_protected_branches).to include(project_with_protected_branches)
        expect(described_class.with_protected_branches).not_to include(project_without_protected_branches)
      end
    end

    describe '.with_repositories_enabled' do
      it 'returns the correct project' do
        project_with_repositories_enabled = create(:project, :repository_enabled)
        project_with_repositories_disabled = create(:project, :repository_disabled)

        expect(described_class.with_repositories_enabled).to include(project_with_repositories_enabled)
        expect(described_class.with_repositories_enabled).not_to include(project_with_repositories_disabled)
      end
    end
153
154
155
156
157
158
159
160
161
162

    describe '.with_github_service_pipeline_events' do
      it 'returns the correct project' do
        project_with_github_service_pipeline_events = create(:project, github_service: create(:github_service))
        project_without_github_service_pipeline_events = create(:project)

        expect(described_class.with_github_service_pipeline_events).to include(project_with_github_service_pipeline_events)
        expect(described_class.with_github_service_pipeline_events).not_to include(project_without_github_service_pipeline_events)
      end
    end
163
164
165
166
167
168
169
170
171
172

    describe '.with_active_prometheus_service' do
      it 'returns the correct project' do
        project_with_active_prometheus_service = create(:prometheus_project)
        project_without_active_prometheus_service = create(:project)

        expect(described_class.with_active_prometheus_service).to include(project_with_active_prometheus_service)
        expect(described_class.with_active_prometheus_service).not_to include(project_without_active_prometheus_service)
      end
    end
173
174
  end

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  describe 'validations' do
    let(:project) { build(:project) }

    describe 'variables' do
      let(:first_variable) { build(:ci_variable, key: 'test_key', value: 'first', environment_scope: 'prod', project: project) }
      let(:second_variable) { build(:ci_variable, key: 'test_key', value: 'other', environment_scope: 'other', project: project) }

      before do
        project.variables << first_variable
        project.variables << second_variable
      end

      context 'with duplicate variables with same environment scope' do
        before do
          project.variables.last.environment_scope = project.variables.first.environment_scope
        end

        it { expect(project).not_to be_valid }
      end

      context 'with same variable keys and different environment scope' do
        it { expect(project).to be_valid }
      end
    end
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

    context 'mirror' do
      subject { build(:project, mirror: true) }

      it { is_expected.to validate_presence_of(:import_url) }
      it { is_expected.to validate_presence_of(:mirror_user) }
    end

    it 'creates import state when mirror gets enabled' do
      project2 = create(:project)

      expect do
        project2.update(mirror: true, import_url: generate(:url), mirror_user: project.creator)
      end.to change { ProjectImportState.where(project: project2).count }.from(0).to(1)
    end
214
215
216
217
218
219
220
221
222
223

    describe 'pull_mirror_branch_prefix' do
      it { is_expected.to validate_length_of(:pull_mirror_branch_prefix).is_at_most(50) }

      it 'rejects invalid git refs' do
        project = build(:project, pull_mirror_branch_prefix: 'an invalid prefix..')

        expect(project).not_to be_valid
      end
    end
224
225
  end

Tiago Botelho's avatar
Tiago Botelho committed
226
227
228
229
230
231
232
233
234
235
  describe 'setting up a mirror' do
    context 'when new project' do
      it 'creates import_state and sets next_execution_timestamp to now' do
        project = build(:project, :mirror)

        Timecop.freeze do
          expect do
            project.save
          end.to change { ProjectImportState.count }.by(1)

236
          expect(project.import_state.next_execution_timestamp).to be_like_time(Time.now)
Tiago Botelho's avatar
Tiago Botelho committed
237
238
239
240
241
242
243
244
245
246
247
        end
      end
    end

    context 'when project already exists' do
      context 'when project is not import' do
        it 'creates import_state and sets next_execution_timestamp to now' do
          project = create(:project)

          Timecop.freeze do
            expect do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
248
              project.update(mirror: true, mirror_user_id: project.creator.id, import_url: generate(:url))
Tiago Botelho's avatar
Tiago Botelho committed
249
250
            end.to change { ProjectImportState.count }.by(1)

251
            expect(project.import_state.next_execution_timestamp).to be_like_time(Time.now)
Tiago Botelho's avatar
Tiago Botelho committed
252
253
254
255
256
257
258
259
260
261
          end
        end
      end

      context 'when project is import' do
        it 'sets current import_state next_execution_timestamp to now' do
          project = create(:project, import_url: generate(:url))

          Timecop.freeze do
            expect do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
262
              project.update(mirror: true, mirror_user_id: project.creator.id)
Tiago Botelho's avatar
Tiago Botelho committed
263
264
            end.not_to change { ProjectImportState.count }

265
            expect(project.import_state.next_execution_timestamp).to be_like_time(Time.now)
Tiago Botelho's avatar
Tiago Botelho committed
266
267
268
269
270
271
          end
        end
      end
    end
  end

272
273
  describe '.mirrors_to_sync' do
    let(:timestamp) { Time.now }
274

275
276
277
    context 'when mirror is scheduled' do
      it 'returns empty' do
        create(:project, :mirror, :import_scheduled)
278

279
        expect(described_class.mirrors_to_sync(timestamp)).to be_empty
280
281
282
      end
    end

283
284
285
    context 'when mirror is started' do
      it 'returns empty' do
        create(:project, :mirror, :import_scheduled)
286

287
288
289
        expect(described_class.mirrors_to_sync(timestamp)).to be_empty
      end
    end
290

291
    context 'when mirror is finished' do
292
293
      let!(:project) { create(:project) }
      let!(:import_state) { create(:import_state, :mirror, :finished, project: project) }
294
295
296
297
298
299

      it 'returns project if next_execution_timestamp is not in the future' do
        expect(described_class.mirrors_to_sync(timestamp)).to match_array(project)
      end

      it 'returns empty if next_execution_timestamp is in the future' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
300
        import_state.update(next_execution_timestamp: timestamp + 2.minutes)
301
302
303

        expect(described_class.mirrors_to_sync(timestamp)).to be_empty
      end
Andreas Brandl's avatar
Andreas Brandl committed
304
305
306
307
308
309
310
311
312
313
314

      context 'when a limit is applied' do
        before do
          another_project = create(:project)
          create(:import_state, :mirror, :finished, project: another_project)
        end

        it 'returns project if next_execution_timestamp is not in the future' do
          expect(described_class.mirrors_to_sync(timestamp, limit: 1)).to match_array(project)
        end
      end
315
316
317
318
319
320
321
322
323
324
    end

    context 'when project is failed' do
      let!(:project) { create(:project, :mirror, :import_failed) }

      it 'returns project if next_execution_timestamp is not in the future' do
        expect(described_class.mirrors_to_sync(timestamp)).to match_array(project)
      end

      it 'returns empty if next_execution_timestamp is in the future' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
325
        project.import_state.update(next_execution_timestamp: timestamp + 2.minutes)
326
327
328
329
330
331
332
333
334
335

        expect(described_class.mirrors_to_sync(timestamp)).to be_empty
      end

      context 'with retry limit exceeded' do
        let!(:project) { create(:project, :mirror, :import_hard_failed) }

        it 'returns empty' do
          expect(described_class.mirrors_to_sync(timestamp)).to be_empty
        end
336
337
338
339
      end
    end
  end

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
  describe '#can_store_security_reports?' do
    context 'when the feature is enabled for the namespace' do
      it 'returns true' do
        stub_licensed_features(sast: true)
        project = create(:project, :private)

        expect(project.can_store_security_reports?).to be_truthy
      end
    end

    context 'when the project is public' do
      it 'returns true' do
        stub_licensed_features(sast: false)
        project = create(:project, :public)

        expect(project.can_store_security_reports?).to be_truthy
      end
    end

    context 'when the feature is disabled for the namespace and the project is not public' do
      it 'returns false' do
        stub_licensed_features(sast: false)
        project = create(:project, :private)

        expect(project.can_store_security_reports?).to be_falsy
      end
    end
  end

369
370
371
372
373
374
375
376
377
378
379
  describe '#deployment_variables' do
    context 'when project has a deployment platforms' do
      context 'when multiple clusters (EEP) is enabled' do
        before do
          stub_licensed_features(multiple_clusters: true)
        end

        let(:project) { create(:project) }

        let!(:default_cluster) do
          create(:cluster,
380
                 :not_managed,
381
382
383
384
385
386
387
388
                 platform_type: :kubernetes,
                 projects: [project],
                 environment_scope: '*',
                 platform_kubernetes: default_cluster_kubernetes)
        end

        let!(:review_env_cluster) do
          create(:cluster,
389
                 :not_managed,
390
391
392
393
394
395
396
397
398
399
400
401
402
403
                 platform_type: :kubernetes,
                 projects: [project],
                 environment_scope: 'review/*',
                 platform_kubernetes: review_env_cluster_kubernetes)
        end

        let(:default_cluster_kubernetes) { create(:cluster_platform_kubernetes, token: 'default-AAA') }
        let(:review_env_cluster_kubernetes) { create(:cluster_platform_kubernetes, token: 'review-AAA') }

        context 'when environment name is review/name' do
          let!(:environment) { create(:environment, project: project, name: 'review/name') }

          it 'returns variables from this service' do
            expect(project.deployment_variables(environment: 'review/name'))
404
              .to include(key: 'KUBE_TOKEN', value: 'review-AAA', public: false, masked: true)
405
406
407
408
409
410
411
412
          end
        end

        context 'when environment name is other' do
          let!(:environment) { create(:environment, project: project, name: 'staging/name') }

          it 'returns variables from this service' do
            expect(project.deployment_variables(environment: 'staging/name'))
413
              .to include(key: 'KUBE_TOKEN', value: 'default-AAA', public: false, masked: true)
414
415
416
417
418
419
          end
        end
      end
    end
  end

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
  describe '#environments_for_scope' do
    set(:project) { create(:project) }

    before do
      create_list(:environment, 2, project: project)
    end

    it 'retrieves all project environments when using the * wildcard' do
      expect(project.environments_for_scope("*")).to eq(project.environments)
    end

    it 'retrieves a specific project environment when using the name of that environment' do
      environment = project.environments.first

      expect(project.environments_for_scope(environment.name)).to eq([environment])
    end
  end

438
439
440
441
442
443
444
445
446
447
  describe '#ensure_external_webhook_token' do
    let(:project) { create(:project, :repository) }

    it "sets external_webhook_token when it's missing" do
      project.update_attribute(:external_webhook_token, nil)
      expect(project.external_webhook_token).to be_blank

      project.ensure_external_webhook_token
      expect(project.external_webhook_token).to be_present
    end
448
  end
449

450
  describe '#push_rule' do
451
    let(:project) { create(:project, push_rule: create(:push_rule)) }
452

453
    subject(:push_rule) { project.reload_push_rule }
454
455
456
457
458
459
460
461
462
463
464
465

    it { is_expected.not_to be_nil }

    context 'push rules unlicensed' do
      before do
        stub_licensed_features(push_rules: false)
      end

      it { is_expected.to be_nil }
    end
  end

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
  describe '#has_active_hooks?' do
    context "with group hooks" do
      let(:group) { create(:group) }
      let(:project) { create(:project, namespace: group) }
      let!(:group_hook) { create(:group_hook, group: group, push_events: true) }

      before do
        stub_licensed_features(group_webhooks: true)
      end

      it 'returns true' do
        expect(project.has_active_hooks?).to be_truthy
        expect(project.has_group_hooks?).to be_truthy
      end
    end

    context 'with no group hooks' do
      it 'returns false' do
        expect(project.has_active_hooks?).to be_falsey
        expect(project.has_group_hooks?).to be_falsey
      end
    end
  end

490
491
492
  describe "#execute_hooks" do
    context "group hooks" do
      let(:group) { create(:group) }
493
      let(:project) { create(:project, namespace: group) }
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
      let(:group_hook) { create(:group_hook, group: group, push_events: true) }

      it 'executes the hook when the feature is enabled' do
        stub_licensed_features(group_webhooks: true)

        fake_service = double
        expect(WebHookService).to receive(:new)
                                    .with(group_hook, { some: 'info' }, 'push_hooks') { fake_service }
        expect(fake_service).to receive(:async_execute)

        project.execute_hooks(some: 'info')
      end

      it 'does not execute the hook when the feature is disabled' do
        stub_licensed_features(group_webhooks: false)

        expect(WebHookService).not_to receive(:new)
                                        .with(group_hook, { some: 'info' }, 'push_hooks')

        project.execute_hooks(some: 'info')
      end
    end
  end

518
519
520
  describe '#execute_hooks' do
    it "triggers project and group hooks" do
      group = create :group, name: 'gitlab'
521
      project = create(:project, name: 'gitlabhq', namespace: group)
522
523
524
525
526
527
528
529
530
531
532
533
534
535
      project_hook = create(:project_hook, push_events: true, project: project)
      group_hook = create(:group_hook, push_events: true, group: group)

      stub_request(:post, project_hook.url)
      stub_request(:post, group_hook.url)

      expect_any_instance_of(GroupHook).to receive(:async_execute).and_return(true)
      expect_any_instance_of(ProjectHook).to receive(:async_execute).and_return(true)

      project.execute_hooks({}, :push_hooks)
    end
  end

  describe '#allowed_to_share_with_group?' do
536
    let(:project) { create(:project) }
537
538
539
540
541
542
543
544
545
546
547

    it "returns true" do
      expect(project.allowed_to_share_with_group?).to be_truthy
    end

    it "returns false" do
      project.namespace.update(share_with_group_lock: true)
      expect(project.allowed_to_share_with_group?).to be_falsey
    end
  end

548
549
  describe '#alpha/beta_feature_available?' do
    it_behaves_like 'an entity with alpha/beta feature support' do
550
551
552
553
      let(:entity) { create(:project) }
    end
  end

554
  describe '#feature_available?' do
555
    let(:namespace) { build(:namespace) }
556
    let(:plan_license) { nil }
557
558
    let(:project) { build(:project, namespace: namespace) }
    let(:user) { build(:user) }
559
560
561
562
563
564
565

    subject { project.feature_available?(feature, user) }

    context 'when feature symbol is included on Namespace features code' do
      before do
        stub_application_setting('check_namespace_plan?' => check_namespace_plan)
        allow(Gitlab).to receive(:com?) { true }
566
        stub_licensed_features(feature => allowed_on_global_license)
567
568
569
        allow(namespace).to receive(:plan) { plan_license }
      end

570
571
      License::EEU_FEATURES.each do |feature_sym|
        let(:feature) { feature_sym }
572

573
574
575
576
        context feature_sym.to_s do
          unless License::GLOBAL_FEATURES.include?(feature_sym)
            context "checking #{feature_sym} availability both on Global and Namespace license" do
              let(:check_namespace_plan) { true }
577

578
579
              context 'allowed by Plan License AND Global License' do
                let(:allowed_on_global_license) { true }
580
581
582
583
584
                let(:plan_license) { build(:gold_plan) }

                before do
                  allow(namespace).to receive(:plans) { [plan_license] }
                end
585

586
587
588
                it 'returns true' do
                  is_expected.to eq(true)
                end
589
590
591
592
593
594
595
596

                context 'when feature is disabled by a feature flag' do
                  it 'returns false' do
                    stub_feature_flags(feature => false)

                    is_expected.to eq(false)
                  end
                end
597
              end
598

599
600
              context 'not allowed by Plan License but project and namespace are public' do
                let(:allowed_on_global_license) { true }
601
                let(:plan_license) { build(:bronze_plan) }
602

603
604
605
                it 'returns true' do
                  allow(namespace).to receive(:public?) { true }
                  allow(project).to receive(:public?) { true }
606

607
608
                  is_expected.to eq(true)
                end
609
              end
610

611
612
613
              unless License.plan_includes_feature?(License::STARTER_PLAN, feature_sym)
                context 'not allowed by Plan License' do
                  let(:allowed_on_global_license) { true }
614
                  let(:plan_license) { build(:bronze_plan) }
615

616
617
618
                  it 'returns false' do
                    is_expected.to eq(false)
                  end
619
620
                end
              end
621

622
623
              context 'not allowed by Global License' do
                let(:allowed_on_global_license) { false }
624
                let(:plan_license) { build(:gold_plan) }
625

626
627
628
                it 'returns false' do
                  is_expected.to eq(false)
                end
629
              end
630
631
632
            end
          end

633
          context "when checking #{feature_sym} only for Global license" do
634
            let(:check_namespace_plan) { false }
635

636
637
            context 'allowed by Global License' do
              let(:allowed_on_global_license) { true }
638

639
640
641
              it 'returns true' do
                is_expected.to eq(true)
              end
642
643
            end

644
645
            context 'not allowed by Global License' do
              let(:allowed_on_global_license) { false }
646

647
648
649
              it 'returns false' do
                is_expected.to eq(false)
              end
650
651
652
653
654
655
            end
          end
        end
      end
    end

Bob Van Landuyt's avatar
Bob Van Landuyt committed
656
657
658
659
660
661
662
    it 'only loads licensed availability once' do
      expect(project).to receive(:load_licensed_feature_available)
                             .once.and_call_original

      2.times { project.feature_available?(:service_desk) }
    end

663
664
665
666
667
668
669
670
671
672
673
    context 'when feature symbol is not included on Namespace features code' do
      let(:feature) { :issues }

      it 'checks availability of licensed feature' do
        expect(project.project_feature).to receive(:feature_available?).with(feature, user)

        subject
      end
    end
  end

674
675
676
677
678
679
680
681
682
683
684
  describe '#fetch_mirror' do
    where(:import_url, :auth_method, :expected) do
      'http://foo:bar@example.com' | 'password'       | 'http://foo:bar@example.com'
      'ssh://foo:bar@example.com'  | 'password'       | 'ssh://foo:bar@example.com'
      'ssh://foo:bar@example.com'  | 'ssh_public_key' | 'ssh://foo@example.com'
    end

    with_them do
      let(:project) { build(:project, :mirror, import_url: import_url, import_data_attributes: { auth_method: auth_method } ) }

      it do
685
        expect(project.repository).to receive(:fetch_upstream).with(expected, forced: false)
686
687
688
689
690
691

        project.fetch_mirror
      end
    end
  end

692
693
694
695
696
697
  describe 'updating import_url' do
    it 'removes previous remote' do
      project = create(:project, :repository, :mirror)

      expect(RepositoryRemoveRemoteWorker).to receive(:perform_async).with(project.id, ::Repository::MIRROR_REMOTE).and_call_original

Lin Jen-Shin's avatar
Lin Jen-Shin committed
698
      project.update(import_url: "http://test.com")
699
700
701
    end
  end

702
  describe '#any_runners_limit' do
703
    let(:project) { create(:project, shared_runners_enabled: shared_runners_enabled) }
704
705
    let(:specific_runner) { create(:ci_runner, :project) }
    let(:shared_runner) { create(:ci_runner, :instance) }
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

    context 'for shared runners enabled' do
      let(:shared_runners_enabled) { true }

      before do
        shared_runner
      end

      it 'has a shared runner' do
        expect(project.any_runners?).to be_truthy
      end

      it 'checks the presence of shared runner' do
        expect(project.any_runners? { |runner| runner == shared_runner }).to be_truthy
      end

722
      context 'with used pipeline minutes' do
723
724
        let(:namespace) { create(:namespace, :with_used_build_minutes_limit) }
        let(:project) do
725
          create(:project,
726
727
728
729
730
731
732
733
734
735
736
737
738
739
            namespace: namespace,
            shared_runners_enabled: shared_runners_enabled)
        end

        it 'does not have a shared runner' do
          expect(project.any_runners?).to be_falsey
        end
      end
    end
  end

  describe '#shared_runners_available?' do
    subject { project.shared_runners_available? }

740
    context 'with used pipeline minutes' do
741
742
      let(:namespace) { create(:namespace, :with_used_build_minutes_limit) }
      let(:project) do
743
        create(:project,
744
745
746
747
748
749
750
751
752
753
754
755
756
757
          namespace: namespace,
          shared_runners_enabled: true)
      end

      before do
        expect(namespace).to receive(:shared_runners_minutes_used?).and_call_original
      end

      it 'shared runners are not available' do
        expect(project.shared_runners_available?).to be_falsey
      end
    end
  end

Thong Kuah's avatar
Thong Kuah committed
758
759
760
761
762
  describe '#root_namespace' do
    let(:project) { build(:project, namespace: parent) }

    subject { project.root_namespace }

763
    context 'when namespace has parent group' do
Thong Kuah's avatar
Thong Kuah committed
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
      let(:root_ancestor) { create(:group) }
      let(:parent) { create(:group, parent: root_ancestor) }

      it 'returns root ancestor' do
        is_expected.to eq(root_ancestor)
      end
    end

    context 'when namespace is root ancestor' do
      let(:parent) { create(:group) }

      it 'returns current namespace' do
        is_expected.to eq(parent)
      end
    end
  end

781
  describe '#shared_runners_limit_namespace' do
ayufanpl's avatar
ayufanpl committed
782
783
784
785
786
787
    set(:root_ancestor) { create(:group) }
    set(:group) { create(:group, parent: root_ancestor) }
    let(:project) { create(:project, namespace: group) }

    subject { project.shared_runners_limit_namespace }

788
789
    it 'returns root namespace' do
      is_expected.to eq(root_ancestor)
ayufanpl's avatar
ayufanpl committed
790
791
792
    end
  end

793
  describe '#shared_runners_minutes_limit_enabled?' do
794
    let(:project) { create(:project) }
795
796
797
798

    subject { project.shared_runners_minutes_limit_enabled? }

    before do
Douwe Maan's avatar
Douwe Maan committed
799
800
      allow(project.namespace).to receive(:shared_runners_minutes_limit_enabled?)
        .and_return(true)
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
    end

    context 'with shared runners enabled' do
      before do
        project.shared_runners_enabled = true
      end

      context 'for public project' do
        before do
          project.visibility_level = Project::PUBLIC
        end

        it { is_expected.to be_falsey }
      end

      context 'for internal project' do
        before do
          project.visibility_level = Project::INTERNAL
        end

        it { is_expected.to be_truthy }
      end

      context 'for private project' do
        before do
          project.visibility_level = Project::INTERNAL
        end

        it { is_expected.to be_truthy }
      end
    end

    context 'without shared runners' do
      before do
        project.shared_runners_enabled = false
      end

      it { is_expected.to be_falsey }
    end
  end
841

842
  describe '#size_limit_enabled?' do
843
    let(:project) { create(:project) }
844
845
846
847
848
849
850
851
852

    context 'when repository_size_limit is not configured' do
      it 'is disabled' do
        expect(project.size_limit_enabled?).to be_falsey
      end
    end

    context 'when repository_size_limit is configured' do
      before do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
853
        project.update(repository_size_limit: 1024)
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
      end

      context 'with an EES license' do
        let!(:license) { create(:license, plan: License::STARTER_PLAN) }

        it 'is enabled' do
          expect(project.size_limit_enabled?).to be_truthy
        end
      end

      context 'with an EEP license' do
        let!(:license) { create(:license, plan: License::PREMIUM_PLAN) }

        it 'is enabled' do
          expect(project.size_limit_enabled?).to be_truthy
        end
      end

      context 'without a License' do
        before do
874
          License.destroy_all # rubocop: disable DestroyAll
875
876
877
878
879
880
881
882
883
        end

        it 'is disabled' do
          expect(project.size_limit_enabled?).to be_falsey
        end
      end
    end
  end

884
  describe '#service_desk_enabled?' do
885
    let!(:license) { create(:license, plan: License::PREMIUM_PLAN) }
886
887
    let(:namespace) { create(:namespace) }

888
    subject(:project) { build(:project, :private, namespace: namespace, service_desk_enabled: true) }
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911

    before do
      allow(::Gitlab).to receive(:com?).and_return(true)
      allow(::Gitlab::IncomingEmail).to receive(:enabled?).and_return(true)
      allow(::Gitlab::IncomingEmail).to receive(:supports_wildcard?).and_return(true)
    end

    it 'is enabled' do
      expect(project.service_desk_enabled?).to be_truthy
      expect(project.service_desk_enabled).to be_truthy
    end

    context 'namespace plans active' do
      before do
        stub_application_setting(check_namespace_plan: true)
      end

      it 'is disabled' do
        expect(project.service_desk_enabled?).to be_falsy
        expect(project.service_desk_enabled).to be_falsy
      end

      context 'Service Desk available in namespace plan' do
912
        let!(:gitlab_subscription) { create(:gitlab_subscription, :silver, namespace: namespace) }
913
914
915
916
917
918
919
920
921

        it 'is enabled' do
          expect(project.service_desk_enabled?).to be_truthy
          expect(project.service_desk_enabled).to be_truthy
        end
      end
    end
  end

922
  describe '#service_desk_address' do
923
    let(:project) { create(:project, service_desk_enabled: true) }
924

Felipe Artur's avatar
Felipe Artur committed
925
    before do
926
      allow(::EE::Gitlab::ServiceDesk).to receive(:enabled?).and_return(true)
Stan Hu's avatar
Stan Hu committed
927
      allow(Gitlab.config.incoming_email).to receive(:enabled).and_return(true)
928
      allow(Gitlab.config.incoming_email).to receive(:address).and_return("test+%{key}@mail.com")
Felipe Artur's avatar
Felipe Artur committed
929
930
    end

931
    it 'uses project full path as service desk address key' do
932
      expect(project.service_desk_address).to eq("test+#{project.full_path_slug}-#{project.project_id}-issue-@mail.com")
933
934
    end
  end
935

936
  describe '#approvals_before_merge' do
937
938
939
940
941
942
    where(:license_value, :db_value, :expected) do
      true  | 5 | 5
      true  | 0 | 0
      false | 5 | 0
      false | 0 | 0
    end
943

944
945
946
947
    with_them do
      let(:project) { build(:project, approvals_before_merge: db_value) }

      subject { project.approvals_before_merge }
948

949
950
      before do
        stub_licensed_features(merge_request_approvers: license_value)
951
      end
952
953

      it { is_expected.to eq(expected) }
954
955
956
957
    end
  end

  describe "#reset_approvals_on_push?" do
958
959
960
961
962
963
    where(:license_value, :db_value, :expected) do
      true  | true  | true
      true  | false | false
      false | true  | false
      false | false | false
    end
964

965
966
967
968
    with_them do
      let(:project) { build(:project, reset_approvals_on_push: db_value) }

      subject { project.reset_approvals_on_push? }
969

970
971
      before do
        stub_licensed_features(merge_request_approvers: license_value)
972
      end
973
974

      it { is_expected.to eq(expected) }
975
976
977
978
    end
  end

  describe '#approvals_before_merge' do
979
980
981
982
983
984
    where(:license_value, :db_value, :expected) do
      true  | 5 | 5
      true  | 0 | 0
      false | 5 | 0
      false | 0 | 0
    end
985

986
987
988
989
    with_them do
      let(:project) { build(:project, approvals_before_merge: db_value) }

      subject { project.approvals_before_merge }
990

991
992
      before do
        stub_licensed_features(merge_request_approvers: license_value)
993
      end
994
995

      it { is_expected.to eq(expected) }
996
997
998
    end
  end

999
  describe '#visible_user_defined_rules' do
1000
    let(:project) { create(:project) }