diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /spec/graphql/resolvers | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'spec/graphql/resolvers')
11 files changed, 265 insertions, 72 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 new file mode 100644 index 00000000000..76854be2daa --- /dev/null +++ b/spec/graphql/resolvers/admin/analytics/instance_statistics/measurements_resolver_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Resolvers::Admin::Analytics::InstanceStatistics::MeasurementsResolver do + include GraphqlHelpers + + 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) } + + subject { resolve_measurements({ identifier: 'projects' }, { current_user: current_user }) } + + context 'when requesting project count measurements' do + context 'as an admin user' do + let(:current_user) { admin_user } + + it 'returns the records, latest first' do + expect(subject).to eq([project_measurement_new, project_measurement_old]) + end + end + + context 'as a non-admin user' do + let(:current_user) { user } + + it 'raises ResourceNotAvailable error' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + + context 'as an unauthenticated user' do + let(:current_user) { nil } + + it 'raises ResourceNotAvailable error' do + expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) + end + end + end + end + + def resolve_measurements(args = {}, context = {}) + resolve(described_class, args: args, ctx: context) + end +end diff --git a/spec/graphql/resolvers/board_list_issues_resolver_spec.rb b/spec/graphql/resolvers/board_list_issues_resolver_spec.rb index e23a37b3d69..4ccf194522f 100644 --- a/spec/graphql/resolvers/board_list_issues_resolver_spec.rb +++ b/spec/graphql/resolvers/board_list_issues_resolver_spec.rb @@ -11,41 +11,59 @@ RSpec.describe Resolvers::BoardListIssuesResolver do let_it_be(:group) { create(:group, :private) } shared_examples_for 'group and project board list issues resolver' do - let!(:board) { create(:board, resource_parent: board_parent) } - before do board_parent.add_developer(user) end # auth is handled by the parent object context 'when authorized' do - let!(:list) { create(:list, board: board, label: label) } + let!(:issue1) { create(:issue, project: project, labels: [label], relative_position: 10) } + let!(:issue2) { create(:issue, project: project, labels: [label, label2], relative_position: 12) } + let!(:issue3) { create(:issue, project: project, labels: [label, label3], relative_position: 10) } it 'returns the issues in the correct order' do - issue1 = create(:issue, project: project, labels: [label], relative_position: 10) - issue2 = create(:issue, project: project, labels: [label], relative_position: 12) - issue3 = create(:issue, project: project, labels: [label], relative_position: 10) - # by relative_position and then ID issues = resolve_board_list_issues.items expect(issues.map(&:id)).to eq [issue3.id, issue1.id, issue2.id] end + + it 'finds only issues matching filters' do + result = resolve_board_list_issues(args: { filters: { label_name: label.title, not: { label_name: label2.title } } }).items + + expect(result).to match_array([issue1, issue3]) + end + + it 'finds only issues matching search param' do + result = resolve_board_list_issues(args: { filters: { search: issue1.title } }).items + + expect(result).to match_array([issue1]) + end end end describe '#resolve' do context 'when project boards' do + let_it_be(:label) { create(:label, project: user_project) } + let_it_be(:label2) { create(:label, project: user_project) } + let_it_be(:label3) { create(:label, project: user_project) } + let_it_be(:board) { create(:board, resource_parent: user_project) } + let_it_be(:list) { create(:list, board: board, label: label) } + let(:board_parent) { user_project } - let!(:label) { create(:label, project: project, name: 'project label') } let(:project) { user_project } it_behaves_like 'group and project board list issues resolver' end context 'when group boards' do + let_it_be(:label) { create(:group_label, group: group) } + let_it_be(:label2) { create(:group_label, group: group) } + let_it_be(:label3) { create(:group_label, group: group) } + let_it_be(:board) { create(:board, resource_parent: group) } + let_it_be(:list) { create(:list, board: board, label: label) } + let(:board_parent) { group } - let!(:label) { create(:group_label, group: group, name: 'group label') } let!(:project) { create(:project, :private, group: group) } it_behaves_like 'group and project board list issues resolver' diff --git a/spec/graphql/resolvers/group_members_resolver_spec.rb b/spec/graphql/resolvers/group_members_resolver_spec.rb new file mode 100644 index 00000000000..bbfea575492 --- /dev/null +++ b/spec/graphql/resolvers/group_members_resolver_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Resolvers::GroupMembersResolver do + include GraphqlHelpers + + it_behaves_like 'querying members with a group' do + let_it_be(:resource_member) { create(:group_member, user: user_1, group: group_1) } + let_it_be(:resource) { group_1 } + end +end diff --git a/spec/graphql/resolvers/issue_status_counts_resolver_spec.rb b/spec/graphql/resolvers/issue_status_counts_resolver_spec.rb index 69e940ee6ca..16eb190efc6 100644 --- a/spec/graphql/resolvers/issue_status_counts_resolver_spec.rb +++ b/spec/graphql/resolvers/issue_status_counts_resolver_spec.rb @@ -41,6 +41,12 @@ RSpec.describe Resolvers::IssueStatusCountsResolver do it_behaves_like 'returns expected results' + context 'project used as parent' do + let(:parent) { project } + + it_behaves_like 'returns expected results' + end + context 'group used as parent' do let(:parent) { project.group } diff --git a/spec/graphql/resolvers/merge_request_pipelines_resolver_spec.rb b/spec/graphql/resolvers/merge_request_pipelines_resolver_spec.rb index 2fe3e86ec14..ae3097c1d9e 100644 --- a/spec/graphql/resolvers/merge_request_pipelines_resolver_spec.rb +++ b/spec/graphql/resolvers/merge_request_pipelines_resolver_spec.rb @@ -29,4 +29,11 @@ RSpec.describe Resolvers::MergeRequestPipelinesResolver do it 'resolves only MRs for the passed merge request' do expect(resolve_pipelines).to contain_exactly(pipeline) end + + describe 'with archived project' do + let(:archived_project) { create(:project, :archived) } + let(:merge_request) { create(:merge_request, source_project: archived_project) } + + it { expect(resolve_pipelines).not_to contain_exactly(pipeline) } + end end diff --git a/spec/graphql/resolvers/merge_requests_resolver_spec.rb b/spec/graphql/resolvers/merge_requests_resolver_spec.rb index e939edae779..aecffc487aa 100644 --- a/spec/graphql/resolvers/merge_requests_resolver_spec.rb +++ b/spec/graphql/resolvers/merge_requests_resolver_spec.rb @@ -6,16 +6,20 @@ RSpec.describe Resolvers::MergeRequestsResolver do include GraphqlHelpers let_it_be(:project) { create(:project, :repository) } + let_it_be(:milestone) { create(:milestone, project: project) } let_it_be(:current_user) { create(:user) } + let_it_be(:other_user) { create(:user) } let_it_be(:common_attrs) { { author: current_user, source_project: project, target_project: project } } let_it_be(:merge_request_1) { create(:merge_request, :simple, **common_attrs) } let_it_be(:merge_request_2) { create(:merge_request, :rebased, **common_attrs) } let_it_be(:merge_request_3) { create(:merge_request, :unique_branches, **common_attrs) } let_it_be(:merge_request_4) { create(:merge_request, :unique_branches, :locked, **common_attrs) } let_it_be(:merge_request_5) { create(:merge_request, :simple, :locked, **common_attrs) } - let_it_be(:merge_request_6) { create(:labeled_merge_request, :unique_branches, labels: create_list(:label, 2), **common_attrs) } + let_it_be(:merge_request_6) { create(:labeled_merge_request, :unique_branches, labels: create_list(:label, 2, project: project), **common_attrs) } + let_it_be(:merge_request_with_milestone) { create(:merge_request, :unique_branches, **common_attrs, milestone: milestone) } let_it_be(:other_project) { create(:project, :repository) } let_it_be(:other_merge_request) { create(:merge_request, source_project: other_project, target_project: other_project) } + let(:iid_1) { merge_request_1.iid } let(:iid_2) { merge_request_2.iid } let(:other_iid) { other_merge_request.iid } @@ -32,7 +36,7 @@ RSpec.describe Resolvers::MergeRequestsResolver do it 'returns all merge requests' do result = resolve_mr(project, {}) - expect(result).to contain_exactly(merge_request_1, merge_request_2, merge_request_3, merge_request_4, merge_request_5, merge_request_6) + expect(result).to contain_exactly(merge_request_1, merge_request_2, merge_request_3, merge_request_4, merge_request_5, merge_request_6, merge_request_with_milestone) end it 'returns only merge requests that the current user can see' do @@ -179,6 +183,20 @@ RSpec.describe Resolvers::MergeRequestsResolver do end end + context 'by milestone' do + it 'filters merge requests by milestone title' do + result = resolve_mr(project, milestone_title: milestone.title) + + expect(result).to eq([merge_request_with_milestone]) + end + + it 'does not find anything' do + result = resolve_mr(project, milestone_title: 'unknown-milestone') + + expect(result).to be_empty + end + end + describe 'combinations' do it 'requires all filters' do create(:merge_request, :closed, source_project: project, target_project: project, source_branch: merge_request_4.source_branch) @@ -188,6 +206,33 @@ RSpec.describe Resolvers::MergeRequestsResolver do expect(result.compact).to contain_exactly(merge_request_4) end end + + describe 'sorting' do + context 'when sorting by created' do + it 'sorts merge requests ascending' do + expect(resolve_mr(project, sort: 'created_asc')).to eq [merge_request_1, merge_request_2, merge_request_3, merge_request_4, merge_request_5, merge_request_6, merge_request_with_milestone] + end + + it 'sorts merge requests descending' do + expect(resolve_mr(project, sort: 'created_desc')).to eq [merge_request_with_milestone, merge_request_6, merge_request_5, merge_request_4, merge_request_3, merge_request_2, merge_request_1] + end + end + + context 'when sorting by merged at' do + before do + merge_request_1.metrics.update!(merged_at: 10.days.ago) + merge_request_3.metrics.update!(merged_at: 5.days.ago) + end + + it 'sorts merge requests ascending' do + expect(resolve_mr(project, sort: :merged_at_asc)).to eq [merge_request_1, merge_request_3, merge_request_with_milestone, merge_request_6, merge_request_5, merge_request_4, merge_request_2] + end + + it 'sorts merge requests descending' do + expect(resolve_mr(project, sort: :merged_at_desc)).to eq [merge_request_3, merge_request_1, merge_request_with_milestone, merge_request_6, merge_request_5, merge_request_4, merge_request_2] + end + end + end end def resolve_mr_single(project, iid) diff --git a/spec/graphql/resolvers/namespace_projects_resolver_spec.rb b/spec/graphql/resolvers/namespace_projects_resolver_spec.rb index 699269b47e0..4ad8f99219f 100644 --- a/spec/graphql/resolvers/namespace_projects_resolver_spec.rb +++ b/spec/graphql/resolvers/namespace_projects_resolver_spec.rb @@ -27,7 +27,7 @@ RSpec.describe Resolvers::NamespaceProjectsResolver do end it 'finds all projects including the subgroups' do - expect(resolve_projects(include_subgroups: true)).to contain_exactly(project1, project2, nested_project) + expect(resolve_projects(include_subgroups: true, sort: nil, search: nil)).to contain_exactly(project1, project2, nested_project) end context 'with an user namespace' do @@ -38,7 +38,52 @@ RSpec.describe Resolvers::NamespaceProjectsResolver do end it 'finds all projects including the subgroups' do - expect(resolve_projects(include_subgroups: true)).to contain_exactly(project1, project2) + expect(resolve_projects(include_subgroups: true, sort: nil, search: nil)).to contain_exactly(project1, project2) + end + end + end + + context 'search and similarity sorting' do + let(:project_1) { create(:project, name: 'Project', path: 'project', namespace: namespace) } + let(:project_2) { create(:project, name: 'Test Project', path: 'test-project', namespace: namespace) } + let(:project_3) { create(:project, name: 'Test', path: 'test', namespace: namespace) } + + before do + project_1.add_developer(current_user) + project_2.add_developer(current_user) + project_3.add_developer(current_user) + end + + it 'returns projects ordered by similarity to the search input' do + projects = resolve_projects(include_subgroups: true, sort: :similarity, search: 'test') + + project_names = projects.map { |proj| proj['name'] } + expect(project_names.first).to eq('Test') + expect(project_names.second).to eq('Test Project') + end + + it 'filters out result that do not match the search input' do + projects = resolve_projects(include_subgroups: true, sort: :similarity, search: 'test') + + project_names = projects.map { |proj| proj['name'] } + expect(project_names).not_to include('Project') + end + + context 'when `search` parameter is not given' do + it 'returns projects not ordered by similarity' do + projects = resolve_projects(include_subgroups: true, sort: :similarity, search: nil) + + project_names = projects.map { |proj| proj['name'] } + expect(project_names.first).not_to eq('Test') + end + end + + context 'when only search term is given' do + it 'filters out result that do not match the search input, but does not sort them' do + projects = resolve_projects(include_subgroups: true, sort: :nil, search: 'test') + + project_names = projects.map { |proj| proj['name'] } + expect(project_names).to contain_exactly('Test', 'Test Project') end end end @@ -63,7 +108,7 @@ RSpec.describe Resolvers::NamespaceProjectsResolver do expect(field.to_graphql.complexity.call({}, { include_subgroups: true }, 1)).to eq 24 end - def resolve_projects(args = { include_subgroups: false }, context = { current_user: current_user }) + def resolve_projects(args = { include_subgroups: false, sort: nil, search: nil }, context = { current_user: current_user }) resolve(described_class, obj: namespace, args: args, ctx: context) end end diff --git a/spec/graphql/resolvers/project_members_resolver_spec.rb b/spec/graphql/resolvers/project_members_resolver_spec.rb index 602225cf632..2f4145b3215 100644 --- a/spec/graphql/resolvers/project_members_resolver_spec.rb +++ b/spec/graphql/resolvers/project_members_resolver_spec.rb @@ -5,58 +5,9 @@ require 'spec_helper' RSpec.describe Resolvers::ProjectMembersResolver do include GraphqlHelpers - context "with a group" do - let_it_be(:root_group) { create(:group) } - let_it_be(:group_1) { create(:group, parent: root_group) } - let_it_be(:group_2) { create(:group, parent: root_group) } - let_it_be(:project) { create(:project, :public, group: group_1) } - - let_it_be(:user_1) { create(:user, name: 'test user') } - let_it_be(:user_2) { create(:user, name: 'test user 2') } - let_it_be(:user_3) { create(:user, name: 'another user 1') } - let_it_be(:user_4) { create(:user, name: 'another user 2') } - - let_it_be(:project_member) { create(:project_member, user: user_1, project: project) } - let_it_be(:group_1_member) { create(:group_member, user: user_2, group: group_1) } - let_it_be(:group_2_member) { create(:group_member, user: user_3, group: group_2) } - let_it_be(:root_group_member) { create(:group_member, user: user_4, group: root_group) } - - let(:args) { {} } - - subject do - resolve(described_class, obj: project, args: args, ctx: { context: user_4 }) - end - - describe '#resolve' do - it 'finds all project members' do - expect(subject).to contain_exactly(project_member, group_1_member, root_group_member) - end - - context 'with search' do - context 'when the search term matches a user' do - let(:args) { { search: 'test' } } - - it 'searches users by user name' do - expect(subject).to contain_exactly(project_member, group_1_member) - end - end - - context 'when the search term does not match any user' do - let(:args) { { search: 'nothing' } } - - it 'is empty' do - expect(subject).to be_empty - end - end - end - - context 'when project is nil' do - let(:project) { nil } - - it 'returns nil' do - expect(subject).to be_empty - end - end - end + it_behaves_like 'querying members with a group' do + let_it_be(:project) { create(:project, group: group_1) } + let_it_be(:resource_member) { create(:project_member, user: user_1, project: project) } + let_it_be(:resource) { project } end end diff --git a/spec/graphql/resolvers/project_merge_requests_resolver_spec.rb b/spec/graphql/resolvers/project_merge_requests_resolver_spec.rb new file mode 100644 index 00000000000..bfb3ce91d58 --- /dev/null +++ b/spec/graphql/resolvers/project_merge_requests_resolver_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Resolvers::ProjectMergeRequestsResolver do + include GraphqlHelpers + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:current_user) { create(:user) } + let_it_be(:other_user) { create(:user) } + + let_it_be(:merge_request_with_author_and_assignee) do + create(:merge_request, + :unique_branches, + source_project: project, + target_project: project, + author: other_user, + assignee: other_user) + end + + before do + project.add_developer(current_user) + end + + context 'by assignee' do + it 'filters merge requests by assignee username' do + result = resolve_mr(project, assignee_username: other_user.username) + + expect(result).to eq([merge_request_with_author_and_assignee]) + end + + it 'does not find anything' do + result = resolve_mr(project, assignee_username: 'unknown-user') + + expect(result).to be_empty + end + end + + context 'by author' do + it 'filters merge requests by author username' do + result = resolve_mr(project, author_username: other_user.username) + + expect(result).to eq([merge_request_with_author_and_assignee]) + end + + it 'does not find anything' do + result = resolve_mr(project, author_username: 'unknown-user') + + expect(result).to be_empty + end + end + + def resolve_mr(project, args, resolver: described_class, user: current_user) + resolve(resolver, obj: project, args: args, ctx: { current_user: user }) + end +end diff --git a/spec/graphql/resolvers/project_pipeline_resolver_spec.rb b/spec/graphql/resolvers/project_pipeline_resolver_spec.rb index fada2f9193c..a6a86c49373 100644 --- a/spec/graphql/resolvers/project_pipeline_resolver_spec.rb +++ b/spec/graphql/resolvers/project_pipeline_resolver_spec.rb @@ -34,12 +34,10 @@ RSpec.describe Resolvers::ProjectPipelineResolver do expect { resolve_pipeline(project, {}) }.to raise_error(ArgumentError) end - context 'when the pipeline is not a ci_config_source' do + context 'when the pipeline is a dangling pipeline' do let(:pipeline) do - config_source_value = Ci::PipelineEnums.non_ci_config_source_values.first - config_source = Ci::PipelineEnums.config_sources.key(config_source_value) - - create(:ci_pipeline, config_source: config_source, project: project) + dangling_source = ::Enums::Ci::Pipeline.dangling_sources.each_value.first + create(:ci_pipeline, source: dangling_source, project: project) end it 'resolves pipeline for the passed iid' do diff --git a/spec/graphql/resolvers/projects_resolver_spec.rb b/spec/graphql/resolvers/projects_resolver_spec.rb index db7c9225c84..d22ffeed740 100644 --- a/spec/graphql/resolvers/projects_resolver_spec.rb +++ b/spec/graphql/resolvers/projects_resolver_spec.rb @@ -71,6 +71,14 @@ RSpec.describe Resolvers::ProjectsResolver do is_expected.to contain_exactly(project, private_project) end end + + context 'when ids filter is provided' do + let(:filters) { { ids: [project.to_global_id.to_s] } } + + it 'returns matching project' do + is_expected.to contain_exactly(project) + end + end end end end |