2019-05-12 17:10:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Resolvers::NamespaceProjectsResolver do
|
2019-05-12 17:10:46 -04:00
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
|
|
|
|
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
|
2020-09-04 23:08:31 -04:00
|
|
|
expect(resolve_projects(include_subgroups: true, sort: nil, search: nil)).to contain_exactly(project1, project2, nested_project)
|
2019-05-12 17:10:46 -04:00
|
|
|
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
|
2020-09-04 23:08:31 -04:00
|
|
|
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')
|
2019-05-12 17:10:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when passing a non existent, batch loaded namespace" do
|
|
|
|
let(:namespace) do
|
2019-09-04 13:42:48 -04:00
|
|
|
BatchLoader::GraphQL.for("non-existent-path").batch do |_fake_paths, loader, _|
|
2019-05-12 17:10:46 -04:00
|
|
|
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
|
2019-06-04 18:52:06 -04:00
|
|
|
field = Types::BaseField.new(name: 'test', type: GraphQL::STRING_TYPE.connection_type, resolver_class: described_class, null: false, max_page_size: 100)
|
2019-05-12 17:10:46 -04:00
|
|
|
|
|
|
|
expect(field.to_graphql.complexity.call({}, {}, 1)).to eq 24
|
|
|
|
expect(field.to_graphql.complexity.call({}, { include_subgroups: true }, 1)).to eq 24
|
|
|
|
end
|
|
|
|
|
2020-09-04 23:08:31 -04:00
|
|
|
def resolve_projects(args = { include_subgroups: false, sort: nil, search: nil }, context = { current_user: current_user })
|
2019-05-12 17:10:46 -04:00
|
|
|
resolve(described_class, obj: namespace, args: args, ctx: context)
|
|
|
|
end
|
|
|
|
end
|