2020-08-05 18:40:47 +02:00
|
|
|
describe APIEntreprise::API do
|
2020-04-27 15:58:04 +02:00
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:procedure_id) { procedure.id }
|
|
|
|
let(:token) { Rails.application.secrets.api_entreprise[:key] }
|
2019-04-30 16:36:40 +02:00
|
|
|
|
2015-08-17 15:53:35 +02:00
|
|
|
describe '.entreprise' do
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure_id).entreprise(siren) }
|
2019-04-30 16:36:40 +02:00
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
before do
|
2023-05-25 11:15:26 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/)
|
|
|
|
.to_return(body: body, status: status)
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
2019-04-30 16:36:40 +02:00
|
|
|
|
2020-12-02 17:17:00 +01:00
|
|
|
context 'when the service throws a bad gateaway exception' do
|
2019-04-30 16:36:40 +02:00
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 502 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_unavailable.json').read }
|
2019-04-30 16:36:40 +02:00
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
it 'raises APIEntreprise::API::Error::RequestFailed' do
|
|
|
|
expect { subject }.to raise_error(APIEntreprise::API::Error::BadGateway)
|
2019-04-30 16:36:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-17 15:53:35 +02:00
|
|
|
context 'when siren does not exist' do
|
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 404 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_not_found.json').read }
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
it 'raises APIEntreprise::API::Error::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(APIEntreprise::API::Error::ResourceNotFound)
|
2015-08-17 15:53:35 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
2019-04-30 16:36:40 +02:00
|
|
|
|
2020-05-19 10:43:49 +02:00
|
|
|
context 'when request has bad format' do
|
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 400 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_not_found.json').read }
|
2020-05-19 10:43:49 +02:00
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
it 'raises APIEntreprise::API::Error::BadFormatRequest' do
|
|
|
|
expect { subject }.to raise_error(APIEntreprise::API::Error::BadFormatRequest)
|
2020-05-19 10:43:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-12 11:39:49 +02:00
|
|
|
context 'when siren infos are private' do
|
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 403 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises_private.json').read }
|
2019-06-12 11:39:49 +02:00
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
it 'raises APIEntreprise::API::Error::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(APIEntreprise::API::Error::ResourceNotFound)
|
2019-06-12 11:39:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siren exist' do
|
2015-08-17 15:53:35 +02:00
|
|
|
let(:siren) { '418166096' }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/entreprises.json').read }
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2015-08-17 15:53:35 +02:00
|
|
|
it 'returns response body' do
|
2018-03-15 14:23:57 +01:00
|
|
|
expect(subject).to eq(JSON.parse(body, symbolize_names: true))
|
2015-08-17 15:53:35 +02:00
|
|
|
end
|
2020-04-27 15:58:04 +02:00
|
|
|
|
|
|
|
context 'with specific token for procedure' do
|
2020-07-08 17:00:21 +02:00
|
|
|
let(:token) { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" }
|
2020-04-27 15:58:04 +02:00
|
|
|
let(:procedure) { create(:procedure, api_entreprise_token: token) }
|
|
|
|
let(:procedure_id) { procedure.id }
|
|
|
|
|
|
|
|
it 'call api-entreprise with specfic token' do
|
|
|
|
subject
|
2023-05-25 11:15:26 +02:00
|
|
|
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/)
|
2020-04-27 15:58:04 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without specific token for procedure' do
|
|
|
|
let(:procedure) { create(:procedure, api_entreprise_token: nil) }
|
|
|
|
let(:procedure_id) { procedure.id }
|
|
|
|
|
|
|
|
it 'call api-entreprise with specfic token' do
|
|
|
|
subject
|
2023-05-25 11:15:26 +02:00
|
|
|
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/)
|
2020-04-27 15:58:04 +02:00
|
|
|
end
|
|
|
|
end
|
2023-07-18 18:13:18 +02:00
|
|
|
|
|
|
|
context 'with a service without siret' do
|
|
|
|
let(:procedure) { create(:procedure, :with_service) }
|
|
|
|
let(:dinum_siret) { "13002526500013" }
|
|
|
|
it 'send default recipient' do
|
|
|
|
ENV["API_ENTREPRISE_DEFAULT_SIRET"] = dinum_siret
|
|
|
|
procedure.service.siret = nil
|
|
|
|
procedure.service.save(validate: false)
|
|
|
|
subject
|
|
|
|
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/).with(query: hash_including({ recipient: dinum_siret }))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a service with siret' do
|
|
|
|
let(:procedure) { create(:procedure, :with_service) }
|
|
|
|
it 'send default recipient' do
|
|
|
|
subject
|
|
|
|
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/).with(query: hash_including({ recipient: procedure.service.siret }))
|
|
|
|
end
|
|
|
|
end
|
2015-08-17 15:53:35 +02:00
|
|
|
end
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2015-08-17 15:53:35 +02:00
|
|
|
describe '.etablissement' do
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure_id).etablissement(siret) }
|
2015-08-10 11:05:06 +02:00
|
|
|
before do
|
2023-05-30 16:27:20 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/etablissements\/#{siret}?.*non_diffusables=true/)
|
2018-01-15 19:34:08 +01:00
|
|
|
.to_return(status: status, body: body)
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2015-08-17 15:53:35 +02:00
|
|
|
context 'when siret does not exist' do
|
|
|
|
let(:siret) { '11111111111111' }
|
|
|
|
let(:status) { 404 }
|
|
|
|
let(:body) { '' }
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
it 'raises APIEntreprise::API::Error::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(APIEntreprise::API::Error::ResourceNotFound)
|
2015-08-17 15:53:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siret exists' do
|
|
|
|
let(:siret) { '41816609600051' }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/etablissements.json').read }
|
2015-08-17 15:53:35 +02:00
|
|
|
|
|
|
|
it 'returns body' do
|
2018-03-15 14:23:57 +01:00
|
|
|
expect(subject).to eq(JSON.parse(body, symbolize_names: true))
|
2015-08-17 15:53:35 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
end
|
2015-11-16 11:23:29 +01:00
|
|
|
|
|
|
|
describe '.exercices' do
|
|
|
|
before do
|
2023-05-29 16:45:21 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/dgfip\/etablissements\/#{siret}\/chiffres_affaires/)
|
2018-01-15 19:34:08 +01:00
|
|
|
.to_return(status: status, body: body)
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
2015-11-16 11:23:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siret does not exist' do
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure_id).exercices(siret) }
|
2015-11-16 11:23:29 +01:00
|
|
|
|
|
|
|
let(:siret) { '11111111111111' }
|
|
|
|
let(:status) { 404 }
|
|
|
|
let(:body) { '' }
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
it 'raises APIEntreprise::API::Error::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(APIEntreprise::API::Error::ResourceNotFound)
|
2015-11-16 11:23:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siret exists' do
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure_id).exercices(siret) }
|
2015-11-16 11:23:29 +01:00
|
|
|
|
|
|
|
let(:siret) { '41816609600051' }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/exercices.json').read }
|
2015-11-16 11:23:29 +01:00
|
|
|
|
2020-03-05 13:50:38 +01:00
|
|
|
it 'success' do
|
2018-03-15 14:23:57 +01:00
|
|
|
expect(subject).to eq(JSON.parse(body, symbolize_names: true))
|
2015-11-16 11:23:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-11 10:36:16 +01:00
|
|
|
|
|
|
|
describe '.rna' do
|
|
|
|
before do
|
2023-05-30 11:32:59 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{siren}/)
|
2018-01-15 19:34:08 +01:00
|
|
|
.to_return(status: status, body: body)
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure_id).rna(siren) }
|
2015-12-11 10:36:16 +01:00
|
|
|
|
|
|
|
context 'when siren does not exist' do
|
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 404 }
|
|
|
|
let(:body) { '' }
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
it 'raises APIEntreprise::API::Error::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(APIEntreprise::API::Error::ResourceNotFound)
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siren exists' do
|
|
|
|
let(:siren) { '418166096' }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/associations.json').read }
|
2015-12-11 10:36:16 +01:00
|
|
|
|
2018-03-15 14:23:57 +01:00
|
|
|
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
end
|
2020-04-28 10:15:08 +02:00
|
|
|
|
|
|
|
describe '.attestation_sociale' do
|
|
|
|
let(:procedure) { create(:procedure, api_entreprise_token: token) }
|
|
|
|
let(:siren) { '418166096' }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/attestation_sociale.json').read }
|
2020-04-28 10:15:08 +02:00
|
|
|
|
|
|
|
before do
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return(roles)
|
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
2023-05-22 14:56:40 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/urssaf\/unites_legales\/#{siren}\/attestation_vigilance/)
|
2020-04-28 10:15:08 +02:00
|
|
|
.to_return(body: body, status: status)
|
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure.id).attestation_sociale(siren) }
|
2020-04-28 10:15:08 +02:00
|
|
|
|
|
|
|
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
|
2020-04-29 17:34:54 +02:00
|
|
|
|
|
|
|
describe '.attestation_fiscale' do
|
|
|
|
let(:procedure) { create(:procedure, api_entreprise_token: token) }
|
|
|
|
let(:siren) { '418166096' }
|
|
|
|
let(:user_id) { 1 }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/attestation_fiscale.json').read }
|
2020-04-29 17:34:54 +02:00
|
|
|
|
|
|
|
before do
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return(roles)
|
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
2023-05-28 18:03:45 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/dgfip\/unites_legales\/#{siren}\/attestation_fiscale/)
|
2020-04-29 17:34:54 +02:00
|
|
|
.to_return(body: body, status: status)
|
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure.id).attestation_fiscale(siren, user_id) }
|
2020-04-29 17:34:54 +02:00
|
|
|
|
|
|
|
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
|
2020-04-30 08:18:17 +02:00
|
|
|
|
|
|
|
describe '.bilans_bdf' do
|
|
|
|
let(:procedure) { create(:procedure, api_entreprise_token: token) }
|
|
|
|
let(:siren) { '418166096' }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json').read }
|
2020-04-30 08:18:17 +02:00
|
|
|
|
|
|
|
before do
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return(roles)
|
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
2023-05-31 16:07:04 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/banque_de_france\/unites_legales\/#{siren}\/bilans/)
|
2020-04-30 08:18:17 +02:00
|
|
|
.to_return(body: body, status: status)
|
|
|
|
end
|
|
|
|
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure.id).bilans_bdf(siren) }
|
2020-04-30 08:18:17 +02:00
|
|
|
|
|
|
|
context 'when token not authorized' do
|
|
|
|
let(:roles) { ["entreprises"] }
|
|
|
|
|
|
|
|
it { expect(subject).to eq(nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is authorized' do
|
|
|
|
let(:roles) { ["bilans_entreprise_bdf"] }
|
|
|
|
|
|
|
|
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
|
|
|
|
end
|
|
|
|
end
|
2020-05-05 16:06:18 +02:00
|
|
|
|
2022-07-20 15:22:18 +02:00
|
|
|
describe '.privileges' do
|
|
|
|
let(:api) { described_class.new }
|
|
|
|
let(:status) { 200 }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/privileges.json').read }
|
2022-07-20 15:22:18 +02:00
|
|
|
subject { api.privileges }
|
|
|
|
|
|
|
|
before do
|
|
|
|
api.token = token
|
|
|
|
|
|
|
|
stub_request(:get, "https://entreprise.api.gouv.fr/v2/privileges")
|
|
|
|
.to_return(body: body, status: status)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is authorized' do
|
|
|
|
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-05 16:06:18 +02:00
|
|
|
describe 'with expired token' do
|
|
|
|
let(:siren) { '111111111' }
|
2022-07-20 14:52:17 +02:00
|
|
|
subject { described_class.new(procedure_id).entreprise(siren) }
|
2020-05-05 16:06:18 +02:00
|
|
|
|
|
|
|
before do
|
2020-08-05 18:40:47 +02:00
|
|
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(true)
|
2020-05-05 16:06:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes no call to api-entreprise' do
|
2022-07-12 19:06:20 +02:00
|
|
|
expect { subject }.to raise_error(APIEntrepriseToken::TokenError)
|
2020-12-10 15:28:39 +01:00
|
|
|
expect(WebMock).not_to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/)
|
2020-05-05 16:06:18 +02:00
|
|
|
end
|
|
|
|
end
|
2022-09-15 17:49:57 +02:00
|
|
|
|
|
|
|
describe 'current_status' do
|
|
|
|
subject { described_class.new.current_status }
|
2023-04-27 17:17:38 +02:00
|
|
|
let(:body) { Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read }
|
2022-09-15 17:49:57 +02:00
|
|
|
|
|
|
|
before do
|
2023-04-27 17:17:38 +02:00
|
|
|
stub_request(:get, "https://status.entreprise.api.gouv.fr/summary.json")
|
2022-09-15 17:49:57 +02:00
|
|
|
.to_return(body: body)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the current status response" do
|
|
|
|
expect(subject).to eq(JSON.parse(body, symbolize_names: true))
|
|
|
|
end
|
|
|
|
end
|
2015-08-20 17:30:17 +02:00
|
|
|
end
|