2022-10-05 12:31:34 +02:00
describe Champs :: RNAController , 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 : :rna } ] ) }
2022-10-05 12:31:34 +02:00
describe '#show' do
let ( :dossier ) { create ( :dossier , user : user , procedure : procedure ) }
2022-11-10 22:21:14 +01:00
let ( :champ ) { dossier . champs_public . first }
2022-10-05 12:31:34 +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 : rna }
2022-10-05 12:31:34 +02:00
champ_attributes
end
let ( :params ) do
{
2024-04-22 10:47:47 +02:00
dossier_id : champ . dossier_id ,
stable_id : champ . stable_id ,
2022-10-05 12:31:34 +02:00
dossier : {
2022-11-10 22:21:14 +01:00
champs_public_attributes : champs_public_attributes
2022-10-05 12:31:34 +02:00
}
}
end
context 'when the user is signed in' do
render_views
before do
sign_in user
2023-05-31 09:43:11 +02:00
stub_request ( :get , / https: \/ \/ entreprise.api.gouv.fr \/ v4 \/ djepva \/ api-association \/ associations \/ open_data \/ #{ rna } / )
2022-10-05 12:31:34 +02:00
. to_return ( body : body , status : status )
allow_any_instance_of ( APIEntrepriseToken ) . to receive ( :expired? ) . and_return ( false )
end
context 'when the RNA is empty' do
let ( :rna ) { '' }
let ( :status ) { 422 }
let ( :body ) { '' }
subject! { get :show , params : params , format : :turbo_stream }
2023-01-13 14:52:16 +01:00
it 'clears the data on the model' do
2023-01-14 11:53:55 +01:00
expect ( champ . reload . data ) . to be_nil
2022-10-05 12:31:34 +02:00
end
it 'clears any information or error message' do
expect ( response . body ) . to include ( ActionView :: RecordIdentifier . dom_id ( champ , :rna_info ) )
end
end
context 'when the RNA is invalid' do
let ( :rna ) { '1234' }
let ( :status ) { 422 }
let ( :body ) { '' }
subject! { get :show , params : params , format : :turbo_stream }
2023-01-13 14:52:16 +01:00
it 'clears the data on the model' do
expect ( champ . reload . data ) . to be_nil
2022-10-05 12:31:34 +02:00
end
it 'displays a “RNA is invalid” error message' do
2024-02-06 15:02:57 +01:00
expect ( response . body ) . to include ( " Le numéro RNA doit commencer par un W majuscule suivi de 9 chiffres ou lettres " )
2022-10-05 12:31:34 +02:00
end
end
context 'when the RNA is unknow' do
let ( :rna ) { 'W111111111' }
let ( :status ) { 404 }
let ( :body ) { '' }
subject! { get :show , params : params , format : :turbo_stream }
it 'clears the data on the model' do
champ . reload
2023-01-14 11:53:55 +01:00
expect ( champ . data ) . to be_nil
2022-10-05 12:31:34 +02:00
end
it 'displays a “RNA is invalid” error message' do
expect ( response . body ) . to include ( " Aucun établissement trouvé " )
end
end
context 'when the API is unavailable due to network error' do
let ( :rna ) { 'W595001988' }
let ( :status ) { 503 }
let ( :body ) { File . read ( 'spec/fixtures/files/api_entreprise/associations.json' ) }
before do
2024-04-05 12:08:40 +02:00
expect ( APIEntrepriseService ) . to receive ( :api_djepva_up? ) . and_return ( false )
2022-10-05 12:31:34 +02:00
end
subject! { get :show , params : params , format : :turbo_stream }
2023-01-13 14:52:16 +01:00
it 'clears the data on the model' do
expect ( champ . reload . data ) . to be_nil
2022-10-05 12:31:34 +02:00
end
it 'displays a “API is unavailable” error message' do
2022-12-01 13:41:12 +01:00
expect ( response . body ) . to include ( " Une erreur réseau a empêché l’ association liée à ce RNA d’ être trouvée " )
2022-10-05 12:31:34 +02:00
end
end
context 'when the RNA informations are retrieved successfully' do
let ( :rna ) { 'W595001988' }
let ( :status ) { 200 }
let ( :body ) { File . read ( 'spec/fixtures/files/api_entreprise/associations.json' ) }
subject! { get :show , params : params , format : :turbo_stream }
it 'populates the data and RNA on the model' do
champ . reload
expect ( champ . value ) . to eq ( rna )
2023-05-31 09:43:11 +02:00
expect ( champ . data [ " association_titre " ] ) . to eq ( " LA PRÉVENTION ROUTIERE " )
expect ( champ . data [ " association_objet " ] ) . to eq ( " L'association a pour objet de promouvoir la pratique du sport de haut niveau et de contribuer à la formation des jeunes sportifs. " )
expect ( champ . data [ " association_date_creation " ] ) . to eq ( " 2015-01-01 " )
expect ( champ . data [ " association_date_declaration " ] ) . to eq ( " 2019-01-01 " )
expect ( champ . data [ " association_date_publication " ] ) . to eq ( " 2018-01-01 " )
expect ( champ . data [ " association_rna " ] ) . to eq ( " W751080001 " )
2022-10-05 12:31:34 +02:00
end
end
end
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 }
2022-10-05 12:31:34 +02:00
2023-05-02 12:27:36 +02:00
it { expect ( response . code ) . to redirect_to ( new_user_session_path ) }
2022-10-05 12:31:34 +02:00
end
end
end