create attestation_fiscale_adapter

This commit is contained in:
Christophe Robillard 2020-04-29 17:34:54 +02:00
parent 15d8155f59
commit 38c68b16e3
5 changed files with 93 additions and 4 deletions

View file

@ -6,6 +6,7 @@ class ApiEntreprise::API
EFFECTIFS_RESOURCE_NAME = "effectifs_mensuels_acoss_covid"
EFFECTIFS_ANNUELS_RESOURCE_NAME = "effectifs_annuels_acoss_covid"
ATTESTATION_SOCIALE_RESOURCE_NAME = "attestations_sociales_acoss"
ATTESTATION_FISCALE_RESOURCE_NAME = "attestations_fiscales_dgfip"
TIMEOUT = 15
@ -45,11 +46,16 @@ class ApiEntreprise::API
call(ATTESTATION_SOCIALE_RESOURCE_NAME, siren, procedure_id) if procedure.api_entreprise_role?("attestations_sociales")
end
def self.attestation_fiscale(siren, procedure_id, user_id)
procedure = Procedure.find(procedure_id)
call(ATTESTATION_FISCALE_RESOURCE_NAME, siren, procedure_id, user_id) if procedure.api_entreprise_role?("attestations_fiscales")
end
private
def self.call(resource_name, siret_or_siren, procedure_id)
def self.call(resource_name, siret_or_siren, procedure_id, user_id = nil)
url = url(resource_name, siret_or_siren)
params = params(siret_or_siren, procedure_id)
params = params(siret_or_siren, procedure_id, user_id)
response = Typhoeus.get(url,
params: params,
@ -74,14 +80,16 @@ class ApiEntreprise::API
base_url
end
def self.params(siret_or_siren, procedure_id)
{
def self.params(siret_or_siren, procedure_id, user_id)
params = {
context: "demarches-simplifiees.fr",
recipient: siret_or_siren,
object: "procedure_id: #{procedure_id}",
non_diffusables: true,
token: token_for_procedure(procedure_id)
}
params[:user_id] = user_id if user_id.present?
params
end
def self.token_for_procedure(procedure_id)

View file

@ -0,0 +1,23 @@
class ApiEntreprise::AttestationFiscaleAdapter < ApiEntreprise::Adapter
def initialize(siren, procedure_id, user_id)
@siren = siren
@procedure_id = procedure_id
@user_id = user_id
end
private
def get_resource
ApiEntreprise::API.attestation_fiscale(@siren, @procedure_id, @user_id)
end
def process_params
if data_source[:url].present?
{
entreprise_attestation_fiscale_url: data_source[:url]
}
else
{}
end
end
end

View file

@ -0,0 +1,4 @@
{
"url":
"https://storage.entreprise.api.gouv.fr/siade/1569156756-f6b7779f99fa95cd60dc03c04fcb-attestation_fiscale_dgfip.pdf"
}

View file

@ -185,4 +185,32 @@ describe ApiEntreprise::API do
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end
end
describe '.attestation_fiscale' do
let(:procedure) { create(:procedure, api_entreprise_token: token) }
let(:siren) { '418166096' }
let(:user_id) { 1 }
let(:status) { 200 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.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_fiscales_dgfip\/#{siren}?.*token=#{token}&user_id=#{user_id}/)
.to_return(body: body, status: status)
end
subject { described_class.attestation_fiscale(siren, procedure.id, user_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_fiscales"] }
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end
end
end

View file

@ -0,0 +1,26 @@
describe ApiEntreprise::AttestationFiscaleAdapter do
let(:siren) { '418166096' }
let(:procedure) { create(:procedure) }
let(:user_id) { 1 }
let(:adapter) { described_class.new(siren, procedure.id, user_id) }
subject { adapter.to_params }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles).and_return(["attestations_fiscales"])
end
context "when the SIREN is valid" do
let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') }
let(:status) { 200 }
it '#to_params class est une Hash ?' do
expect(subject).to be_an_instance_of(Hash)
end
it "returns url of attestation_fiscale" do
expect(subject[:entreprise_attestation_fiscale_url]).to eq("https://storage.entreprise.api.gouv.fr/siade/1569156756-f6b7779f99fa95cd60dc03c04fcb-attestation_fiscale_dgfip.pdf")
end
end
end