diff options
Diffstat (limited to 'spec/requests/api/graphql/gitlab_schema_spec.rb')
-rw-r--r-- | spec/requests/api/graphql/gitlab_schema_spec.rb | 85 |
1 files changed, 63 insertions, 22 deletions
diff --git a/spec/requests/api/graphql/gitlab_schema_spec.rb b/spec/requests/api/graphql/gitlab_schema_spec.rb index dd518274f82..a724c5c3f1c 100644 --- a/spec/requests/api/graphql/gitlab_schema_spec.rb +++ b/spec/requests/api/graphql/gitlab_schema_spec.rb @@ -3,41 +3,82 @@ 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) } + let(:project) { create(:project) } - 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 + shared_examples 'imposing query limits' do + 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) + subject - expect(graphql_errors.first['message']).to include('which exceeds max complexity of 1') + expect(graphql_errors.flatten.first['message']).to include('which exceeds max complexity of 1') + end 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 + 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) + subject - expect(graphql_errors).to eq(errors) + expect(graphql_errors.flatten).to include(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 + + subject + + expect(Array.wrap(graphql_errors).compact).to be_empty + end + end + end + end + + context 'regular queries' do + subject do + query = graphql_query_for('project', { 'fullPath' => project.full_path }, %w(id name description)) + post_graphql(query) end - context 'when query depth is within range' do - it 'has no error' do - allow(GitlabSchema).to receive(:max_query_depth).and_return 5 + it_behaves_like 'imposing query limits' + end + + context 'multiplexed queries' do + subject do + queries = [ + { query: graphql_query_for('project', { 'fullPath' => project.full_path }, %w(id name description)) }, + { query: graphql_query_for('echo', { 'text' => "$test" }, []), variables: { "test" => "Hello world" } } + ] + + post_multiplex(queries) + end + + it_behaves_like 'imposing query limits' do + it "fails all queries when only one of the queries is too complex" do + # The `project` query above has a complexity of 5 + allow(GitlabSchema).to receive(:max_query_complexity).and_return 4 + + subject - post_graphql(query) + # Expect a response for each query, even though it will be empty + expect(json_response.size).to eq(2) + json_response.each do |single_query_response| + expect(single_query_response).not_to have_key('data') + end - expect(graphql_errors).to be_nil + # Expect errors for each query + expect(graphql_errors.size).to eq(2) + graphql_errors.each do |single_query_errors| + expect(single_query_errors.first['message']).to include('which exceeds max complexity of 4') + end end end end |