2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe SearchHelper do
|
|
|
|
# Override simple_sanitize for our testing purposes
|
|
|
|
def simple_sanitize(str)
|
|
|
|
str
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
describe 'parsing result' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:repository) { project.repository }
|
|
|
|
let(:results) { repository.search_files('feature', 'master') }
|
|
|
|
let(:search_result) { results.first }
|
|
|
|
|
|
|
|
subject { helper.parse_search_result(search_result) }
|
|
|
|
|
|
|
|
it "returns a valid OpenStruct object" do
|
|
|
|
is_expected.to be_an OpenStruct
|
|
|
|
expect(subject.filename).to eq('CHANGELOG')
|
|
|
|
expect(subject.basename).to eq('CHANGELOG')
|
|
|
|
expect(subject.ref).to eq('master')
|
2016-11-03 12:29:30 +05:30
|
|
|
expect(subject.startline).to eq(188)
|
2016-09-29 09:46:39 +05:30
|
|
|
expect(subject.data.lines[2]).to eq(" - Feature: Replace teams with group membership\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when filename has extension" do
|
|
|
|
let(:search_result) { "master:CONTRIBUTE.md:5:- [Contribute to GitLab](#contribute-to-gitlab)\n" }
|
|
|
|
|
|
|
|
it { expect(subject.filename).to eq('CONTRIBUTE.md') }
|
|
|
|
it { expect(subject.basename).to eq('CONTRIBUTE') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when file under directory" do
|
|
|
|
let(:search_result) { "master:a/b/c.md:5:a b c\n" }
|
|
|
|
|
|
|
|
it { expect(subject.filename).to eq('a/b/c.md') }
|
|
|
|
it { expect(subject.basename).to eq('a/b/c') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
describe 'search_autocomplete_source' do
|
|
|
|
context "with no current user" do
|
|
|
|
before do
|
|
|
|
allow(self).to receive(:current_user).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "it returns nil" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(search_autocomplete_opts("q")).to be_nil
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a user" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(self).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes Help sections" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(search_autocomplete_opts("hel").size).to eq(9)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "includes default sections" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(search_autocomplete_opts("adm").size).to eq(1)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
it "does not allow regular expression in search term" do
|
|
|
|
expect(search_autocomplete_opts("(webhooks|api)").size).to eq(0)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
it "includes the user's groups" do
|
|
|
|
create(:group).add_owner(user)
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(search_autocomplete_opts("gro").size).to eq(1)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the user's projects" do
|
|
|
|
project = create(:project, namespace: create(:namespace, owner: user))
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(search_autocomplete_opts(project.name).size).to eq(1)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it "does not include the public group" do
|
2016-01-14 18:37:52 +05:30
|
|
|
group = create(:group)
|
2016-04-02 18:10:28 +05:30
|
|
|
expect(search_autocomplete_opts(group.name).size).to eq(0)
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
context "with a current project" do
|
|
|
|
before { @project = create(:project) }
|
|
|
|
|
|
|
|
it "includes project-specific sections" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(search_autocomplete_opts("Files").size).to eq(1)
|
|
|
|
expect(search_autocomplete_opts("Commits").size).to eq(1)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|