2019-04-11 08:17:24 -04:00
# frozen_string_literal: true
2017-03-31 09:03:55 -04:00
require 'spec_helper'
2020-06-24 11:08:50 -04:00
RSpec . describe Search :: GlobalService do
2017-03-31 09:03:55 -04:00
let ( :user ) { create ( :user ) }
let ( :internal_user ) { create ( :user ) }
2017-08-02 15:55:11 -04:00
let! ( :found_project ) { create ( :project , :private , name : 'searchable_project' ) }
let! ( :unfound_project ) { create ( :project , :private , name : 'unfound_project' ) }
let! ( :internal_project ) { create ( :project , :internal , name : 'searchable_internal_project' ) }
let! ( :public_project ) { create ( :project , :public , name : 'searchable_public_project' ) }
2017-03-31 09:03:55 -04:00
before do
2018-07-11 10:36:08 -04:00
found_project . add_maintainer ( user )
2017-03-31 09:03:55 -04:00
end
describe '#execute' do
context 'unauthenticated' do
it 'returns public projects only' do
2017-07-25 13:09:00 -04:00
results = described_class . new ( nil , search : " searchable " ) . execute
2017-03-31 09:03:55 -04:00
expect ( results . objects ( 'projects' ) ) . to match_array [ public_project ]
end
end
context 'authenticated' do
it 'returns public, internal and private projects' do
2017-07-25 13:09:00 -04:00
results = described_class . new ( user , search : " searchable " ) . execute
2017-03-31 09:03:55 -04:00
expect ( results . objects ( 'projects' ) ) . to match_array [ public_project , found_project , internal_project ]
end
it 'returns only public & internal projects' do
2017-07-25 13:09:00 -04:00
results = described_class . new ( internal_user , search : " searchable " ) . execute
2017-03-31 09:03:55 -04:00
expect ( results . objects ( 'projects' ) ) . to match_array [ internal_project , public_project ]
end
2017-11-24 05:23:14 -05:00
it 'project name is searchable' do
results = described_class . new ( user , search : found_project . name ) . execute
2017-03-31 09:03:55 -04:00
expect ( results . objects ( 'projects' ) ) . to match_array [ found_project ]
end
2019-09-10 18:41:21 -04:00
it 'does not return archived projects' do
archived_project = create ( :project , :public , archived : true , name : 'archived_project' )
results = described_class . new ( user , search : " archived " ) . execute
expect ( results . objects ( 'projects' ) ) . not_to include ( archived_project )
end
2017-03-31 09:03:55 -04:00
end
end
2020-10-07 02:09:03 -04:00
context 'issues' do
let ( :scope ) { 'issues' }
2021-02-10 07:09:45 -05:00
context 'sorting' do
2021-02-10 13:09:02 -05:00
let_it_be ( :project ) { create ( :project , :public ) }
2021-02-10 07:09:45 -05:00
2020-10-07 02:09:03 -04:00
let! ( :old_result ) { create ( :issue , project : project , title : 'sorted old' , created_at : 1 . month . ago ) }
let! ( :new_result ) { create ( :issue , project : project , title : 'sorted recent' , created_at : 1 . day . ago ) }
let! ( :very_old_result ) { create ( :issue , project : project , title : 'sorted very old' , created_at : 1 . year . ago ) }
2021-02-10 07:09:45 -05:00
let! ( :old_updated ) { create ( :issue , project : project , title : 'updated old' , updated_at : 1 . month . ago ) }
let! ( :new_updated ) { create ( :issue , project : project , title : 'updated recent' , updated_at : 1 . day . ago ) }
let! ( :very_old_updated ) { create ( :issue , project : project , title : 'updated very old' , updated_at : 1 . year . ago ) }
2020-10-07 02:09:03 -04:00
include_examples 'search results sorted' do
2021-02-10 07:09:45 -05:00
let ( :results_created ) { described_class . new ( nil , search : 'sorted' , sort : sort ) . execute }
let ( :results_updated ) { described_class . new ( nil , search : 'updated' , sort : sort ) . execute }
2020-10-07 02:09:03 -04:00
end
end
end
context 'merge_request' do
let ( :scope ) { 'merge_requests' }
2021-02-10 07:09:45 -05:00
context 'sorting' do
2020-10-07 02:09:03 -04:00
let! ( :project ) { create ( :project , :public ) }
2021-02-10 07:09:45 -05:00
2020-10-07 02:09:03 -04:00
let! ( :old_result ) { create ( :merge_request , :opened , source_project : project , source_branch : 'old-1' , title : 'sorted old' , created_at : 1 . month . ago ) }
let! ( :new_result ) { create ( :merge_request , :opened , source_project : project , source_branch : 'new-1' , title : 'sorted recent' , created_at : 1 . day . ago ) }
let! ( :very_old_result ) { create ( :merge_request , :opened , source_project : project , source_branch : 'very-old-1' , title : 'sorted very old' , created_at : 1 . year . ago ) }
2021-02-10 07:09:45 -05:00
let! ( :old_updated ) { create ( :merge_request , :opened , source_project : project , source_branch : 'updated-old-1' , title : 'updated old' , updated_at : 1 . month . ago ) }
let! ( :new_updated ) { create ( :merge_request , :opened , source_project : project , source_branch : 'updated-new-1' , title : 'updated recent' , updated_at : 1 . day . ago ) }
let! ( :very_old_updated ) { create ( :merge_request , :opened , source_project : project , source_branch : 'updated-very-old-1' , title : 'updated very old' , updated_at : 1 . year . ago ) }
2020-10-07 02:09:03 -04:00
include_examples 'search results sorted' do
2021-02-10 07:09:45 -05:00
let ( :results_created ) { described_class . new ( nil , search : 'sorted' , sort : sort ) . execute }
let ( :results_updated ) { described_class . new ( nil , search : 'updated' , sort : sort ) . execute }
2020-10-07 02:09:03 -04:00
end
end
end
2017-03-31 09:03:55 -04:00
end