2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2018-09-06 19:23:27 +02:00
describe Champs :: SiretController , type : :controller do
let ( :user ) { create ( :user ) }
2024-01-23 09:52:20 +01:00
let ( :procedure ) { create ( :procedure , :published , types_de_champ_public : [ { type : :siret } ] ) }
2018-09-06 19:23:27 +02:00
describe '#show' do
let ( :dossier ) { create ( :dossier , user : user , procedure : procedure ) }
2024-09-27 15:37:11 +02:00
let ( :champ ) { dossier . project_champs_public . first }
2020-07-20 17:18:44 +02:00
2022-11-10 22:21:14 +01:00
let ( :champs_public_attributes ) do
2024-04-15 15:06:05 +02:00
champ_attributes = { }
champ_attributes [ champ . public_id ] = { value : siret }
2022-04-28 15:04:21 +02:00
champ_attributes
end
2018-09-06 19:23:27 +02:00
let ( :params ) do
{
2024-04-15 15:06:05 +02:00
dossier_id : champ . dossier_id ,
stable_id : champ . stable_id ,
2018-09-06 19:23:27 +02:00
dossier : {
2022-11-10 22:21:14 +01:00
champs_public_attributes : champs_public_attributes
2022-04-28 15:04:21 +02:00
}
2018-09-06 19:23:27 +02:00
}
end
let ( :siret ) { '' }
2019-04-30 16:54:11 +02:00
context 'when the user is signed in' do
2018-09-06 19:23:27 +02:00
render_views
2020-05-18 14:26:11 +02:00
let ( :api_etablissement_status ) { 200 }
let ( :api_etablissement_body ) { File . read ( 'spec/fixtures/files/api_entreprise/etablissements.json' ) }
let ( :token_expired ) { false }
2020-07-20 17:18:44 +02:00
2020-05-18 14:26:11 +02:00
before do
sign_in user
2023-05-30 16:27:20 +02:00
stub_request ( :get , / https: \/ \/ entreprise.api.gouv.fr \/ v3 \/ insee \/ sirene \/ etablissements \/ #{ siret } / )
2020-05-18 14:26:11 +02:00
. to_return ( status : api_etablissement_status , body : api_etablissement_body )
2023-05-25 11:15:26 +02:00
stub_request ( :get , / https: \/ \/ entreprise.api.gouv.fr \/ v3 \/ insee \/ sirene \/ unites_legales \/ #{ siret [ 0 .. 8 ] } / )
2023-02-02 09:59:59 +01:00
. to_return ( status : 200 , body : File . read ( 'spec/fixtures/files/api_entreprise/entreprises.json' ) )
2020-08-05 18:40:47 +02:00
allow_any_instance_of ( APIEntrepriseToken ) . to receive ( :roles )
2020-05-18 14:26:11 +02:00
. and_return ( [ " attestations_fiscales " , " attestations_sociales " , " bilans_entreprise_bdf " ] )
2020-08-05 18:40:47 +02:00
allow_any_instance_of ( APIEntrepriseToken ) . to receive ( :expired? ) . and_return ( token_expired )
2020-05-18 14:26:11 +02:00
end
2018-09-06 19:23:27 +02:00
2019-04-30 16:54:11 +02:00
context 'when the SIRET is empty' do
2022-05-03 19:43:49 +02:00
subject! { get :show , params : params , format : :turbo_stream }
2018-09-06 19:23:27 +02:00
2023-01-11 15:04:45 +01:00
it 'clears the etablissement on the model' do
expect ( champ . reload . etablissement ) . to be_nil
2018-09-06 19:23:27 +02:00
end
2019-04-30 16:54:11 +02:00
it 'clears any information or error message' do
2022-05-03 19:43:49 +02:00
expect ( response . body ) . to include ( ActionView :: RecordIdentifier . dom_id ( champ , :siret_info ) )
2019-04-30 16:54:11 +02:00
end
2018-09-06 19:23:27 +02:00
end
2023-01-16 15:14:53 +01:00
context " when the SIRET is invalid because of it's length " do
2018-09-06 19:23:27 +02:00
let ( :siret ) { '1234' }
2022-05-03 19:43:49 +02:00
subject! { get :show , params : params , format : :turbo_stream }
2019-04-30 16:54:11 +02:00
2023-01-11 15:04:45 +01:00
it 'clears the etablissement on the model' do
expect ( champ . reload . etablissement ) . to be_nil
2018-09-06 19:23:27 +02:00
end
2019-04-30 16:54:11 +02:00
it 'displays a “SIRET is invalid” error message' do
expect ( response . body ) . to include ( 'Le numéro de SIRET doit comporter exactement 14 chiffres.' )
end
2018-09-06 19:23:27 +02:00
end
2023-01-16 15:14:53 +01:00
context " when the SIRET is invalid because of it's checksum " do
let ( :siret ) { '82812345600023' }
subject! { get :show , params : params , format : :turbo_stream }
it 'clears the etablissement on the model' do
expect ( champ . reload . etablissement ) . to be_nil
end
it 'displays a “SIRET is invalid” error message' do
expect ( response . body ) . to include ( 'Le format du numéro de SIRET est invalide.' )
end
end
2022-09-19 10:27:20 +02:00
context 'when the API is unavailable due to network error' do
2019-05-02 11:24:22 +02:00
let ( :siret ) { '82161143100015' }
2020-05-18 14:26:11 +02:00
let ( :api_etablissement_status ) { 503 }
2019-05-02 11:24:22 +02:00
2022-09-19 10:27:20 +02:00
before do
2024-04-05 12:07:40 +02:00
expect ( APIEntrepriseService ) . to receive ( :api_insee_up? ) . and_return ( true )
2022-09-19 10:27:20 +02:00
end
2022-05-03 19:43:49 +02:00
subject! { get :show , params : params , format : :turbo_stream }
2019-05-02 11:24:22 +02:00
2023-01-11 15:04:45 +01:00
it 'clears the etablissement on the model' do
expect ( champ . reload . etablissement ) . to be_nil
2019-05-02 11:24:22 +02:00
end
it 'displays a “API is unavailable” error message' do
2021-02-02 15:48:53 +01:00
expect ( response . body ) . to include ( 'Désolé, la récupération des informations SIRET est temporairement indisponible. Veuillez réessayer dans quelques instants.' )
2019-05-02 11:24:22 +02:00
end
end
2022-09-19 10:27:20 +02:00
context 'when the API is unavailable due to an api maintenance or pb' do
let ( :siret ) { '82161143100015' }
let ( :api_etablissement_status ) { 502 }
before do
2024-04-05 12:07:40 +02:00
expect ( APIEntrepriseService ) . to receive ( :api_insee_up? ) . and_return ( false )
2022-09-19 10:27:20 +02:00
end
subject! { get :show , params : params , format : :turbo_stream }
it 'saves the etablissement in degraded mode and SIRET on the model' do
champ . reload
expect ( champ . etablissement . siret ) . to eq ( siret )
expect ( champ . etablissement . as_degraded_mode? ) . to be true
end
it 'displays a “API entreprise down” error message' do
expect ( response . body ) . to include ( 'Notre fournisseur de données semble en panne, nous récupérerons les données plus tard.' )
end
end
2019-04-30 16:54:11 +02:00
context 'when the SIRET is valid but unknown' do
let ( :siret ) { '00000000000000' }
2020-05-18 14:26:11 +02:00
let ( :api_etablissement_status ) { 404 }
2019-04-30 16:54:11 +02:00
2022-05-03 19:43:49 +02:00
subject! { get :show , params : params , format : :turbo_stream }
2019-04-30 16:54:11 +02:00
2023-01-11 15:04:45 +01:00
it 'clears the etablissement on the model' do
expect ( champ . reload . etablissement ) . to be_nil
2018-09-06 19:23:27 +02:00
end
2019-04-30 16:54:11 +02:00
it 'displays a “SIRET not found” error message' do
expect ( response . body ) . to include ( 'Nous n’ avons pas trouvé d’ établissement correspondant à ce numéro de SIRET.' )
end
2018-09-06 19:23:27 +02:00
end
2019-04-30 16:54:11 +02:00
context 'when the SIRET informations are retrieved successfully' do
2023-05-30 16:27:20 +02:00
let ( :siret ) { '30613890001294' }
2020-05-18 14:26:11 +02:00
let ( :api_etablissement_status ) { 200 }
let ( :api_etablissement_body ) { File . read ( 'spec/fixtures/files/api_entreprise/etablissements.json' ) }
2019-04-30 16:54:11 +02:00
2022-05-03 19:43:49 +02:00
subject! { get :show , params : params , format : :turbo_stream }
2019-04-30 16:54:11 +02:00
it 'populates the etablissement and SIRET on the model' do
2019-03-12 14:57:03 +01:00
champ . reload
2020-05-18 14:26:11 +02:00
expect ( champ . etablissement . siret ) . to eq ( siret )
2023-05-30 16:27:20 +02:00
expect ( champ . etablissement . naf ) . to eq ( " 8411Z " )
2020-05-26 17:55:45 +02:00
expect ( dossier . reload . etablissement ) . to eq ( nil )
2019-04-30 16:54:11 +02:00
end
2018-09-06 19:23:27 +02:00
end
end
2019-04-30 16:54:11 +02:00
context 'when user is not signed in' do
2024-03-28 12:42:29 +01:00
subject! { get :show , params : { dossier_id : champ . dossier_id , stable_id : champ . stable_id } , format : :turbo_stream }
2018-09-06 19:23:27 +02:00
2023-05-02 12:27:36 +02:00
it { expect ( response ) . to redirect_to ( new_user_session_path ) }
2018-09-06 19:23:27 +02:00
end
end
end