fix(procedure): accept dpo emails with accents

This commit is contained in:
Paul Chavard 2023-07-18 15:16:31 +02:00
parent 361f6e67c5
commit 08f0184700
3 changed files with 22 additions and 9 deletions

View file

@ -301,8 +301,7 @@ class Procedure < ApplicationRecord
validates :lien_site_web, presence: true, if: :publiee?
validates :lien_notice, url: { no_local: true, allow_blank: true }
validates :lien_dpo, format: { with: Devise.email_regexp, message: "n'est pas valide" }, if: :lien_dpo_email?
validates :lien_dpo, url: { no_local: true, allow_blank: true }, unless: :lien_dpo_email?
validates :lien_dpo, url: { no_local: true, allow_blank: true, accept_email: true }
validates :draft_types_de_champ_public,
'types_de_champ/no_empty_block': true,

View file

@ -14,6 +14,7 @@ class URLValidator < ActiveModel::EachValidator
options.reverse_merge!(no_local: false)
options.reverse_merge!(public_suffix: false)
options.reverse_merge!(accept_array: false)
options.reverse_merge!(accept_email: false)
super(options)
end
@ -53,15 +54,18 @@ class URLValidator < ActiveModel::EachValidator
def validate_url(record, attribute, value, message, schemes)
uri = Addressable::URI.parse(value)
host = uri && uri.host
scheme = uri && uri.scheme
valid_scheme = host && scheme && schemes.include?(scheme)
valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, default_rule: nil))
unless options.fetch(:accept_email) && uri.path.match?(/^(.+)@(.+)$/)
host = uri && uri.host
scheme = uri && uri.scheme
unless valid_scheme && valid_no_local && valid_suffix
record.errors.add(attribute, message, **filtered_options(value))
valid_scheme = host && scheme && schemes.include?(scheme)
valid_no_local = !options.fetch(:no_local) || (host && host.include?('.'))
valid_suffix = !options.fetch(:public_suffix) || (host && PublicSuffix.valid?(host, default_rule: nil))
unless valid_scheme && valid_no_local && valid_suffix
record.errors.add(attribute, message, **filtered_options(value))
end
end
rescue Addressable::URI::InvalidURIError
record.errors.add(attribute, message, **filtered_options(value))

View file

@ -1598,6 +1598,11 @@ describe Procedure do
let(:lien_notice) { 'www.démarches-simplifiées.fr' }
it { expect(procedure.valid?).to be_falsey }
end
context 'when an email' do
let(:lien_notice) { 'test@demarches-simplifiees.fr' }
it { expect(procedure.valid?).to be_falsey }
end
end
describe 'lien_dpo' do
@ -1623,6 +1628,11 @@ describe Procedure do
it { expect(procedure.valid?).to be_truthy }
end
context 'when valid email with accents' do
let(:lien_dpo) { 'test@démarches-simplifiées.fr' }
it { expect(procedure.valid?).to be_truthy }
end
context 'when not a valid link' do
let(:lien_dpo) { 'www.démarches-simplifiées.fr' }
it { expect(procedure.valid?).to be_falsey }