Welcome to mirror list, hosted at ThFree Co, Russian Federation.

all_jobs_resolver_spec.rb « ci « resolvers « graphql « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6b9e3a484b14fab5d938457c6df3ba7efadffbd6 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Resolvers::Ci::AllJobsResolver, feature_category: :continuous_integration do
  include GraphqlHelpers

  let_it_be(:instance_runner) { create(:ci_runner, :instance) }
  let_it_be(:successful_job) { create(:ci_build, :success, name: 'successful_job') }
  let_it_be(:successful_job_two) { create(:ci_build, :success, name: 'successful_job_two') }
  let_it_be(:failed_job) { create(:ci_build, :failed, name: 'failed_job') }
  let_it_be(:pending_job) { create(:ci_build, :pending, name: 'pending_job') }

  let(:args) { {} }

  describe '#resolve' do
    subject(:request) { resolve_jobs(args) }

    context 'when current user is an admin' do
      let_it_be(:current_user) { create(:admin) }

      shared_examples 'executes as admin' do
        context "with argument `statuses`" do
          using RSpec::Parameterized::TableSyntax

          where(:statuses, :expected_jobs) do
            nil                | lazy { [successful_job, successful_job_two, failed_job, pending_job] }
            %w[SUCCESS]        | lazy { [successful_job, successful_job_two] }
            %w[SUCCESS FAILED] | lazy { [successful_job, successful_job_two, failed_job] }
            %w[CANCELED]       | lazy { [] }
          end

          with_them do
            let(:args) do
              { statuses: statuses&.map { |status| Types::Ci::JobStatusEnum.coerce_isolated_input(status) } }
            end

            it { is_expected.to contain_exactly(*expected_jobs) }
          end
        end

        context "with argument `runner_types`" do
          let_it_be(:successful_job_with_instance_runner) do
            create(:ci_build, :success, name: 'successful_job_with_instance_runner', runner: instance_runner)
          end

          context 'with feature flag :admin_jobs_filter_runner_type enabled' do
            using RSpec::Parameterized::TableSyntax

            where(:runner_types, :expected_jobs) do
              nil | lazy do
                [
                  successful_job,
                  successful_job_two,
                  failed_job,
                  pending_job,
                  successful_job_with_instance_runner
                ]
              end
              %w[INSTANCE_TYPE]            | lazy { [successful_job_with_instance_runner] }
              %w[INSTANCE_TYPE GROUP_TYPE] | lazy { [successful_job_with_instance_runner] }
              %w[PROJECT_TYPE]             | lazy { [] }
            end

            with_them do
              let(:args) do
                {
                  runner_types: runner_types&.map { |type| Types::Ci::RunnerTypeEnum.coerce_isolated_input(type) }
                }
              end

              it { is_expected.to match_array(expected_jobs) }
            end
          end
        end

        context "with argument combination" do
          let_it_be(:successful_job_with_instance_runner) do
            create(
              :ci_build,
              :success,
              name: 'successful_job_with_instance_runner',
              runner: instance_runner
            )
          end

          let_it_be(:running_job_with_group_runner) do
            create(:ci_build, :running, name: 'running_job_with_instance_runner', runner: create(:ci_runner, :group))
          end

          context 'with feature flag :admin_jobs_filter_runner_type enabled' do
            using RSpec::Parameterized::TableSyntax

            where(:statuses, :runner_types, :expected_jobs) do
              %w[SUCCESS]         | %w[INSTANCE_TYPE]            | lazy { [successful_job_with_instance_runner] }
              %w[CANCELED]        | %w[INSTANCE_TYPE]            | lazy { [] }
              %w[SUCCESS RUNNING] | %w[INSTANCE_TYPE GROUP_TYPE] | lazy do
                                                                     [
                                                                       successful_job_with_instance_runner,
                                                                       running_job_with_group_runner
                                                                     ]
                                                                   end
            end

            with_them do
              let(:args) do
                {
                  statuses: statuses&.map { |status| Types::Ci::JobStatusEnum.coerce_isolated_input(status) },
                  runner_types: runner_types&.map { |type| Types::Ci::RunnerTypeEnum.coerce_isolated_input(type) }
                }
              end

              it { is_expected.to contain_exactly(*expected_jobs) }
            end
          end
        end
      end

      context 'when admin mode setting is disabled', :do_not_mock_admin_mode_setting do
        it_behaves_like 'executes as admin'
      end

      context 'when admin mode setting is enabled' do
        context 'when in admin mode', :enable_admin_mode do
          it_behaves_like 'executes as admin'
        end

        context 'when not in admin mode' do
          it { is_expected.to be_empty }
        end
      end
    end

    context 'with unauthorized user' do
      let_it_be(:unauth_user) { create(:user) }

      let(:current_user) { unauth_user }

      it { is_expected.to be_empty }
    end
  end

  private

  def resolve_jobs(args = {}, context = { current_user: current_user })
    resolve(described_class, args: args, ctx: context)
  end
end