summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/groups_query_spec.rb
blob: 84c8d3c3388fbb02561caf45e4efffff3d7b1b62 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'searching groups', :with_license, feature_category: :subgroups do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:public_group) { create(:group, :public) }
  let_it_be(:private_group) { create(:group, :private) }

  let(:fields) do
    <<~FIELDS
      nodes {
        #{all_graphql_fields_for('Group')}
      }
    FIELDS
  end

  let(:query) do
    <<~QUERY
      query {
        groups {
          #{fields}
        }
      }
    QUERY
  end

  subject { post_graphql(query, current_user: user) }

  describe "Query groups(search)" do
    let(:groups) { graphql_data_at(:groups, :nodes) }
    let(:names) { groups.map { |group| group["name"] } } # rubocop: disable Rails/Pluck

    it_behaves_like 'a working graphql query' do
      before do
        subject
      end
    end

    it 'includes public groups' do
      subject

      expect(names).to eq([public_group.name])
    end

    it 'includes accessible private groups ordered by name' do
      private_group.add_maintainer(user)

      subject

      expect(names).to eq([public_group.name, private_group.name])
    end

    context 'with `search` argument' do
      let_it_be(:other_group) { create(:group, name: 'other-group') }

      let(:query) do
        <<~QUERY
          query {
            groups(search: "oth") {
              #{fields}
            }
          }
        QUERY
      end

      it 'filters groups by name' do
        subject

        expect(names).to contain_exactly(other_group.name)
      end
    end
  end
end