From 02395e732cb3e7fb8232fcd3d276528560f389c8 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 11 Jan 2018 18:16:38 +0100 Subject: [PATCH] FC service: return domain info --- .../france_connect/particulier_controller.rb | 17 +++++----------- app/services/france_connect_service.rb | 17 ++++++++++------ .../particulier_controller_spec.rb | 5 +++-- .../france_connect_particulier_spec.rb | 2 +- spec/services/france_connect_service_spec.rb | 20 +++++++++---------- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/app/controllers/france_connect/particulier_controller.rb b/app/controllers/france_connect/particulier_controller.rb index 71b9f8e0e..d93d2ef0a 100644 --- a/app/controllers/france_connect/particulier_controller.rb +++ b/app/controllers/france_connect/particulier_controller.rb @@ -8,22 +8,15 @@ class FranceConnect::ParticulierController < ApplicationController return redirect_to new_user_session_path end - user_infos = FranceConnectService.retrieve_user_informations_particulier(params[:code]) + fetched_fc_information = FranceConnectService.retrieve_user_informations_particulier(params[:code]) - if user_infos.present? + if fetched_fc_information.present? france_connect_information = FranceConnectInformation - .find_by(france_connect_particulier_id: user_infos[:france_connect_particulier_id]) + .find_by(france_connect_particulier_id: fetched_fc_information[:france_connect_particulier_id]) if france_connect_information.nil? - france_connect_information = FranceConnectInformation.create( - {gender: user_infos[:gender], - given_name: user_infos[:given_name], - family_name: user_infos[:family_name], - email_france_connect: user_infos[:email], - birthdate: user_infos[:birthdate], - birthplace: user_infos[:birthplace], - france_connect_particulier_id: user_infos[:france_connect_particulier_id]} - ) + fetched_fc_information.save + france_connect_information = fetched_fc_information end user = france_connect_information.user diff --git a/app/services/france_connect_service.rb b/app/services/france_connect_service.rb index 3ccca2f50..bbfe73794 100644 --- a/app/services/france_connect_service.rb +++ b/app/services/france_connect_service.rb @@ -11,12 +11,17 @@ class FranceConnectService def self.retrieve_user_informations_particulier(code) client = FranceConnectParticulierClient.new(code) - access_token = client.access_token!(client_auth_method: :secret) + user_info = client.access_token!(client_auth_method: :secret) + .userinfo! + .raw_attributes - user_info = access_token.userinfo! - hash = Hashie::Mash.new(user_info.raw_attributes) - - hash.france_connect_particulier_id = hash.sub - hash + FranceConnectInformation.new( + gender: user_info[:gender], + given_name: user_info[:given_name], + family_name: user_info[:family_name], + email_france_connect: user_info[:email], + birthdate: user_info[:birthdate], + birthplace: user_info[:birthplace], + france_connect_particulier_id: user_info[:sub]) end end diff --git a/spec/controllers/france_connect/particulier_controller_spec.rb b/spec/controllers/france_connect/particulier_controller_spec.rb index 279f7b9ef..9bc9e61fd 100644 --- a/spec/controllers/france_connect/particulier_controller_spec.rb +++ b/spec/controllers/france_connect/particulier_controller_spec.rb @@ -11,7 +11,7 @@ describe FranceConnect::ParticulierController, type: :controller do let(:email) { 'test@test.com' } let(:password) { '' } - let(:user_info) { Hashie::Mash.new(france_connect_particulier_id: france_connect_particulier_id, given_name: given_name, family_name: family_name, birthdate: birthdate, birthplace: birthplace, gender: gender, email: email, password: password) } + let(:user_info) { { france_connect_particulier_id: france_connect_particulier_id, given_name: given_name, family_name: family_name, birthdate: birthdate, birthplace: birthplace, gender: gender, email_france_connect: email } } describe '.auth' do it 'redirect to france connect serveur' do @@ -31,7 +31,8 @@ describe FranceConnect::ParticulierController, type: :controller do context 'when params code is present' do context 'when code is correct' do before do - allow(FranceConnectService).to receive(:retrieve_user_informations_particulier).and_return(user_info) + allow(FranceConnectService).to receive(:retrieve_user_informations_particulier) + .and_return(FranceConnectInformation.new(user_info)) end context 'when france_connect_particulier_id exist in database' do diff --git a/spec/features/france_connect/france_connect_particulier_spec.rb b/spec/features/france_connect/france_connect_particulier_spec.rb index bbf163217..7f1b263af 100644 --- a/spec/features/france_connect/france_connect_particulier_spec.rb +++ b/spec/features/france_connect/france_connect_particulier_spec.rb @@ -47,7 +47,7 @@ feature 'France Connect Particulier Connexion' do before do allow_any_instance_of(FranceConnectParticulierClient).to receive(:authorization_uri).and_return(france_connect_particulier_callback_path(code: code)) - allow(FranceConnectService).to receive(:retrieve_user_informations_particulier).and_return(user_info) + allow(FranceConnectService).to receive(:retrieve_user_informations_particulier).and_return(FranceConnectInformation.new(user_info)) end context 'when is the first connexion' do diff --git a/spec/services/france_connect_service_spec.rb b/spec/services/france_connect_service_spec.rb index 7374ecb32..342b8eb2e 100644 --- a/spec/services/france_connect_service_spec.rb +++ b/spec/services/france_connect_service_spec.rb @@ -7,7 +7,7 @@ describe FranceConnectService do let(:given_name) { 'plop1' } let(:family_name) { 'plop2' } - let(:birthdate) { 'plop3' } + let(:birthdate) { '2012-12-31' } let(:gender) { 'plop4' } let(:birthplace) { 'plop5' } let(:email) { 'plop@emaiL.com' } @@ -29,15 +29,15 @@ describe FranceConnectService do subject end - it 'returns user informations in a object' do - expect(subject.given_name).to eq(given_name) - expect(subject.family_name).to eq(family_name) - expect(subject.birthdate).to eq(birthdate) - expect(subject.gender).to eq(gender) - expect(subject.email).to eq(email) - expect(subject.phone).to eq(phone) - expect(subject.birthplace).to eq(birthplace) - expect(subject.france_connect_particulier_id).to eq(france_connect_particulier_id) + it 'returns user informations' do + expect(subject).to have_attributes({ + given_name: given_name, + family_name: family_name, + birthdate: DateTime.parse(birthdate), + birthplace: birthplace, + gender: gender, + email_france_connect: email, + france_connect_particulier_id: france_connect_particulier_id }) end end end