2018-03-15 16:56:21 +01: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
|
2018-03-15 12:02:45 +01:00
|
|
|
subject { described_class.entreprise(siren, procedure_id) }
|
2019-04-30 16:36:40 +02:00
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
before do
|
2020-04-27 15:58:04 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
|
2018-01-15 19:34:08 +01:00
|
|
|
.to_return(status: status, body: body)
|
2020-05-05 16:06:18 +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
|
|
|
|
|
|
|
context 'when the service is unavailable' do
|
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 502 }
|
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_unavailable.json') }
|
|
|
|
|
2020-03-05 13:50:38 +01:00
|
|
|
it 'raises ApiEntreprise::API::RequestFailed' do
|
|
|
|
expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
|
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 }
|
2019-04-30 16:36:40 +02:00
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_not_found.json') }
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2020-03-05 13:50:38 +01:00
|
|
|
it 'raises ApiEntreprise::API::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(ApiEntreprise::API::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
|
|
|
|
2019-06-12 11:39:49 +02:00
|
|
|
context 'when siren infos are private' do
|
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 403 }
|
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises_private.json') }
|
|
|
|
|
2020-03-05 13:50:38 +01:00
|
|
|
it 'raises ApiEntreprise::API::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(ApiEntreprise::API::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 }
|
2018-10-16 00:01:53 +02:00
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
|
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
|
|
|
|
let(:token) { 'token-for-demarche' }
|
|
|
|
let(:procedure) { create(:procedure, api_entreprise_token: token) }
|
|
|
|
let(:procedure_id) { procedure.id }
|
|
|
|
|
|
|
|
it 'call api-entreprise with specfic token' do
|
|
|
|
subject
|
|
|
|
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=token-for-demarche/)
|
|
|
|
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
|
|
|
|
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
|
|
|
|
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
|
2018-03-15 12:02:45 +01:00
|
|
|
subject { described_class.etablissement(siret, procedure_id) }
|
2015-08-10 11:05:06 +02:00
|
|
|
before do
|
2020-03-04 17:12:50 +01:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*non_diffusables=true&.*token=/)
|
2018-01-15 19:34:08 +01:00
|
|
|
.to_return(status: status, body: body)
|
2020-05-05 16:06:18 +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-03-05 13:50:38 +01:00
|
|
|
it 'raises ApiEntreprise::API::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
|
2015-08-17 15:53:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siret exists' do
|
|
|
|
let(:siret) { '41816609600051' }
|
|
|
|
let(:status) { 200 }
|
2018-10-16 00:01:53 +02:00
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
|
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
|
2018-05-09 12:03:04 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
|
2018-01-15 19:34:08 +01:00
|
|
|
.to_return(status: status, body: body)
|
2020-05-05 16:06:18 +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
|
2018-03-15 12:02:45 +01:00
|
|
|
subject { described_class.exercices(siret, procedure_id) }
|
2015-11-16 11:23:29 +01:00
|
|
|
|
|
|
|
let(:siret) { '11111111111111' }
|
|
|
|
let(:status) { 404 }
|
|
|
|
let(:body) { '' }
|
|
|
|
|
2020-03-05 13:50:38 +01:00
|
|
|
it 'raises ApiEntreprise::API::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
|
2015-11-16 11:23:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siret exists' do
|
2018-03-15 12:02:45 +01:00
|
|
|
subject { described_class.exercices(siret, procedure_id) }
|
2015-11-16 11:23:29 +01:00
|
|
|
|
|
|
|
let(:siret) { '41816609600051' }
|
|
|
|
let(:status) { 200 }
|
2018-10-16 00:01:53 +02:00
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') }
|
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
|
2018-05-09 12:03:04 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
|
2018-01-15 19:34:08 +01:00
|
|
|
.to_return(status: status, body: body)
|
2020-05-05 16:06:18 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
subject { described_class.rna(siren, procedure_id) }
|
2015-12-11 10:36:16 +01:00
|
|
|
|
|
|
|
context 'when siren does not exist' do
|
|
|
|
let(:siren) { '111111111' }
|
|
|
|
let(:status) { 404 }
|
|
|
|
let(:body) { '' }
|
|
|
|
|
2020-03-05 13:50:38 +01:00
|
|
|
it 'raises ApiEntreprise::API::ResourceNotFound' do
|
|
|
|
expect { subject }.to raise_error(ApiEntreprise::API::ResourceNotFound)
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when siren exists' do
|
|
|
|
let(:siren) { '418166096' }
|
|
|
|
let(:status) { 200 }
|
2018-10-16 00:01:53 +02:00
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
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 }
|
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_sociale.json') }
|
|
|
|
|
|
|
|
before do
|
2020-05-05 15:26:08 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
2020-05-05 16:06:18 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
2020-04-28 10:15:08 +02:00
|
|
|
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
|
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 }
|
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') }
|
|
|
|
|
|
|
|
before do
|
2020-05-05 15:26:08 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
2020-05-05 16:06:18 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
2020-04-29 17:34:54 +02:00
|
|
|
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
|
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 }
|
|
|
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
|
|
|
|
|
|
|
|
before do
|
2020-05-05 15:26:08 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return(roles)
|
2020-05-05 16:06:18 +02:00
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
2020-04-30 08:18:17 +02:00
|
|
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=#{token}/)
|
|
|
|
.to_return(body: body, status: status)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.bilans_bdf(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) { ["bilans_entreprise_bdf"] }
|
|
|
|
|
|
|
|
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' }
|
|
|
|
subject { described_class.entreprise(siren, procedure_id) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes no call to api-entreprise' do
|
|
|
|
subject
|
|
|
|
expect(WebMock).not_to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
|
|
|
|
end
|
|
|
|
end
|
2015-08-20 17:30:17 +02:00
|
|
|
end
|