From 7d65a34bc6ce4a665f6fa7f397475175b5e55950 Mon Sep 17 00:00:00 2001 From: sebastiencarceles Date: Fri, 13 Jan 2023 13:47:17 +0100 Subject: [PATCH] rna champ can fetch it's own association --- app/models/champs/rna_champ.rb | 2 + ...rna_champ_association_fetchable_concern.rb | 16 ++++ ...hamp_association_fetchable_concern_spec.rb | 90 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 app/models/concerns/rna_champ_association_fetchable_concern.rb create mode 100644 spec/models/concern/rna_champ_association_fetchable_concern_spec.rb diff --git a/app/models/champs/rna_champ.rb b/app/models/champs/rna_champ.rb index bd1f08096..781e21014 100644 --- a/app/models/champs/rna_champ.rb +++ b/app/models/champs/rna_champ.rb @@ -21,6 +21,8 @@ # row_id :string # class Champs::RNAChamp < Champ + include RNAChampAssociationFetchableConcern + validates :value, allow_blank: true, format: { with: /\AW[0-9]{9}\z/, message: I18n.t(:not_a_rna, scope: 'activerecord.errors.messages') }, if: -> { validation_context != :brouillon } diff --git a/app/models/concerns/rna_champ_association_fetchable_concern.rb b/app/models/concerns/rna_champ_association_fetchable_concern.rb new file mode 100644 index 000000000..f6ffbadac --- /dev/null +++ b/app/models/concerns/rna_champ_association_fetchable_concern.rb @@ -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 diff --git a/spec/models/concern/rna_champ_association_fetchable_concern_spec.rb b/spec/models/concern/rna_champ_association_fetchable_concern_spec.rb new file mode 100644 index 000000000..74c7a5fa6 --- /dev/null +++ b/spec/models/concern/rna_champ_association_fetchable_concern_spec.rb @@ -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