FC service: return domain info
This commit is contained in:
parent
7024e14d1c
commit
02395e732c
5 changed files with 30 additions and 31 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue