Merge pull request #5176 from betagouv/5113-async-for-api-ent-calls

Passe la récupération des données API Entreprise en asynchrone
This commit is contained in:
krichtof 2020-05-20 11:22:12 +02:00 committed by GitHub
commit 7c6d870925
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 389 additions and 400 deletions

View file

@ -19,7 +19,7 @@ class Champs::SiretController < ApplicationController
rescue ApiEntreprise::API::RequestFailed rescue ApiEntreprise::API::RequestFailed
return siret_error(:network_error) return siret_error(:network_error)
end end
if etablissement.blank? if etablissement.nil?
return siret_error(:not_found) return siret_error(:not_found)
end end
@ -50,10 +50,7 @@ class Champs::SiretController < ApplicationController
end end
def find_etablissement_with_siret def find_etablissement_with_siret
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(@siret, @procedure_id) ApiEntrepriseService.create_etablissement(@champ.dossier, @siret, current_user.id)
if etablissement_attributes.present?
Etablissement.new(etablissement_attributes)
end
end end
def clear_siret_and_etablissement def clear_siret_and_etablissement

View file

@ -104,20 +104,14 @@ module Users
sanitized_siret = siret_model.siret sanitized_siret = siret_model.siret
begin begin
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(sanitized_siret, @dossier.procedure.id, current_user.id) etablissement = ApiEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id)
rescue ApiEntreprise::API::RequestFailed rescue ApiEntreprise::API::RequestFailed
return render_siret_error(t('errors.messages.siret_network_error')) return render_siret_error(t('errors.messages.siret_network_error'))
end end
if etablissement_attributes.blank? if etablissement.nil?
return render_siret_error(t('errors.messages.siret_unknown')) return render_siret_error(t('errors.messages.siret_unknown'))
end end
attestation_sociale_url = etablissement_attributes.delete(:entreprise_attestation_sociale_url)
attestation_fiscale_url = etablissement_attributes.delete(:entreprise_attestation_fiscale_url)
etablissement = @dossier.build_etablissement(etablissement_attributes)
etablissement.upload_attestation_sociale(attestation_sociale_url) if attestation_sociale_url.present?
etablissement.upload_attestation_fiscale(attestation_fiscale_url) if attestation_fiscale_url.present?
etablissement.save!
current_user.update!(siret: sanitized_siret) current_user.update!(siret: sanitized_siret)
@dossier.update!(autorisation_donnees: true) @dossier.update!(autorisation_donnees: true)

View file

@ -0,0 +1,7 @@
class ApiEntreprise::AssociationJob < ApplicationJob
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::RNAAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end
end

View file

@ -0,0 +1,8 @@
class ApiEntreprise::AttestationFiscaleJob < ApplicationJob
def perform(etablissement_id, procedure_id, user_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::AttestationFiscaleAdapter.new(etablissement.siret, procedure_id, user_id).to_params
attestation_fiscale_url = etablissement_params.delete(:entreprise_attestation_fiscale_url)
etablissement.upload_attestation_fiscale(attestation_fiscale_url) if attestation_fiscale_url.present?
end
end

View file

@ -0,0 +1,8 @@
class ApiEntreprise::AttestationSocialeJob < ApplicationJob
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::AttestationSocialeAdapter.new(etablissement.siret, procedure_id).to_params
attestation_sociale_url = etablissement_params.delete(:entreprise_attestation_sociale_url)
etablissement.upload_attestation_sociale(attestation_sociale_url) if attestation_sociale_url.present?
end
end

View file

@ -0,0 +1,7 @@
class ApiEntreprise::BilansBdfJob < ApplicationJob
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::BilansBdfAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end
end

View file

@ -0,0 +1,7 @@
class ApiEntreprise::EffectifsAnnuelsJob < ApplicationJob
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::EffectifsAnnuelsAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end
end

View file

@ -0,0 +1,21 @@
class ApiEntreprise::EffectifsJob < ApplicationJob
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::EffectifsAdapter.new(etablissement.siret, procedure_id, *get_current_valid_month_for_effectif).to_params
etablissement.update!(etablissement_params)
end
private
def get_current_valid_month_for_effectif
today = Date.today
date_update = Date.new(today.year, today.month, 15)
if today >= date_update
[today.strftime("%Y"), today.strftime("%m")]
else
date = today - 1.month
[date.strftime("%Y"), date.strftime("%m")]
end
end
end

View file

@ -0,0 +1,7 @@
class ApiEntreprise::EntrepriseJob < ApplicationJob
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::EntrepriseAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end
end

View file

@ -0,0 +1,7 @@
class ApiEntreprise::ExercicesJob < ApplicationJob
def perform(etablissement_id, procedure_id)
etablissement = Etablissement.find(etablissement_id)
etablissement_params = ApiEntreprise::ExercicesAdapter.new(etablissement.siret, procedure_id).to_params
etablissement.update!(etablissement_params)
end
end

View file

@ -7,6 +7,14 @@ class ApplicationJob < ActiveJob::Base
Rails.logger.info("#{job.class.name} ended at #{Time.zone.now}") Rails.logger.info("#{job.class.name} ended at #{Time.zone.now}")
end end
rescue_from(ApiEntreprise::API::ResourceNotFound) do |exception|
error(self, exception)
end
rescue_from(ApiEntreprise::API::BadFormatRequest) do |exception|
error(self, exception)
end
def error(job, exception) def error(job, exception)
Raven.capture_exception(exception) Raven.capture_exception(exception)
end end

View file

@ -25,4 +25,8 @@ class ApiEntreprise::Adapter
def valid_params?(params) def valid_params?(params)
!params.has_value?(UNAVAILABLE) !params.has_value?(UNAVAILABLE)
end end
def siren
@siret[0..8]
end
end end

View file

@ -17,6 +17,9 @@ class ApiEntreprise::API
class RequestFailed < StandardError class RequestFailed < StandardError
end end
class BadFormatRequest < StandardError
end
def self.entreprise(siren, procedure_id) def self.entreprise(siren, procedure_id)
call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id) call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
end end
@ -72,8 +75,10 @@ class ApiEntreprise::API
JSON.parse(response.body, symbolize_names: true) JSON.parse(response.body, symbolize_names: true)
elsif response.code&.between?(401, 499) elsif response.code&.between?(401, 499)
raise ResourceNotFound raise ResourceNotFound
elsif response.code == 400
raise BadFormatRequest
else else
raise RequestFailed raise RequestFailed, "HTTP Error Code: #{response.code}"
end end
end end

View file

@ -1,6 +1,6 @@
class ApiEntreprise::AttestationFiscaleAdapter < ApiEntreprise::Adapter class ApiEntreprise::AttestationFiscaleAdapter < ApiEntreprise::Adapter
def initialize(siren, procedure_id, user_id) def initialize(siret, procedure_id, user_id)
@siren = siren @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
@user_id = user_id @user_id = user_id
end end
@ -8,7 +8,7 @@ class ApiEntreprise::AttestationFiscaleAdapter < ApiEntreprise::Adapter
private private
def get_resource def get_resource
ApiEntreprise::API.attestation_fiscale(@siren, @procedure_id, @user_id) ApiEntreprise::API.attestation_fiscale(siren, @procedure_id, @user_id)
end end
def process_params def process_params

View file

@ -1,13 +1,13 @@
class ApiEntreprise::AttestationSocialeAdapter < ApiEntreprise::Adapter class ApiEntreprise::AttestationSocialeAdapter < ApiEntreprise::Adapter
def initialize(siren, procedure_id) def initialize(siret, procedure_id)
@siren = siren @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
end end
private private
def get_resource def get_resource
ApiEntreprise::API.attestation_sociale(@siren, @procedure_id) ApiEntreprise::API.attestation_sociale(siren, @procedure_id)
end end
def process_params def process_params

View file

@ -1,13 +1,13 @@
class ApiEntreprise::BilansBdfAdapter < ApiEntreprise::Adapter class ApiEntreprise::BilansBdfAdapter < ApiEntreprise::Adapter
def initialize(siren, procedure_id) def initialize(siret, procedure_id)
@siren = siren @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
end end
private private
def get_resource def get_resource
ApiEntreprise::API.bilans_bdf(@siren, @procedure_id) ApiEntreprise::API.bilans_bdf(siren, @procedure_id)
end end
def process_params def process_params

View file

@ -1,6 +1,6 @@
class ApiEntreprise::EffectifsAdapter < ApiEntreprise::Adapter class ApiEntreprise::EffectifsAdapter < ApiEntreprise::Adapter
def initialize(siren, procedure_id, annee, mois) def initialize(siret, procedure_id, annee, mois)
@siren = siren @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
@annee = annee @annee = annee
@mois = mois @mois = mois
@ -9,7 +9,7 @@ class ApiEntreprise::EffectifsAdapter < ApiEntreprise::Adapter
private private
def get_resource def get_resource
ApiEntreprise::API.effectifs(@siren, @procedure_id, @annee, @mois) ApiEntreprise::API.effectifs(siren, @procedure_id, @annee, @mois)
end end
def process_params def process_params

View file

@ -1,13 +1,13 @@
class ApiEntreprise::EffectifsAnnuelsAdapter < ApiEntreprise::Adapter class ApiEntreprise::EffectifsAnnuelsAdapter < ApiEntreprise::Adapter
def initialize(siren, procedure_id) def initialize(siret, procedure_id)
@siren = siren @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
end end
private private
def get_resource def get_resource
ApiEntreprise::API.effectifs_annuels(@siren, @procedure_id) ApiEntreprise::API.effectifs_annuels(siren, @procedure_id)
end end
def process_params def process_params

View file

@ -2,7 +2,6 @@ class ApiEntreprise::EntrepriseAdapter < ApiEntreprise::Adapter
private private
def get_resource def get_resource
siren = @siret[0..8]
ApiEntreprise::API.entreprise(siren, @procedure_id) ApiEntreprise::API.entreprise(siren, @procedure_id)
end end

View file

@ -1,73 +1,28 @@
class ApiEntrepriseService class ApiEntrepriseService
# Retrieve all informations we can get about a SIRET. # create etablissement with EtablissementAdapter
# enqueue api_entreprise jobs to retrieve
# all informations we can get about a SIRET.
# #
# Returns nil if the SIRET is unknown; and nested params # Returns nil if the SIRET is unknown
# suitable for being saved into a Etablissement object otherwise.
# #
# Raises a ApiEntreprise::API::RequestFailed exception on transcient errors # Raises a ApiEntreprise::API::RequestFailed exception on transient errors
# (timeout, 5XX HTTP error code, etc.) # (timeout, 5XX HTTP error code, etc.)
def self.get_etablissement_params_for_siret(siret, procedure_id, user_id = nil) def self.create_etablissement(dossier, siret, user_id = nil)
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, dossier.procedure.id).to_params
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params return nil if etablissement_params.empty?
if etablissement_params.present? && entreprise_params.present? etablissement = dossier.build_etablissement(etablissement_params)
begin etablissement.save
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
etablissement_params.merge!(association_params)
rescue ApiEntreprise::API::RequestFailed
end
begin [
exercices_params = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_params ApiEntreprise::EntrepriseJob, ApiEntreprise::AssociationJob, ApiEntreprise::ExercicesJob,
etablissement_params.merge!(exercices_params) ApiEntreprise::EffectifsJob, ApiEntreprise::EffectifsAnnuelsJob, ApiEntreprise::AttestationSocialeJob,
rescue ApiEntreprise::API::RequestFailed ApiEntreprise::BilansBdfJob
end ].each do |job|
job.perform_later(etablissement.id, dossier.procedure.id)
begin
effectifs_params = ApiEntreprise::EffectifsAdapter.new(entreprise_params[:entreprise_siren], procedure_id, *get_current_valid_month_for_effectif).to_params
etablissement_params.merge!(effectifs_params)
rescue ApiEntreprise::API::RequestFailed
end
begin
effectifs_annuels_params = ApiEntreprise::EffectifsAnnuelsAdapter.new(entreprise_params[:entreprise_siren], procedure_id).to_params
etablissement_params.merge!(effectifs_annuels_params)
rescue ApiEntreprise::API::RequestFailed
end
begin
attestation_sociale_params = ApiEntreprise::AttestationSocialeAdapter.new(entreprise_params[:entreprise_siren], procedure_id).to_params
etablissement_params.merge!(attestation_sociale_params)
rescue ApiEntreprise::API::RequestFailed
end
begin
attestation_fiscale_params = ApiEntreprise::AttestationFiscaleAdapter.new(entreprise_params[:entreprise_siren], procedure_id, user_id).to_params
etablissement_params.merge!(attestation_fiscale_params)
rescue ApiEntreprise::API::RequestFailed
end
begin
bilans_bdf_params = ApiEntreprise::BilansBdfAdapter.new(entreprise_params[:entreprise_siren], procedure_id).to_params
etablissement_params.merge!(bilans_bdf_params)
rescue ApiEntreprise::API::RequestFailed
end
etablissement_params.merge(entreprise_params)
end end
end ApiEntreprise::AttestationFiscaleJob.perform_later(etablissement.id, dossier.procedure.id, user_id)
private etablissement
def self.get_current_valid_month_for_effectif
today = Date.today
date_update = Date.new(today.year, today.month, 15)
if today >= date_update
[today.strftime("%Y"), today.strftime("%m")]
else
date = today - 1.month
[date.strftime("%Y"), date.strftime("%m")]
end
end end
end end

View file

@ -24,9 +24,6 @@
= render partial: 'users/dossiers/etablissement/infos_entreprise', locals: { etablissement: etablissement } = render partial: 'users/dossiers/etablissement/infos_entreprise', locals: { etablissement: etablissement }
- if etablissement.association?
= render partial: 'users/dossiers/etablissement/infos_association', locals: { etablissement: etablissement }
.actions .actions
= link_to 'Utiliser un autre numéro SIRET', siret_dossier_path(@dossier), class: 'button' = link_to 'Utiliser un autre numéro SIRET', siret_dossier_path(@dossier), class: 'button'
= link_to 'Continuer avec ces informations', brouillon_dossier_path(@dossier), class: 'button primary' = link_to 'Continuer avec ces informations', brouillon_dossier_path(@dossier), class: 'button primary'

View file

@ -1,24 +0,0 @@
%ul.etablissement-infos-association
%li
Numéro denregistrement au Registre National des Associations :
= etablissement.association_rna
%li
Titre :
= etablissement.association_titre
%li
Objet :
= etablissement.association_objet
%li
Date de création :
= try_format_date(etablissement.association_date_creation)
%li
Date de déclaration :
= try_format_date(etablissement.association_date_declaration)
%li
Date de publication :
= try_format_date(etablissement.association_date_publication)

View file

@ -3,15 +3,6 @@
Siret : Siret :
= etablissement.siret = etablissement.siret
- if etablissement.siret != etablissement.entreprise.siret_siege_social
%li
SIRET siègle social :
= etablissement.entreprise.siret_siege_social
%li
Forme juridique :
= etablissement.entreprise.forme_juridique
%li %li
Libellé NAF : Libellé NAF :
= etablissement.libelle_naf = etablissement.libelle_naf
@ -20,34 +11,24 @@
Code NAF : Code NAF :
= etablissement.naf = etablissement.naf
%li
Date de création :
= try_format_date(etablissement.entreprise.date_creation)
%li
Effectif organisation :
= effectif(etablissement)
%li
Code effectif :
= etablissement.entreprise.code_effectif_entreprise
%li
Numéro TVA intracommunautaire :
= etablissement.entreprise.numero_tva_intracommunautaire
%li %li
Adresse : Adresse :
- etablissement.adresse.split("\n").each do |line| - etablissement.adresse.split("\n").each do |line|
= line = line
%li %p.etablissement-exercices
Capital social : Nous allons également récupérer la forme juridique, la date de création, les effectifs, le numéro TVA intracommunautaire, le capital social de votre organisation.
= pretty_currency(etablissement.entreprise.capital_social) Pour les associations, nous récupérerons également l'objet, la date de création, de déclaration et de publication.
- if etablissement.exercices.present? %p.etablissement-exercices Les exercices comptables des trois dernières années pourront être joints à votre dossier.
%p.etablissement-exercices Les exercices comptables des trois dernières années seront joints à votre dossier.
- procedure = etablissement.dossier.procedure
- if procedure.api_entreprise_role?("attestations_sociales")
%p.etablissement-exercices Une attestation sociale sera jointe à votre dossier
- if procedure.api_entreprise_role?("attestations_fiscales")
%p.etablissement-exercices Une attestation fiscale sera jointe à votre dossier
- if procedure.api_entreprise_role?("bilans_bdf")
%p.etablissement-exercices Les 3 derniers bilans connus de votre entreprise par la Banque de France ont été joints à votre dossier.
%p %p
= link_to '➡ Autres informations sur lorganisme sur « entreprise.data.gouv.fr »', = link_to '➡ Autres informations sur lorganisme sur « entreprise.data.gouv.fr »',
"https://entreprise.data.gouv.fr/etablissement/#{etablissement.siret}", "https://entreprise.data.gouv.fr/etablissement/#{etablissement.siret}",

View file

@ -20,7 +20,17 @@ describe Champs::SiretController, type: :controller do
context 'when the user is signed in' do context 'when the user is signed in' do
render_views 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 context 'when the SIRET is empty' do
subject! { get :show, params: params, format: 'js' } subject! { get :show, params: params, format: 'js' }
@ -55,10 +65,7 @@ describe Champs::SiretController, type: :controller do
context 'when the API is unavailable' do context 'when the API is unavailable' do
let(:siret) { '82161143100015' } let(:siret) { '82161143100015' }
let(:api_etablissement_status) { 503 }
before do
allow(controller).to receive(:find_etablissement_with_siret).and_raise(ApiEntreprise::API::RequestFailed)
end
subject! { get :show, params: params, format: 'js' } 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 context 'when the SIRET is valid but unknown' do
let(:siret) { '00000000000000' } let(:siret) { '00000000000000' }
let(:api_etablissement_status) { 404 }
before do
allow(controller).to receive(:find_etablissement_with_siret).and_return(false)
end
subject! { get :show, params: params, format: 'js' } subject! { get :show, params: params, format: 'js' }
@ -94,23 +98,17 @@ describe Champs::SiretController, type: :controller do
end end
context 'when the SIRET informations are retrieved successfully' do context 'when the SIRET informations are retrieved successfully' do
let(:siret) { etablissement.siret } let(:siret) { '41816609600051' }
let(:etablissement) { build(:etablissement) } let(:api_etablissement_status) { 200 }
let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
before do
allow(controller).to receive(:find_etablissement_with_siret).and_return(etablissement)
end
subject! { get :show, params: params, format: 'js' } subject! { get :show, params: params, format: 'js' }
it 'populates the etablissement and SIRET on the model' do it 'populates the etablissement and SIRET on the model' do
champ.reload champ.reload
expect(champ.value).to eq(etablissement.siret) expect(champ.value).to eq(siret)
expect(champ.etablissement.siret).to eq(etablissement.siret) expect(champ.etablissement.siret).to eq(siret)
end expect(champ.reload.etablissement.naf).to eq("6202A")
it 'displays the name of the company' do
expect(response.body).to include(etablissement.entreprise_raison_sociale)
end end
end end
end end

View file

@ -200,72 +200,18 @@ describe Users::DossiersController, type: :controller do
let(:dossier) { create(:dossier, user: user) } let(:dossier) { create(:dossier, user: user) }
let(:siret) { params_siret.delete(' ') } let(:siret) { params_siret.delete(' ') }
let(:siren) { siret[0..8] } let(:siren) { siret[0..8] }
let(:api_etablissement_status) { 200 } let(:api_etablissement_status) { 200 }
let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') } 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_entreprise_effectifs_mensuels_status) { 200 }
let(:api_entreprise_effectifs_mensuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs.json') }
let(:annee) { "2020" }
let(:mois) { "02" }
let(:api_entreprise_effectifs_annuels_status) { 200 }
let(:api_entreprise_effectifs_annuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs_annuels.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') }
let(:api_entreprise_attestation_sociale_status) { 200 }
let(:api_entreprise_attestation_sociale_body) { File.read('spec/fixtures/files/api_entreprise/attestation_sociale.json') }
let(:api_entreprise_attestation_fiscale_status) { 200 }
let(:api_entreprise_attestation_fiscale_body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') }
let(:api_entreprise_bilans_bdf_status) { 200 }
let(:api_entreprise_bilans_bdf_body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
let(:token_expired) { false } let(:token_expired) { false }
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)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/#{annee}\/#{mois}\/entreprise\/#{siren}?.*token=/)
.to_return(body: api_entreprise_effectifs_mensuels_body, status: api_entreprise_effectifs_mensuels_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_annuels_acoss_covid\/#{siren}?.*token=/)
.to_return(body: api_entreprise_effectifs_annuels_body, status: api_entreprise_effectifs_annuels_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
.to_return(body: api_entreprise_attestation_sociale_body, status: api_entreprise_attestation_sociale_status)
stub_request(:get, "https://storage.entreprise.api.gouv.fr/siade/1569156881-f749d75e2bfd443316e2e02d59015f-attestation_vigilance_acoss.pdf")
.to_return(body: "body attestation", status: 200)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=/)
.to_return(body: api_entreprise_attestation_fiscale_body, status: api_entreprise_attestation_fiscale_status)
stub_request(:get, "https://storage.entreprise.api.gouv.fr/siade/1569156756-f6b7779f99fa95cd60dc03c04fcb-attestation_fiscale_dgfip.pdf")
.to_return(body: "body attestation", status: 200)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=/)
.to_return(body: api_entreprise_bilans_bdf_body, status: api_entreprise_bilans_bdf_status)
end
before do before do
sign_in(user) sign_in(user)
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)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles) allow_any_instance_of(ApiEntrepriseToken).to receive(:roles)
.and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"]) .and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(token_expired) allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(token_expired)
end end
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
after { Timecop.return }
subject! { post :update_siret, params: { id: dossier.id, user: { siret: params_siret } } } subject! { post :update_siret, params: { id: dossier.id, user: { siret: params_siret } } }
@ -307,68 +253,29 @@ describe Users::DossiersController, type: :controller do
let(:params_siret) { '418 166 096 00051' } let(:params_siret) { '418 166 096 00051' }
context 'When API-Entreprise is down' do context 'When API-Entreprise is down' do
let(:api_etablissement_status) { 502 } let(:api_etablissement_status) { 502 }
let(:api_body_status) { File.read('spec/fixtures/files/api_entreprise/exercices_unavailable.json') }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error') it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_network_error')
end end
context 'when API-Entreprise doesnt know this SIRET' do context 'when API-Entreprise doesnt know this SIRET' do
let(:api_etablissement_status) { 404 } let(:api_etablissement_status) { 404 }
let(:api_body_status) { '' }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown') it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end end
context 'when default token has expired' do context 'when default token has expired' do
let(:api_etablissement_status) { 200 } let(:api_etablissement_status) { 200 }
let(:api_body_status) { '' }
let(:token_expired) { true } let(:token_expired) { true }
it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown') it_behaves_like 'the request fails with an error', I18n.t('errors.messages.siret_unknown')
end 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 context 'when all API informations available' do
it_behaves_like 'SIRET informations are successfully saved' it_behaves_like 'SIRET informations are successfully saved'
it 'saves the associated informations on the etablissement' do it 'saves the associated informations on the etablissement' do
dossier.reload dossier.reload
expect(dossier.etablissement.entreprise).to be_present expect(dossier.etablissement.entreprise).to be_present
expect(dossier.etablissement.exercices).to be_present
expect(dossier.etablissement.association?).to be(true)
expect(dossier.etablissement.entreprise_effectif_mensuel).to be_present
expect(dossier.etablissement.entreprise_effectif_annuel).to be_present
expect(dossier.etablissement.entreprise_attestation_sociale).to be_attached
expect(dossier.etablissement.entreprise_attestation_fiscale).to be_attached
expect(dossier.etablissement.entreprise_bilans_bdf).to be_present
end end
end end
end end

View file

@ -95,7 +95,7 @@ feature 'Creating a new dossier:' do
click_on 'Valider' click_on 'Valider'
expect(page).to have_current_path(etablissement_dossier_path(dossier)) expect(page).to have_current_path(etablissement_dossier_path(dossier))
expect(page).to have_content('OCTO-TECHNOLOGY') expect(page).to have_content('OCTO TECHNOLOGY')
click_on 'Continuer avec ces informations' click_on 'Continuer avec ces informations'
expect(page).to have_current_path(brouillon_dossier_path(dossier)) expect(page).to have_current_path(brouillon_dossier_path(dossier))

View file

@ -0,0 +1,21 @@
RSpec.describe ApiEntreprise::AssociationJob, type: :job do
let(:siret) { '50480511000013' }
let(:etablissement) { create(:etablissement, siret: siret) }
let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
let(:status) { 200 }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { ApiEntreprise::AssociationJob.new.perform(etablissement.id, procedure_id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).association_rna).to eq('W595001988')
end
end

View file

@ -0,0 +1,25 @@
RSpec.describe ApiEntreprise::AttestationFiscaleJob, type: :job do
let(:etablissement) { create(:etablissement, siret: siret) }
let(:siret) { '41816609600069' }
let(:siren) { '418166096' }
let(:procedure) { create(:procedure) }
let(:user_id) { 1 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') }
let(:status) { 200 }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}\?.*token=/)
.to_return(body: body, status: status)
stub_request(:get, "https://storage.entreprise.api.gouv.fr/siade/1569156756-f6b7779f99fa95cd60dc03c04fcb-attestation_fiscale_dgfip.pdf")
.to_return(body: "body attestation", status: 200)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["attestations_fiscales"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { ApiEntreprise::AttestationFiscaleJob.new.perform(etablissement.id, procedure.id, user_id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).entreprise_attestation_fiscale).to be_attached
end
end

View file

@ -0,0 +1,24 @@
RSpec.describe ApiEntreprise::AttestationSocialeJob, type: :job do
let(:etablissement) { create(:etablissement, siret: siret) }
let(:siret) { '41816609600069' }
let(:siren) { '418166096' }
let(:procedure) { create(:procedure) }
let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_sociale.json') }
let(:status) { 200 }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}\?.*token=/)
.to_return(body: body, status: status)
stub_request(:get, "https://storage.entreprise.api.gouv.fr/siade/1569156881-f749d75e2bfd443316e2e02d59015f-attestation_vigilance_acoss.pdf")
.to_return(body: "body attestation", status: 200)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["attestations_sociales"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { ApiEntreprise::AttestationSocialeJob.new.perform(etablissement.id, procedure.id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).entreprise_attestation_sociale).to be_attached
end
end

View file

@ -0,0 +1,24 @@
RSpec.describe ApiEntreprise::BilansBdfJob, type: :job do
let(:etablissement) { create(:etablissement, siret: siret) }
let(:siret) { '41816609600069' }
let(:siren) { '418166096' }
let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
let(:status) { 200 }
let(:bilans_bdf) { JSON.parse(body)["bilans"] }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}\?.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["bilans_entreprise_bdf"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { ApiEntreprise::BilansBdfJob.new.perform(etablissement.id, procedure_id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).entreprise_bilans_bdf).to eq(bilans_bdf)
end
end

View file

@ -0,0 +1,22 @@
RSpec.describe ApiEntreprise::EffectifsAnnuelsJob, type: :job do
let(:etablissement) { create(:etablissement, siret: siret) }
let(:siret) { '41816609600069' }
let(:siren) { '418166096' }
let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:body) { File.read('spec/fixtures/files/api_entreprise/effectifs_annuels.json') }
let(:status) { 200 }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_annuels_acoss_covid\/#{siren}\?.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { ApiEntreprise::EffectifsAnnuelsJob.new.perform(etablissement.id, procedure_id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).entreprise_effectif_annuel).to eq(100.5)
end
end

View file

@ -0,0 +1,28 @@
RSpec.describe ApiEntreprise::EffectifsJob, type: :job do
let(:siret) { '41816609600069' }
let(:siren) { '418166096' }
let(:etablissement) { create(:etablissement, siret: siret) }
let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:now) { Time.zone.local(2020, 3, 12) }
let(:annee) { "2020" }
let(:mois) { "02" }
let(:body) { File.read('spec/fixtures/files/api_entreprise/effectifs.json') }
let(:status) { 200 }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/#{annee}\/#{mois}\/entreprise\/#{siren}\?.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
before { Timecop.freeze(now) }
after { Timecop.return }
subject { ApiEntreprise::EffectifsJob.new.perform(etablissement.id, procedure_id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).entreprise_effectif_mensuel).to eq(100.5)
end
end

View file

@ -0,0 +1,22 @@
RSpec.describe ApiEntreprise::EntrepriseJob, type: :job do
let(:siret) { '41816609600051' }
let(:siren) { '418166096' }
let(:etablissement) { create(:etablissement, siret: siret) }
let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
let(:status) { 200 }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { ApiEntreprise::EntrepriseJob.new.perform(etablissement.id, procedure_id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).entreprise_numero_tva_intracommunautaire).to eq('FR16418166096')
end
end

View file

@ -0,0 +1,20 @@
RSpec.describe ApiEntreprise::ExercicesJob, type: :job do
let(:siret) { '41816609600051' }
let(:procedure) { create(:procedure) }
let(:etablissement) { create(:etablissement, siret: siret) }
let(:body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') }
let(:status) { 200 }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end
subject { ApiEntreprise::ExercicesJob.new.perform(etablissement.id, procedure.id) }
it 'updates etablissement' do
subject
expect(Etablissement.find(etablissement.id).exercices[0].ca).to eq('21009417')
end
end

View file

@ -32,6 +32,16 @@ describe ApiEntreprise::API do
end end
end end
context 'when request has bad format' do
let(:siren) { '111111111' }
let(:status) { 400 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') }
it 'raises ApiEntreprise::API::BadFormatRequest' do
expect { subject }.to raise_error(ApiEntreprise::API::BadFormatRequest)
end
end
context 'when siren infos are private' do context 'when siren infos are private' do
let(:siren) { '111111111' } let(:siren) { '111111111' }
let(:status) { 403 } let(:status) { 403 }

View file

@ -1,12 +1,13 @@
describe ApiEntreprise::AttestationFiscaleAdapter do describe ApiEntreprise::AttestationFiscaleAdapter do
let(:siret) { '41816609600069' }
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:user_id) { 1 } let(:user_id) { 1 }
let(:adapter) { described_class.new(siren, procedure.id, user_id) } let(:adapter) { described_class.new(siret, procedure.id, user_id) }
subject { adapter.to_params } subject { adapter.to_params }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}\?.*token=/)
.to_return(body: body, status: status) .to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["attestations_fiscales"]) allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["attestations_fiscales"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)

