2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-23 03:55:14 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe 'getting project information' do
|
2018-05-23 03:55:14 -04:00
|
|
|
include GraphqlHelpers
|
|
|
|
|
2020-12-13 22:10:06 -05:00
|
|
|
let_it_be(:group) { create(:group) }
|
2020-12-21 10:10:05 -05:00
|
|
|
let_it_be(:project, reload: true) { create(:project, :repository, group: group) }
|
2020-12-13 22:10:06 -05:00
|
|
|
let_it_be(:current_user) { create(:user) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2020-12-21 10:10:05 -05:00
|
|
|
let(:project_fields) { all_graphql_fields_for('project'.to_s.classify, max_depth: 1) }
|
2018-05-23 03:55:14 -04:00
|
|
|
|
|
|
|
let(:query) do
|
2020-12-21 10:10:05 -05:00
|
|
|
graphql_query_for(:project, { full_path: project.full_path }, project_fields)
|
2020-04-08 02:09:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user has full access to the project' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(current_user)
|
|
|
|
end
|
|
|
|
|
2020-12-13 22:10:06 -05:00
|
|
|
it 'includes the project', :use_clean_rails_memory_store_caching, :request_store do
|
2020-12-21 10:10:05 -05:00
|
|
|
post_graphql(query, current_user: current_user)
|
2020-04-08 02:09:54 -04:00
|
|
|
|
|
|
|
expect(graphql_data['project']).not_to be_nil
|
|
|
|
end
|
2018-05-23 03:55:14 -04:00
|
|
|
end
|
|
|
|
|
2020-12-13 22:10:06 -05:00
|
|
|
context 'when the user has access to the project', :use_clean_rails_memory_store_caching, :request_store do
|
|
|
|
before_all do
|
2018-05-23 03:55:14 -04:00
|
|
|
project.add_developer(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the project' do
|
2018-06-14 09:06:53 -04:00
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
2018-05-23 03:55:14 -04:00
|
|
|
expect(graphql_data['project']).not_to be_nil
|
|
|
|
end
|
|
|
|
|
2018-06-14 09:06:53 -04:00
|
|
|
it_behaves_like 'a working graphql query' do
|
|
|
|
before do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
end
|
|
|
|
end
|
2018-06-26 12:31:05 -04:00
|
|
|
|
|
|
|
context 'when there are pipelines present' do
|
2020-12-21 10:10:05 -05:00
|
|
|
let(:project_fields) { query_nodes(:pipelines) }
|
|
|
|
|
2018-06-26 12:31:05 -04:00
|
|
|
before do
|
|
|
|
create(:ci_pipeline, project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is included in the pipelines connection' do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
2020-12-13 22:10:06 -05:00
|
|
|
expect(graphql_data_at(:project, :pipelines, :nodes)).to contain_exactly(a_kind_of(Hash))
|
2018-06-26 12:31:05 -04:00
|
|
|
end
|
|
|
|
end
|
2020-08-21 08:10:22 -04:00
|
|
|
|
2021-05-12 05:10:19 -04:00
|
|
|
context 'topics' do
|
|
|
|
it 'includes empty topics array if no topics set' do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data_at(:project, :topics)).to match([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes topics array' do
|
2021-06-11 17:09:58 -04:00
|
|
|
project.update!(topic_list: 'topic1, topic2, topic3')
|
2021-05-12 05:10:19 -04:00
|
|
|
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data_at(:project, :topics)).to match(%w[topic1 topic2 topic3])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-21 08:10:22 -04:00
|
|
|
it 'includes inherited members in project_members' do
|
|
|
|
group_member = create(:group_member, group: group)
|
|
|
|
project_member = create(:project_member, project: project)
|
|
|
|
member_query = <<~GQL
|
|
|
|
query {
|
|
|
|
project(fullPath: "#{project.full_path}") {
|
|
|
|
projectMembers {
|
|
|
|
nodes {
|
|
|
|
id
|
|
|
|
user {
|
|
|
|
username
|
|
|
|
}
|
|
|
|
... on ProjectMember {
|
|
|
|
project {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
... on GroupMember {
|
|
|
|
group {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GQL
|
|
|
|
|
|
|
|
post_graphql(member_query, current_user: current_user)
|
|
|
|
|
|
|
|
member_ids = graphql_data.dig('project', 'projectMembers', 'nodes')
|
|
|
|
expect(member_ids).to include(
|
|
|
|
a_hash_including(
|
|
|
|
'id' => group_member.to_global_id.to_s,
|
|
|
|
'group' => { 'id' => group.to_global_id.to_s }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
expect(member_ids).to include(
|
|
|
|
a_hash_including(
|
|
|
|
'id' => project_member.to_global_id.to_s,
|
|
|
|
'project' => { 'id' => project.to_global_id.to_s }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2018-06-14 09:06:53 -04:00
|
|
|
end
|
2018-05-23 03:55:14 -04:00
|
|
|
|
2021-06-07 08:10:00 -04:00
|
|
|
context 'when the user has reporter access to the project' do
|
|
|
|
let(:statistics_query) do
|
|
|
|
<<~GRAPHQL
|
|
|
|
{
|
|
|
|
project(fullPath: "#{project.full_path}") {
|
|
|
|
statistics { wikiSize }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GRAPHQL
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_reporter(current_user)
|
|
|
|
create(:project_statistics, project: project, wiki_size: 100)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows fetching project statistics' do
|
|
|
|
post_graphql(statistics_query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data.dig('project', 'statistics')).to include('wikiSize' => 100.0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-29 22:13:04 -05:00
|
|
|
context 'when the user has guest access' do
|
|
|
|
context 'when the project has public pipelines' do
|
|
|
|
before do
|
|
|
|
pipeline = create(:ci_pipeline, project: project)
|
|
|
|
create(:ci_build, project: project, pipeline: pipeline, name: 'a test job')
|
|
|
|
project.add_guest(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows all jobs' do
|
|
|
|
query = <<~GQL
|
|
|
|
query {
|
|
|
|
project(fullPath: "#{project.full_path}") {
|
|
|
|
jobs {
|
|
|
|
nodes {
|
|
|
|
name
|
|
|
|
stage {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GQL
|
|
|
|
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data_at(:project, :jobs, :nodes)).to contain_exactly({
|
|
|
|
'name' => 'a test job',
|
|
|
|
'stage' => { 'name' => 'test' }
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-14 09:06:53 -04:00
|
|
|
context 'when the user does not have access to the project' do
|
2018-05-23 03:55:14 -04:00
|
|
|
it 'returns an empty field' do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data['project']).to be_nil
|
|
|
|
end
|
|
|
|
|
2018-06-14 09:06:53 -04:00
|
|
|
it_behaves_like 'a working graphql query' do
|
|
|
|
before do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
end
|
|
|
|
end
|
2018-05-23 03:55:14 -04:00
|
|
|
end
|
2022-08-03 08:11:38 -04:00
|
|
|
|
|
|
|
context 'with timelog categories' do
|
|
|
|
let_it_be(:timelog_category) do
|
|
|
|
create(:timelog_category, namespace: project.project_namespace, name: 'TimelogCategoryTest')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:project_fields) do
|
|
|
|
<<~GQL
|
|
|
|
timelogCategories {
|
|
|
|
nodes {
|
|
|
|
#{all_graphql_fields_for('TimeTrackingTimelogCategory')}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GQL
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is guest and the project is public' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes empty timelog categories array' do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data_at(:project, :timelogCategories, :nodes)).to match([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has reporter role' do
|
|
|
|
before do
|
|
|
|
project.add_reporter(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the timelog category with all its fields' do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data_at(:project, :timelogCategories, :nodes))
|
|
|
|
.to contain_exactly(a_graphql_entity_for(timelog_category))
|
|
|
|
end
|
2022-08-16 08:12:38 -04:00
|
|
|
|
|
|
|
context 'when timelog_categories flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(timelog_categories: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no timelog categories' do
|
|
|
|
post_graphql(query, current_user: current_user)
|
|
|
|
|
|
|
|
expect(graphql_data_at(:project, :timelogCategories)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2022-08-03 08:11:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for N+1 queries' do
|
|
|
|
let!(:project1) { create(:project) }
|
|
|
|
let!(:project2) { create(:project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project1.add_reporter(current_user)
|
|
|
|
project2.add_reporter(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N+1 database queries' do
|
|
|
|
pending('See: https://gitlab.com/gitlab-org/gitlab/-/issues/369396')
|
|
|
|
|
|
|
|
ctx = { current_user: current_user }
|
|
|
|
|
|
|
|
baseline_query = <<~GQL
|
|
|
|
query {
|
|
|
|
a: project(fullPath: "#{project1.full_path}") { ... p }
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment p on Project {
|
|
|
|
timelogCategories { nodes { name } }
|
|
|
|
}
|
|
|
|
GQL
|
|
|
|
|
|
|
|
query = <<~GQL
|
|
|
|
query {
|
|
|
|
a: project(fullPath: "#{project1.full_path}") { ... p }
|
|
|
|
b: project(fullPath: "#{project2.full_path}") { ... p }
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment p on Project {
|
|
|
|
timelogCategories { nodes { name } }
|
|
|
|
}
|
|
|
|
GQL
|
|
|
|
|
|
|
|
control = ActiveRecord::QueryRecorder.new do
|
|
|
|
run_with_clean_state(baseline_query, context: ctx)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { run_with_clean_state(query, context: ctx) }.not_to exceed_query_limit(control)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-23 03:55:14 -04:00
|
|
|
end
|