From ce40e1127d1461d899e4efc24a9a342eb4f0477d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 13 Oct 2021 09:26:54 +0200 Subject: [PATCH] merge with another new account --- .../france_connect/particulier_controller.rb | 16 ++++++++++++- app/javascript/new_design/fc-fusion.js | 6 +++++ app/javascript/packs/application.js | 4 +++- .../particulier/merge.html.haml | 12 +++++++++- config/routes.rb | 1 + .../particulier_controller_spec.rb | 23 +++++++++++++++++++ 6 files changed, 59 insertions(+), 3 deletions(-) diff --git a/app/controllers/france_connect/particulier_controller.rb b/app/controllers/france_connect/particulier_controller.rb index e856a84fa..b025afd60 100644 --- a/app/controllers/france_connect/particulier_controller.rb +++ b/app/controllers/france_connect/particulier_controller.rb @@ -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 diff --git a/app/javascript/new_design/fc-fusion.js b/app/javascript/new_design/fc-fusion.js index bb1c904c3..8b23b0263 100644 --- a/app/javascript/new_design/fc-fusion.js +++ b/app/javascript/new_design/fc-fusion.js @@ -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')); } diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 7a529b41e..50a2b3a5b 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -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 diff --git a/app/views/france_connect/particulier/merge.html.haml b/app/views/france_connect/particulier/merge.html.haml index 1ee48aa00..25f0ff021 100644 --- a/app/views/france_connect/particulier/merge.html.haml +++ b/app/views/france_connect/particulier/merge.html.haml @@ -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' + diff --git a/config/routes.rb b/config/routes.rb index 8d27d54db..38bb0241d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/france_connect/particulier_controller_spec.rb b/spec/controllers/france_connect/particulier_controller_spec.rb index 172bb9f4e..45a4d7635 100644 --- a/spec/controllers/france_connect/particulier_controller_spec.rb +++ b/spec/controllers/france_connect/particulier_controller_spec.rb @@ -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