debian-mirror-gitlab/spec/graphql/resolvers/namespace_projects_resolver_spec.rb

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

158 lines
5 KiB
Ruby
Raw Normal View History

2019-09-04 21:01:54 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe Resolvers::NamespaceProjectsResolver, feature_category: :subgroups do
2019-09-04 21:01:54 +05:30
include GraphqlHelpers
let(:current_user) { create(:user) }
2021-04-17 20:07:23 +05:30
let(:include_subgroups) { true }
let(:sort) { nil }
let(:search) { nil }
let(:ids) { nil }
let(:args) do
{
include_subgroups: include_subgroups,
sort: sort,
search: search,
ids: ids
}
end
2019-09-04 21:01:54 +05:30
context "with a group" do
let(:group) { create(:group) }
let(:namespace) { group }
let(:project1) { create(:project, namespace: namespace) }
let(:project2) { create(:project, namespace: namespace) }
let(:nested_group) { create(:group, parent: group) }
let(:nested_project) { create(:project, group: nested_group) }
before do
project1.add_developer(current_user)
project2.add_developer(current_user)
nested_project.add_developer(current_user)
end
describe '#resolve' do
it 'finds all projects' do
expect(resolve_projects).to contain_exactly(project1, project2)
end
it 'finds all projects including the subgroups' do
2021-04-17 20:07:23 +05:30
expect(resolve_projects(args)).to contain_exactly(project1, project2, nested_project)
2019-09-04 21:01:54 +05:30
end
context 'with an user namespace' do
let(:namespace) { current_user.namespace }
it 'finds all projects' do
expect(resolve_projects).to contain_exactly(project1, project2)
end
it 'finds all projects including the subgroups' do
2021-04-17 20:07:23 +05:30
expect(resolve_projects(args)).to contain_exactly(project1, project2)
2020-11-24 15:15:51 +05:30
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) }
2021-04-17 20:07:23 +05:30
let(:sort) { :similarity }
let(:search) { 'test' }
2020-11-24 15:15:51 +05:30
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
2021-04-17 20:07:23 +05:30
projects = resolve_projects(args)
2020-11-24 15:15:51 +05:30
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
2021-04-17 20:07:23 +05:30
projects = resolve_projects(args)
2020-11-24 15:15:51 +05:30
project_names = projects.map { |proj| proj['name'] }
expect(project_names).not_to include('Project')
end
context 'when `search` parameter is not given' do
2021-04-17 20:07:23 +05:30
let(:search) { nil }
2020-11-24 15:15:51 +05:30
it 'returns projects not ordered by similarity' do
2021-04-17 20:07:23 +05:30
projects = resolve_projects(args)
2020-11-24 15:15:51 +05:30
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
2021-04-17 20:07:23 +05:30
let(:sort) { nil }
let(:search) { 'test' }
2020-11-24 15:15:51 +05:30
it 'filters out result that do not match the search input, but does not sort them' do
2021-04-17 20:07:23 +05:30
projects = resolve_projects(args)
2020-11-24 15:15:51 +05:30
project_names = projects.map { |proj| proj['name'] }
expect(project_names).to contain_exactly('Test', 'Test Project')
2019-09-04 21:01:54 +05:30
end
end
end
2021-04-17 20:07:23 +05:30
context 'ids filtering' do
subject(:projects) { resolve_projects(args) }
let(:include_subgroups) { false }
2021-04-29 21:17:54 +05:30
let!(:project_3) { create(:project, name: 'Project', path: 'project', namespace: namespace) }
2021-04-17 20:07:23 +05:30
context 'when ids is provided' do
let(:ids) { [project_3.to_global_id.to_s] }
it 'returns matching project' do
expect(projects).to contain_exactly(project_3)
end
end
context 'when ids is nil' do
let(:ids) { nil }
it 'returns all projects' do
expect(projects).to contain_exactly(project1, project2, project_3)
end
end
end
2019-09-04 21:01:54 +05:30
end
context "when passing a non existent, batch loaded namespace" do
let(:namespace) do
2019-12-04 20:38:33 +05:30
BatchLoader::GraphQL.for("non-existent-path").batch do |_fake_paths, loader, _|
2019-09-04 21:01:54 +05:30
loader.call("non-existent-path", nil)
end
end
it "returns nil without breaking" do
expect(resolve_projects).to be_empty
end
end
it 'has an high complexity regardless of arguments' do
2021-10-27 15:23:28 +05:30
field = Types::BaseField.new(name: 'test', type: GraphQL::Types::String.connection_type, resolver_class: described_class, null: false, max_page_size: 100)
2019-09-04 21:01:54 +05:30
2022-05-07 20:08:51 +05:30
expect(field.complexity.call({}, {}, 1)).to eq 24
expect(field.complexity.call({}, { include_subgroups: true }, 1)).to eq 24
2019-09-04 21:01:54 +05:30
end
2021-04-17 20:07:23 +05:30
def resolve_projects(args = { include_subgroups: false, sort: nil, search: nil, ids: nil }, context = { current_user: current_user })
2022-07-23 23:45:48 +05:30
resolve(described_class, obj: namespace, args: args, ctx: context, arg_style: :internal)
2019-09-04 21:01:54 +05:30
end
end