2018-09-06 19:23:27 +02:00
describe Champs :: SiretController , type : :controller do
let ( :user ) { create ( :user ) }
2020-07-20 17:18:44 +02:00
let ( :procedure ) do
tdc_siret = build ( :type_de_champ_siret , procedure : nil )
create ( :procedure , :published , types_de_champ : [ tdc_siret ] )
end
2018-09-06 19:23:27 +02:00
describe '#show' do
let ( :dossier ) { create ( :dossier , user : user , procedure : procedure ) }
2020-07-20 17:18:44 +02:00
let ( :champ ) { dossier . champs . first }
2018-09-06 19:23:27 +02:00
let ( :params ) do
{
2019-03-12 14:57:03 +01:00
champ_id : champ . id ,
2018-09-06 19:23:27 +02:00
dossier : {
champs_attributes : {
2018-10-01 14:03:05 +02:00
'1' = > { value : siret . to_s }
2018-09-06 19:23:27 +02:00
}
} ,
position : '1'
}
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
2020-12-10 15:28:39 +01:00
stub_request ( :get , / https: \/ \/ entreprise.api.gouv.fr \/ v2 \/ etablissements \/ #{ siret } / )
2020-05-18 14:26:11 +02:00
. to_return ( status : api_etablissement_status , body : api_etablissement_body )
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
2021-06-22 11:37:48 +02:00
subject! { get :show , params : params , format : :js , xhr : true }
2018-09-06 19:23:27 +02:00
2019-04-30 16:54:11 +02:00
it 'clears the etablissement and SIRET on the model' do
2019-03-12 14:57:03 +01:00
champ . reload
expect ( champ . etablissement ) . to be_nil
expect ( champ . value ) . to be_empty
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
expect ( response . body ) . to include ( '.siret-info-1' )
expect ( response . body ) . to include ( 'innerHTML = ""' )
end
2018-09-06 19:23:27 +02:00
end
2019-04-30 16:54:11 +02:00
context 'when the SIRET is invalid' do
2018-09-06 19:23:27 +02:00
let ( :siret ) { '1234' }
2021-06-22 11:37:48 +02:00
subject! { get :show , params : params , format : :js , xhr : true }
2019-04-30 16:54:11 +02:00
it 'clears the etablissement and SIRET on the model' do
2019-03-12 14:57:03 +01:00
champ . reload
expect ( champ . etablissement ) . to be_nil
expect ( champ . value ) . to be_empty
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
2019-05-02 11:24:22 +02:00
context 'when the API is unavailable' do
let ( :siret ) { '82161143100015' }
2020-05-18 14:26:11 +02:00
let ( :api_etablissement_status ) { 503 }
2019-05-02 11:24:22 +02:00
2021-06-22 11:37:48 +02:00
subject! { get :show , params : params , format : :js , xhr : true }
2019-05-02 11:24:22 +02:00
it 'clears the etablissement and SIRET on the model' do
champ . reload
expect ( champ . etablissement ) . to be_nil
expect ( champ . value ) . to be_empty
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
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
2021-06-22 11:37:48 +02:00
subject! { get :show , params : params , format : :js , xhr : true }
2019-04-30 16:54:11 +02:00
it 'clears the etablissement and SIRET on the model' do
2019-03-12 14:57:03 +01:00
champ . reload
expect ( champ . etablissement ) . to be_nil
expect ( champ . value ) . to be_empty
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
2020-05-18 14:26:11 +02:00
let ( :siret ) { '41816609600051' }
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
2021-06-22 11:37:48 +02:00
subject! { get :show , params : params , format : :js , xhr : true }
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 . value ) . to eq ( siret )
expect ( champ . etablissement . siret ) . to eq ( siret )
2020-07-20 17:18:44 +02:00
expect ( champ . etablissement . naf ) . to eq ( " 6202A " )
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
2021-06-22 11:37:48 +02:00
subject! { get :show , params : { position : '1' } , format : :js , xhr : true }
2018-09-06 19:23:27 +02:00
it { expect ( response . code ) . to eq ( '401' ) }
end
end
end