|
3 | 3 | describe 'GitlabSchema configurations' do
|
4 | 4 | include GraphqlHelpers
|
5 | 5 |
|
6 |
| - let(:project) { create(:project, :repository) } |
7 |
| - let(:query) { graphql_query_for('project', { 'fullPath' => project.full_path }, %w(id name description)) } |
8 |
| - let(:current_user) { create(:user) } |
| 6 | + let(:project) { create(:project) } |
9 | 7 |
|
10 |
| - describe '#max_complexity' do |
11 |
| - context 'when complexity is too high' do |
12 |
| - it 'shows an error' do |
13 |
| - allow(GitlabSchema).to receive(:max_query_complexity).and_return 1 |
| 8 | + shared_examples 'imposing query limits' do |
| 9 | + describe '#max_complexity' do |
| 10 | + context 'when complexity is too high' do |
| 11 | + it 'shows an error' do |
| 12 | + allow(GitlabSchema).to receive(:max_query_complexity).and_return 1 |
14 | 13 |
|
15 |
| - post_graphql(query, current_user: nil) |
| 14 | + subject |
16 | 15 |
|
17 |
| - expect(graphql_errors.first['message']).to include('which exceeds max complexity of 1') |
| 16 | + expect(graphql_errors.flatten.first['message']).to include('which exceeds max complexity of 1') |
| 17 | + end |
18 | 18 | end
|
19 | 19 | end
|
20 |
| - end |
21 | 20 |
|
22 |
| - describe '#max_depth' do |
23 |
| - context 'when query depth is too high' do |
24 |
| - it 'shows error' do |
25 |
| - errors = [{ "message" => "Query has depth of 2, which exceeds max depth of 1" }] |
26 |
| - allow(GitlabSchema).to receive(:max_query_depth).and_return 1 |
| 21 | + describe '#max_depth' do |
| 22 | + context 'when query depth is too high' do |
| 23 | + it 'shows error' do |
| 24 | + errors = { "message" => "Query has depth of 2, which exceeds max depth of 1" } |
| 25 | + allow(GitlabSchema).to receive(:max_query_depth).and_return 1 |
27 | 26 |
|
28 |
| - post_graphql(query) |
| 27 | + subject |
29 | 28 |
|
30 |
| - expect(graphql_errors).to eq(errors) |
| 29 | + expect(graphql_errors.flatten).to include(errors) |
| 30 | + end |
31 | 31 | end
|
| 32 | + |
| 33 | + context 'when query depth is within range' do |
| 34 | + it 'has no error' do |
| 35 | + allow(GitlabSchema).to receive(:max_query_depth).and_return 5 |
| 36 | + |
| 37 | + subject |
| 38 | + |
| 39 | + expect(Array.wrap(graphql_errors).compact).to be_empty |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'regular queries' do |
| 46 | + subject do |
| 47 | + query = graphql_query_for('project', { 'fullPath' => project.full_path }, %w(id name description)) |
| 48 | + post_graphql(query) |
32 | 49 | end
|
33 | 50 |
|
34 |
| - context 'when query depth is within range' do |
35 |
| - it 'has no error' do |
36 |
| - allow(GitlabSchema).to receive(:max_query_depth).and_return 5 |
| 51 | + it_behaves_like 'imposing query limits' |
| 52 | + end |
| 53 | + |
| 54 | + context 'multiplexed queries' do |
| 55 | + subject do |
| 56 | + queries = [ |
| 57 | + { query: graphql_query_for('project', { 'fullPath' => project.full_path }, %w(id name description)) }, |
| 58 | + { query: graphql_query_for('echo', { 'text' => "$test" }, []), variables: { "test" => "Hello world" } } |
| 59 | + ] |
| 60 | + |
| 61 | + post_multiplex(queries) |
| 62 | + end |
| 63 | + |
| 64 | + it_behaves_like 'imposing query limits' do |
| 65 | + it "fails all queries when only one of the queries is too complex" do |
| 66 | + # The `project` query above has a complexity of 5 |
| 67 | + allow(GitlabSchema).to receive(:max_query_complexity).and_return 4 |
| 68 | + |
| 69 | + subject |
37 | 70 |
|
38 |
| - post_graphql(query) |
| 71 | + # Expect a response for each query, even though it will be empty |
| 72 | + expect(json_response.size).to eq(2) |
| 73 | + json_response.each do |single_query_response| |
| 74 | + expect(single_query_response).not_to have_key('data') |
| 75 | + end |
39 | 76 |
|
40 |
| - expect(graphql_errors).to be_nil |
| 77 | + # Expect errors for each query |
| 78 | + expect(graphql_errors.size).to eq(2) |
| 79 | + graphql_errors.each do |single_query_errors| |
| 80 | + expect(single_query_errors.first['message']).to include('which exceeds max complexity of 4') |
| 81 | + end |
41 | 82 | end
|
42 | 83 | end
|
43 | 84 | end
|
|
0 commit comments