summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/gitlab_schema_spec.rb
blob: dd518274f82c00435062a9dbbaeb985a41f816b2 (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
require 'spec_helper'

describe 'GitlabSchema configurations' do
  include GraphqlHelpers

  let(:project) { create(:project, :repository) }
  let(:query) { graphql_query_for('project', { 'fullPath' => project.full_path }, %w(id name description)) }
  let(:current_user) { create(:user) }

  describe '#max_complexity' do
    context 'when complexity is too high' do
      it 'shows an error' do
        allow(GitlabSchema).to receive(:max_query_complexity).and_return 1

        post_graphql(query, current_user: nil)

        expect(graphql_errors.first['message']).to include('which exceeds max complexity of 1')
      end
    end
  end

  describe '#max_depth' do
    context 'when query depth is too high' do
      it 'shows error' do
        errors = [{ "message" => "Query has depth of 2, which exceeds max depth of 1" }]
        allow(GitlabSchema).to receive(:max_query_depth).and_return 1

        post_graphql(query)

        expect(graphql_errors).to eq(errors)
      end
    end

    context 'when query depth is within range' do
      it 'has no error' do
        allow(GitlabSchema).to receive(:max_query_depth).and_return 5

        post_graphql(query)

        expect(graphql_errors).to be_nil
      end
    end
  end

  context 'when IntrospectionQuery' do
    it 'is not too complex' do
      query = File.read(Rails.root.join('spec/fixtures/api/graphql/introspection.graphql'))

      post_graphql(query, current_user: nil)

      expect(graphql_errors).to be_nil
    end
  end
end