diff options
Diffstat (limited to 'spec/requests/api/graphql/project/project_members_spec.rb')
-rw-r--r-- | spec/requests/api/graphql/project/project_members_spec.rb | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/spec/requests/api/graphql/project/project_members_spec.rb b/spec/requests/api/graphql/project/project_members_spec.rb index 466464f600c..315d44884ff 100644 --- a/spec/requests/api/graphql/project/project_members_spec.rb +++ b/spec/requests/api/graphql/project/project_members_spec.rb @@ -110,6 +110,102 @@ RSpec.describe 'getting project members information' do end end + context 'merge request interactions' do + let(:project_path) { var('ID!').with(parent_project.full_path) } + let(:mr_a) do + var('MergeRequestID!') + .with(global_id_of(create(:merge_request, source_project: parent_project, source_branch: 'branch-1'))) + end + + let(:mr_b) do + var('MergeRequestID!') + .with(global_id_of(create(:merge_request, source_project: parent_project, source_branch: 'branch-2'))) + end + + let(:interaction_query) do + <<~HEREDOC + edges { + node { + user { + id + } + mrA: #{query_graphql_field(:merge_request_interaction, { id: mr_a }, 'canMerge')} + } + } + HEREDOC + end + + let(:interaction_b_query) do + <<~HEREDOC + edges { + node { + user { + id + } + mrA: #{query_graphql_field(:merge_request_interaction, { id: mr_a }, 'canMerge')} + mrB: #{query_graphql_field(:merge_request_interaction, { id: mr_b }, 'canMerge')} + } + } + HEREDOC + end + + it 'avoids N+1 queries, when requesting multiple MRs' do + control_query = with_signature( + [project_path, mr_a], + graphql_query_for(:project, { full_path: project_path }, + query_graphql_field(:project_members, nil, interaction_query)) + ) + query_two = with_signature( + [project_path, mr_a, mr_b], + graphql_query_for(:project, { full_path: project_path }, + query_graphql_field(:project_members, nil, interaction_b_query)) + ) + + control_count = ActiveRecord::QueryRecorder.new do + post_graphql(control_query, current_user: user, variables: [project_path, mr_a]) + end + + # two project members, neither of whom can merge + expect(can_merge(:mrA)).to eq [false, false] + + expect do + post_graphql(query_two, current_user: user, variables: [project_path, mr_a, mr_b]) + + expect(can_merge(:mrA)).to eq [false, false] + expect(can_merge(:mrB)).to eq [false, false] + end.not_to exceed_query_limit(control_count) + end + + it 'avoids N+1 queries, when more users are involved' do + new_user = create(:user) + + query = with_signature( + [project_path, mr_a], + graphql_query_for(:project, { full_path: project_path }, + query_graphql_field(:project_members, nil, interaction_query)) + ) + + control_count = ActiveRecord::QueryRecorder.new do + post_graphql(query, current_user: user, variables: [project_path, mr_a]) + end + + # two project members, neither of whom can merge + expect(can_merge(:mrA)).to eq [false, false] + + parent_project.add_guest(new_user) + + expect do + post_graphql(query, current_user: user, variables: [project_path, mr_a]) + + expect(can_merge(:mrA)).to eq [false, false, false] + end.not_to exceed_query_limit(control_count) + end + + def can_merge(name) + graphql_data_at(:project, :project_members, :edges, :node, name, :can_merge) + end + end + context 'when unauthenticated' do it 'returns members' do fetch_members(current_user: nil, project: parent_project) |