add external_id and value validation

This commit is contained in:
Sébastien Carceles 2023-01-20 11:23:52 +01:00 committed by sebastiencarceles
parent a235829ddd
commit 12abf9045b
8 changed files with 104 additions and 7 deletions

View file

@ -21,6 +21,9 @@
# type_de_champ_id :integer
#
class Champs::DepartementChamp < Champs::TextChamp
validate :value_in_departement_names, unless: -> { value.nil? }
validate :external_id_in_departement_codes, unless: -> { external_id.nil? }
def for_export
[name, code]
end
@ -65,6 +68,9 @@ class Champs::DepartementChamp < Champs::TextChamp
elsif code.blank?
self.external_id = nil
super(nil)
else
self.external_id = APIGeoService.departement_code(code)
super(code)
end
end
@ -73,4 +79,16 @@ class Champs::DepartementChamp < Champs::TextChamp
def formatted_value
blank? ? "" : "#{code} #{name}"
end
def value_in_departement_names
return if value.in?(APIGeoService.departements.pluck(:name))
errors.add(:value, :not_in_departement_names)
end
def external_id_in_departement_codes
return if external_id.in?(APIGeoService.departements.pluck(:code))
errors.add(:external_id, :not_in_departement_codes)
end
end

View file

@ -40,7 +40,8 @@ class PrefillParams
TypeDeChamp.type_champs.fetch(:yes_no),
TypeDeChamp.type_champs.fetch(:checkbox),
TypeDeChamp.type_champs.fetch(:pays),
TypeDeChamp.type_champs.fetch(:regions)
TypeDeChamp.type_champs.fetch(:regions),
TypeDeChamp.type_champs.fetch(:departements)
]
attr_reader :champ, :value

View file

@ -475,6 +475,12 @@ en:
not_in_region_names: "must be a valid region name"
external_id:
not_in_region_codes: "must be a valid region code"
"champs/departement_champ":
attributes:
value:
not_in_departement_names: "must be a valid departement name"
external_id:
not_in_departement_codes: "must be a valid departement code"
errors:
format: "Field « %{attribute} » %{message}"
messages:

View file

@ -470,6 +470,12 @@ fr:
not_in_region_names: "doit être un nom de région valide"
external_id:
not_in_region_codes: "doit être un code de région valide"
"champs/departement_champ":
attributes:
value:
not_in_departement_names: "doit être un nom de département valide"
external_id:
not_in_departement_codes: "doit être un code de département valide"
errors:
format: "Le champ « %{attribute} » %{message}"
messages:

View file

@ -4,6 +4,13 @@ describe API::V1::DossiersController do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, administrateur: admin) }
let(:wrong_procedure) { create(:procedure) }
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
before do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
end
it { expect(described_class).to be < APIController }
describe 'GET index (with bearer token)' do
@ -258,7 +265,7 @@ describe API::V1::DossiersController do
end
end
describe 'departement' do
describe 'departement', vcr: { cassette_name: 'api_geo_departements' } do
let(:procedure) { create(:procedure, :with_departement, administrateur: admin) }
let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure: procedure) }

View file

@ -42,7 +42,6 @@ describe API::V2::GraphqlController do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
allow(APIGeoService).to receive(:departement_name).with('01').and_return('Ain')
instructeur.assign_to_procedure(procedure)
end
@ -397,7 +396,7 @@ describe API::V2::GraphqlController do
dossier
end
context "for individual", vcr: { cassette_name: 'api_geo_regions' } do
context "for individual", vcr: { cassette_name: 'api_geo_all' } do
let(:procedure) { create(:procedure, :published, :for_individual, :with_service, :with_all_champs, :with_all_annotations, administrateurs: [admin]) }
let(:query) do
"{

View file

@ -6,9 +6,69 @@ describe Champs::DepartementChamp, type: :model do
Rails.cache.clear
end
let(:champ) { described_class.new }
describe 'validations', vcr: { cassette_name: 'api_geo_departements' } do
describe 'external link' do
subject { build(:champ_departements, external_id: external_id) }
context 'when nil' do
let(:external_id) { nil }
it { is_expected.to be_valid }
end
context 'when blank' do
let(:external_id) { '' }
it { is_expected.not_to be_valid }
end
context 'when included in the departement codes' do
let(:external_id) { "01" }
it { is_expected.to be_valid }
end
context 'when not included in the departement codes' do
let(:external_id) { "totoro" }
it { is_expected.not_to be_valid }
end
end
describe 'value' do
subject { create(:champ_departements) }
before { subject.update_columns(value: value) }
context 'when nil' do
let(:value) { nil }
it { is_expected.to be_valid }
end
context 'when blank' do
let(:value) { '' }
it { is_expected.not_to be_valid }
end
context 'when included in the departement names' do
let(:value) { "Ain" }
it { is_expected.to be_valid }
end
context 'when not included in the departement names' do
let(:value) { "totoro" }
it { is_expected.not_to be_valid }
end
end
end
describe 'value', vcr: { cassette_name: 'api_geo_departements' } do
let(:champ) { described_class.new }
it 'with code having 2 chars' do
champ.value = '01'
expect(champ.external_id).to eq('01')

View file

@ -1,5 +1,5 @@
RSpec.describe PrefillParams do
describe "#to_a", vcr: { cassette_name: 'api_geo_regions' } do
describe "#to_a", vcr: { cassette_name: 'api_geo_all' } do
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
let(:procedure) { create(:procedure, :published, types_de_champ_public:, types_de_champ_private:) }
@ -171,7 +171,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ public value that is unauthorized", :address, "value"
it_behaves_like "a champ public value that is unauthorized", :pays, "value"
it_behaves_like "a champ public value that is unauthorized", :regions, "value"
# TODO: SEB add validation it_behaves_like "a champ public value that is unauthorized", :departements, "value"
it_behaves_like "a champ public value that is unauthorized", :departements, "value"
it_behaves_like "a champ public value that is unauthorized", :siret, "value"
it_behaves_like "a champ public value that is unauthorized", :rna, "value"
it_behaves_like "a champ public value that is unauthorized", :annuaire_education, "value"