rna champ can fetch it's own association
This commit is contained in:
parent
899bb73ce8
commit
7d65a34bc6
3 changed files with 108 additions and 0 deletions
|
@ -21,6 +21,8 @@
|
||||||
# row_id :string
|
# row_id :string
|
||||||
#
|
#
|
||||||
class Champs::RNAChamp < Champ
|
class Champs::RNAChamp < Champ
|
||||||
|
include RNAChampAssociationFetchableConcern
|
||||||
|
|
||||||
validates :value, allow_blank: true, format: {
|
validates :value, allow_blank: true, format: {
|
||||||
with: /\AW[0-9]{9}\z/, message: I18n.t(:not_a_rna, scope: 'activerecord.errors.messages')
|
with: /\AW[0-9]{9}\z/, message: I18n.t(:not_a_rna, scope: 'activerecord.errors.messages')
|
||||||
}, if: -> { validation_context != :brouillon }
|
}, if: -> { validation_context != :brouillon }
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
module RNAChampAssociationFetchableConcern
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
def fetch_association!(rna)
|
||||||
|
data = APIEntreprise::RNAAdapter.new(rna, procedure_id).to_params
|
||||||
|
update!(data: data, value: rna)
|
||||||
|
nil
|
||||||
|
rescue APIEntreprise::API::Error, ActiveRecord::RecordInvalid => error
|
||||||
|
update(data: nil, value: nil)
|
||||||
|
if error.try(:network_error?) && !APIEntrepriseService.api_up?
|
||||||
|
:network_error
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,90 @@
|
||||||
|
RSpec.describe RNAChampAssociationFetchableConcern do
|
||||||
|
describe '.fetch_association!' do
|
||||||
|
let!(:champ) { create(:champ_rna, data: "not nil data") }
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//)
|
||||||
|
.to_return(body: body, status: status)
|
||||||
|
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
subject(:fetch_association!) { champ.fetch_association!(rna) }
|
||||||
|
|
||||||
|
shared_examples "an association fetcher" do |expected_result, expected_value, expected_data|
|
||||||
|
it { expect(fetch_association!).to eq(expected_result) }
|
||||||
|
|
||||||
|
it { expect { fetch_association! }.to change { champ.reload.value }.to(expected_value) }
|
||||||
|
|
||||||
|
it { expect { fetch_association! }.to change { champ.reload.data }.to(expected_data) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the RNA is empty' do
|
||||||
|
let(:rna) { '' }
|
||||||
|
let(:status) { 422 }
|
||||||
|
let(:body) { '' }
|
||||||
|
|
||||||
|
it_behaves_like "an association fetcher", nil, '', {}
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the RNA is invalid' do
|
||||||
|
let(:rna) { '1234' }
|
||||||
|
let(:status) { 422 }
|
||||||
|
let(:body) { '' }
|
||||||
|
|
||||||
|
it_behaves_like "an association fetcher", nil, nil, nil
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the RNA is unknow' do
|
||||||
|
let(:rna) { 'W111111111' }
|
||||||
|
let(:status) { 404 }
|
||||||
|
let(:body) { '' }
|
||||||
|
|
||||||
|
it_behaves_like "an association fetcher", nil, 'W111111111', {}
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the API is unavailable due to network error' do
|
||||||
|
let(:rna) { 'W595001988' }
|
||||||
|
let(:status) { 503 }
|
||||||
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||||
|
|
||||||
|
before { expect(APIEntrepriseService).to receive(:api_up?).and_return(false) }
|
||||||
|
|
||||||
|
it_behaves_like "an association fetcher", :network_error, nil, nil
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the RNA informations are retrieved successfully' do
|
||||||
|
let(:rna) { 'W595001988' }
|
||||||
|
let(:status) { 200 }
|
||||||
|
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||||
|
|
||||||
|
it_behaves_like "an association fetcher", nil, 'W595001988', {
|
||||||
|
"association_id" => "W595001988",
|
||||||
|
"association_titre" => "UN SUR QUATRE",
|
||||||
|
"association_objet" => "valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement",
|
||||||
|
"association_siret" => nil,
|
||||||
|
"association_date_creation" => "2014-01-23",
|
||||||
|
"association_date_declaration" => "2014-01-24",
|
||||||
|
"association_date_publication" => "2014-02-08",
|
||||||
|
"association_date_dissolution" => "0001-01-01",
|
||||||
|
"association_adresse_siege" => {
|
||||||
|
"complement" => "",
|
||||||
|
"numero_voie" => "61",
|
||||||
|
"type_voie" => "RUE",
|
||||||
|
"libelle_voie" => "des Noyers",
|
||||||
|
"distribution" => "_",
|
||||||
|
"code_insee" => "93063",
|
||||||
|
"code_postal" => "93230",
|
||||||
|
"commune" => "Romainville"
|
||||||
|
},
|
||||||
|
"association_code_civilite_dirigeant" => "PM",
|
||||||
|
"association_civilite_dirigeant" => "Monsieur le Président",
|
||||||
|
"association_code_etat" => "A",
|
||||||
|
"association_etat" => "Active",
|
||||||
|
"association_code_groupement" => "S",
|
||||||
|
"association_groupement" => "simple",
|
||||||
|
"association_mise_a_jour" => 1392295833,
|
||||||
|
"association_rna" => "W595001988"
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue