diff options
Diffstat (limited to 'spec/lib/gitlab/graphql/query_analyzers/ast')
-rw-r--r-- | spec/lib/gitlab/graphql/query_analyzers/ast/logger_analyzer_spec.rb | 51 | ||||
-rw-r--r-- | spec/lib/gitlab/graphql/query_analyzers/ast/recursion_analyzer_spec.rb | 72 |
2 files changed, 123 insertions, 0 deletions
diff --git a/spec/lib/gitlab/graphql/query_analyzers/ast/logger_analyzer_spec.rb b/spec/lib/gitlab/graphql/query_analyzers/ast/logger_analyzer_spec.rb new file mode 100644 index 00000000000..a5274d49fdb --- /dev/null +++ b/spec/lib/gitlab/graphql/query_analyzers/ast/logger_analyzer_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Graphql::QueryAnalyzers::AST::LoggerAnalyzer do + let(:query) { GraphQL::Query.new(GitlabSchema, document: document, context: {}, variables: { body: 'some note' }) } + let(:document) do + GraphQL.parse <<-GRAPHQL + mutation createNote($body: String!) { + createNote(input: {noteableId: "gid://gitlab/Noteable/1", body: $body}) { + note { + id + } + } + } + GRAPHQL + end + + describe '#result' do + let(:monotonic_time_before) { 42 } + let(:monotonic_time_after) { 500 } + let(:monotonic_time_duration) { monotonic_time_after - monotonic_time_before } + + before do + RequestStore.store[:graphql_logs] = nil + + allow(Gitlab::Metrics::System).to receive(:monotonic_time) + .and_return(monotonic_time_before, monotonic_time_before, + monotonic_time_before, monotonic_time_before, + monotonic_time_after) + end + + it 'returns the complexity, depth, duration, etc' do + results = GraphQL::Analysis::AST.analyze_query(query, [described_class], multiplex_analyzers: []) + result = results.first + + expect(result[:duration_s]).to eq monotonic_time_duration + expect(result[:depth]).to eq 3 + expect(result[:complexity]).to eq 3 + expect(result[:used_fields]).to eq ['Note.id', 'CreateNotePayload.note', 'Mutation.createNote'] + expect(result[:used_deprecated_fields]).to eq [] + + request = result.except(:duration_s).merge({ + operation_name: 'createNote', + variables: { body: "[FILTERED]" }.to_s + }) + + expect(RequestStore.store[:graphql_logs]).to match([request]) + end + end +end diff --git a/spec/lib/gitlab/graphql/query_analyzers/ast/recursion_analyzer_spec.rb b/spec/lib/gitlab/graphql/query_analyzers/ast/recursion_analyzer_spec.rb new file mode 100644 index 00000000000..997fb129f42 --- /dev/null +++ b/spec/lib/gitlab/graphql/query_analyzers/ast/recursion_analyzer_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Graphql::QueryAnalyzers::AST::RecursionAnalyzer do + let(:query) { GraphQL::Query.new(GitlabSchema, document: document, context: {}, variables: { body: 'some note' }) } + + context 'when recursion threshold not exceeded' do + let(:document) do + GraphQL.parse <<-GRAPHQL + query recurse { + group(fullPath: "h5bp") { + projects { + nodes { + name + group { + projects { + nodes { + name + } + } + } + } + } + } + } + GRAPHQL + end + + it 'returns the complexity, depth, duration, etc' do + result = GraphQL::Analysis::AST.analyze_query(query, [described_class], multiplex_analyzers: []) + + expect(result.first).to be_nil + end + end + + context 'when recursion threshold exceeded' do + let(:document) do + GraphQL.parse <<-GRAPHQL + query recurse { + group(fullPath: "h5bp") { + projects { + nodes { + name + group { + projects { + nodes { + name + group { + projects { + nodes { + name + } + } + } + } + } + } + } + } + } + } + GRAPHQL + end + + it 'returns error' do + result = GraphQL::Analysis::AST.analyze_query(query, [described_class], multiplex_analyzers: []) + + expect(result.first.is_a?(GraphQL::AnalysisError)).to be_truthy + end + end +end |