sign_in: extract the procedure context to a ProcedureContextConcern

This commit is contained in:
Pierre de La Morinerie 2019-01-14 16:25:48 +01:00
parent e580d336e4
commit 4fd9fa6610
7 changed files with 147 additions and 54 deletions

View file

@ -0,0 +1,34 @@
module ProcedureContextConcern
extend ActiveSupport::Concern
include Devise::Controllers::StoreLocation
include Devise::StoreLocationExtension
def restore_procedure_context
if stored_procedure_id.present?
@procedure = Procedure.publiees.find_by(id: stored_procedure_id)
if @procedure.blank?
invalid_procedure_context
end
end
end
private
def stored_procedure_id
stored_location = get_stored_location_for(:user)
if stored_location.present? && stored_location.include?('procedure_id=')
stored_location.split('procedure_id=').second
else
nil
end
end
def invalid_procedure_context
clear_stored_location_for(:user)
flash.alert = t('errors.messages.procedure_not_found')
redirect_to root_path
end
end

View file

@ -1,6 +1,15 @@
module Devise
# Useful helpers additions to Devise::Controllers::StoreLocation
module StoreLocationExtension
# A variant of `stored_location_key_for` which doesn't delete the stored path.
def get_stored_location_for(resource_or_scope)
location = stored_location_for(resource_or_scope)
if location
store_location_for(resource_or_scope, location)
end
location
end
# Delete the url stored in the session for the given scope.
def clear_stored_location_for(resource_or_scope)
session_key = send(:stored_location_key_for, resource_or_scope)

View file

@ -1,18 +1,15 @@
class Users::SessionsController < Sessions::SessionsController
include ProcedureContextConcern
include TrustedDeviceConcern
include ActionView::Helpers::DateHelper
layout 'procedure_context', only: [:new, :create]
before_action :restore_procedure_context, only: [:new, :create]
# GET /resource/sign_in
def new
if user_return_to_procedure_id.present?
@procedure = Procedure.active(user_return_to_procedure_id)
end
@user = User.new
rescue ActiveRecord::RecordNotFound
error_procedure
end
# POST /resource/sign_in
@ -80,7 +77,7 @@ class Users::SessionsController < Sessions::SessionsController
end
def no_procedure
session['user_return_to'] = nil
clear_stored_location_for(:user)
redirect_to new_user_session_path
end
@ -112,20 +109,6 @@ class Users::SessionsController < Sessions::SessionsController
end
end
def error_procedure
session["user_return_to"] = nil
flash.alert = t('errors.messages.procedure_not_found')
redirect_to url_for root_path
end
def user_return_to_procedure_id
if session["user_return_to"].nil?
return nil
end
NumberService.to_number session["user_return_to"].split("?procedure_id=").second
end
def try_to_authenticate(klass, remember_me = false)
resource = klass.find_for_database_authentication(email: params[:user][:email])

View file

@ -1,5 +0,0 @@
class NumberService
def self.to_number(string)
string.to_s
end
end

View file

@ -0,0 +1,77 @@
require 'rails_helper'
RSpec.describe ProcedureContextConcern, type: :controller do
class TestController < ActionController::Base
include ProcedureContextConcern
before_action :restore_procedure_context
def index
head :ok
end
end
controller TestController do
end
describe '#restore_procedure_context' do
subject { get :index }
context 'when no return location has been stored' do
it 'succeeds, without defining a procedure on the controller' do
expect(subject.status).to eq 200
expect(assigns(:procedure)).to be nil
end
end
context 'when no procedure_id is present in the stored return location' do
before do
controller.store_location_for(:user, dossiers_path)
end
it 'succeeds, without assigns a procedure on the controller' do
expect(subject.status).to eq 200
expect(assigns(:procedure)).to be nil
end
end
context 'when a procedure location has been stored' do
context 'when the stored procedure does not exist' do
before do
controller.store_location_for(:user, new_dossier_path(procedure_id: '0'))
end
it 'redirects with an error' do
expect(subject.status).to eq 302
expect(subject).to redirect_to root_path
end
end
context 'when the stored procedure is not published' do
let(:procedure) { create :procedure }
before do
controller.store_location_for(:user, new_dossier_path(procedure_id: procedure.id))
end
it 'redirects with an error' do
expect(subject.status).to eq 302
expect(subject).to redirect_to root_path
end
end
context 'when the stored procedure exists' do
let(:procedure) { create :procedure, :published }
before do
controller.store_location_for(:user, new_dossier_path(procedure_id: procedure.id))
end
it 'succeeds, and assigns the procedure on the controller' do
expect(subject.status).to eq 200
expect(assigns(:procedure)).to eq procedure
end
end
end
end
end

View file

@ -9,6 +9,21 @@ RSpec.describe Devise::StoreLocationExtension, type: :controller do
controller TestController do
end
describe '#get_stored_location_for' do
context 'when a location has been previously stored' do
before { subject.store_location_for(:user, dossiers_path) }
it 'returns the stored location without clearing it' do
expect(subject.get_stored_location_for(:user)).to eq dossiers_path
expect(subject.stored_location_for(:user)).to eq dossiers_path
end
end
context 'when no location has been stored' do
it { expect(subject.get_stored_location_for(:user)).to be nil }
end
end
describe "#clear_stored_location_for" do
context 'when a location has been previously stored' do
before { subject.store_location_for(:user, dossiers_path) }

View file

@ -173,38 +173,18 @@ describe Users::SessionsController, type: :controller do
describe '#new' do
subject { get :new }
context 'when procedure_id is not present in user_return_to session params' do
it { expect(subject.status).to eq 200 }
end
it { expect(subject.status).to eq 200 }
context 'when procedure_id is present in user_return_to session params' do
context 'when procedure_id does not exist' do
before do
session["user_return_to"] = '?procedure_id=0'
end
context 'when a procedure location has been stored' do
let(:procedure) { create :procedure, :published }
it { expect(subject.status).to eq 302 }
it { expect(subject).to redirect_to root_path }
before do
controller.store_location_for(:user, new_dossier_path(procedure_id: procedure.id))
end
context 'when procedure is not published' do
let(:procedure) { create :procedure }
before do
session["user_return_to"] = "?procedure_id=#{procedure.id}"
end
it { expect(subject.status).to eq 302 }
it { expect(subject).to redirect_to root_path }
end
context 'when procedure_id exist' do
let(:procedure) { create :procedure, :published }
before do
session["user_return_to"] = "?procedure_id=#{procedure.id}"
end
it { expect(subject.status).to eq 200 }
it 'makes the saved procedure available' do
expect(subject.status).to eq 200
expect(assigns(:procedure)).to eq procedure
end
end
end