2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-06-09 14:43:08 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::ProjectSearchResults do
|
2019-09-09 13:46:24 +00:00
|
|
|
include SearchHelpers
|
|
|
|
|
2020-09-03 21:08:18 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
2021-06-29 03:07:32 +00:00
|
|
|
|
2015-06-09 14:43:08 +00:00
|
|
|
let(:query) { 'hello world' }
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:repository_ref) { nil }
|
2020-09-03 21:08:18 +00:00
|
|
|
let(:filters) { {} }
|
2015-06-09 14:43:08 +00:00
|
|
|
|
2020-09-03 21:08:18 +00:00
|
|
|
subject(:results) { described_class.new(user, query, project: project, repository_ref: repository_ref, filters: filters) }
|
2015-06-09 14:43:08 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'with a repository_ref' do
|
|
|
|
context 'when empty' do
|
|
|
|
let(:repository_ref) { '' }
|
|
|
|
|
|
|
|
it { expect(results.project).to eq(project) }
|
|
|
|
it { expect(results.query).to eq('hello world') }
|
|
|
|
end
|
2015-06-09 14:43:08 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'when set' do
|
|
|
|
let(:repository_ref) { 'refs/heads/test' }
|
2015-06-09 14:43:08 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it { expect(results.project).to eq(project) }
|
|
|
|
it { expect(results.repository_ref).to eq(repository_ref) }
|
|
|
|
it { expect(results.query).to eq('hello world') }
|
|
|
|
end
|
2015-06-09 14:43:08 +00:00
|
|
|
end
|
2016-03-17 20:48:19 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
describe '#formatted_count' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:scope, :count_method, :expected) do
|
2020-02-16 21:08:53 +00:00
|
|
|
'blobs' | :limited_blobs_count | max_limited_count
|
2019-09-09 13:46:24 +00:00
|
|
|
'notes' | :limited_notes_count | max_limited_count
|
2019-07-15 17:59:57 +00:00
|
|
|
'wiki_blobs' | :wiki_blobs_count | '1234'
|
2020-05-25 03:08:11 +00:00
|
|
|
'commits' | :commits_count | max_limited_count
|
2019-09-09 13:46:24 +00:00
|
|
|
'projects' | :limited_projects_count | max_limited_count
|
2019-07-15 17:59:57 +00:00
|
|
|
'unknown' | nil | nil
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns the expected formatted count' do
|
|
|
|
expect(results).to receive(count_method).and_return(1234) if count_method
|
|
|
|
expect(results.formatted_count(scope)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
2020-05-07 21:09:26 +00:00
|
|
|
|
|
|
|
context 'blobs' do
|
|
|
|
it "limits the search to #{described_class::COUNT_LIMIT} items" do
|
|
|
|
expect(results).to receive(:blobs).with(limit: described_class::COUNT_LIMIT).and_call_original
|
|
|
|
expect(results.formatted_count('blobs')).to eq('0')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'wiki_blobs' do
|
|
|
|
it "limits the search to #{described_class::COUNT_LIMIT} items" do
|
|
|
|
expect(results).to receive(:wiki_blobs).with(limit: described_class::COUNT_LIMIT).and_call_original
|
|
|
|
expect(results.formatted_count('wiki_blobs')).to eq('0')
|
|
|
|
end
|
|
|
|
end
|
2019-07-15 17:59:57 +00:00
|
|
|
end
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
shared_examples 'general blob search' do |entity_type, blob_type|
|
2018-06-04 11:41:37 +00:00
|
|
|
let(:query) { 'files' }
|
2020-08-21 03:10:16 +00:00
|
|
|
|
|
|
|
subject(:objects) { results.objects(blob_type) }
|
2017-04-24 15:09:29 +00:00
|
|
|
|
2018-06-04 11:41:37 +00:00
|
|
|
context "when #{entity_type} is disabled" do
|
|
|
|
let(:project) { disabled_project }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it "hides #{blob_type} from members" do
|
2017-04-24 15:09:29 +00:00
|
|
|
project.add_reporter(user)
|
|
|
|
|
|
|
|
is_expected.to be_empty
|
|
|
|
end
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it "hides #{blob_type} from non-members" do
|
2017-04-24 15:09:29 +00:00
|
|
|
is_expected.to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-04 11:41:37 +00:00
|
|
|
context "when #{entity_type} is internal" do
|
|
|
|
let(:project) { private_project }
|
2017-04-24 15:09:29 +00:00
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it "finds #{blob_type} for members" do
|
2017-04-24 15:09:29 +00:00
|
|
|
project.add_reporter(user)
|
|
|
|
|
|
|
|
is_expected.not_to be_empty
|
|
|
|
end
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it "hides #{blob_type} from non-members" do
|
2017-04-24 15:09:29 +00:00
|
|
|
is_expected.to be_empty
|
|
|
|
end
|
|
|
|
end
|
2016-11-10 17:27:09 +00:00
|
|
|
|
|
|
|
it 'finds by name' do
|
2020-08-21 03:10:16 +00:00
|
|
|
expect(objects.map(&:path)).to include(expected_file_by_path)
|
2018-12-02 21:47:33 +00:00
|
|
|
end
|
|
|
|
|
2019-10-24 00:07:18 +00:00
|
|
|
it "loads all blobs for path matches in single batch" do
|
2018-12-02 21:47:33 +00:00
|
|
|
expect(Gitlab::Git::Blob).to receive(:batch).once.and_call_original
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
expect { objects.map(&:data) }.not_to raise_error
|
2016-11-10 17:27:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds by content' do
|
2020-08-21 03:10:16 +00:00
|
|
|
blob = objects.select { |result| result.path == expected_file_by_content }.flatten.last
|
2016-11-10 17:27:09 +00:00
|
|
|
|
2019-10-24 00:07:18 +00:00
|
|
|
expect(blob.path).to eq(expected_file_by_content)
|
2018-06-04 11:41:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
shared_examples 'blob search repository ref' do |entity_type, blob_type|
|
2018-07-20 13:49:30 +00:00
|
|
|
let(:query) { 'files' }
|
|
|
|
let(:file_finder) { double }
|
|
|
|
let(:project_branch) { 'project_branch' }
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
subject(:objects) { results.objects(blob_type) }
|
2018-07-20 13:49:30 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(entity).to receive(:default_branch).and_return(project_branch)
|
|
|
|
allow(file_finder).to receive(:find).and_return([])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository_ref exists' do
|
|
|
|
let(:repository_ref) { 'ref_branch' }
|
|
|
|
|
|
|
|
it 'uses it' do
|
|
|
|
expect(Gitlab::FileFinder).to receive(:new).with(project, repository_ref).and_return(file_finder)
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
expect { objects }.not_to raise_error
|
2018-07-20 13:49:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository_ref is not present' do
|
|
|
|
let(:repository_ref) { nil }
|
|
|
|
|
|
|
|
it "uses #{entity_type} repository default reference" do
|
|
|
|
expect(Gitlab::FileFinder).to receive(:new).with(project, project_branch).and_return(file_finder)
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
expect { objects }.not_to raise_error
|
2018-07-20 13:49:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository_ref is blank' do
|
|
|
|
let(:repository_ref) { '' }
|
|
|
|
|
|
|
|
it "uses #{entity_type} repository default reference" do
|
|
|
|
expect(Gitlab::FileFinder).to receive(:new).with(project, project_branch).and_return(file_finder)
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
expect { objects }.not_to raise_error
|
2018-07-20 13:49:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
shared_examples 'blob search pagination' do |blob_type|
|
|
|
|
let(:per_page) { 20 }
|
|
|
|
let(:count_limit) { described_class::COUNT_LIMIT }
|
|
|
|
let(:file_finder) { instance_double('Gitlab::FileFinder') }
|
|
|
|
let(:repository_ref) { 'master' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(file_finder).to receive(:find).and_return([])
|
|
|
|
expect(Gitlab::FileFinder).to receive(:new).with(project, repository_ref).and_return(file_finder)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'limits search results based on the first page' do
|
|
|
|
expect(file_finder).to receive(:find).with(query, content_match_cutoff: count_limit)
|
|
|
|
results.objects(blob_type, page: 1, per_page: per_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'limits search results based on the second page' do
|
|
|
|
expect(file_finder).to receive(:find).with(query, content_match_cutoff: count_limit + per_page)
|
|
|
|
results.objects(blob_type, page: 2, per_page: per_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'limits search results based on the third page' do
|
|
|
|
expect(file_finder).to receive(:find).with(query, content_match_cutoff: count_limit + per_page * 2)
|
|
|
|
results.objects(blob_type, page: 3, per_page: per_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the per_page value when passed' do
|
|
|
|
expect(file_finder).to receive(:find).with(query, content_match_cutoff: count_limit + 10 * 2)
|
|
|
|
results.objects(blob_type, page: 3, per_page: 10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-04 11:41:37 +00:00
|
|
|
describe 'blob search' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
|
|
|
|
it_behaves_like 'general blob search', 'repository', 'blobs' do
|
|
|
|
let(:disabled_project) { create(:project, :public, :repository, :repository_disabled) }
|
|
|
|
let(:private_project) { create(:project, :public, :repository, :repository_private) }
|
2019-10-24 00:07:18 +00:00
|
|
|
let(:expected_file_by_path) { 'files/images/wm.svg' }
|
2018-06-04 11:41:37 +00:00
|
|
|
let(:expected_file_by_content) { 'CHANGELOG' }
|
2016-11-10 17:27:09 +00:00
|
|
|
end
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it_behaves_like 'blob search repository ref', 'project', 'blobs' do
|
2018-07-20 13:49:30 +00:00
|
|
|
let(:entity) { project }
|
|
|
|
end
|
2020-02-16 21:08:53 +00:00
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it_behaves_like 'blob search pagination', 'blobs'
|
2016-11-10 17:27:09 +00:00
|
|
|
end
|
|
|
|
|
2017-04-24 15:09:29 +00:00
|
|
|
describe 'wiki search' do
|
2018-05-04 09:45:16 +00:00
|
|
|
let(:project) { create(:project, :public, :wiki_repo) }
|
2017-04-24 15:09:29 +00:00
|
|
|
|
2018-06-04 11:41:37 +00:00
|
|
|
before do
|
2021-06-30 09:08:07 +00:00
|
|
|
project.wiki.create_page('Files/Title', 'Content')
|
|
|
|
project.wiki.create_page('CHANGELOG', 'Files example')
|
2017-04-24 15:09:29 +00:00
|
|
|
end
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it_behaves_like 'general blob search', 'wiki', 'wiki_blobs' do
|
2018-06-04 11:41:37 +00:00
|
|
|
let(:blob_type) { 'wiki_blobs' }
|
|
|
|
let(:disabled_project) { create(:project, :public, :wiki_repo, :wiki_disabled) }
|
|
|
|
let(:private_project) { create(:project, :public, :wiki_repo, :wiki_private) }
|
2019-10-24 00:07:18 +00:00
|
|
|
let(:expected_file_by_path) { 'Files/Title.md' }
|
2018-06-04 11:41:37 +00:00
|
|
|
let(:expected_file_by_content) { 'CHANGELOG.md' }
|
2017-04-24 15:09:29 +00:00
|
|
|
end
|
2018-07-20 13:49:30 +00:00
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
it_behaves_like 'blob search repository ref', 'wiki', 'wiki_blobs' do
|
2018-07-20 13:49:30 +00:00
|
|
|
let(:entity) { project.wiki }
|
|
|
|
end
|
2020-05-07 21:09:26 +00:00
|
|
|
|
|
|
|
it_behaves_like 'blob search pagination', 'wiki_blobs'
|
2020-05-08 15:09:28 +00:00
|
|
|
|
|
|
|
context 'return type' do
|
|
|
|
let(:blobs) { [Gitlab::Search::FoundBlob.new(project: project)] }
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:query) { "Files" }
|
|
|
|
|
|
|
|
subject(:objects) { results.objects('wiki_blobs', per_page: 20) }
|
2020-05-08 15:09:28 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(results).to receive(:wiki_blobs).and_return(blobs)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns list of FoundWikiPage type object' do
|
|
|
|
expect(objects).to be_present
|
|
|
|
expect(objects).to all(be_a(Gitlab::Search::FoundWikiPage))
|
|
|
|
end
|
|
|
|
end
|
2017-04-24 15:09:29 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
describe 'issues search' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:query) { issue.title }
|
2020-09-11 21:08:44 +00:00
|
|
|
let(:scope) { 'issues' }
|
2016-03-17 20:48:19 +00:00
|
|
|
|
2020-09-11 21:08:44 +00:00
|
|
|
subject(:objects) { results.objects(scope) }
|
2016-03-17 20:48:19 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it 'does not list issues on private projects' do
|
|
|
|
expect(objects).not_to include issue
|
2016-03-17 20:48:19 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
describe "confidential issues" do
|
|
|
|
include_examples "access restricted confidential issues"
|
2016-03-17 20:48:19 +00:00
|
|
|
end
|
2020-09-03 21:08:18 +00:00
|
|
|
|
|
|
|
context 'filtering' do
|
|
|
|
let_it_be(:project) { create(:project, :public) }
|
2020-09-11 21:08:44 +00:00
|
|
|
let_it_be(:closed_result) { create(:issue, :closed, project: project, title: 'foo closed') }
|
|
|
|
let_it_be(:opened_result) { create(:issue, :opened, project: project, title: 'foo opened') }
|
2020-09-25 09:09:40 +00:00
|
|
|
let_it_be(:confidential_result) { create(:issue, :confidential, project: project, title: 'foo confidential') }
|
2021-06-29 03:07:32 +00:00
|
|
|
|
2020-09-03 21:08:18 +00:00
|
|
|
let(:query) { 'foo' }
|
|
|
|
|
2020-09-25 09:09:40 +00:00
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2020-09-11 21:08:44 +00:00
|
|
|
include_examples 'search results filtered by state'
|
2020-09-25 09:09:40 +00:00
|
|
|
include_examples 'search results filtered by confidential'
|
2020-09-03 21:08:18 +00:00
|
|
|
end
|
2020-09-11 21:08:44 +00:00
|
|
|
end
|
2020-09-03 21:08:18 +00:00
|
|
|
|
2020-09-11 21:08:44 +00:00
|
|
|
describe 'merge requests search' do
|
|
|
|
let(:scope) { 'merge_requests' }
|
|
|
|
let(:project) { create(:project, :public) }
|
2020-09-03 21:08:18 +00:00
|
|
|
|
2020-09-11 21:08:44 +00:00
|
|
|
context 'filtering' do
|
|
|
|
let!(:project) { create(:project, :public) }
|
|
|
|
let!(:opened_result) { create(:merge_request, :opened, source_project: project, title: 'foo opened') }
|
|
|
|
let!(:closed_result) { create(:merge_request, :closed, source_project: project, title: 'foo closed') }
|
|
|
|
let(:query) { 'foo' }
|
2020-09-03 21:08:18 +00:00
|
|
|
|
2020-09-11 21:08:44 +00:00
|
|
|
include_examples 'search results filtered by state'
|
2020-09-03 21:08:18 +00:00
|
|
|
end
|
2016-03-17 20:48:19 +00:00
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
|
|
|
|
describe 'notes search' do
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:query) { note.note }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
subject(:notes) { results.objects('notes') }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'with a public project' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:note) { create(:note, project: project) }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it 'lists notes' do
|
|
|
|
expect(notes).to include note
|
|
|
|
end
|
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'with private issues' do
|
|
|
|
let(:project) { create(:project, :public, :issues_private) }
|
|
|
|
let(:note) { create(:note_on_issue, project: project) }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it "doesn't list issue notes when access is restricted" do
|
|
|
|
expect(notes).not_to include note
|
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'with private merge requests' do
|
|
|
|
let(:project) { create(:project, :public, :merge_requests_private) }
|
|
|
|
let(:note) { create(:note_on_merge_request, project: project) }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it "doesn't list merge_request notes when access is restricted" do
|
|
|
|
expect(notes).not_to include note
|
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2018-03-05 13:25:56 +00:00
|
|
|
describe '#limited_notes_count' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:note) { create(:note_on_issue, project: project) }
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:query) { note.note }
|
2018-03-05 13:25:56 +00:00
|
|
|
|
|
|
|
context 'when count_limit is lower than total amount' do
|
|
|
|
before do
|
|
|
|
allow(results).to receive(:count_limit).and_return(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls note finder once to get the limited amount of notes' do
|
|
|
|
expect(results).to receive(:notes_finder).once.and_call_original
|
|
|
|
expect(results.limited_notes_count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when count_limit is higher than total amount' do
|
|
|
|
it 'calls note finder multiple times to get the limited amount of notes' do
|
|
|
|
expect(results).to receive(:notes_finder).exactly(4).times.and_call_original
|
|
|
|
expect(results.limited_notes_count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-25 03:08:11 +00:00
|
|
|
describe '#commits_count' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
|
|
|
|
it 'limits the number of commits requested' do
|
|
|
|
expect(project.repository)
|
|
|
|
.to receive(:find_commits_by_message)
|
|
|
|
.with(anything, anything, anything, described_class::COUNT_LIMIT)
|
|
|
|
.and_call_original
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
results.commits_count
|
2020-05-25 03:08:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-08 14:54:45 +00:00
|
|
|
# Examples for commit access level test
|
|
|
|
#
|
|
|
|
# params:
|
|
|
|
# * search_phrase
|
|
|
|
# * commit
|
|
|
|
#
|
|
|
|
shared_examples 'access restricted commits' do
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:query) { search_phrase }
|
|
|
|
|
2016-12-08 14:54:45 +00:00
|
|
|
context 'when project is internal' do
|
2017-01-24 23:42:12 +00:00
|
|
|
let(:project) { create(:project, :internal, :repository) }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
subject(:commits) { results.objects('commits') }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it 'searches if user is authenticated' do
|
|
|
|
expect(commits).to contain_exactly commit
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'when the user is not authenticated' do
|
|
|
|
let(:user) { nil }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it 'does not search' do
|
|
|
|
expect(commits).to be_empty
|
|
|
|
end
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is private' do
|
|
|
|
let!(:creator) { create(:user, username: 'private-project-author') }
|
2017-01-24 23:42:12 +00:00
|
|
|
let!(:private_project) { create(:project, :private, :repository, creator: creator, namespace: creator.namespace) }
|
2016-12-08 14:54:45 +00:00
|
|
|
let(:team_master) do
|
|
|
|
user = create(:user, username: 'private-project-master')
|
2018-07-11 14:36:08 +00:00
|
|
|
private_project.add_maintainer(user)
|
2016-12-08 14:54:45 +00:00
|
|
|
user
|
|
|
|
end
|
2020-08-11 03:11:00 +00:00
|
|
|
|
2016-12-08 14:54:45 +00:00
|
|
|
let(:team_reporter) do
|
|
|
|
user = create(:user, username: 'private-project-reporter')
|
2017-12-22 08:18:28 +00:00
|
|
|
private_project.add_reporter(user)
|
2016-12-08 14:54:45 +00:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:project) { private_project }
|
|
|
|
|
|
|
|
subject(:commits) { results.objects('commits') }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'when the user is not authenticated' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
it 'does not show commit to stranger' do
|
|
|
|
expect(commits).to be_empty
|
|
|
|
end
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'team access' do
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'when the user is the creator' do
|
|
|
|
let(:user) { creator }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it { expect(commits).to contain_exactly commit }
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'when the user is a master' do
|
|
|
|
let(:user) { team_master }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it { expect(commits).to contain_exactly commit }
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'when the user is a reporter' do
|
|
|
|
let(:user) { team_reporter }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it { expect(commits).to contain_exactly commit }
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'commit search' do
|
2020-05-25 03:08:11 +00:00
|
|
|
context 'pagination' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
|
|
|
|
it 'returns the correct results for each page' do
|
|
|
|
expect(results_page(1)).to contain_exactly(commit('b83d6e391c22777fca1ed3012fce84f633d7fed0'))
|
|
|
|
expect(results_page(2)).to contain_exactly(commit('498214de67004b1da3d820901307bed2a68a8ef6'))
|
|
|
|
expect(results_page(3)).to contain_exactly(commit('1b12f15a11fc6e62177bef08f47bc7b5ce50b141'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct number of pages' do
|
|
|
|
expect(results_page(1).total_pages).to eq(project.repository.commit_count)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'limiting requested commits' do
|
|
|
|
context 'on page 1' do
|
|
|
|
it "limits to #{described_class::COUNT_LIMIT}" do
|
|
|
|
expect(project.repository)
|
|
|
|
.to receive(:find_commits_by_message)
|
|
|
|
.with(anything, anything, anything, described_class::COUNT_LIMIT)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
results_page(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on subsequent pages' do
|
|
|
|
it "limits to #{described_class::COUNT_LIMIT} plus page offset" do
|
|
|
|
expect(project.repository)
|
|
|
|
.to receive(:find_commits_by_message)
|
|
|
|
.with(anything, anything, anything, described_class::COUNT_LIMIT + 1)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
results_page(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def results_page(page)
|
2020-08-21 03:10:16 +00:00
|
|
|
described_class.new(user, '.', project: project).objects('commits', per_page: 1, page: page)
|
2020-05-25 03:08:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def commit(hash)
|
|
|
|
project.repository.commit(hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-08 14:54:45 +00:00
|
|
|
context 'by commit message' do
|
2017-01-24 23:42:12 +00:00
|
|
|
let(:project) { create(:project, :public, :repository) }
|
2016-12-08 14:54:45 +00:00
|
|
|
let(:commit) { project.repository.commit('59e29889be61e6e0e5e223bfa9ac2721d31605b8') }
|
|
|
|
let(:message) { 'Sorry, I did a mistake' }
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:query) { message }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
subject(:commits) { results.objects('commits') }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it 'finds commit by message' do
|
2016-12-08 14:54:45 +00:00
|
|
|
expect(commits).to contain_exactly commit
|
|
|
|
end
|
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
context 'when there are not hits' do
|
|
|
|
let(:query) { 'not really an existing description' }
|
2016-12-08 14:54:45 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it 'handles when no commit match' do
|
|
|
|
expect(commits).to be_empty
|
|
|
|
end
|
2016-12-08 14:54:45 +00:00
|
|
|
end
|
|
|
|
|
2020-06-03 00:08:38 +00:00
|
|
|
context 'when repository_ref is provided' do
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:query) { 'Feature added' }
|
2020-06-11 15:08:36 +00:00
|
|
|
let(:repository_ref) { 'feature' }
|
2020-06-03 00:08:38 +00:00
|
|
|
|
|
|
|
it 'searches in the specified ref' do
|
|
|
|
# This commit is unique to the feature branch
|
|
|
|
expect(commits).to contain_exactly(project.repository.commit('0b4bc9a49b562e85de7cc9e834518ea6828729b9'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-08 14:54:45 +00:00
|
|
|
it_behaves_like 'access restricted commits' do
|
|
|
|
let(:search_phrase) { message }
|
|
|
|
let(:commit) { project.repository.commit('59e29889be61e6e0e5e223bfa9ac2721d31605b8') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'by commit hash' do
|
2017-01-24 23:42:12 +00:00
|
|
|
let(:project) { create(:project, :public, :repository) }
|
2016-12-08 14:54:45 +00:00
|
|
|
let(:commit) { project.repository.commit('0b4bc9a') }
|
2017-04-24 15:09:29 +00:00
|
|
|
|
2016-12-08 14:54:45 +00:00
|
|
|
commit_hashes = { short: '0b4bc9a', full: '0b4bc9a49b562e85de7cc9e834518ea6828729b9' }
|
|
|
|
|
|
|
|
commit_hashes.each do |type, commit_hash|
|
|
|
|
it "shows commit by #{type} hash id" do
|
2020-08-21 03:10:16 +00:00
|
|
|
commits = described_class.new(user, commit_hash, project: project).objects('commits')
|
2016-12-08 14:54:45 +00:00
|
|
|
|
|
|
|
expect(commits).to contain_exactly commit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles not existing commit hash correctly' do
|
2020-08-21 03:10:16 +00:00
|
|
|
commits = described_class.new(user, 'deadbeef', project: project).objects('commits')
|
2016-12-08 14:54:45 +00:00
|
|
|
|
|
|
|
expect(commits).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'access restricted commits' do
|
|
|
|
let(:search_phrase) { '0b4bc9a49' }
|
|
|
|
let(:commit) { project.repository.commit('0b4bc9a') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-15 11:53:23 +00:00
|
|
|
|
|
|
|
describe 'user search' do
|
2020-08-21 03:10:16 +00:00
|
|
|
let(:query) { 'gob' }
|
2021-06-15 12:10:11 +00:00
|
|
|
|
|
|
|
let_it_be(:user_1) { create(:user, username: 'gob_bluth') }
|
|
|
|
let_it_be(:user_2) { create(:user, username: 'michael_bluth') }
|
|
|
|
let_it_be(:user_3) { create(:user, username: 'gob_2018') }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, namespace: group) }
|
2020-08-21 03:10:16 +00:00
|
|
|
|
|
|
|
subject(:objects) { results.objects('users') }
|
2018-08-15 11:53:23 +00:00
|
|
|
|
2020-08-21 03:10:16 +00:00
|
|
|
it 'returns the user belonging to the project matching the search query' do
|
2021-06-15 12:10:11 +00:00
|
|
|
create(:project_member, :developer, user: user_1, project: project)
|
|
|
|
create(:project_member, :developer, user: user_2, project: project)
|
2018-08-15 11:53:23 +00:00
|
|
|
|
2021-06-15 12:10:11 +00:00
|
|
|
expect(objects).to contain_exactly(user_1)
|
|
|
|
end
|
2018-08-15 11:53:23 +00:00
|
|
|
|
2021-06-15 12:10:11 +00:00
|
|
|
it 'returns the user belonging to the group matching the search query' do
|
|
|
|
create(:group_member, :developer, user: user_1, group: group)
|
2018-08-15 11:53:23 +00:00
|
|
|
|
2021-06-15 12:10:11 +00:00
|
|
|
expect(objects).to contain_exactly(user_1)
|
2018-12-11 12:25:52 +00:00
|
|
|
end
|
|
|
|
|
2021-06-15 12:10:11 +00:00
|
|
|
context 'when multiple projects provided' do
|
|
|
|
let_it_be(:project_2) { create(:project, namespace: group) }
|
|
|
|
|
|
|
|
subject(:results) { described_class.new(user, query, project: [project, project_2], repository_ref: repository_ref, filters: filters) }
|
2018-12-11 12:25:52 +00:00
|
|
|
|
2021-06-15 12:10:11 +00:00
|
|
|
it 'returns users belonging to projects matching the search query' do
|
|
|
|
create(:project_member, :developer, user: user_1, project: project)
|
|
|
|
create(:project_member, :developer, user: user_3, project: project_2)
|
2018-12-11 12:25:52 +00:00
|
|
|
|
2021-06-15 12:10:11 +00:00
|
|
|
expect(objects).to contain_exactly(user_1, user_3)
|
|
|
|
end
|
2018-08-15 11:53:23 +00:00
|
|
|
end
|
|
|
|
end
|
2015-06-09 14:43:08 +00:00
|
|
|
end
|