debian-mirror-gitlab/spec/requests/api/graphql/issues_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

273 lines
9 KiB
Ruby
Raw Permalink Normal View History

2023-01-13 00:05:48 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-17 16:20:25 +05:30
# rubocop:disable RSpec/MultipleMemoizedHelpers
2023-03-04 22:38:38 +05:30
RSpec.describe 'getting an issue list at root level', feature_category: :team_planning do
2023-01-13 00:05:48 +05:30
include GraphqlHelpers
let_it_be(:developer) { create(:user) }
let_it_be(:reporter) { create(:user) }
2023-03-17 16:20:25 +05:30
let_it_be(:current_user) { developer }
2023-01-13 00:05:48 +05:30
let_it_be(:group1) { create(:group).tap { |group| group.add_developer(developer) } }
let_it_be(:group2) { create(:group).tap { |group| group.add_developer(developer) } }
let_it_be(:project_a) { create(:project, :repository, :public, group: group1) }
let_it_be(:project_b) { create(:project, :repository, :private, group: group1) }
let_it_be(:project_c) { create(:project, :repository, :public, group: group2) }
let_it_be(:project_d) { create(:project, :repository, :private, group: group2) }
2023-05-27 22:25:52 +05:30
let_it_be(:archived_project) { create(:project, :repository, :archived, group: group2) }
2023-03-04 22:38:38 +05:30
let_it_be(:milestone1) { create(:milestone, project: project_c, due_date: 10.days.from_now) }
let_it_be(:milestone2) { create(:milestone, project: project_d, due_date: 20.days.from_now) }
let_it_be(:milestone3) { create(:milestone, project: project_d, due_date: 30.days.from_now) }
let_it_be(:milestone4) { create(:milestone, project: project_a, due_date: 40.days.from_now) }
2023-01-13 00:05:48 +05:30
let_it_be(:priority1) { create(:label, project: project_c, priority: 1) }
let_it_be(:priority2) { create(:label, project: project_d, priority: 5) }
let_it_be(:priority3) { create(:label, project: project_a, priority: 10) }
2023-03-04 22:38:38 +05:30
let_it_be(:priority4) { create(:label, project: project_d, priority: 15) }
let_it_be(:issue_a) do
create(
:issue,
project: project_a,
labels: [priority3],
due_date: 1.day.ago,
milestone: milestone4,
relative_position: 1000
)
end
let_it_be(:issue_b) do
create(
:issue,
:with_alert,
project: project_b,
discussion_locked: true,
due_date: 1.day.from_now,
relative_position: 3000
)
end
2023-01-13 00:05:48 +05:30
let_it_be(:issue_c) do
create(
:issue,
2023-03-04 22:38:38 +05:30
:confidential,
2023-01-13 00:05:48 +05:30
project: project_c,
title: 'title matching issue plus',
labels: [priority1],
2023-03-04 22:38:38 +05:30
milestone: milestone1,
due_date: 3.days.from_now,
relative_position: nil
2023-01-13 00:05:48 +05:30
)
end
2023-03-04 22:38:38 +05:30
let_it_be(:issue_d) do
create(
:issue,
:with_alert,
project: project_d,
discussion_locked: true,
labels: [priority2],
milestone: milestone3,
relative_position: 5000
)
end
2023-01-13 00:05:48 +05:30
2023-03-04 22:38:38 +05:30
let_it_be(:issue_e) do
create(
:issue,
:confidential,
project: project_d,
milestone: milestone2,
due_date: 3.days.ago,
relative_position: nil,
labels: [priority2, priority4]
)
end
2023-01-13 00:05:48 +05:30
2023-05-27 22:25:52 +05:30
let_it_be(:archived_issue) { create(:issue, project: archived_project) }
2023-03-04 22:38:38 +05:30
let_it_be(:issues, reload: true) { [issue_a, issue_b, issue_c, issue_d, issue_e] }
2023-03-17 16:20:25 +05:30
# we need to always provide at least one filter to the query so it doesn't fail
let_it_be(:base_params) { { iids: issues.map { |issue| issue.iid.to_s } } }
2023-03-04 22:38:38 +05:30
let(:issue_filter_params) { {} }
2023-03-17 16:20:25 +05:30
let(:all_query_params) { base_params.merge(**issue_filter_params) }
2023-01-13 00:05:48 +05:30
let(:fields) do
<<~QUERY
2023-03-04 22:38:38 +05:30
nodes { id }
2023-01-13 00:05:48 +05:30
QUERY
end
before_all do
group2.add_reporter(reporter)
end
2023-03-17 16:20:25 +05:30
shared_examples 'query that requires at least one filter' do
it 'requires at least one filter to be provided to the query' do
post_graphql(query, current_user: developer)
expect(graphql_errors).to contain_exactly(
hash_including('message' => _('You must provide at least one filter argument for this query'))
)
end
end
2023-05-27 22:25:52 +05:30
describe 'includeArchived filter' do
let(:base_params) { { iids: [archived_issue.iid.to_s] } }
it 'excludes issues from archived projects' do
post_query
issue_ids = graphql_dig_at(graphql_data_at('issues', 'nodes'), :id)
expect(issue_ids).not_to include(archived_issue.to_gid.to_s)
end
context 'when includeArchived is true' do
let(:issue_filter_params) { { include_archived: true } }
it 'includes issues from archived projects' do
post_query
issue_ids = graphql_dig_at(graphql_data_at('issues', 'nodes'), :id)
expect(issue_ids).to include(archived_issue.to_gid.to_s)
end
end
end
it 'excludes issues from archived projects' do
post_query
issue_ids = graphql_dig_at(graphql_data_at('issues', 'nodes'), :id)
expect(issue_ids).not_to include(archived_issue.to_gid.to_s)
end
2023-03-17 16:20:25 +05:30
context 'when no filters are provided' do
let(:all_query_params) { {} }
it_behaves_like 'query that requires at least one filter'
end
context 'when only non filter arguments are provided' do
let(:all_query_params) { { sort: :SEVERITY_ASC } }
it_behaves_like 'query that requires at least one filter'
end
2023-03-04 22:38:38 +05:30
# All new specs should be added to the shared example if the change also
# affects the `issues` query at the root level of the API.
# Shared example also used in spec/requests/api/graphql/project/issues_spec.rb
2023-01-13 00:05:48 +05:30
it_behaves_like 'graphql issue list request spec' do
2023-03-04 22:38:38 +05:30
let_it_be(:external_user) { create(:user) }
2023-03-17 16:20:25 +05:30
let_it_be(:another_user) { reporter }
2023-03-04 22:38:38 +05:30
let(:public_projects) { [project_a, project_c] }
2023-01-13 00:05:48 +05:30
2023-03-04 22:38:38 +05:30
let(:issue_nodes_path) { %w[issues nodes] }
2023-01-13 00:05:48 +05:30
# filters
let(:expected_negated_assignee_issues) { [issue_b, issue_c, issue_d, issue_e] }
let(:voted_issues) { [issue_a, issue_c] }
let(:no_award_issues) { [issue_b, issue_d, issue_e] }
let(:locked_discussion_issues) { [issue_b, issue_d] }
let(:unlocked_discussion_issues) { [issue_a, issue_c, issue_e] }
let(:search_title_term) { 'matching issue' }
let(:title_search_issue) { issue_c }
2023-03-04 22:38:38 +05:30
let(:confidential_issues) { [issue_c, issue_e] }
let(:non_confidential_issues) { [issue_a, issue_b, issue_d] }
let(:public_non_confidential_issues) { [issue_a] }
2023-01-13 00:05:48 +05:30
# sorting
let(:data_path) { [:issues] }
2023-03-04 22:38:38 +05:30
let(:expected_priority_sorted_asc) { [issue_c, issue_e, issue_d, issue_a, issue_b] }
let(:expected_priority_sorted_desc) { [issue_a, issue_d, issue_e, issue_c, issue_b] }
let(:expected_due_date_sorted_desc) { [issue_c, issue_b, issue_a, issue_e, issue_d] }
let(:expected_due_date_sorted_asc) { [issue_e, issue_a, issue_b, issue_c, issue_d] }
let(:expected_relative_position_sorted_asc) { [issue_a, issue_b, issue_d, issue_c, issue_e] }
let(:expected_label_priority_sorted_asc) { [issue_c, issue_e, issue_d, issue_a, issue_b] }
let(:expected_label_priority_sorted_desc) { [issue_a, issue_e, issue_d, issue_c, issue_b] }
let(:expected_milestone_sorted_asc) { [issue_c, issue_e, issue_d, issue_a, issue_b] }
let(:expected_milestone_sorted_desc) { [issue_a, issue_d, issue_e, issue_c, issue_b] }
# N+1 queries
let(:same_project_issue1) { issue_d }
let(:same_project_issue2) { issue_e }
2023-01-13 00:05:48 +05:30
before_all do
create(:award_emoji, :upvote, user: developer, awardable: issue_a)
create(:award_emoji, :upvote, user: developer, awardable: issue_c)
end
def pagination_query(params)
graphql_query_for(
:issues,
2023-03-17 16:20:25 +05:30
base_params.merge(**params.to_h),
2023-01-13 00:05:48 +05:30
"#{page_info} nodes { id }"
)
end
end
2023-03-04 22:38:38 +05:30
context 'when fetching issues from multiple projects' do
2023-04-23 21:23:45 +05:30
it 'avoids N+1 queries', :use_sql_query_cache do
2023-03-04 22:38:38 +05:30
post_query # warm-up
2023-04-23 21:23:45 +05:30
control = ActiveRecord::QueryRecorder.new(skip_cached: false) { post_query }
expect_graphql_errors_to_be_empty
2023-03-04 22:38:38 +05:30
new_private_project = create(:project, :private).tap { |project| project.add_developer(current_user) }
create(:issue, project: new_private_project)
2023-04-23 21:23:45 +05:30
private_group = create(:group, :private).tap { |group| group.add_developer(current_user) }
private_project = create(:project, :private, group: private_group)
create(:issue, project: private_project)
expect { post_query }.not_to exceed_all_query_limit(control)
expect_graphql_errors_to_be_empty
2023-03-04 22:38:38 +05:30
end
end
2023-03-17 16:20:25 +05:30
context 'with rate limiting' do
it_behaves_like 'rate limited endpoint', rate_limit_key: :search_rate_limit, graphql: true do
let_it_be(:current_user) { developer }
let(:error_message) do
'This endpoint has been requested with the search argument too many times. Try again later.'
end
def request
post_graphql(query({ search: 'test' }), current_user: developer)
end
end
it_behaves_like 'rate limited endpoint', rate_limit_key: :search_rate_limit_unauthenticated, graphql: true do
let_it_be(:current_user) { nil }
let(:error_message) do
'This endpoint has been requested with the search argument too many times. Try again later.'
end
def request
post_graphql(query({ search: 'test' }))
end
end
end
2023-03-04 22:38:38 +05:30
def execute_query
post_query
end
def post_query(request_user = current_user)
post_graphql(query, current_user: request_user)
end
2023-03-17 16:20:25 +05:30
def query(params = all_query_params)
2023-01-13 00:05:48 +05:30
graphql_query_for(
:issues,
params,
fields
)
end
end
2023-03-17 16:20:25 +05:30
# rubocop:enable RSpec/MultipleMemoizedHelpers