From 22473adec77e6c507040139199eb16414337a2b5 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 3 Sep 2024 19:03:12 +0200 Subject: [PATCH 1/2] [#10742] Set email as verified when instructeur signs in with Agent Connect --- app/controllers/agent_connect/agent_controller.rb | 1 + .../controllers/agent_connect/agent_controller_spec.rb | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/agent_connect/agent_controller.rb b/app/controllers/agent_connect/agent_controller.rb index e7374113b..510edf5a9 100644 --- a/app/controllers/agent_connect/agent_controller.rb +++ b/app/controllers/agent_connect/agent_controller.rb @@ -32,6 +32,7 @@ class AgentConnect::AgentController < ApplicationController end instructeur.update(agent_connect_id_token: id_token) + instructeur.user.update(email_verified_at: Time.zone.now) aci = AgentConnectInformation.find_or_initialize_by(instructeur:, sub: user_info['sub']) aci.update(user_info.slice('given_name', 'usual_name', 'email', 'sub', 'siret', 'organizational_unit', 'belonging_population', 'phone')) diff --git a/spec/controllers/agent_connect/agent_controller_spec.rb b/spec/controllers/agent_connect/agent_controller_spec.rb index 11fb9aaca..80a255d16 100644 --- a/spec/controllers/agent_connect/agent_controller_spec.rb +++ b/spec/controllers/agent_connect/agent_controller_spec.rb @@ -33,7 +33,7 @@ describe AgentConnect::AgentController, type: :controller do context 'when the callback code is correct' do let(:code) { 'correct' } let(:state) { original_state } - let(:user_info) { { 'sub' => 'sub', 'email' => ' I@email.com', 'given_name' => 'given', 'usual_name' => 'usual' } } + let(:user_info) { { 'sub' => 'sub', 'email' => email, 'given_name' => 'given', 'usual_name' => 'usual' } } context 'and user_info returns some info' do before do @@ -52,6 +52,7 @@ describe AgentConnect::AgentController, type: :controller do expect(last_user.email).to eq(email) expect(last_user.confirmed_at).to be_present + expect(last_user.email_verified_at).to be_present expect(last_user.instructeur.agent_connect_id_token).to eq('id_token') expect(response).to redirect_to(instructeur_procedures_path) expect(state_cookie).to be_nil @@ -68,12 +69,17 @@ describe AgentConnect::AgentController, type: :controller do it 'reuses the account, signs in and redirects to procedure_path' do expect { subject }.to change { User.count }.by(0).and change { Instructeur.count }.by(0) - instructeur.reload expect(instructeur.agent_connect_id_token).to eq('id_token') expect(response).to redirect_to(instructeur_procedures_path) end + + it "sets email_verified_at" do + expect { subject }.to change { instructeur.user.reload.email_verified_at }.from( + nil + ) + end end context 'and the instructeur already has an account as a user' do From 628be7c7037e1e00494d6f1021f633cf266e2d48 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 6 Sep 2024 15:37:33 +0200 Subject: [PATCH 2/2] update => update! --- app/controllers/agent_connect/agent_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/agent_connect/agent_controller.rb b/app/controllers/agent_connect/agent_controller.rb index 510edf5a9..346c19a8d 100644 --- a/app/controllers/agent_connect/agent_controller.rb +++ b/app/controllers/agent_connect/agent_controller.rb @@ -31,8 +31,8 @@ class AgentConnect::AgentController < ApplicationController instructeur = user.instructeur end - instructeur.update(agent_connect_id_token: id_token) - instructeur.user.update(email_verified_at: Time.zone.now) + instructeur.update!(agent_connect_id_token: id_token) + instructeur.user.update!(email_verified_at: Time.zone.now) aci = AgentConnectInformation.find_or_initialize_by(instructeur:, sub: user_info['sub']) aci.update(user_info.slice('given_name', 'usual_name', 'email', 'sub', 'siret', 'organizational_unit', 'belonging_population', 'phone'))