2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-09-24 14:21:30 +02:00
|
|
|
|
describe Champs::CnafChamp, type: :model do
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:champ) { described_class.new(dossier: build(:dossier)) }
|
2024-08-30 16:43:30 +02:00
|
|
|
|
before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_cnaf)) }
|
2021-09-24 14:21:30 +02:00
|
|
|
|
describe 'numero_allocataire and code_postal' do
|
|
|
|
|
before do
|
|
|
|
|
champ.numero_allocataire = '1234567'
|
|
|
|
|
champ.code_postal = '12345'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'saves numero_allocataire and code_postal' do
|
|
|
|
|
expect(champ.numero_allocataire).to eq('1234567')
|
|
|
|
|
expect(champ.code_postal).to eq('12345')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'external_id' do
|
|
|
|
|
context 'when only one data is given' do
|
|
|
|
|
before do
|
|
|
|
|
champ.numero_allocataire = '1234567'
|
|
|
|
|
champ.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(champ.external_id).to be_nil }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when all data required for an external fetch are given' do
|
|
|
|
|
before do
|
|
|
|
|
champ.numero_allocataire = '1234567'
|
|
|
|
|
champ.code_postal = '12345'
|
|
|
|
|
champ.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(JSON.parse(champ.external_id)).to eq({ "code_postal" => "12345", "numero_allocataire" => "1234567" }) }
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-10-05 14:03:30 +02:00
|
|
|
|
|
|
|
|
|
describe '#validate' do
|
|
|
|
|
let(:numero_allocataire) { '1234567' }
|
|
|
|
|
let(:code_postal) { '12345' }
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:champ) { described_class.new(dossier: build(:dossier)) }
|
2023-10-26 16:02:23 +02:00
|
|
|
|
let(:validation_context) { :champs_public_value }
|
2021-10-05 14:03:30 +02:00
|
|
|
|
|
2021-10-06 12:03:12 +02:00
|
|
|
|
subject { champ.valid?(validation_context) }
|
2021-10-05 14:03:30 +02:00
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
champ.numero_allocataire = numero_allocataire
|
|
|
|
|
champ.code_postal = code_postal
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when numero_allocataire and code_postal are valids' do
|
|
|
|
|
it { is_expected.to be true }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when numero_allocataire and code_postal are nil' do
|
|
|
|
|
let(:numero_allocataire) { nil }
|
|
|
|
|
let(:code_postal) { nil }
|
|
|
|
|
|
|
|
|
|
it { is_expected.to be true }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when only code_postal is nil' do
|
|
|
|
|
let(:code_postal) { nil }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to be false
|
2022-12-20 17:51:36 +01:00
|
|
|
|
expect(champ.errors.full_messages).to eq(["Le champ « Code postal » doit posséder 5 caractères"])
|
2021-10-05 14:03:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when only numero_allocataire is nil' do
|
|
|
|
|
let(:numero_allocataire) { nil }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to be false
|
2022-12-20 17:51:36 +01:00
|
|
|
|
expect(champ.errors.full_messages).to eq(["Le champ « Numero allocataire » doit être composé au maximum de 7 chiffres"])
|
2021-10-05 14:03:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when numero_allocataire is invalid' do
|
|
|
|
|
let(:numero_allocataire) { '123456a' }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to be false
|
2022-12-20 17:51:36 +01:00
|
|
|
|
expect(champ.errors.full_messages).to eq(["Le champ « Numero allocataire » doit être composé au maximum de 7 chiffres"])
|
2021-10-05 14:03:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when code_postal is invalid' do
|
|
|
|
|
let(:code_postal) { '123456' }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to be false
|
2022-12-20 17:51:36 +01:00
|
|
|
|
expect(champ.errors.full_messages).to eq(["Le champ « Code postal » doit posséder 5 caractères"])
|
2021-10-05 14:03:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-09-24 14:21:30 +02:00
|
|
|
|
end
|