summaryrefslogtreecommitdiff
path: root/spec/graphql/resolvers/ci/group_runners_resolver_spec.rb
blob: 5d06db904d5efa5602ca04172e59fb35385e3f7a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Resolvers::Ci::GroupRunnersResolver, feature_category: :runner_fleet do
  include GraphqlHelpers

  describe '#resolve' do
    subject do
      resolve(described_class, obj: obj, ctx: { current_user: user }, args: args,
                               arg_style: :internal)
    end

    include_context 'runners resolver setup'

    let(:obj) { group }
    let(:args) { {} }

    # First, we can do a couple of basic real tests to verify common cases. That ensures that the code works.
    context 'when user cannot see runners' do
      it 'returns no runners' do
        expect(subject.items.to_a).to eq([])
      end
    end

    context 'with user as group owner' do
      before do
        group.add_owner(user)
      end

      it 'returns all the runners' do
        expect(subject.items.to_a).to contain_exactly(inactive_project_runner, offline_project_runner, group_runner, subgroup_runner)
      end

      context 'with membership direct' do
        let(:args) { { membership: :direct } }

        it 'returns only direct runners' do
          expect(subject.items.to_a).to contain_exactly(group_runner)
        end
      end
    end

    # Then, we can check specific edge cases for this resolver
    context 'with obj set to nil' do
      let(:obj) { nil }

      it 'raises an error' do
        expect { subject }.to raise_error('Expected group missing')
      end
    end

    context 'with obj not set to group' do
      let(:obj) { build(:project) }

      it 'raises an error' do
        expect { subject }.to raise_error('Expected group missing')
      end
    end

    # Here we have a mocked part. We assume that all possible edge cases are covered in RunnersFinder spec. So we don't need to test them twice.
    # Only thing we can do is to verify that args from the resolver is correctly transformed to params of the Finder and we return the Finder's result back.
    describe 'Allowed query arguments' do
      let(:finder) { instance_double(::Ci::RunnersFinder) }
      let(:args) do
        {
          status: 'active',
          type: :group_type,
          tag_list: ['active_runner'],
          search: 'abc',
          sort: :contacted_asc,
          membership: :descendants
        }
      end

      let(:expected_params) do
        {
          status_status: 'active',
          type_type: :group_type,
          tag_name: ['active_runner'],
          preload: { tag_name: false },
          search: 'abc',
          sort: 'contacted_asc',
          membership: :descendants,
          group: group
        }
      end

      it 'calls RunnersFinder with expected arguments' do
        allow(::Ci::RunnersFinder).to receive(:new).with(current_user: user, params: expected_params).once.and_return(finder)
        allow(finder).to receive(:execute).once.and_return([:execute_return_value])

        expect(subject.items.to_a).to eq([:execute_return_value])
      end
    end
  end
end