update siret_controller when finding etablissement

now, `ApiEntrepriseService` does'nt return a hash anymore but an
etablissement which is already persisted.
This commit is contained in:
Christophe Robillard 2020-05-18 14:26:11 +02:00
parent eebfb5ee5b
commit 637bde7326
2 changed files with 21 additions and 26 deletions

View file

@ -20,7 +20,17 @@ describe Champs::SiretController, type: :controller do
context 'when the user is signed in' do
render_views
before { sign_in user }
let(:api_etablissement_status) { 200 }
let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
let(:token_expired) { false }
before do
sign_in user
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
.to_return(status: api_etablissement_status, body: api_etablissement_body)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles)
.and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(token_expired)
end
context 'when the SIRET is empty' do
subject! { get :show, params: params, format: 'js' }
@ -55,10 +65,7 @@ describe Champs::SiretController, type: :controller do
context 'when the API is unavailable' do
let(:siret) { '82161143100015' }
before do
allow(controller).to receive(:find_etablissement_with_siret).and_raise(ApiEntreprise::API::RequestFailed)
end
let(:api_etablissement_status) { 503 }
subject! { get :show, params: params, format: 'js' }
@ -75,10 +82,7 @@ describe Champs::SiretController, type: :controller do
context 'when the SIRET is valid but unknown' do
let(:siret) { '00000000000000' }
before do
allow(controller).to receive(:find_etablissement_with_siret).and_return(false)
end
let(:api_etablissement_status) { 404 }
subject! { get :show, params: params, format: 'js' }
@ -94,23 +98,17 @@ describe Champs::SiretController, type: :controller do
end
context 'when the SIRET informations are retrieved successfully' do
let(:siret) { etablissement.siret }
let(:etablissement) { build(:etablissement) }
before do
allow(controller).to receive(:find_etablissement_with_siret).and_return(etablissement)
end
let(:siret) { '41816609600051' }
let(:api_etablissement_status) { 200 }
let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
subject! { get :show, params: params, format: 'js' }
it 'populates the etablissement and SIRET on the model' do
champ.reload
expect(champ.value).to eq(etablissement.siret)
expect(champ.etablissement.siret).to eq(etablissement.siret)
end
it 'displays the name of the company' do
expect(response.body).to include(etablissement.entreprise_raison_sociale)
expect(champ.value).to eq(siret)
expect(champ.etablissement.siret).to eq(siret)
expect(champ.reload.etablissement.naf).to eq("6202A")
end
end
end