allow draft to be saved with invalid cnaf champ

This commit is contained in:
simon lehericey 2021-10-06 12:03:12 +02:00
parent cd7bafaa0d
commit 87de9e38c6
4 changed files with 42 additions and 12 deletions

View file

@ -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

View file

@ -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

View file

@ -125,14 +125,23 @@ feature 'fetch API Particulier Data', js: true do
click_button('Continuer')
fill_in 'Le numéro dallocataire 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/)

View file

@ -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