diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /spec/graphql/resolvers | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'spec/graphql/resolvers')
11 files changed, 386 insertions, 27 deletions
diff --git a/spec/graphql/resolvers/admin/analytics/instance_statistics/measurements_resolver_spec.rb b/spec/graphql/resolvers/admin/analytics/instance_statistics/measurements_resolver_spec.rb index 76854be2daa..c5637d43382 100644 --- a/spec/graphql/resolvers/admin/analytics/instance_statistics/measurements_resolver_spec.rb +++ b/spec/graphql/resolvers/admin/analytics/instance_statistics/measurements_resolver_spec.rb @@ -5,9 +5,11 @@ require 'spec_helper' RSpec.describe Resolvers::Admin::Analytics::InstanceStatistics::MeasurementsResolver do include GraphqlHelpers + let_it_be(:admin_user) { create(:user, :admin) } + let(:current_user) { admin_user } + describe '#resolve' do let_it_be(:user) { create(:user) } - let_it_be(:admin_user) { create(:user, :admin) } let_it_be(:project_measurement_new) { create(:instance_statistics_measurement, :project_count, recorded_at: 2.days.ago) } let_it_be(:project_measurement_old) { create(:instance_statistics_measurement, :project_count, recorded_at: 10.days.ago) } @@ -39,6 +41,37 @@ RSpec.describe Resolvers::Admin::Analytics::InstanceStatistics::MeasurementsReso end end end + + context 'when requesting pipeline counts by pipeline status' do + let_it_be(:pipelines_succeeded_measurement) { create(:instance_statistics_measurement, :pipelines_succeeded_count, recorded_at: 2.days.ago) } + let_it_be(:pipelines_skipped_measurement) { create(:instance_statistics_measurement, :pipelines_skipped_count, recorded_at: 2.days.ago) } + + subject { resolve_measurements({ identifier: identifier }, { current_user: current_user }) } + + context 'filter for pipelines_succeeded' do + let(:identifier) { 'pipelines_succeeded' } + + it { is_expected.to eq([pipelines_succeeded_measurement]) } + end + + context 'filter for pipelines_skipped' do + let(:identifier) { 'pipelines_skipped' } + + it { is_expected.to eq([pipelines_skipped_measurement]) } + end + + context 'filter for pipelines_failed' do + let(:identifier) { 'pipelines_failed' } + + it { is_expected.to be_empty } + end + + context 'filter for pipelines_canceled' do + let(:identifier) { 'pipelines_canceled' } + + it { is_expected.to be_empty } + end + end end def resolve_measurements(args = {}, context = {}) diff --git a/spec/graphql/resolvers/board_lists_resolver_spec.rb b/spec/graphql/resolvers/board_lists_resolver_spec.rb index fb6a5ccb781..c1d8041a1e0 100644 --- a/spec/graphql/resolvers/board_lists_resolver_spec.rb +++ b/spec/graphql/resolvers/board_lists_resolver_spec.rb @@ -101,6 +101,12 @@ RSpec.describe Resolvers::BoardListsResolver do end def resolve_board_lists(args: {}, current_user: user) - resolve(described_class, obj: board, args: args, ctx: { current_user: current_user }) + context = GraphQL::Query::Context.new( + query: OpenStruct.new(schema: nil), + values: { current_user: current_user }, + object: nil + ) + + resolve(described_class, obj: board, args: args, ctx: context ) end end diff --git a/spec/graphql/resolvers/board_resolver_spec.rb b/spec/graphql/resolvers/board_resolver_spec.rb new file mode 100644 index 00000000000..c70c696005f --- /dev/null +++ b/spec/graphql/resolvers/board_resolver_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Resolvers::BoardResolver do + include GraphqlHelpers + + let_it_be(:user) { create(:user) } + + let(:dummy_gid) { 'gid://gitlab/Board/1' } + + shared_examples_for 'group and project boards resolver' do + it 'does not create a default board' do + expect(resolve_board(id: dummy_gid)).to eq nil + end + + it 'calls Boards::ListService' do + expect_next_instance_of(Boards::ListService) do |service| + expect(service).to receive(:execute).and_return([]) + end + + resolve_board(id: dummy_gid) + end + + it 'requires an ID' do + expect do + resolve(described_class, obj: board_parent, args: {}, ctx: { current_user: user }) + end.to raise_error(Gitlab::Graphql::Errors::ArgumentError) + end + + context 'when querying for a single board' do + let(:board1) { create(:board, name: 'One', resource_parent: board_parent) } + + it 'returns specified board' do + expect(resolve_board(id: global_id_of(board1))).to eq board1 + end + + it 'returns nil if board not found' do + outside_parent = create(board_parent.class.underscore.to_sym) # rubocop:disable Rails/SaveBang + outside_board = create(:board, name: 'outside board', resource_parent: outside_parent) + + expect(resolve_board(id: global_id_of(outside_board))).to eq nil + end + end + end + + describe '#resolve' do + context 'when there is no parent' do + let(:board_parent) { nil } + + it 'returns nil if parent is nil' do + expect(resolve_board(id: dummy_gid)).to eq(nil) + end + end + + context 'when project boards' do + let(:board_parent) { create(:project, :public, creator_id: user.id, namespace: user.namespace ) } + + it_behaves_like 'group and project boards resolver' + end + + context 'when group boards' do + let(:board_parent) { create(:group) } + + it_behaves_like 'group and project boards resolver' + end + end + + def resolve_board(id:) + resolve(described_class, obj: board_parent, args: { id: id }, ctx: { current_user: user }) + end +end diff --git a/spec/graphql/resolvers/ci/runner_platforms_resolver_spec.rb b/spec/graphql/resolvers/ci/runner_platforms_resolver_spec.rb new file mode 100644 index 00000000000..1eb6f363d5b --- /dev/null +++ b/spec/graphql/resolvers/ci/runner_platforms_resolver_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Resolvers::Ci::RunnerPlatformsResolver do + include GraphqlHelpers + + describe '#resolve' do + subject(:resolve_subject) { resolve(described_class) } + + it 'returns all possible runner platforms' do + expect(resolve_subject).to include( + hash_including(name: :linux), hash_including(name: :osx), + hash_including(name: :windows), hash_including(name: :docker), + hash_including(name: :kubernetes) + ) + end + end +end diff --git a/spec/graphql/resolvers/concerns/looks_ahead_spec.rb b/spec/graphql/resolvers/concerns/looks_ahead_spec.rb index f13823085b8..ebea9e5522b 100644 --- a/spec/graphql/resolvers/concerns/looks_ahead_spec.rb +++ b/spec/graphql/resolvers/concerns/looks_ahead_spec.rb @@ -117,20 +117,6 @@ RSpec.describe LooksAhead do query.result end - context 'the feature flag is off' do - before do - stub_feature_flags(described_class::FEATURE_FLAG => false) - end - - it_behaves_like 'a working query on the test schema' - - it 'does not preload labels on issues' do - expect(the_user.issues).not_to receive(:preload).with(:labels) - - query.result - end - end - it 'issues fewer queries than the naive approach' do the_user.reload # ensure no attributes are loaded before we begin naive = <<-GQL diff --git a/spec/graphql/resolvers/group_milestones_resolver_spec.rb b/spec/graphql/resolvers/group_milestones_resolver_spec.rb index 05d0ec38192..d8ff8e9c1f2 100644 --- a/spec/graphql/resolvers/group_milestones_resolver_spec.rb +++ b/spec/graphql/resolvers/group_milestones_resolver_spec.rb @@ -15,6 +15,12 @@ RSpec.describe Resolvers::GroupMilestonesResolver do let_it_be(:now) { Time.now } let_it_be(:group) { create(:group, :private) } + def args(**arguments) + satisfy("contain only #{arguments.inspect}") do |passed| + expect(passed.compact).to match(arguments) + end + end + before_all do group.add_developer(current_user) end @@ -30,7 +36,7 @@ RSpec.describe Resolvers::GroupMilestonesResolver do context 'without parameters' do it 'calls MilestonesFinder to retrieve all milestones' do expect(MilestonesFinder).to receive(:new) - .with(ids: nil, group_ids: group.id, state: 'all', start_date: nil, end_date: nil) + .with(args(group_ids: group.id, state: 'all')) .and_call_original resolve_group_milestones @@ -43,11 +49,22 @@ RSpec.describe Resolvers::GroupMilestonesResolver do end_date = start_date + 1.hour expect(MilestonesFinder).to receive(:new) - .with(ids: nil, group_ids: group.id, state: 'closed', start_date: start_date, end_date: end_date) + .with(args(group_ids: group.id, state: 'closed', start_date: start_date, end_date: end_date)) .and_call_original resolve_group_milestones(start_date: start_date, end_date: end_date, state: 'closed') end + + it 'understands the timeframe argument' do + start_date = now + end_date = start_date + 1.hour + + expect(MilestonesFinder).to receive(:new) + .with(args(group_ids: group.id, state: 'closed', start_date: start_date, end_date: end_date)) + .and_call_original + + resolve_group_milestones(timeframe: { start: start_date, end: end_date }, state: 'closed') + end end context 'by ids' do @@ -55,7 +72,7 @@ RSpec.describe Resolvers::GroupMilestonesResolver do milestone = create(:milestone, group: group) expect(MilestonesFinder).to receive(:new) - .with(ids: [milestone.id.to_s], group_ids: group.id, state: 'all', start_date: nil, end_date: nil) + .with(args(ids: [milestone.id.to_s], group_ids: group.id, state: 'all')) .and_call_original resolve_group_milestones(ids: [milestone.to_global_id]) diff --git a/spec/graphql/resolvers/issues_resolver_spec.rb b/spec/graphql/resolvers/issues_resolver_spec.rb index db5d009f0e7..3a6507f906c 100644 --- a/spec/graphql/resolvers/issues_resolver_spec.rb +++ b/spec/graphql/resolvers/issues_resolver_spec.rb @@ -46,6 +46,13 @@ RSpec.describe Resolvers::IssuesResolver do expect(resolve_issues(assignee_username: assignee.username)).to contain_exactly(issue2) end + it 'filters by two assignees' do + assignee2 = create(:user) + issue2.update!(assignees: [assignee, assignee2]) + + expect(resolve_issues(assignee_id: [assignee.id, assignee2.id])).to contain_exactly(issue2) + end + it 'filters by assignee_id' do expect(resolve_issues(assignee_id: assignee.id)).to contain_exactly(issue2) end @@ -58,6 +65,10 @@ RSpec.describe Resolvers::IssuesResolver do expect(resolve_issues(assignee_id: IssuableFinder::Params::FILTER_NONE)).to contain_exactly(issue1) end + it 'filters by author' do + expect(resolve_issues(author_username: issue1.author.username)).to contain_exactly(issue1, issue2) + end + it 'filters by labels' do expect(resolve_issues(label_name: [label1.title])).to contain_exactly(issue1, issue2) expect(resolve_issues(label_name: [label1.title, label2.title])).to contain_exactly(issue2) @@ -219,6 +230,21 @@ RSpec.describe Resolvers::IssuesResolver do expect(resolve_issues(sort: :milestone_due_desc).items).to eq([milestone_issue3, milestone_issue2, milestone_issue1]) end end + + context 'when sorting by severity' do + let_it_be(:project) { create(:project) } + let_it_be(:issue_high_severity) { create_issue_with_severity(project, severity: :high) } + let_it_be(:issue_low_severity) { create_issue_with_severity(project, severity: :low) } + let_it_be(:issue_no_severity) { create(:incident, project: project) } + + it 'sorts issues ascending' do + expect(resolve_issues(sort: :severity_asc)).to eq([issue_no_severity, issue_low_severity, issue_high_severity]) + end + + it 'sorts issues descending' do + expect(resolve_issues(sort: :severity_desc)).to eq([issue_high_severity, issue_low_severity, issue_no_severity]) + end + end end it 'returns issues user can see' do @@ -304,6 +330,13 @@ RSpec.describe Resolvers::IssuesResolver do expect(field.to_graphql.complexity.call({}, { labelName: 'foo' }, 1)).to eq 8 end + def create_issue_with_severity(project, severity:) + issue = create(:incident, project: project) + create(:issuable_severity, issue: issue, severity: severity) + + issue + end + def resolve_issues(args = {}, context = { current_user: current_user }) resolve(described_class, obj: project, args: args, ctx: context) end diff --git a/spec/graphql/resolvers/project_milestones_resolver_spec.rb b/spec/graphql/resolvers/project_milestones_resolver_spec.rb index e0b250cfe7c..b641a54393e 100644 --- a/spec/graphql/resolvers/project_milestones_resolver_spec.rb +++ b/spec/graphql/resolvers/project_milestones_resolver_spec.rb @@ -13,13 +13,19 @@ RSpec.describe Resolvers::ProjectMilestonesResolver do project.add_developer(current_user) end + def args(**arguments) + satisfy("contain only #{arguments.inspect}") do |passed| + expect(passed.compact).to match(arguments) + end + end + def resolve_project_milestones(args = {}, context = { current_user: current_user }) resolve(described_class, obj: project, args: args, ctx: context) end it 'calls MilestonesFinder to retrieve all milestones' do expect(MilestonesFinder).to receive(:new) - .with(ids: nil, project_ids: project.id, state: 'all', start_date: nil, end_date: nil) + .with(args(project_ids: project.id, state: 'all')) .and_call_original resolve_project_milestones @@ -36,7 +42,7 @@ RSpec.describe Resolvers::ProjectMilestonesResolver do it 'calls MilestonesFinder with correct parameters' do expect(MilestonesFinder).to receive(:new) - .with(ids: nil, project_ids: project.id, group_ids: contain_exactly(group, parent_group), state: 'all', start_date: nil, end_date: nil) + .with(args(project_ids: project.id, group_ids: contain_exactly(group, parent_group), state: 'all')) .and_call_original resolve_project_milestones(include_ancestors: true) @@ -48,7 +54,7 @@ RSpec.describe Resolvers::ProjectMilestonesResolver do milestone = create(:milestone, project: project) expect(MilestonesFinder).to receive(:new) - .with(ids: [milestone.id.to_s], project_ids: project.id, state: 'all', start_date: nil, end_date: nil) + .with(args(ids: [milestone.id.to_s], project_ids: project.id, state: 'all')) .and_call_original resolve_project_milestones(ids: [milestone.to_global_id]) @@ -58,7 +64,7 @@ RSpec.describe Resolvers::ProjectMilestonesResolver do context 'by state' do it 'calls MilestonesFinder with correct parameters' do expect(MilestonesFinder).to receive(:new) - .with(ids: nil, project_ids: project.id, state: 'closed', start_date: nil, end_date: nil) + .with(args(project_ids: project.id, state: 'closed')) .and_call_original resolve_project_milestones(state: 'closed') @@ -72,7 +78,7 @@ RSpec.describe Resolvers::ProjectMilestonesResolver do end_date = Time.now + 5.days expect(MilestonesFinder).to receive(:new) - .with(ids: nil, project_ids: project.id, state: 'all', start_date: start_date, end_date: end_date) + .with(args(project_ids: project.id, state: 'all', start_date: start_date, end_date: end_date)) .and_call_original resolve_project_milestones(start_date: start_date, end_date: end_date) @@ -102,6 +108,51 @@ RSpec.describe Resolvers::ProjectMilestonesResolver do end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, /Both startDate and endDate/) end end + + context 'when passing a timeframe' do + it 'calls MilestonesFinder with correct parameters' do + start_date = Time.now + end_date = Time.now + 5.days + + expect(MilestonesFinder).to receive(:new) + .with(args(project_ids: project.id, state: 'all', start_date: start_date, end_date: end_date)) + .and_call_original + + resolve_project_milestones(timeframe: { start: start_date, end: end_date }) + end + end + end + + context 'when title is present' do + it 'calls MilestonesFinder with correct parameters' do + expect(MilestonesFinder).to receive(:new) + .with(args(title: '13.5', state: 'all', project_ids: project.id)) + .and_call_original + + resolve_project_milestones(title: '13.5') + end + end + + context 'when search_title is present' do + it 'calls MilestonesFinder with correct parameters' do + expect(MilestonesFinder).to receive(:new) + .with(args(search_title: '13', state: 'all', project_ids: project.id)) + .and_call_original + + resolve_project_milestones(search_title: '13') + end + end + + context 'when containing date is present' do + it 'calls MilestonesFinder with correct parameters' do + t = Time.now + + expect(MilestonesFinder).to receive(:new) + .with(args(containing_date: t, state: 'all', project_ids: project.id)) + .and_call_original + + resolve_project_milestones(containing_date: t) + end end context 'when user cannot read milestones' do diff --git a/spec/graphql/resolvers/projects_resolver_spec.rb b/spec/graphql/resolvers/projects_resolver_spec.rb index d22ffeed740..83a26062957 100644 --- a/spec/graphql/resolvers/projects_resolver_spec.rb +++ b/spec/graphql/resolvers/projects_resolver_spec.rb @@ -8,10 +8,14 @@ RSpec.describe Resolvers::ProjectsResolver do describe '#resolve' do subject { resolve(described_class, obj: nil, args: filters, ctx: { current_user: current_user }) } + let_it_be(:group) { create(:group, name: 'public-group') } + let_it_be(:private_group) { create(:group, name: 'private-group') } let_it_be(:project) { create(:project, :public) } let_it_be(:other_project) { create(:project, :public) } + let_it_be(:group_project) { create(:project, :public, group: group) } let_it_be(:private_project) { create(:project, :private) } let_it_be(:other_private_project) { create(:project, :private) } + let_it_be(:private_group_project) { create(:project, :private, group: private_group) } let_it_be(:user) { create(:user) } @@ -20,6 +24,11 @@ RSpec.describe Resolvers::ProjectsResolver do before_all do project.add_developer(user) private_project.add_developer(user) + private_group.add_developer(user) + end + + before do + stub_feature_flags(project_finder_similarity_sort: false) end context 'when user is not logged in' do @@ -27,7 +36,7 @@ RSpec.describe Resolvers::ProjectsResolver do context 'when no filters are applied' do it 'returns all public projects' do - is_expected.to contain_exactly(project, other_project) + is_expected.to contain_exactly(project, other_project, group_project) end context 'when search filter is provided' do @@ -45,6 +54,22 @@ RSpec.describe Resolvers::ProjectsResolver do is_expected.to be_empty end end + + context 'when searchNamespaces filter is provided' do + let(:filters) { { search: 'group', search_namespaces: true } } + + it 'returns projects in a matching namespace' do + is_expected.to contain_exactly(group_project) + end + end + + context 'when searchNamespaces filter false' do + let(:filters) { { search: 'group', search_namespaces: false } } + + it 'returns ignores namespace matches' do + is_expected.to be_empty + end + end end end @@ -53,7 +78,7 @@ RSpec.describe Resolvers::ProjectsResolver do context 'when no filters are applied' do it 'returns all visible projects for the user' do - is_expected.to contain_exactly(project, other_project, private_project) + is_expected.to contain_exactly(project, other_project, group_project, private_project, private_group_project) end context 'when search filter is provided' do @@ -68,7 +93,23 @@ RSpec.describe Resolvers::ProjectsResolver do let(:filters) { { membership: true } } it 'returns projects that user is member of' do - is_expected.to contain_exactly(project, private_project) + is_expected.to contain_exactly(project, private_project, private_group_project) + end + end + + context 'when searchNamespaces filter is provided' do + let(:filters) { { search: 'group', search_namespaces: true } } + + it 'returns projects from matching group' do + is_expected.to contain_exactly(group_project, private_group_project) + end + end + + context 'when searchNamespaces filter false' do + let(:filters) { { search: 'group', search_namespaces: false } } + + it 'returns ignores namespace matches' do + is_expected.to be_empty end end @@ -79,6 +120,24 @@ RSpec.describe Resolvers::ProjectsResolver do is_expected.to contain_exactly(project) end end + + context 'when sort is similarity' do + let_it_be(:named_project1) { create(:project, :public, name: 'projAB', path: 'projAB') } + let_it_be(:named_project2) { create(:project, :public, name: 'projABC', path: 'projABC') } + let_it_be(:named_project3) { create(:project, :public, name: 'projA', path: 'projA') } + + let(:filters) { { search: 'projA', sort: 'similarity' } } + + it 'returns projects in order of similarity to search' do + stub_feature_flags(project_finder_similarity_sort: true) + + is_expected.to eq([named_project3, named_project1, named_project2]) + end + + it 'returns projects not in order of similarity to search if flag is off' do + is_expected.not_to eq([named_project3, named_project1, named_project2]) + end + end end end end diff --git a/spec/graphql/resolvers/snippets/blobs_resolver_spec.rb b/spec/graphql/resolvers/snippets/blobs_resolver_spec.rb new file mode 100644 index 00000000000..fdbd87c32be --- /dev/null +++ b/spec/graphql/resolvers/snippets/blobs_resolver_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Resolvers::Snippets::BlobsResolver do + include GraphqlHelpers + + describe '#resolve' do + let_it_be(:current_user) { create(:user) } + let_it_be(:snippet) { create(:personal_snippet, :private, :repository, author: current_user) } + + context 'when user is not authorized' do + let(:other_user) { create(:user) } + + it 'raises an error' do + expect do + resolve_blobs(snippet, user: other_user) + end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + + context 'when using no filter' do + it 'returns all snippet blobs' do + expect(resolve_blobs(snippet).map(&:path)).to contain_exactly(*snippet.list_files) + end + end + + context 'when using filters' do + context 'when paths is a single string' do + it 'returns an array of files' do + path = 'CHANGELOG' + + expect(resolve_blobs(snippet, args: { paths: path }).first.path).to eq(path) + end + end + + context 'when paths is an array of string' do + it 'returns an array of files' do + paths = ['CHANGELOG', 'README.md'] + + expect(resolve_blobs(snippet, args: { paths: paths }).map(&:path)).to contain_exactly(*paths) + end + end + end + end + + def resolve_blobs(snippet, user: current_user, args: {}) + resolve(described_class, args: args, ctx: { current_user: user }, obj: snippet) + end +end diff --git a/spec/graphql/resolvers/terraform/states_resolver_spec.rb b/spec/graphql/resolvers/terraform/states_resolver_spec.rb new file mode 100644 index 00000000000..64b515528cd --- /dev/null +++ b/spec/graphql/resolvers/terraform/states_resolver_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Resolvers::Terraform::StatesResolver do + include GraphqlHelpers + + it { expect(described_class.type).to eq(Types::Terraform::StateType) } + it { expect(described_class.null).to be_truthy } + + describe '#resolve' do + let_it_be(:project) { create(:project) } + + let_it_be(:production_state) { create(:terraform_state, project: project) } + let_it_be(:staging_state) { create(:terraform_state, project: project) } + let_it_be(:other_state) { create(:terraform_state) } + + let(:ctx) { Hash(current_user: user) } + let(:user) { create(:user, developer_projects: [project]) } + + subject { resolve(described_class, obj: project, ctx: ctx) } + + it 'returns states associated with the agent' do + expect(subject).to contain_exactly(production_state, staging_state) + end + + context 'user does not have permission' do + let(:user) { create(:user) } + + it { is_expected.to be_empty } + end + end +end |