2021-12-16 10:10:28 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Resolvers::PackagePipelinesResolver do
|
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
let_it_be_with_reload(:package) { create(:package) }
|
|
|
|
let_it_be(:pipelines) { create_list(:ci_pipeline, 3, project: package.project) }
|
|
|
|
|
2022-01-20 04:11:11 -05:00
|
|
|
let(:user) { package.project.first_owner }
|
2021-12-16 10:10:28 -05:00
|
|
|
|
|
|
|
describe '#resolve' do
|
2022-05-10 14:08:27 -04:00
|
|
|
let(:returned_pipelines) { graphql_dig_at(subject, 'data', 'package', 'pipelines', 'nodes') }
|
2022-05-03 14:07:53 -04:00
|
|
|
let(:returned_errors) { graphql_dig_at(subject, 'errors', 'message') }
|
|
|
|
let(:pagination_args) { {} }
|
|
|
|
let(:query) do
|
|
|
|
pipelines_nodes = 'nodes { id }'
|
|
|
|
graphql_query_for(
|
|
|
|
:package,
|
|
|
|
{ id: global_id_of(package) },
|
|
|
|
query_graphql_field('pipelines', pagination_args, pipelines_nodes)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
GitlabSchema.execute(query, context: { current_user: user })
|
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
|
|
|
|
before do
|
2022-01-20 16:14:18 -05:00
|
|
|
pipelines.each do |pipeline|
|
|
|
|
create(:package_build_info, package: package, pipeline: pipeline)
|
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
end
|
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'contains the expected pipelines' do
|
|
|
|
expect_to_contain_exactly(*pipelines)
|
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with valid after' do
|
|
|
|
let(:pagination_args) { { first: 1, after: encode_cursor(id: pipelines[1].id) } }
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'contains the expected pipelines' do
|
|
|
|
expect_to_contain_exactly(pipelines[0])
|
2021-12-16 10:10:28 -05:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with valid before' do
|
|
|
|
let(:pagination_args) { { last: 1, before: encode_cursor(id: pipelines[1].id) } }
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'contains the expected pipelines' do
|
|
|
|
expect_to_contain_exactly(pipelines[2])
|
2021-12-16 10:10:28 -05:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with invalid after' do
|
|
|
|
let(:pagination_args) { { first: 1, after: 'not_json_string' } }
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'generates an argument error' do
|
|
|
|
expect(returned_errors).to include('Please provide a valid cursor')
|
2021-12-16 10:10:28 -05:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with invalid after key' do
|
|
|
|
let(:pagination_args) { { first: 1, after: encode_cursor(foo: 3) } }
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'generates an argument error' do
|
|
|
|
expect(returned_errors).to include('Please provide a valid cursor')
|
2021-12-16 10:10:28 -05:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with invalid before' do
|
|
|
|
let(:pagination_args) { { last: 1, before: 'not_json_string' } }
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'generates an argument error' do
|
|
|
|
expect(returned_errors).to include('Please provide a valid cursor')
|
2022-05-03 14:07:53 -04:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with invalid before key' do
|
|
|
|
let(:pagination_args) { { last: 1, before: encode_cursor(foo: 3) } }
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'generates an argument error' do
|
|
|
|
expect(returned_errors).to include('Please provide a valid cursor')
|
2022-05-03 14:07:53 -04:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with unauthorized user' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'returns nothing' do
|
|
|
|
expect(returned_pipelines).to be_nil
|
2022-05-03 14:07:53 -04:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
context 'with many packages' do
|
|
|
|
let_it_be_with_reload(:other_package) { create(:package, project: package.project) }
|
|
|
|
let_it_be(:other_pipelines) { create_list(:ci_pipeline, 3, project: package.project) }
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
let(:returned_pipelines) do
|
|
|
|
graphql_dig_at(subject, 'data', 'project', 'packages', 'nodes', 'pipelines', 'nodes')
|
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
let(:query) do
|
|
|
|
pipelines_query = query_graphql_field('pipelines', pagination_args, 'nodes { id }')
|
|
|
|
<<~QUERY
|
|
|
|
{
|
|
|
|
project(fullPath: "#{package.project.full_path}") {
|
|
|
|
packages {
|
|
|
|
nodes { #{pipelines_query} }
|
2022-05-03 14:07:53 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 02:07:53 -04:00
|
|
|
}
|
|
|
|
QUERY
|
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
before do
|
|
|
|
other_pipelines.each do |pipeline|
|
|
|
|
create(:package_build_info, package: other_package, pipeline: pipeline)
|
2022-05-03 14:07:53 -04:00
|
|
|
end
|
2022-05-12 02:07:53 -04:00
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'contains the expected pipelines' do
|
|
|
|
expect_to_contain_exactly(*(pipelines + other_pipelines))
|
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
it 'handles n+1 situations' do
|
|
|
|
control = ActiveRecord::QueryRecorder.new do
|
|
|
|
GitlabSchema.execute(query, context: { current_user: user })
|
2022-05-03 14:07:53 -04:00
|
|
|
end
|
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
create_package_with_pipelines(package.project)
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
expectation = expect { GitlabSchema.execute(query, context: { current_user: user }) }
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
expectation.not_to exceed_query_limit(control)
|
2022-05-03 14:07:53 -04:00
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
|
2022-05-12 02:07:53 -04:00
|
|
|
def create_package_with_pipelines(project)
|
|
|
|
extra_package = create(:package, project: project)
|
|
|
|
create_list(:ci_pipeline, 3, project: project).each do |pipeline|
|
|
|
|
create(:package_build_info, package: extra_package, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def encode_cursor(json)
|
|
|
|
GitlabSchema.cursor_encoder.encode(
|
|
|
|
Gitlab::Json.dump(json),
|
|
|
|
nonce: true
|
|
|
|
)
|
|
|
|
end
|
2022-05-03 14:07:53 -04:00
|
|
|
|
|
|
|
def expect_to_contain_exactly(*pipelines)
|
2022-05-10 14:08:27 -04:00
|
|
|
entities = pipelines.map { |pipeline| a_graphql_entity_for(pipeline) }
|
|
|
|
expect(returned_pipelines).to match_array(entities)
|
2022-05-03 14:07:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.field options' do
|
|
|
|
let(:field) do
|
|
|
|
field_options = described_class.field_options.merge(
|
|
|
|
owner: resolver_parent,
|
|
|
|
name: 'dummy_field'
|
|
|
|
)
|
|
|
|
::Types::BaseField.new(**field_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets them properly' do
|
|
|
|
expect(field).not_to be_connection
|
|
|
|
expect(field.extras).to match_array([:lookahead])
|
|
|
|
end
|
2021-12-16 10:10:28 -05:00
|
|
|
end
|
|
|
|
end
|