demarches-normaliennes/spec/controllers/champs/siret_controller_spec.rb

125 lines
3.9 KiB
Ruby
Raw Normal View History

2018-09-06 19:23:27 +02:00
describe Champs::SiretController, type: :controller do
let(:user) { create(:user) }
let(:procedure) { create(:procedure, :published) }
describe '#show' do
let(:dossier) { create(:dossier, user: user, procedure: procedure) }
let(:champ) { create(:champ_siret, dossier: dossier, value: nil, etablissement: nil) }
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
before { sign_in user }
2019-04-30 16:54:11 +02:00
context 'when the SIRET is empty' do
subject! { get :show, params: params, format: 'js' }
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' }
2019-04-30 16:54:11 +02:00
subject! { get :show, params: params, format: 'js' }
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
context 'when the API is unavailable' do
let(:siret) { '82161143100015' }
before do
2020-03-05 13:50:38 +01:00
allow(controller).to receive(:find_etablissement_with_siret).and_raise(ApiEntreprise::API::RequestFailed)
end
subject! { get :show, params: params, format: 'js' }
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
expect(response.body).to include(I18n.t('errors.messages.siret_network_error'))
end
end
2019-04-30 16:54:11 +02:00
context 'when the SIRET is valid but unknown' do
let(:siret) { '00000000000000' }
2018-09-06 19:23:27 +02:00
2019-04-30 16:54:11 +02:00
before do
allow(controller).to receive(:find_etablissement_with_siret).and_return(false)
end
subject! { get :show, params: params, format: 'js' }
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 navons 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
2018-09-06 19:23:27 +02:00
let(:siret) { etablissement.siret }
let(:etablissement) { build(:etablissement) }
2019-04-30 16:54:11 +02:00
before do
allow(controller).to receive(:find_etablissement_with_siret).and_return(etablissement)
end
subject! { get :show, params: params, format: 'js' }
it 'populates the etablissement and SIRET on the model' do
2019-03-12 14:57:03 +01:00
champ.reload
expect(champ.value).to eq(etablissement.siret)
expect(champ.etablissement.siret).to eq(etablissement.siret)
2018-09-06 19:23:27 +02:00
end
2019-04-30 16:54:11 +02:00
it 'displays the name of the company' do
expect(response.body).to include(etablissement.entreprise_raison_sociale)
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
subject! { get :show, params: { position: '1' }, format: 'js' }
2018-09-06 19:23:27 +02:00
it { expect(response.code).to eq('401') }
end
end
end