merge with another new account

This commit is contained in:
simon lehericey 2021-10-13 09:26:54 +02:00
parent 19f81b594b
commit ce40e1127d
6 changed files with 59 additions and 3 deletions

View file

@ -1,6 +1,6 @@
class FranceConnect::ParticulierController < ApplicationController
before_action :redirect_to_login_if_fc_aborted, only: [:callback]
before_action :securely_retrieve_fci, only: [:merge, :merge_with_existing_account]
before_action :securely_retrieve_fci, only: [:merge, :merge_with_existing_account, :merge_with_new_account]
def login
if FranceConnectService.enabled?
@ -63,6 +63,20 @@ class FranceConnect::ParticulierController < ApplicationController
end
end
def merge_with_new_account
user = User.find_by(email: sanitized_email_params)
if user.nil?
@fci.associate_user!(sanitized_email_params)
@fci.delete_merge_token!
flash.notice = "Les comptes FranceConnect et #{APPLICATION_NAME} sont à présent fusionnés"
connect_france_connect_particulier(@fci.user)
else
# TODO
end
end
private
def securely_retrieve_fci

View file

@ -2,4 +2,10 @@ import { show, hide } from '@utils';
export function showFusion() {
show(document.querySelector('.fusion'));
hide(document.querySelector('.new-account'));
}
export function showNewAccount() {
hide(document.querySelector('.fusion'));
show(document.querySelector('.new-account'));
}

View file

@ -42,7 +42,8 @@ import {
discardEmailSuggestionBox
} from '../new_design/user-sign_up';
import {
showFusion
showFusion,
showNewAccount
} from '../new_design/fc-fusion';
// This is the global application namespace where we expose helpers used from rails views
@ -53,6 +54,7 @@ const DS = {
motivationCancel,
showImportJustificatif,
showFusion,
showNewAccount,
replaceSemicolonByComma,
acceptEmailSuggestion,
discardEmailSuggestionBox

View file

@ -18,7 +18,7 @@
= radio_button_tag :value, true, false, autocomplete: "off"
Oui
%label
%label{ onclick: "DS.showNewAccount(event);" }
= radio_button_tag :value, false, false, autocomplete: "off"
Non
@ -31,3 +31,13 @@
= label_tag :password, 'Mot de passe (8 caractères minimum)'
= password_field_tag :password, nil, autocomplete: 'current-password'
= submit_tag 'Fusionner les comptes', class: 'button primary'
.new-account.hidden
%p Donnez-nous alors le mail que #{APPLICATION_NAME} utilisera pour vous contacter
= form_tag france_connect_particulier_merge_with_new_account_path, remote: true, class: 'mt-2 form' do
= hidden_field_tag :merge_token, @fci.merge_token
= label_tag :email, 'Email (nom@site.com)'
= email_field_tag :email
= submit_tag 'Utiliser ce mail', class: 'button primary'

View file

@ -126,6 +126,7 @@ Rails.application.routes.draw do
get 'particulier/callback' => 'particulier#callback'
get 'particulier/merge/:merge_token' => 'particulier#merge', as: :particulier_merge
post 'particulier/merge_with_existing_account' => 'particulier#merge_with_existing_account'
post 'particulier/merge_with_new_account' => 'particulier#merge_with_new_account'
end
namespace :champs do

View file

@ -229,4 +229,27 @@ describe FranceConnect::ParticulierController, type: :controller do
end
end
end
describe '#merge_with_new_account' do
let(:fci) { FranceConnectInformation.create!(user_info) }
let(:merge_token) { fci.create_merge_token! }
let(:email) { ' Account@a.com ' }
let(:format) { :js }
subject { post :merge_with_new_account, params: { merge_token: merge_token, email: email }, format: format }
it_behaves_like "a method that needs a valid merge token"
context 'when the email does not belong to any user' do
it 'creates the account, signs in, and delete the merge token' do
subject
fci.reload
expect(fci.user.email).to eq(email.downcase.strip)
expect(fci.merge_token).to be_nil
expect(controller.current_user).to eq(fci.user)
expect(response.body).to include("window.location.href='/'")
end
end
end
end