From d05bab3df3081ab9daefa04435bb04baaa2b1c74 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 8 Jul 2019 15:19:43 +0000 Subject: [PATCH 1/3] france_connect: refactor specs --- .../particulier_controller_spec.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spec/controllers/france_connect/particulier_controller_spec.rb b/spec/controllers/france_connect/particulier_controller_spec.rb index 7e4a00f15..18c4b56cf 100644 --- a/spec/controllers/france_connect/particulier_controller_spec.rb +++ b/spec/controllers/france_connect/particulier_controller_spec.rb @@ -71,11 +71,19 @@ describe FranceConnect::ParticulierController, type: :controller do end context 'when france_connect_particulier_id does not have an associate user' do - it { is_expected.to redirect_to(root_path) } + context 'when the email address is not used yet' do + it { expect { subject }.to change(User, :count).by(1) } + it { is_expected.to redirect_to(root_path) } + end - it do - subject - expect(User.find_by(email: email)).not_to be_nil + context 'when the email address is already used' do + let!(:user) { create(:user, email: email, france_connect_information: nil) } + + it 'associates the france_connect infos with the existing user' do + expect { subject }.not_to change(User, :count) + expect(user.reload.loged_in_with_france_connect).to eq(User.loged_in_with_france_connects.fetch(:particulier)) + expect(subject).to redirect_to(root_path) + end end end end From 6b27ac85141e4ca7bfe03aa4f17e91948fa34113 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 8 Jul 2019 17:28:35 +0200 Subject: [PATCH 2/3] france_connect: make existing user lookup case-insensitive Fix #4053 --- .../france_connect/particulier_controller.rb | 2 +- .../france_connect/particulier_controller_spec.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/france_connect/particulier_controller.rb b/app/controllers/france_connect/particulier_controller.rb index a1f95b292..745d46be1 100644 --- a/app/controllers/france_connect/particulier_controller.rb +++ b/app/controllers/france_connect/particulier_controller.rb @@ -13,7 +13,7 @@ class FranceConnect::ParticulierController < ApplicationController fetched_fci.tap(&:save) if fci.user.nil? - user = User.find_or_create_by(email: fci.email_france_connect) do |new_user| + user = User.find_or_create_by(email: fci.email_france_connect.downcase) do |new_user| new_user.password = Devise.friendly_token[0, 20] new_user.confirmed_at = Time.zone.now end diff --git a/spec/controllers/france_connect/particulier_controller_spec.rb b/spec/controllers/france_connect/particulier_controller_spec.rb index 18c4b56cf..4e1f1f66b 100644 --- a/spec/controllers/france_connect/particulier_controller_spec.rb +++ b/spec/controllers/france_connect/particulier_controller_spec.rb @@ -85,6 +85,17 @@ describe FranceConnect::ParticulierController, type: :controller do expect(subject).to redirect_to(root_path) end end + + context 'when a differently cased email address is already used' do + let(:email) { 'TEST@test.com' } + let!(:user) { create(:user, email: email.downcase, france_connect_information: nil) } + + it 'associates the france_connect infos with the existing user' do + expect { subject }.not_to change(User, :count) + expect(user.reload.loged_in_with_france_connect).to eq(User.loged_in_with_france_connects.fetch(:particulier)) + expect(subject).to redirect_to(root_path) + end + end end end From 06afc3890fc9d385c8ee367debdec2fca7966d9a Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 8 Jul 2019 17:30:07 +0200 Subject: [PATCH 3/3] france_connect: raise an error if some validation error occurs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a validation error occurs in `find_or_create`, an object without `id` will be returned–and the code will crash soon after. Ensure that we crash immediately, so that we can report the root cause (the validation error) instead of a seemingly-unrelated crash later. --- app/controllers/france_connect/particulier_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/france_connect/particulier_controller.rb b/app/controllers/france_connect/particulier_controller.rb index 745d46be1..49f97f180 100644 --- a/app/controllers/france_connect/particulier_controller.rb +++ b/app/controllers/france_connect/particulier_controller.rb @@ -13,7 +13,7 @@ class FranceConnect::ParticulierController < ApplicationController fetched_fci.tap(&:save) if fci.user.nil? - user = User.find_or_create_by(email: fci.email_france_connect.downcase) do |new_user| + user = User.find_or_create_by!(email: fci.email_france_connect.downcase) do |new_user| new_user.password = Devise.friendly_token[0, 20] new_user.confirmed_at = Time.zone.now end