From 8dc966feb44c41b2de814e6cf3ff65f4826c0503 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 2 Aug 2022 12:37:29 +0200 Subject: [PATCH] fix(dossier_transfer): requires an actual email Closes #7621 --- app/models/dossier_transfer.rb | 2 ++ config/locales/models/dossier_transfer/en.yml | 9 +++++++ config/locales/models/dossier_transfer/fr.yml | 9 +++++++ spec/models/dossier_transfer_spec.rb | 26 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 config/locales/models/dossier_transfer/en.yml create mode 100644 config/locales/models/dossier_transfer/fr.yml diff --git a/app/models/dossier_transfer.rb b/app/models/dossier_transfer.rb index 4ae5f88b3..8fbf6ec1d 100644 --- a/app/models/dossier_transfer.rb +++ b/app/models/dossier_transfer.rb @@ -12,6 +12,8 @@ class DossierTransfer < ApplicationRecord EXPIRATION_LIMIT = 2.weeks + validates :email, format: { with: Devise.email_regexp } + scope :pending, -> { where('created_at > ?', (Time.zone.now - EXPIRATION_LIMIT)) } scope :stale, -> { where('created_at < ?', (Time.zone.now - EXPIRATION_LIMIT)) } scope :with_dossiers, -> { joins(:dossiers).merge(Dossier.visible_by_user) } diff --git a/config/locales/models/dossier_transfer/en.yml b/config/locales/models/dossier_transfer/en.yml new file mode 100644 index 000000000..77c95aa97 --- /dev/null +++ b/config/locales/models/dossier_transfer/en.yml @@ -0,0 +1,9 @@ +en: + activerecord: + errors: + models: + dossier_transfer: + attributes: + email: + format: "Email address %{message}" + invalid: "is invalid" diff --git a/config/locales/models/dossier_transfer/fr.yml b/config/locales/models/dossier_transfer/fr.yml new file mode 100644 index 000000000..751410cef --- /dev/null +++ b/config/locales/models/dossier_transfer/fr.yml @@ -0,0 +1,9 @@ +fr: + activerecord: + errors: + models: + dossier_transfer: + attributes: + email: + format: "L'adresse email %{message}" + invalid: "est invalide" diff --git a/spec/models/dossier_transfer_spec.rb b/spec/models/dossier_transfer_spec.rb index aa2b08a90..06cf2ff2c 100644 --- a/spec/models/dossier_transfer_spec.rb +++ b/spec/models/dossier_transfer_spec.rb @@ -78,4 +78,30 @@ RSpec.describe DossierTransfer, type: :model do expect(DossierTransfer.count).to eq(0) end end + + describe "validation" do + let(:email) { build(:dossier_transfer).email } + + subject { build(:dossier_transfer, email: email) } + + it "factory is valid" do + expect(subject).to be_valid + end + + context "when email is blank" do + let(:email) { "" } + + it "requires a valid email" do + expect(subject).to be_invalid + end + end + + context "when email is not an email" do + let(:email) { "test" } + + it "requires a valid email" do + expect(subject).to be_invalid + end + end + end end