2023-10-27 14:20:40 +02:00
|
|
|
|
describe Champs::RNFChamp, type: :model do
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:champ) { described_class.new(external_id:) }
|
2023-10-27 14:20:40 +02:00
|
|
|
|
let(:external_id) { '075-FDD-00003-01' }
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:body) { Rails.root.join('spec', 'fixtures', 'files', 'api_rnf', "#{response_type}.json").read }
|
2023-10-27 14:20:40 +02:00
|
|
|
|
let(:response_type) { 'valid' }
|
|
|
|
|
|
|
|
|
|
describe 'fetch_external_data' do
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:url) { RNFService.new.send(:url) }
|
|
|
|
|
let(:status) { 200 }
|
|
|
|
|
before { stub_request(:get, "#{url}/#{external_id}").to_return(body:, status:) }
|
|
|
|
|
|
|
|
|
|
subject { champ.fetch_external_data }
|
2023-10-27 14:20:40 +02:00
|
|
|
|
|
|
|
|
|
context 'success' do
|
|
|
|
|
it do
|
|
|
|
|
expect(subject.value!).to eq({
|
|
|
|
|
id: 3,
|
|
|
|
|
rnfId: '075-FDD-00003-01',
|
|
|
|
|
type: 'FDD',
|
|
|
|
|
department: '75',
|
|
|
|
|
title: 'Fondation SFR',
|
|
|
|
|
dissolvedAt: nil,
|
|
|
|
|
phone: '+33185060000',
|
|
|
|
|
email: 'fondation@sfr.fr',
|
|
|
|
|
addressId: 3,
|
|
|
|
|
createdAt: "2023-09-07T13:26:10.358Z",
|
|
|
|
|
updatedAt: "2023-09-07T13:26:10.358Z",
|
|
|
|
|
address: {
|
|
|
|
|
id: 3,
|
|
|
|
|
createdAt: "2023-09-07T13:26:10.358Z",
|
|
|
|
|
updatedAt: "2023-09-07T13:26:10.358Z",
|
|
|
|
|
label: "16 Rue du Général de Boissieu 75015 Paris",
|
|
|
|
|
type: "housenumber",
|
|
|
|
|
streetAddress: "16 Rue du Général de Boissieu",
|
|
|
|
|
streetNumber: "16",
|
|
|
|
|
streetName: "Rue du Général de Boissieu",
|
|
|
|
|
postalCode: "75015",
|
|
|
|
|
cityName: "Paris",
|
|
|
|
|
cityCode: "75115",
|
|
|
|
|
departmentName: "Paris",
|
|
|
|
|
departmentCode: "75",
|
|
|
|
|
regionName: "Île-de-France",
|
|
|
|
|
regionCode: "11"
|
|
|
|
|
},
|
|
|
|
|
status: nil,
|
|
|
|
|
persons: []
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'failure (schema)' do
|
|
|
|
|
let(:response_type) { 'invalid' }
|
|
|
|
|
it {
|
|
|
|
|
expect(subject.failure.retryable).to be_falsey
|
|
|
|
|
expect(subject.failure.reason).to be_a(API::Client::SchemaError)
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'failure (http 500)' do
|
|
|
|
|
let(:status) { 500 }
|
|
|
|
|
let(:response_type) { 'invalid' }
|
|
|
|
|
it {
|
|
|
|
|
expect(subject.failure.retryable).to be_truthy
|
|
|
|
|
expect(subject.failure.reason).to be_a(API::Client::HTTPError)
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'failure (http 401)' do
|
|
|
|
|
let(:status) { 401 }
|
|
|
|
|
let(:response_type) { 'invalid' }
|
|
|
|
|
it {
|
|
|
|
|
expect(subject.failure.retryable).to be_falsey
|
|
|
|
|
expect(subject.failure.reason).to be_a(API::Client::HTTPError)
|
|
|
|
|
}
|
|
|
|
|
end
|
2024-03-29 12:20:04 +01:00
|
|
|
|
|
|
|
|
|
context 'failure (http 400)' do
|
|
|
|
|
let(:status) { 400 }
|
|
|
|
|
let(:response_type) { 'invalid' }
|
|
|
|
|
it {
|
|
|
|
|
expect(subject.failure.retryable).to be_falsey
|
|
|
|
|
expect(subject.failure.reason).to be_a(API::Client::HTTPError)
|
|
|
|
|
}
|
|
|
|
|
end
|
2023-10-27 14:20:40 +02:00
|
|
|
|
end
|
2023-11-21 11:02:33 +01:00
|
|
|
|
|
|
|
|
|
describe 'for_export' do
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:champ) { described_class.new(external_id:, data: JSON.parse(body)) }
|
|
|
|
|
before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_rnf)) }
|
2023-11-21 11:02:33 +01:00
|
|
|
|
it do
|
2024-04-02 17:05:44 +02:00
|
|
|
|
expect(champ.for_export(:value)).to eq '075-FDD-00003-01'
|
|
|
|
|
expect(champ.for_export(:nom)).to eq 'Fondation SFR'
|
|
|
|
|
expect(champ.for_export(:address)).to eq '16 Rue du Général de Boissieu 75015 Paris'
|
|
|
|
|
expect(champ.for_export(:code_insee)).to eq '75115'
|
|
|
|
|
expect(champ.for_export(:departement)).to eq '75 – Paris'
|
2023-11-21 11:02:33 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2023-10-27 14:20:40 +02:00
|
|
|
|
end
|