summaryrefslogtreecommitdiff
path: root/spec/helpers/search_helper_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-10-21 07:08:36 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-10-21 07:08:36 +0000
commit48aff82709769b098321c738f3444b9bdaa694c6 (patch)
treee00c7c43e2d9b603a5a6af576b1685e400410dee /spec/helpers/search_helper_spec.rb
parent879f5329ee916a948223f8f43d77fba4da6cd028 (diff)
downloadgitlab-ce-48aff82709769b098321c738f3444b9bdaa694c6.tar.gz
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'spec/helpers/search_helper_spec.rb')
-rw-r--r--spec/helpers/search_helper_spec.rb116
1 files changed, 97 insertions, 19 deletions
diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb
index 594c5c11994..6fe071521cd 100644
--- a/spec/helpers/search_helper_spec.rb
+++ b/spec/helpers/search_helper_spec.rb
@@ -29,7 +29,7 @@ RSpec.describe SearchHelper do
end
it "includes Help sections" do
- expect(search_autocomplete_opts("hel").size).to eq(9)
+ expect(search_autocomplete_opts("hel").size).to eq(8)
end
it "includes default sections" do
@@ -73,7 +73,7 @@ RSpec.describe SearchHelper do
expect(result.keys).to match_array(%i[category id label url avatar_url])
end
- it 'includes the first 5 of the users recent issues' do
+ it 'includes the users recently viewed issues' do
recent_issues = instance_double(::Gitlab::Search::RecentIssues)
expect(::Gitlab::Search::RecentIssues).to receive(:new).with(user: user).and_return(recent_issues)
project1 = create(:project, :with_avatar, namespace: user.namespace)
@@ -81,13 +81,11 @@ RSpec.describe SearchHelper do
issue1 = create(:issue, title: 'issue 1', project: project1)
issue2 = create(:issue, title: 'issue 2', project: project2)
- other_issues = create_list(:issue, 5)
-
- expect(recent_issues).to receive(:search).with('the search term').and_return(Issue.id_in_ordered([issue1.id, issue2.id, *other_issues.map(&:id)]))
+ expect(recent_issues).to receive(:search).with('the search term').and_return(Issue.id_in_ordered([issue1.id, issue2.id]))
results = search_autocomplete_opts("the search term")
- expect(results.count).to eq(5)
+ expect(results.count).to eq(2)
expect(results[0]).to include({
category: 'Recent issues',
@@ -106,7 +104,7 @@ RSpec.describe SearchHelper do
})
end
- it 'includes the first 5 of the users recent merge requests' do
+ it 'includes the users recently viewed merge requests' do
recent_merge_requests = instance_double(::Gitlab::Search::RecentMergeRequests)
expect(::Gitlab::Search::RecentMergeRequests).to receive(:new).with(user: user).and_return(recent_merge_requests)
project1 = create(:project, :with_avatar, namespace: user.namespace)
@@ -114,13 +112,11 @@ RSpec.describe SearchHelper do
merge_request1 = create(:merge_request, :unique_branches, title: 'Merge request 1', target_project: project1, source_project: project1)
merge_request2 = create(:merge_request, :unique_branches, title: 'Merge request 2', target_project: project2, source_project: project2)
- other_merge_requests = create_list(:merge_request, 5)
-
- expect(recent_merge_requests).to receive(:search).with('the search term').and_return(MergeRequest.id_in_ordered([merge_request1.id, merge_request2.id, *other_merge_requests.map(&:id)]))
+ expect(recent_merge_requests).to receive(:search).with('the search term').and_return(MergeRequest.id_in_ordered([merge_request1.id, merge_request2.id]))
results = search_autocomplete_opts("the search term")
- expect(results.count).to eq(5)
+ expect(results.count).to eq(2)
expect(results[0]).to include({
category: 'Recent merge requests',
@@ -357,14 +353,6 @@ RSpec.describe SearchHelper do
describe '#show_user_search_tab?' do
subject { show_user_search_tab? }
- context 'when users_search feature is disabled' do
- before do
- stub_feature_flags(users_search: false)
- end
-
- it { is_expected.to eq(false) }
- end
-
context 'when project search' do
before do
@project = :some_project
@@ -399,4 +387,94 @@ RSpec.describe SearchHelper do
end
end
end
+
+ describe '#repository_ref' do
+ let_it_be(:project) { create(:project, :repository) }
+ let(:params) { { repository_ref: 'the-repository-ref-param' } }
+
+ subject { repository_ref(project) }
+
+ it { is_expected.to eq('the-repository-ref-param') }
+
+ context 'when the param :repository_ref is not set' do
+ let(:params) { { repository_ref: nil } }
+
+ it { is_expected.to eq(project.default_branch) }
+ end
+
+ context 'when the repository_ref param is a number' do
+ let(:params) { { repository_ref: 111111 } }
+
+ it { is_expected.to eq('111111') }
+ end
+ end
+
+ describe '#highlight_and_truncate_issue' do
+ let(:description) { 'hello world' }
+ let(:issue) { create(:issue, description: description) }
+ let(:user) { create(:user) }
+
+ before do
+ allow(self).to receive(:current_user).and_return(user)
+ end
+
+ subject { highlight_and_truncate_issue(issue, 'test', {}) }
+
+ context 'when description is not present' do
+ let(:description) { nil }
+
+ it 'does nothing' do
+ expect(self).not_to receive(:simple_search_highlight_and_truncate)
+
+ subject
+ end
+ end
+
+ context 'when description present' do
+ using RSpec::Parameterized::TableSyntax
+
+ where(:description, :expected) do
+ 'test' | '<span class="gl-text-black-normal gl-font-weight-bold">test</span>'
+ '<span style="color: blue;">this test should not be blue</span>' | '<span>this <span class="gl-text-black-normal gl-font-weight-bold">test</span> should not be blue</span>'
+ '<a href="#" onclick="alert(\'XSS\')">Click Me test</a>' | '<a href="#">Click Me <span class="gl-text-black-normal gl-font-weight-bold">test</span></a>'
+ '<script type="text/javascript">alert(\'Another XSS\');</script> test' | ' <span class="gl-text-black-normal gl-font-weight-bold">test</span>'
+ 'Lorem test ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec.' | 'Lorem <span class="gl-text-black-normal gl-font-weight-bold">test</span> ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Don...'
+ end
+
+ with_them do
+ it 'sanitizes, truncates, and highlights the search term' do
+ expect(subject).to eq(expected)
+ end
+ end
+ end
+ end
+
+ describe '#search_service' do
+ using RSpec::Parameterized::TableSyntax
+
+ subject { search_service }
+
+ before do
+ allow(self).to receive(:current_user).and_return(:the_current_user)
+ end
+
+ where(:confidential, :expected) do
+ '0' | false
+ '1' | true
+ 'yes' | true
+ 'no' | false
+ true | true
+ false | false
+ end
+
+ let(:params) {{ confidential: confidential }}
+
+ with_them do
+ it 'transforms confidentiality param' do
+ expect(::SearchService).to receive(:new).with(:the_current_user, { confidential: expected })
+
+ subject
+ end
+ end
+ end
end