dossier: add action to save SIRET number

This commit is contained in:
Pierre de La Morinerie 2018-10-15 16:41:53 +00:00 committed by gregoirenovel
parent 54ba0f8e50
commit 310f4b92d1
6 changed files with 176 additions and 1 deletions

View file

@ -214,6 +214,127 @@ describe NewUser::DossiersController, type: :controller do
it { is_expected.to render_template(:siret) }
end
describe '#update_siret' do
let(:dossier) { create(:dossier, user: user) }
let(:siret) { params_siret.delete(' ') }
let(:siren) { siret[0..8] }
let(:api_etablissement_status) { 200 }
let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
let(:api_entreprise_status) { 200 }
let(:api_entreprise_body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
let(:api_exercices_status) { 200 }
let(:api_exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') }
let(:api_association_status) { 200 }
let(:api_association_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
def stub_api_entreprise_requests
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
.to_return(status: api_etablissement_status, body: api_etablissement_body)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
.to_return(status: api_entreprise_status, body: api_entreprise_body)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/#{siret}?.*token=/)
.to_return(status: api_exercices_status, body: api_exercices_body)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/#{siret}?.*token=/)
.to_return(status: api_association_status, body: api_association_body)
end
before do
sign_in(user)
stub_api_entreprise_requests
end
subject! { post :update_siret, params: { id: dossier.id, user: { siret: params_siret } } }
shared_examples 'SIRET informations are successfully saved' do
it do
dossier.reload
user.reload
expect(dossier.etablissement).to be_present
expect(dossier.autorisation_donnees).to be(true)
expect(user.siret).to eq(siret)
expect(response).to redirect_to(etablissement_dossier_path)
end
end
shared_examples 'the request fails with an error' do |error|
it 'doesnt save an etablissement' do
expect(dossier.reload.etablissement).to be_nil
end
it 'displays the SIRET that was sent by the user in the form' do
expect(controller.current_user.siret).to eq(siret)
end
it 'renders an error' do
expect(flash.alert).to eq(error)
expect(response).to render_template(:siret)
end
end
context 'with an invalid SIRET' do
let(:params_siret) { '000 000' }
it_behaves_like 'the request fails with an error', ['Siret Le numéro SIRET doit comporter 14 chiffres']
end
context 'with a valid SIRET' do
let(:params_siret) { '440 117 620 01530' }
context 'when API-Entreprise doesnt know this SIRET' do
let(:api_etablissement_status) { 404 }
let(:api_body_status) { '' }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end
context 'when the API returns no Entreprise' do
let(:api_entreprise_status) { 404 }
let(:api_entreprise_body) { '' }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end
context 'when the API returns no Exercices' do
let(:api_exercices_status) { 404 }
let(:api_exercices_body) { '' }
it_behaves_like 'SIRET informations are successfully saved'
it 'doesnt save the etablissement exercices' do
expect(dossier.reload.etablissement.exercices).to be_empty
end
end
context 'when the RNA doesnt have informations on the SIRET' do
let(:api_association_status) { 404 }
let(:api_association_body) { '' }
it_behaves_like 'SIRET informations are successfully saved'
it 'doesnt save the RNA informations' do
expect(dossier.reload.etablissement.association?).to be(false)
end
end
context 'when all API informations available' do
it_behaves_like 'SIRET informations are successfully saved'
it 'saves the associated informations on the etablissement' do
dossier.reload
expect(dossier.etablissement.entreprise).to be_present
expect(dossier.etablissement.exercices).to be_present
expect(dossier.etablissement.association?).to be(true)
end
end
end
end
describe '#brouillon' do
before { sign_in(user) }
let!(:dossier) { create(:dossier, user: user, autorisation_donnees: true) }