validate epci to reject incorrect prefill values
This commit is contained in:
parent
5ac80f968a
commit
edf90a77f3
6 changed files with 198 additions and 2 deletions
|
@ -24,6 +24,10 @@ class Champs::EpciChamp < Champs::TextChamp
|
||||||
store_accessor :value_json, :code_departement
|
store_accessor :value_json, :code_departement
|
||||||
before_validation :on_departement_change
|
before_validation :on_departement_change
|
||||||
|
|
||||||
|
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
|
||||||
|
validate :external_id_in_departement_epci_codes, unless: -> { code_departement.nil? || external_id.nil? }
|
||||||
|
validate :value_in_departement_epci_names, unless: -> { code_departement.nil? || external_id.nil? || value.nil? }
|
||||||
|
|
||||||
def for_export
|
def for_export
|
||||||
[value, code, "#{code_departement} – #{departement_name}"]
|
[value, code, "#{code_departement} – #{departement_name}"]
|
||||||
end
|
end
|
||||||
|
@ -74,4 +78,22 @@ class Champs::EpciChamp < Champs::TextChamp
|
||||||
self.value = nil
|
self.value = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def code_departement_in_departement_codes
|
||||||
|
return if code_departement.in?(APIGeoService.departements.pluck(:code))
|
||||||
|
|
||||||
|
errors.add(:code_departement, :not_in_departement_codes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def external_id_in_departement_epci_codes
|
||||||
|
return if external_id.in?(APIGeoService.epcis(code_departement).pluck(:code))
|
||||||
|
|
||||||
|
errors.add(:external_id, :not_in_departement_epci_codes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def value_in_departement_epci_names
|
||||||
|
return if value.in?(APIGeoService.epcis(code_departement).pluck(:name))
|
||||||
|
|
||||||
|
errors.add(:value, :not_in_departement_epci_names)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,7 +41,8 @@ class PrefillParams
|
||||||
TypeDeChamp.type_champs.fetch(:checkbox),
|
TypeDeChamp.type_champs.fetch(:checkbox),
|
||||||
TypeDeChamp.type_champs.fetch(:pays),
|
TypeDeChamp.type_champs.fetch(:pays),
|
||||||
TypeDeChamp.type_champs.fetch(:regions),
|
TypeDeChamp.type_champs.fetch(:regions),
|
||||||
TypeDeChamp.type_champs.fetch(:departements)
|
TypeDeChamp.type_champs.fetch(:departements),
|
||||||
|
TypeDeChamp.type_champs.fetch(:epci)
|
||||||
]
|
]
|
||||||
|
|
||||||
attr_reader :champ, :value
|
attr_reader :champ, :value
|
||||||
|
|
|
@ -483,6 +483,14 @@ en:
|
||||||
not_in_departement_names: "must be a valid department name"
|
not_in_departement_names: "must be a valid department name"
|
||||||
external_id:
|
external_id:
|
||||||
not_in_departement_codes: "must be a valid department code"
|
not_in_departement_codes: "must be a valid department code"
|
||||||
|
"champs/epci_champ":
|
||||||
|
attributes:
|
||||||
|
code_departement:
|
||||||
|
not_in_departement_codes: "must be a valid department code"
|
||||||
|
external_id:
|
||||||
|
not_in_departement_epci_codes: "must be a valid EPCI code of the matching department"
|
||||||
|
value:
|
||||||
|
not_in_departement_epci_names: "must be a valid EPCI name of the matching department"
|
||||||
errors:
|
errors:
|
||||||
format: "Field « %{attribute} » %{message}"
|
format: "Field « %{attribute} » %{message}"
|
||||||
messages:
|
messages:
|
||||||
|
|
|
@ -478,6 +478,14 @@ fr:
|
||||||
not_in_departement_names: "doit être un nom de département valide"
|
not_in_departement_names: "doit être un nom de département valide"
|
||||||
external_id:
|
external_id:
|
||||||
not_in_departement_codes: "doit être un code de département valide"
|
not_in_departement_codes: "doit être un code de département valide"
|
||||||
|
"champs/epci_champ":
|
||||||
|
attributes:
|
||||||
|
code_departement:
|
||||||
|
not_in_departement_codes: "doit être un code de département valide"
|
||||||
|
external_id:
|
||||||
|
not_in_departement_epci_codes: "doit être un code d'EPCI du département correspondant"
|
||||||
|
value:
|
||||||
|
not_in_departement_epci_names: "doit être un nom d'EPCI du département correspondant"
|
||||||
errors:
|
errors:
|
||||||
format: "Le champ « %{attribute} » %{message}"
|
format: "Le champ « %{attribute} » %{message}"
|
||||||
messages:
|
messages:
|
||||||
|
|
|
@ -6,9 +6,156 @@ describe Champs::EpciChamp, type: :model do
|
||||||
Rails.cache.clear
|
Rails.cache.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:champ) { described_class.new }
|
describe 'validations' do
|
||||||
|
describe 'code_departement', vcr: { cassette_name: 'api_geo_departements' } do
|
||||||
|
subject { build(:champ_epci, code_departement: code_departement) }
|
||||||
|
|
||||||
|
context 'when nil' do
|
||||||
|
let(:code_departement) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when empty' do
|
||||||
|
let(:code_departement) { '' }
|
||||||
|
|
||||||
|
it { is_expected.not_to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when included in the departement codes' do
|
||||||
|
let(:code_departement) { "01" }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when not included in the departement codes' do
|
||||||
|
let(:code_departement) { "totoro" }
|
||||||
|
|
||||||
|
it { is_expected.not_to be_valid }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'external_id' do
|
||||||
|
let(:champ) { build(:champ_epci, code_departement: code_departement, external_id: nil) }
|
||||||
|
|
||||||
|
subject { champ }
|
||||||
|
|
||||||
|
before do
|
||||||
|
VCR.insert_cassette('api_geo_departements')
|
||||||
|
VCR.insert_cassette('api_geo_epcis')
|
||||||
|
|
||||||
|
champ.save!
|
||||||
|
champ.update_columns(external_id: external_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
VCR.eject_cassette('api_geo_departements')
|
||||||
|
VCR.eject_cassette('api_geo_epcis')
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when code_departement is nil' do
|
||||||
|
let(:code_departement) { nil }
|
||||||
|
let(:external_id) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when code_departement is not nil and valid' do
|
||||||
|
let(:code_departement) { "01" }
|
||||||
|
|
||||||
|
context 'when external_id is nil' do
|
||||||
|
let(:external_id) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when external_id is empty' do
|
||||||
|
let(:external_id) { '' }
|
||||||
|
|
||||||
|
it { is_expected.not_to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when external_id is included in the epci codes of the departement' do
|
||||||
|
let(:external_id) { '200042935' }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when external_id is not included in the epci codes of the departement' do
|
||||||
|
let(:external_id) { 'totoro' }
|
||||||
|
|
||||||
|
it { is_expected.not_to be_valid }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'value' do
|
||||||
|
let(:champ) { build(:champ_epci, code_departement: code_departement, external_id: nil, value: nil) }
|
||||||
|
|
||||||
|
subject { champ }
|
||||||
|
|
||||||
|
before do
|
||||||
|
VCR.insert_cassette('api_geo_departements')
|
||||||
|
VCR.insert_cassette('api_geo_epcis')
|
||||||
|
|
||||||
|
champ.save!
|
||||||
|
champ.update_columns(external_id: external_id, value: value)
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
VCR.eject_cassette('api_geo_departements')
|
||||||
|
VCR.eject_cassette('api_geo_epcis')
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when code_departement is nil' do
|
||||||
|
let(:code_departement) { nil }
|
||||||
|
let(:external_id) { nil }
|
||||||
|
let(:value) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when external_id is nil' do
|
||||||
|
let(:code_departement) { '01' }
|
||||||
|
let(:external_id) { nil }
|
||||||
|
let(:value) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when code_departement and external_id are not nil and valid' do
|
||||||
|
let(:code_departement) { '01' }
|
||||||
|
let(:external_id) { '200042935' }
|
||||||
|
|
||||||
|
context 'when value is nil' do
|
||||||
|
let(:value) { nil }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when value is empty' do
|
||||||
|
let(:value) { '' }
|
||||||
|
|
||||||
|
it { is_expected.not_to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when value is in departement epci names' do
|
||||||
|
let(:value) { 'CA Haut - Bugey Agglomération' }
|
||||||
|
|
||||||
|
it { is_expected.to be_valid }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when value is not in departement epci names' do
|
||||||
|
let(:value) { 'totoro' }
|
||||||
|
|
||||||
|
it { is_expected.not_to be_valid }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'value', vcr: { cassette_name: 'api_geo_epcis' } do
|
describe 'value', vcr: { cassette_name: 'api_geo_epcis' } do
|
||||||
|
let(:champ) { described_class.new }
|
||||||
it 'with departement and code' do
|
it 'with departement and code' do
|
||||||
champ.code_departement = '01'
|
champ.code_departement = '01'
|
||||||
champ.value = '200042935'
|
champ.value = '200042935'
|
||||||
|
|
|
@ -12,6 +12,16 @@ RSpec.describe PrefillParams do
|
||||||
before do
|
before do
|
||||||
allow(Rails).to receive(:cache).and_return(memory_store)
|
allow(Rails).to receive(:cache).and_return(memory_store)
|
||||||
Rails.cache.clear
|
Rails.cache.clear
|
||||||
|
|
||||||
|
VCR.insert_cassette('api_geo_regions')
|
||||||
|
VCR.insert_cassette('api_geo_departements')
|
||||||
|
VCR.insert_cassette('api_geo_epcis')
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
VCR.eject_cassette('api_geo_regions')
|
||||||
|
VCR.eject_cassette('api_geo_departements')
|
||||||
|
VCR.eject_cassette('api_geo_epcis')
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when the stable ids match the TypeDeChamp of the corresponding procedure" do
|
context "when the stable ids match the TypeDeChamp of the corresponding procedure" do
|
||||||
|
|
Loading…
Reference in a new issue