diff --git a/app/models/champs/departement_champ.rb b/app/models/champs/departement_champ.rb index 13edb4bb4..5a99ce8ad 100644 --- a/app/models/champs/departement_champ.rb +++ b/app/models/champs/departement_champ.rb @@ -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 diff --git a/app/models/prefill_params.rb b/app/models/prefill_params.rb index e4ded1975..33261a8ac 100644 --- a/app/models/prefill_params.rb +++ b/app/models/prefill_params.rb @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index dc8be05ec..b6ebae365 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index d4b864c28..7ff6a3e7f 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -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: diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index af705addd..9c7ef5fe3 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -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) } diff --git a/spec/controllers/api/v2/graphql_controller_spec.rb b/spec/controllers/api/v2/graphql_controller_spec.rb index acfe61304..6f70743cd 100644 --- a/spec/controllers/api/v2/graphql_controller_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_spec.rb @@ -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 "{ diff --git a/spec/models/champs/departement_champ_spec.rb b/spec/models/champs/departement_champ_spec.rb index 35353f865..1a70439d5 100644 --- a/spec/models/champs/departement_champ_spec.rb +++ b/spec/models/champs/departement_champ_spec.rb @@ -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') diff --git a/spec/models/prefill_params_spec.rb b/spec/models/prefill_params_spec.rb index 680a7beca..2c9ff077a 100644 --- a/spec/models/prefill_params_spec.rb +++ b/spec/models/prefill_params_spec.rb @@ -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"