Commit aea37f50 authored by Luke Duncalfe's avatar Luke Duncalfe

Rename max complexity constants for consistency

Renaming the constants so they all end with `_MAX_COMPLEXITY`. This is
for consistency and clarity.

Also refactoring the specs in `spec/graphql/gitlab_schema_spec.rb` to
be more thorough.
parent 0abdf377
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
class GitlabSchema < GraphQL::Schema class GitlabSchema < GraphQL::Schema
# Currently an IntrospectionQuery has a complexity of 179. # Currently an IntrospectionQuery has a complexity of 179.
# These values will evolve over time. # These values will evolve over time.
DEFAULT_MAX_COMPLEXITY = 200 DEFAULT_MAX_COMPLEXITY = 200
AUTHENTICATED_COMPLEXITY = 250 AUTHENTICATED_MAX_COMPLEXITY = 250
ADMIN_COMPLEXITY = 300 ADMIN_MAX_COMPLEXITY = 300
DEFAULT_MAX_DEPTH = 15 DEFAULT_MAX_DEPTH = 15
AUTHENTICATED_MAX_DEPTH = 20 AUTHENTICATED_MAX_DEPTH = 20
...@@ -132,9 +132,9 @@ class GitlabSchema < GraphQL::Schema ...@@ -132,9 +132,9 @@ class GitlabSchema < GraphQL::Schema
current_user = ctx&.fetch(:current_user, nil) current_user = ctx&.fetch(:current_user, nil)
if current_user&.admin if current_user&.admin
ADMIN_COMPLEXITY ADMIN_MAX_COMPLEXITY
elsif current_user elsif current_user
AUTHENTICATED_COMPLEXITY AUTHENTICATED_MAX_COMPLEXITY
else else
DEFAULT_MAX_COMPLEXITY DEFAULT_MAX_COMPLEXITY
end end
......
...@@ -129,7 +129,7 @@ module Types ...@@ -129,7 +129,7 @@ module Types
description: "Find runners visible to the current user.", description: "Find runners visible to the current user.",
feature_flag: :runner_graphql_query feature_flag: :runner_graphql_query
field :ci_config, resolver: Resolvers::Ci::ConfigResolver, complexity: 126 # AUTHENTICATED_COMPLEXITY / 2 + 1 field :ci_config, resolver: Resolvers::Ci::ConfigResolver, complexity: 126 # AUTHENTICATED_MAX_COMPLEXITY / 2 + 1
def design_management def design_management
DesignManagementObject.new(nil) DesignManagementObject.new(nil)
......
...@@ -56,9 +56,9 @@ namespace :gitlab do ...@@ -56,9 +56,9 @@ namespace :gitlab do
color = case complexity color = case complexity
when 0..GitlabSchema::DEFAULT_MAX_COMPLEXITY when 0..GitlabSchema::DEFAULT_MAX_COMPLEXITY
:green :green
when GitlabSchema::DEFAULT_MAX_COMPLEXITY..GitlabSchema::AUTHENTICATED_COMPLEXITY when GitlabSchema::DEFAULT_MAX_COMPLEXITY..GitlabSchema::AUTHENTICATED_MAX_COMPLEXITY
:yellow :yellow
when GitlabSchema::AUTHENTICATED_COMPLEXITY..GitlabSchema::ADMIN_COMPLEXITY when GitlabSchema::AUTHENTICATED_MAX_COMPLEXITY..GitlabSchema::ADMIN_MAX_COMPLEXITY
:orange :orange
else else
:red :red
......
...@@ -56,6 +56,7 @@ module DeprecationToolkitEnv ...@@ -56,6 +56,7 @@ module DeprecationToolkitEnv
def self.allowed_kwarg_warning_paths def self.allowed_kwarg_warning_paths
%w[ %w[
actionpack-6.1.3.2/lib/action_dispatch/routing/route_set.rb actionpack-6.1.3.2/lib/action_dispatch/routing/route_set.rb
graphql-1.11.8/lib/graphql/schema.rb
] ]
end end
......
...@@ -36,75 +36,66 @@ RSpec.describe GitlabSchema do ...@@ -36,75 +36,66 @@ RSpec.describe GitlabSchema do
end end
describe '.execute' do describe '.execute' do
context 'with different types of users' do describe 'setting query `max_complexity` and `max_depth`' do
context 'when no context' do subject(:result) { described_class.execute('query', kwargs).query }
it 'returns DEFAULT_MAX_COMPLEXITY' do
expect(GraphQL::Schema) shared_examples 'sets default limits' do
.to receive(:execute) specify do
.with('query', hash_including(max_complexity: GitlabSchema::DEFAULT_MAX_COMPLEXITY)) expect(result).to have_attributes(
max_complexity: GitlabSchema::DEFAULT_MAX_COMPLEXITY,
described_class.execute('query') max_depth: GitlabSchema::DEFAULT_MAX_DEPTH
)
end end
end end
context 'when no user' do context 'with no context' do
it 'returns DEFAULT_MAX_COMPLEXITY' do let(:kwargs) { {} }
expect(GraphQL::Schema)
.to receive(:execute)
.with('query', hash_including(max_complexity: GitlabSchema::DEFAULT_MAX_COMPLEXITY))
described_class.execute('query', context: {}) include_examples 'sets default limits'
end
it 'returns DEFAULT_MAX_DEPTH' do
expect(GraphQL::Schema)
.to receive(:execute)
.with('query', hash_including(max_depth: GitlabSchema::DEFAULT_MAX_DEPTH))
described_class.execute('query', context: {})
end
end end
context 'when a logged in user' do context 'with no :current_user' do
it 'returns AUTHENTICATED_COMPLEXITY' do let(:kwargs) { { context: {} } }
expect(GraphQL::Schema).to receive(:execute)
.with('query', hash_including(max_complexity: GitlabSchema::AUTHENTICATED_COMPLEXITY))
described_class.execute('query', context: { current_user: user }) include_examples 'sets default limits'
end end
it 'returns AUTHENTICATED_MAX_DEPTH' do context 'with anonymous user' do
expect(GraphQL::Schema).to receive(:execute) let(:kwargs) { { context: { current_user: nil } } }
.with('query', hash_including(max_depth: GitlabSchema::AUTHENTICATED_MAX_DEPTH))
described_class.execute('query', context: { current_user: user }) include_examples 'sets default limits'
end
end end
context 'when an admin user' do context 'with a logged in user' do
it 'returns ADMIN_COMPLEXITY' do let(:kwargs) { { context: { current_user: user } } }
user = build :user, :admin
expect(GraphQL::Schema).to receive(:execute)
.with('query', hash_including(max_complexity: GitlabSchema::ADMIN_COMPLEXITY))
described_class.execute('query', context: { current_user: user }) it 'sets authenticated user limits' do
expect(result).to have_attributes(
max_complexity: GitlabSchema::AUTHENTICATED_MAX_COMPLEXITY,
max_depth: GitlabSchema::AUTHENTICATED_MAX_DEPTH
)
end end
end end
context 'when max_complexity passed on the query' do context 'with an admin user' do
it 'returns what was passed on the query' do let(:kwargs) { { context: { current_user: build(:user, :admin) } } }
expect(GraphQL::Schema).to receive(:execute).with('query', hash_including(max_complexity: 1234))
described_class.execute('query', max_complexity: 1234) it 'sets admin/authenticated user limits' do
expect(result).to have_attributes(
max_complexity: GitlabSchema::ADMIN_MAX_COMPLEXITY,
max_depth: GitlabSchema::AUTHENTICATED_MAX_DEPTH
)
end end
end end
context 'when max_depth passed on the query' do context 'when limits passed as kwargs' do
it 'returns what was passed on the query' do let(:kwargs) { { max_complexity: 1234, max_depth: 4321 } }
expect(GraphQL::Schema).to receive(:execute).with('query', hash_including(max_depth: 1234))
described_class.execute('query', max_depth: 1234) it 'sets limits from the kwargs' do
expect(result).to have_attributes(
max_complexity: 1234,
max_depth: 4321
)
end end
end end
end end
......
...@@ -385,7 +385,7 @@ RSpec.describe 'GraphQL' do ...@@ -385,7 +385,7 @@ RSpec.describe 'GraphQL' do
context 'authenticated user' do context 'authenticated user' do
subject { post_graphql(query, current_user: user) } subject { post_graphql(query, current_user: user) }
it 'does not raise an error as it uses the `AUTHENTICATED_COMPLEXITY`' do it 'does not raise an error as it uses the `AUTHENTICATED_MAX_COMPLEXITY`' do
subject subject
expect(graphql_errors).to be_nil expect(graphql_errors).to be_nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment