fetch attestation sociale url

This commit is contained in:
Christophe Robillard 2020-04-28 10:15:08 +02:00
parent b147956a92
commit d74b14c205
7 changed files with 53 additions and 2 deletions

View file

@ -42,6 +42,7 @@ gem 'groupdate'
gem 'haml-rails' gem 'haml-rails'
gem 'hashie' gem 'hashie'
gem 'jquery-rails' # Use jquery as the JavaScript library gem 'jquery-rails' # Use jquery as the JavaScript library
gem 'jwt'
gem 'kaminari', '= 1.1.1' # Pagination gem 'kaminari', '= 1.1.1' # Pagination
gem 'lograge' gem 'lograge'
gem 'logstash-event' gem 'logstash-event'

View file

@ -775,6 +775,7 @@ DEPENDENCIES
haml-rails haml-rails
hashie hashie
jquery-rails jquery-rails
jwt
kaminari (= 1.1.1) kaminari (= 1.1.1)
launchy launchy
letter_opener_web letter_opener_web

View file

@ -41,7 +41,8 @@ class ApiEntreprise::API
end end
def self.attestation_sociale(siren, procedure_id) def self.attestation_sociale(siren, procedure_id)
call(ATTESTATION_SOCIALE_RESOURCE_NAME, siren, procedure_id) procedure = Procedure.find(procedure_id)
call(ATTESTATION_SOCIALE_RESOURCE_NAME, siren, procedure_id) if procedure.api_entreprise_role?("attestations_sociales")
end end
private private

View file

@ -547,6 +547,14 @@ class Procedure < ApplicationRecord
"Procedure;#{id}" "Procedure;#{id}"
end end
def api_entreprise_roles
JWT.decode(api_entreprise_token, nil, false)[0]["roles"] if api_entreprise_token.present?
end
def api_entreprise_role?(role)
api_entreprise_roles&.include?(role)
end
private private
def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index) def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index)

View file

@ -35,6 +35,12 @@ class ApiEntrepriseService
rescue ApiEntreprise::API::RequestFailed rescue ApiEntreprise::API::RequestFailed
end 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
etablissement_params.merge(entreprise_params) etablissement_params.merge(entreprise_params)
end end
end end

View file

@ -158,4 +158,31 @@ describe ApiEntreprise::API do
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) } it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end end
end end
describe '.attestation_sociale' do
let(:procedure) { create(:procedure, api_entreprise_token: token) }
let(:siren) { '418166096' }
let(:status) { 200 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_sociale.json') }
before do
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles).and_return(roles)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
.to_return(body: body, status: status)
end
subject { described_class.attestation_sociale(siren, procedure.id) }
context 'when token not authorized' do
let(:roles) { ["entreprises"] }
it { expect(subject).to eq(nil) }
end
context 'when token is authorized' do
let(:roles) { ["attestations_sociales"] }
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end
end
end end

View file

@ -13,6 +13,8 @@ describe ApiEntrepriseService do
.to_return(body: effectifs_mensuels_body, status: effectifs_mensuels_status) .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=/) 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) .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)
end end
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) } before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
@ -38,13 +40,17 @@ describe ApiEntrepriseService do
let(:effectifs_annuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs_annuels.json') } let(:effectifs_annuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs_annuels.json') }
let(:effectif_annuel) { 100.5 } 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(:exercices_status) { 200 } let(:exercices_status) { 200 }
let(:exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') } let(:exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') }
let(:associations_status) { 200 } let(:associations_status) { 200 }
let(:associations_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') } let(:associations_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure, api_entreprise_token: 'un-jeton') }
let(:result) { ApiEntrepriseService.get_etablissement_params_for_siret(siret, procedure.id) } let(:result) { ApiEntrepriseService.get_etablissement_params_for_siret(siret, procedure.id) }
context 'when service is up' do context 'when service is up' do
@ -55,6 +61,7 @@ describe ApiEntrepriseService do
expect(result[:exercices_attributes]).to_not be_empty expect(result[:exercices_attributes]).to_not be_empty
expect(result[:entreprise_effectif_mensuel]).to eq(effectif_mensuel) expect(result[:entreprise_effectif_mensuel]).to eq(effectif_mensuel)
expect(result[:entreprise_effectif_annuel]).to eq(effectif_annuel) expect(result[:entreprise_effectif_annuel]).to eq(effectif_annuel)
expect(result[:entreprise_attestation_sociale_url]).to eq(attestation_sociale_url)
end end
end end