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

96 lines
2.8 KiB
Ruby
Raw Normal View History

2018-09-06 19:23:27 +02:00
require 'spec_helper'
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) { '' }
context 'when user is connected' do
render_views
before { sign_in user }
context 'when siret empty' do
before {
get :show, params: params, format: 'js'
}
it 'empty info message' do
expect(response.body).to include('.siret-info-1')
expect(response.body).to include('innerHTML = ""')
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
end
context 'when siret invalid' do
let(:siret) { '1234' }
before {
get :show, params: params, format: 'js'
}
it 'invalid error' do
expect(response.body).to include('Le numéro de SIRET doit comporter exactement 14 chiffres.')
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
end
context 'when siret not found' do
let(:siret) { '0' * 14 }
before {
expect(subject).to receive(:find_etablisement_with_siret).and_return(false)
get :show, params: params, format: 'js'
}
it 'not found error' do
expect(response.body).to include('Nous navons pas trouvé détablissement correspondant à ce numéro de SIRET.')
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
end
context 'when siret found' do
let(:siret) { etablissement.siret }
let(:etablissement) { build(:etablissement) }
before {
expect(subject).to receive(:find_etablisement_with_siret).and_return(etablissement)
get :show, params: params, format: 'js'
}
it 'etablissement info message' do
expect(response.body).to include(etablissement.entreprise_raison_sociale)
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
end
end
context 'when user is not connected' do
before {
get :show, params: { position: '1' }, format: 'js'
}
it { expect(response.code).to eq('401') }
end
end
end