Merge pull request #2624 from betagouv/refactor-after-confirmation-callback

invite: move invites link to the `after_confirmation` callback
This commit is contained in:
Pierre de La Morinerie 2018-09-19 12:20:15 +02:00 committed by GitHub
commit faff741bf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 63 deletions

View file

@ -1,38 +0,0 @@
# frozen_string_literal: true
class Users::ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation/new
# def new
# super
# end
# POST /resource/confirmation
# def create
# super
# end
# GET /resource/confirmation?confirmation_token=abcdef
# def show
# super
# end
protected
# The path used after resending confirmation instructions.
# def after_resending_confirmation_instructions_path_for(resource_name)
# super(resource_name)
# end
# The path used after confirmation.
def after_confirmation_path_for(resource_name, resource)
check_invite!(resource)
super(resource_name, resource)
end
private
def check_invite!(user)
Invite.where(email: user.email).update_all user_id: user.id
end
end

View file

@ -24,6 +24,11 @@ class User < ApplicationRecord
before_validation -> { sanitize_email(:email) }
# Callback provided by Devise
def after_confirmation
link_invites!
end
def self.find_for_france_connect(email, siret)
user = User.find_by(email: email)
if user.nil?
@ -49,4 +54,10 @@ class User < ApplicationRecord
def owns_or_invite?(dossier)
owns?(dossier) || invite?(dossier.id)
end
private
def link_invites!
Invite.where(email: email).update_all(user_id: id)
end
end

View file

@ -70,8 +70,7 @@ Rails.application.routes.draw do
devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations',
passwords: 'users/passwords',
confirmations: 'users/confirmations'
passwords: 'users/passwords'
}
devise_scope :user do

View file

@ -1,23 +0,0 @@
describe Users::ConfirmationsController, type: :controller do
let(:email) { 'mail@beta.gouv.fr' }
let(:user) do
create(:user,
email: email,
password: 'a good password',
confirmation_token: '123',
confirmed_at: nil)
end
before { @request.env["devise.mapping"] = Devise.mappings[:user] }
describe '#check_invite!' do
let!(:invite) { create(:invite, email: email) }
let!(:invite2) { create(:invite, email: email) }
before { get :show, params: { confirmation_token: user.confirmation_token } }
it 'the new user is connect at his two invite' do
expect(User.last.invites.size).to eq(2)
end
end
end

View file

@ -1,6 +1,25 @@
require 'spec_helper'
describe User, type: :model do
describe '#after_confirmation' do
let(:email) { 'mail@beta.gouv.fr' }
let!(:invite) { create(:invite, email: email) }
let!(:invite2) { create(:invite, email: email) }
let(:user) do
create(:user,
email: email,
password: 'a good password',
confirmation_token: '123',
confirmed_at: nil)
end
it 'when confirming a user, it links the pending invitations to this user' do
expect(user.invites.size).to eq(0)
user.confirm
expect(user.reload.invites.size).to eq(2)
end
end
describe '#find_for_france_connect' do
let(:siret) { '00000000000000' }
context 'when user exist' do