View file

@ -1,11 +1,12 @@
describe ApiEntreprise::AttestationSocialeAdapter do describe ApiEntreprise::AttestationSocialeAdapter do
let(:siret) { '41816609600069' }
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:adapter) { described_class.new(siren, procedure.id) } let(:adapter) { described_class.new(siret, procedure.id) }
subject { adapter.to_params } subject { adapter.to_params }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}\?.*token=/)
.to_return(body: body, status: status) .to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["attestations_sociales"]) allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["attestations_sociales"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)

View file

@ -1,12 +1,13 @@
describe ApiEntreprise::BilansBdfAdapter do describe ApiEntreprise::BilansBdfAdapter do
let(:siret) { '41816609600069' }
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
let(:adapter) { described_class.new(siren, procedure_id) } let(:adapter) { described_class.new(siret, procedure_id) }
subject { adapter.to_params } subject { adapter.to_params }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}\?.*token=/)
.to_return(body: body, status: status) .to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["bilans_entreprise_bdf"]) allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(["bilans_entreprise_bdf"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)

View file

@ -1,14 +1,15 @@
describe ApiEntreprise::EffectifsAdapter do describe ApiEntreprise::EffectifsAdapter do
let(:siret) { '41816609600069' }
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
let(:annee) { "2020" } let(:annee) { "2020" }
let(:mois) { "02" } let(:mois) { "02" }
let(:adapter) { described_class.new(siren, procedure_id, annee, mois) } let(:adapter) { described_class.new(siret, procedure_id, annee, mois) }
subject { adapter.to_params } subject { adapter.to_params }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/#{annee}\/#{mois}\/entreprise\/#{siren}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/#{annee}\/#{mois}\/entreprise\/#{siren}\?.*token=/)
.to_return(body: body, status: status) .to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end end

View file

@ -1,12 +1,13 @@
describe ApiEntreprise::EffectifsAnnuelsAdapter do describe ApiEntreprise::EffectifsAnnuelsAdapter do
let(:siret) { '41816609600069' }
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
let(:adapter) { described_class.new(siren, procedure_id) } let(:adapter) { described_class.new(siret, procedure_id) }
subject { adapter.to_params } subject { adapter.to_params }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_annuels_acoss_covid\/#{siren}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_annuels_acoss_covid\/#{siren}\?.*token=/)
.to_return(body: body, status: status) .to_return(body: body, status: status)
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end end

View file

@ -1,129 +1,44 @@
describe ApiEntrepriseService do describe ApiEntrepriseService do
describe '#get_etablissement_params_for_siret' do describe '#create_etablissement' do
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
.to_return(body: entreprises_body, status: entreprises_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
.to_return(body: etablissements_body, status: etablissements_status) .to_return(body: etablissements_body, status: etablissements_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
.to_return(body: exercices_body, status: exercices_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
.to_return(body: associations_body, status: associations_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/#{annee}\/#{mois}\/entreprise\/#{siren}?.*token=/)
.to_return(body: effectifs_mensuels_body, status: effectifs_mensuels_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_annuels_acoss_covid\/#{siren}?.*token=/)
.to_return(body: effectifs_annuels_body, status: effectifs_annuels_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
.to_return(body: attestation_sociale_body, status: attestation_sociale_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
.to_return(body: attestation_sociale_body, status: attestation_sociale_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=/)
.to_return(body: attestation_fiscale_body, status: attestation_fiscale_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=/)
.to_return(body: bilans_bdf_body, status: bilans_bdf_status)
end end
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
after { Timecop.return }
let(:siren) { '418166096' }
let(:siret) { '41816609600051' } let(:siret) { '41816609600051' }
let(:rna) { 'W595001988' }
let(:entreprises_status) { 200 }
let(:entreprises_body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
let(:etablissements_status) { 200 } let(:etablissements_status) { 200 }
let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') } let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
let(:effectifs_mensuels_status) { 200 }
let(:effectifs_mensuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs.json') }
let(:annee) { "2020" }
let(:mois) { "02" }
let(:effectif_mensuel) { 100.5 }
let(:effectifs_annuels_status) { 200 }
let(:effectifs_annuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs_annuels.json') }
let(:effectif_annuel) { 100.5 }
let(:attestation_sociale_status) { 200 }
let(:attestation_sociale_body) { File.read('spec/fixtures/files/api_entreprise/attestation_sociale.json') }
let(:attestation_sociale_url) { "https://storage.entreprise.api.gouv.fr/siade/1569156881-f749d75e2bfd443316e2e02d59015f-attestation_vigilance_acoss.pdf" }
let(:attestation_fiscale_status) { 200 }
let(:attestation_fiscale_body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') }
let(:attestation_fiscale_url) { "https://storage.entreprise.api.gouv.fr/siade/1569156756-f6b7779f99fa95cd60dc03c04fcb-attestation_fiscale_dgfip.pdf" }
let(:bilans_bdf_status) { 200 }
let(:bilans_bdf_body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
let(:bilans_bdf) { JSON.parse(bilans_bdf_body, symbolize_names: true)[:bilans] }
let(:exercices_status) { 200 }
let(:exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') }
let(:associations_status) { 200 }
let(:associations_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
let(:procedure) { create(:procedure, api_entreprise_token: 'un-jeton') } let(:procedure) { create(:procedure, api_entreprise_token: 'un-jeton') }
let(:result) { ApiEntrepriseService.get_etablissement_params_for_siret(siret, procedure.id) } let(:dossier) { create(:dossier, procedure: procedure) }
let(:subject) { ApiEntrepriseService.create_etablissement(dossier, siret, procedure.id) }
before do before do
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles) allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return([])
.and_return(["attestations_sociales", "attestations_fiscales", "bilans_entreprise_bdf"])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end end
context 'when service is up' do context 'when service is up' do
it 'should fetch etablissement params' do it 'should fetch etablissement params' do
expect(result[:entreprise_siren]).to eq(siren) expect(subject[:siret]).to eq(siret)
expect(result[:siret]).to eq(siret)
expect(result[:association_rna]).to eq(rna)
expect(result[:exercices_attributes]).to_not be_empty
expect(result[:entreprise_effectif_mensuel]).to eq(effectif_mensuel)
expect(result[:entreprise_effectif_annuel]).to eq(effectif_annuel)
expect(result[:entreprise_attestation_sociale_url]).to eq(attestation_sociale_url)
expect(result[:entreprise_attestation_fiscale_url]).to eq(attestation_fiscale_url)
expect(result[:entreprise_bilans_bdf]).to eq(bilans_bdf)
end end
end
context 'when exercices api down' do [
let(:exercices_status) { 400 } ApiEntreprise::EntrepriseJob, ApiEntreprise::AssociationJob, ApiEntreprise::ExercicesJob,
let(:exercices_body) { '' } ApiEntreprise::EffectifsJob, ApiEntreprise::EffectifsAnnuelsJob, ApiEntreprise::AttestationSocialeJob,
ApiEntreprise::BilansBdfJob
it 'should fetch etablissement params' do ].each do |job|
expect(result[:entreprise_siren]).to eq(siren) it "should enqueue #{job.class.name}" do
expect(result[:siret]).to eq(siret) expect { subject }.to have_enqueued_job(job)
expect(result[:exercices_attributes]).to be_nil end
end
end
context 'when associations api down' do
let(:associations_status) { 400 }
let(:associations_body) { '' }
it 'should fetch etablissement params' do
expect(result[:entreprise_siren]).to eq(siren)
expect(result[:siret]).to eq(siret)
expect(result[:association_rna]).to be_nil
end
end
context 'when entreprise api down' do
let(:entreprises_status) { 400 }
let(:entreprises_body) { '' }
it 'should raise ApiEntreprise::API::RequestFailed' do
expect { result }.to raise_error(ApiEntreprise::API::RequestFailed)
end end
end end
context 'when etablissement api down' do context 'when etablissement api down' do
let(:etablissements_status) { 400 } let(:etablissements_status) { 504 }
let(:etablissements_body) { '' } let(:etablissements_body) { '' }
it 'should raise ApiEntreprise::API::RequestFailed' do it 'should raise ApiEntreprise::API::RequestFailed' do
expect { result }.to raise_error(ApiEntreprise::API::RequestFailed) expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
end end
end end
@ -132,16 +47,7 @@ describe ApiEntrepriseService do
let(:etablissements_body) { '' } let(:etablissements_body) { '' }
it 'should return nil' do it 'should return nil' do
expect(result).to be_nil expect(subject).to be_nil
end
end
context 'when entreprise not found' do
let(:etablissements_status) { 404 }
let(:etablissements_body) { '' }
it 'should return nil' do
expect(result).to be_nil
end end
end end
end end

View file

@ -6,27 +6,16 @@ describe 'users/dossiers/etablissement.html.haml', type: :view do
before do before do
sign_in dossier.user sign_in dossier.user
assign(:dossier, dossier) assign(:dossier, dossier)
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return([])
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
end end
subject! { render } subject! { render }
it 'affiche les informations de létablissement' do it 'affiche les informations de létablissement' do
expect(rendered).to have_text(etablissement.entreprise_raison_sociale)
expect(rendered).to have_text(etablissement.siret) expect(rendered).to have_text(etablissement.siret)
end end
it 'naffiche pas publiquement les derniers exercices comptables' do
expect(rendered).not_to have_text(number_to_currency(etablissement.exercices.first.ca))
end
context 'quand létablissement est une association' do
let(:etablissement) { create(:etablissement, :is_association) }
it 'affiche les informations de lassociation' do
expect(rendered).to have_text(etablissement.association_titre)
end
end
context 'etablissement avec infos non diffusables' do context 'etablissement avec infos non diffusables' do
let(:etablissement) { create(:etablissement, :with_exercices, :non_diffusable) } let(:etablissement) { create(:etablissement, :with_exercices, :non_diffusable) }
it "affiche uniquement le nom de l'établissement si infos non diffusables" do it "affiche uniquement le nom de l'établissement si infos non diffusables" do