diff --git a/app/controllers/france_connect/particulier_controller.rb b/app/controllers/france_connect/particulier_controller.rb index 3eb3c494d..6f9cbda83 100644 --- a/app/controllers/france_connect/particulier_controller.rb +++ b/app/controllers/france_connect/particulier_controller.rb @@ -58,6 +58,12 @@ class FranceConnect::ParticulierController < ApplicationController destination_path = destination_path(user) render :confirmation_sent, locals: { email:, destination_path: } + rescue ActiveRecord::RecordInvalid => e + if e.record.errors.where(:email, :taken) + redirect_to new_user_session_path, alert: t('errors.messages.france_connect.email_taken', reset_link: new_user_password_path) + else + redirect_to new_user_session_path, alert: t('errors.messages.france_connect.unknown_error') + end end def merge diff --git a/config/locales/en.yml b/config/locales/en.yml index 4725ab107..c7341c757 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -751,6 +751,9 @@ en: # # etablissement_fail: 'Désolé, nous n’avons pas réussi à enregistrer l’établissement correspondant à ce numéro SIRET' france_connect: connexion: "Error trying to connect to France Connect." + forbidden_html: "Only citizen can use FranceConnect. As an instructor or administrator, you should reset your password." + email_taken: "This email is already in use. You should reset your password." + unknown_error: "An error occured, please retry." evil_regexp: The regular expression you have entered is potentially dangerous and could lead to performance issues. mismatch_regexp: The provided example must match the regular expression syntax_error_regexp: The syntax of the regular expression is invalid diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 93e40c552..8fb2c88b2 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -757,6 +757,8 @@ fr: france_connect: connexion: "Erreur lors de la connexion à France Connect." forbidden_html: "Seul-e-s les usagers peuvent se connecter via France Connect. En tant qu’instructeur ou administrateur, nous vous invitons à réininitialiser votre mot de passe." + email_taken: "Cet email est déjà utilisé. Nous vous invitons à réininitialiser votre mot de passe." + unknown_error: "Une erreure est survenue. Veuillez réessayer." evil_regexp: L'expression régulière que vous avez entrée est potentiellement dangereuse et pourrait entraîner des problèmes de performance mismatch_regexp: L'exemple doit correspondre à l'expression régulière fournie syntax_error_regexp: La syntaxe de l'expression régulière n'est pas valide diff --git a/spec/controllers/france_connect/particulier_controller_spec.rb b/spec/controllers/france_connect/particulier_controller_spec.rb index 6fe560588..5eb8c0165 100644 --- a/spec/controllers/france_connect/particulier_controller_spec.rb +++ b/spec/controllers/france_connect/particulier_controller_spec.rb @@ -253,6 +253,27 @@ describe FranceConnect::ParticulierController, type: :controller do subject end end + + context 'when associating the user conflict with existing one' do + let(:fci) { instance_double('FranceConnectInformation') } + let(:email) { 'user@example.com' } + let(:user) { instance_double('User', id: 1) } + let(:destination_path) { '/' } + + before do + create(:user, email:) + invalid_user = build(:user, email:) + allow(FranceConnectInformation).to receive(:find_by).with(merge_token: merge_token).and_return(fci) + allow(fci).to receive(:valid_for_merge?).and_return(true) + allow(fci).to receive(:email_france_connect).and_return(email) + invalid_user.valid? + allow(fci).to receive(:associate_user!).and_raise(ActiveRecord::RecordInvalid.new(invalid_user)) + end + + it 'fails' do + subject + end + end end describe '#confirm_email' do