From 87de9e38c620762458f7f523fecc84699d587a1a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 6 Oct 2021 12:03:12 +0200 Subject: [PATCH] allow draft to be saved with invalid cnaf champ --- app/controllers/users/dossiers_controller.rb | 14 +++++++++++++- app/models/champs/cnaf_champ.rb | 18 ++++++++++-------- .../api_particulier/api_particulier_spec.rb | 13 +++++++++++-- spec/models/champs/cnaf_champ_spec.rb | 9 ++++++++- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 31fcb4b82..721ddf54b 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -382,7 +382,8 @@ module Users if @dossier.champs.any?(&:changed_for_autosave?) @dossier.last_champ_updated_at = Time.zone.now end - if !@dossier.save + + if !@dossier.save(**validation_options) errors += @dossier.errors.full_messages elsif change_groupe_instructeur? @dossier.assign_to_groupe_instructeur(groupe_instructeur_from_params) @@ -453,5 +454,16 @@ module Users def save_draft? dossier.brouillon? && !params[:submit_draft] end + + def validation_options + if save_draft? + { context: :brouillon } + else + # rubocop:disable Lint/BooleanSymbol + # Force ActiveRecord to re-validate associated records. + { context: :false } + # rubocop:enable Lint/BooleanSymbol + end + end end end diff --git a/app/models/champs/cnaf_champ.rb b/app/models/champs/cnaf_champ.rb index 586430877..fc3c10744 100644 --- a/app/models/champs/cnaf_champ.rb +++ b/app/models/champs/cnaf_champ.rb @@ -20,8 +20,8 @@ # class Champs::CnafChamp < Champs::TextChamp # see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/cnaf-input-validation.middleware.ts - validates :numero_allocataire, format: { with: /\A\d{1,7}\z/ }, if: -> { code_postal.present? } - validates :code_postal, format: { with: /\A\w{5}\z/ }, if: -> { numero_allocataire.present? } + validates :numero_allocataire, format: { with: /\A\d{1,7}\z/ }, if: -> { code_postal.present? && validation_context != :brouillon } + validates :code_postal, format: { with: /\A\w{5}\z/ }, if: -> { numero_allocataire.present? && validation_context != :brouillon } store_accessor :value_json, :numero_allocataire, :code_postal @@ -34,12 +34,14 @@ class Champs::CnafChamp < Champs::TextChamp end def fetch_external_data - APIParticulier::CnafAdapter.new( - procedure.api_particulier_token, - numero_allocataire, - code_postal, - procedure.api_particulier_sources - ).to_params + if valid? + APIParticulier::CnafAdapter.new( + procedure.api_particulier_token, + numero_allocataire, + code_postal, + procedure.api_particulier_sources + ).to_params + end end def external_id diff --git a/spec/features/api_particulier/api_particulier_spec.rb b/spec/features/api_particulier/api_particulier_spec.rb index ce7b1cbf0..de793f453 100644 --- a/spec/features/api_particulier/api_particulier_spec.rb +++ b/spec/features/api_particulier/api_particulier_spec.rb @@ -125,14 +125,23 @@ feature 'fetch API Particulier Data', js: true do click_button('Continuer') fill_in 'Le numéro d’allocataire CAF', with: numero_allocataire + fill_in 'Le code postal', with: 'wrong_code' + + blur + expect(page).to have_css('span', text: 'Brouillon enregistré', visible: true) + + dossier = Dossier.last + expect(dossier.champs.first.code_postal).to eq('wrong_code') + + click_on 'Déposer le dossier' + expect(page).to have_content(/code postal doit posséder 5 caractères/) + fill_in 'Le code postal', with: code_postal VCR.use_cassette("api_particulier/success/composition_familiale") do perform_enqueued_jobs { click_on 'Déposer le dossier' } end - dossier = Dossier.last - visit demande_dossier_path(dossier) expect(page).to have_content(/Des données.*ont été reçues depuis la CAF/) diff --git a/spec/models/champs/cnaf_champ_spec.rb b/spec/models/champs/cnaf_champ_spec.rb index 924e9fffe..405c820a2 100644 --- a/spec/models/champs/cnaf_champ_spec.rb +++ b/spec/models/champs/cnaf_champ_spec.rb @@ -38,8 +38,9 @@ describe Champs::CnafChamp, type: :model do let(:numero_allocataire) { '1234567' } let(:code_postal) { '12345' } let(:champ) { described_class.new(dossier: create(:dossier), type_de_champ: create(:type_de_champ_cnaf)) } + let(:validation_context) { :create } - subject { champ.valid? } + subject { champ.valid?(validation_context) } before do champ.numero_allocataire = numero_allocataire @@ -82,6 +83,12 @@ describe Champs::CnafChamp, type: :model do is_expected.to be false expect(champ.errors.full_messages).to eq(["Numero allocataire doit être composé au maximum de 7 chiffres"]) end + + context 'and the validation_context is :brouillon' do + let(:validation_context) { :brouillon } + + it { is_expected.to be true } + end end context 'when code_postal is invalid' do