2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-31 15:47:49 +02:00
|
|
|
describe DossierSearchService do
|
2021-05-06 01:14:57 +02:00
|
|
|
describe '#matching_dossiers' do
|
2024-12-03 11:55:13 +01:00
|
|
|
let!(:dossiers) { Dossier.where(id: dossier.id) }
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
before { perform_enqueued_jobs(only: DossierIndexSearchTermsJob) }
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-04 19:02:53 +01:00
|
|
|
def searching(terms, with_annotations: false)
|
|
|
|
described_class.matching_dossiers(dossiers, terms, with_annotations)
|
|
|
|
end
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
describe 'ignores brouillon' do
|
|
|
|
let(:dossier) { create(:dossier, state: :brouillon) }
|
2024-04-03 10:21:50 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
it { expect(searching(dossier.id.to_s)).to eq([]) }
|
2017-04-12 17:13:31 +02:00
|
|
|
end
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
context 'with a dossier not in brouillon' do
|
|
|
|
let(:user) { create(:user, email: 'nicolas@email.com') }
|
|
|
|
let(:etablissement) { create(:etablissement, entreprise_raison_sociale: 'Direction Interministerielle Du Numérique', siret: '13002526500013') }
|
2024-12-04 19:02:53 +01:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :text }], types_de_champ_private: [{ type: :text }]) }
|
2024-12-03 11:55:13 +01:00
|
|
|
let(:dossier) do
|
2024-12-04 19:02:53 +01:00
|
|
|
create(:dossier, procedure:, state: :en_construction, user:, etablissement:).tap do |dossier|
|
|
|
|
dossier.project_champs_public.first.update!(value: 'Hélène mange des pommes')
|
|
|
|
dossier.project_champs_private.first.update!(value: 'annotations')
|
2024-12-03 11:55:13 +01:00
|
|
|
end
|
|
|
|
end
|
2018-07-31 15:47:49 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
it do
|
|
|
|
expect(searching('')).to eq([])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# by dossier id
|
|
|
|
expect(searching(dossier.id.to_s)).to eq([dossier.id])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-04 19:02:53 +01:00
|
|
|
# annotations is unsearchable by default
|
|
|
|
expect(searching('annotations')).to eq([])
|
|
|
|
# but can be searched with the with_annotations option
|
|
|
|
expect(searching('annotations', with_annotations: true)).to eq([dossier.id])
|
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# by email
|
|
|
|
expect(searching('nicolas@email.com')).to eq([dossier.id])
|
|
|
|
expect(searching('nicolas')).to eq([dossier.id])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# by SIRET
|
|
|
|
expect(searching('13002526500013')).to eq([dossier.id])
|
|
|
|
expect(searching('1300')).to eq([dossier.id])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# by raison sociale
|
|
|
|
expect(searching('Direction Interministerielle Du Numérique')).to eq([dossier.id])
|
|
|
|
expect(searching('Direction')).to eq([dossier.id])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# with multiple terms
|
|
|
|
expect(searching('Direction nicolas')).to eq([dossier.id])
|
2018-10-18 15:21:13 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# with forbidden characters
|
|
|
|
expect(searching("'?\\:&!(Direction) <Interministerielle>")).to eq([dossier.id])
|
2018-10-18 15:21:13 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
# with a single forbidden character should not crash postgres
|
|
|
|
expect(searching('? Direction')).to eq([dossier.id])
|
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# with supirious spaces
|
|
|
|
expect(searching(" nicolas ")).to eq([dossier.id])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# with wrong case
|
|
|
|
expect(searching('direction')).to eq([dossier.id])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# by champ text
|
|
|
|
expect(searching('Hélène')).to eq([dossier.id])
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
# by singular
|
|
|
|
expect(searching('la pomme')).to eq([dossier.id])
|
2024-12-03 12:49:25 +01:00
|
|
|
|
|
|
|
# without accent
|
|
|
|
expect(searching('helene')).to eq([dossier.id])
|
2024-12-03 17:18:34 +01:00
|
|
|
|
|
|
|
# NOT WORKING YET
|
|
|
|
# with a single faulty character
|
|
|
|
expect(searching('des pammes')).to eq([])
|
2017-04-12 17:13:31 +02:00
|
|
|
end
|
2016-10-29 00:53:04 +02:00
|
|
|
end
|
2019-01-22 17:16:49 +01:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
describe 'does not ignore archived dossiers' do
|
|
|
|
let(:dossier) { create(:dossier, state: :en_construction, archived: true) }
|
2019-01-22 17:16:49 +01:00
|
|
|
|
2024-12-03 11:55:13 +01:00
|
|
|
it { expect(searching(dossier.id.to_s)).to eq([dossier.id]) }
|
2019-01-22 17:16:49 +01:00
|
|
|
end
|
2016-10-29 00:53:04 +02:00
|
|
|
end
|
2020-04-08 17:10:29 +02:00
|
|
|
|
|
|
|
describe '#matching_dossiers_for_user' do
|
2024-12-03 16:24:41 +01:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:another_user) { create(:user) }
|
2020-04-08 17:10:29 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
before { perform_enqueued_jobs(only: DossierIndexSearchTermsJob) }
|
2020-04-08 17:10:29 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
def searching(terms, user) = described_class.matching_dossiers_for_user(terms, user)
|
2020-04-08 17:10:29 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
context 'when the dossier is brouillon' do
|
2024-12-04 19:02:53 +01:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_private: [{ type: :text }]) }
|
|
|
|
let(:dossier) do
|
|
|
|
create(:dossier, procedure:, state: :brouillon, user:).tap do |dossier|
|
|
|
|
dossier.project_champs_private.first.update!(value: 'annotations')
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 17:10:29 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
it do
|
|
|
|
# searching its own dossier by id
|
|
|
|
expect(searching(dossier.id.to_s, user)).to eq([dossier])
|
2020-04-08 17:10:29 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
# searching another dossier by id
|
|
|
|
expect(searching(dossier.id.to_s, another_user)).to eq([])
|
2024-12-04 19:02:53 +01:00
|
|
|
|
|
|
|
# annotations is unsearchable
|
|
|
|
expect(searching('annotations', user)).to eq([])
|
2020-04-08 17:10:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
context 'when the user is invited on the dossier' do
|
|
|
|
let(:dossier) { create(:dossier) }
|
2020-04-20 14:27:43 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
before { create(:invite, dossier:, user:) }
|
2020-04-20 14:27:43 +02:00
|
|
|
|
2024-12-03 16:24:41 +01:00
|
|
|
it { expect(searching(dossier.id.to_s, user)).to eq([dossier]) }
|
2020-04-20 14:27:43 +02:00
|
|
|
end
|
2020-04-08 17:10:29 +02:00
|
|
|
end
|
2016-10-25 18:45:05 +02:00
|
|
|
end
|