Microtest dossier prefill get without stored queries

This commit is contained in:
Damien Le Thiec 2023-02-13 18:15:24 +01:00
parent 041d1c372f
commit 802a23057b
7 changed files with 19 additions and 134 deletions

View file

@ -1,31 +0,0 @@
module QueryParamsStoreConcern
extend ActiveSupport::Concern
included do
helper_method :stored_query_params?
end
def store_query_params
# Don't override already stored params, because we could do goings and comings with authentication, and
# lost previously stored params
return if stored_query_params? || filtered_query_params.empty?
session[:stored_params] = filtered_query_params.to_json
end
def retrieve_and_delete_stored_query_params
return {} unless stored_query_params?
JSON.parse(session.delete(:stored_params))
end
def stored_query_params?
session[:stored_params].present?
end
private
def filtered_query_params
request.query_parameters.except(:locale, "locale")
end
end

View file

@ -1,7 +1,5 @@
module Users
class CommencerController < ApplicationController
include QueryParamsStoreConcern
layout 'procedure_context'
before_action :retrieve_prefilled_dossier, if: -> { params[:prefill_token].present? }, only: :commencer
@ -12,8 +10,6 @@ module Users
@procedure = retrieve_procedure
return procedure_not_found if @procedure.blank? || @procedure.brouillon?
store_query_params
@revision = @procedure.published_revision
render 'commencer/show'
end
@ -26,6 +22,22 @@ module Users
render 'commencer/show'
end
def preremplir
@procedure = retrieve_procedure
dossier = Dossier.new(
revision: @procedure.active_revision,
groupe_instructeur: @procedure.defaut_groupe_instructeur_for_new_dossier,
state: Dossier.states.fetch(:brouillon),
prefilled: true
)
dossier.build_default_individual
if dossier.save
dossier.prefill!(PrefillParams.new(dossier, params.to_unsafe_h).to_a)
end
redirect_to commencer_url(@procedure.path, prefill_token: dossier.prefill_token)
end
def dossier_vide_pdf
@procedure = retrieve_procedure_with_closed
return procedure_not_found if @procedure.blank? || @procedure.brouillon?

View file

@ -1,7 +1,6 @@
module Users
class DossiersController < UserController
include DossierHelper
include QueryParamsStoreConcern
layout 'procedure_context', only: [:identite, :update_identite, :siret, :update_siret]
@ -295,7 +294,7 @@ module Users
)
dossier.build_default_individual
dossier.save!
dossier.prefill!(PrefillParams.new(dossier, retrieve_and_delete_stored_query_params).to_a)
dossier.prefill!(PrefillParams.new(dossier, params.to_unsafe_h).to_a)
DossierMailer.with(dossier:).notify_new_draft.deliver_later
if dossier.procedure.for_individual

View file

@ -27,7 +27,7 @@ class PrefillDescription < SimpleDelegator
end
def prefill_link
@prefill_link ||= commencer_url({ path: path }.merge(prefilled_champs_for_link))
@prefill_link ||= commencer_preremplissage_url({ path: path }.merge(prefilled_champs_for_link))
end
def prefill_query

View file

@ -21,11 +21,6 @@
%p= t('views.commencer.show.prefilled_draft_detail_html', time_ago: time_ago_in_words(@prefilled_dossier.created_at), procedure: @procedure.libelle)
= link_to t('views.commencer.show.go_to_prefilled_file'), brouillon_dossier_path(@prefilled_dossier), class: 'fr-btn fr-btn--lg fr-my-2w'
- elsif stored_query_params?
%h2.huge-title= t('views.commencer.show.prefilled_draft')
%p= t('views.commencer.show.prefill_dossier_detail_html', procedure: @procedure.libelle)
= link_to t('views.commencer.show.go_to_prefilled_file'), url_for_new_dossier(@revision), class: 'fr-btn fr-btn--lg fr-my-2w'
- elsif dossiers.empty?
= link_to t('views.commencer.show.start_procedure'), url_for_new_dossier(@revision), class: 'fr-btn fr-btn--lg fr-my-2w'

View file

@ -295,6 +295,7 @@ Rails.application.routes.draw do
get '/test/:path/dossier_vide', action: :dossier_vide_pdf_test, as: :dossier_vide_test
get '/test/:path', action: 'commencer_test', as: :test
get '/:path', action: 'commencer'
get '/preremplir/:path', action: 'preremplir', as: :preremplissage
get '/:path/dossier_vide', action: 'dossier_vide_pdf', as: :dossier_vide
get '/:path/sign_in', action: 'sign_in', as: :sign_in
get '/:path/sign_up', action: 'sign_up', as: :sign_up

View file

@ -1,91 +0,0 @@
RSpec.describe QueryParamsStoreConcern, type: :controller do
class TestController < ActionController::Base
include QueryParamsStoreConcern
end
controller TestController do
end
before { allow_any_instance_of(ActionDispatch::Request).to receive(:query_parameters).and_return(params) }
describe '#store_query_params' do
subject(:store_query_params) { controller.store_query_params }
context 'when params are already stored' do
let(:params) { { param1: "param1" } }
it "does nothing" do
session[:stored_params] = "there is already something in there"
expect { store_query_params }.not_to change { session[:stored_params] }
end
end
context 'when params are empty' do
let(:params) { {} }
it "does nothing" do
expect { store_query_params }.not_to change { session[:stored_params] }
end
end
context 'when the store is empty and we have params' do
let(:params) { { param1: "param1", param2: "param2" } }
it "stores the query params" do
expect { store_query_params }.to change { session[:stored_params] }.from(nil).to(params.to_json)
end
end
context 'when params contain a locale' do
let(:params) { { locale: "fr", param2: "param2" } }
it "does not store the locale" do
expect { store_query_params }.to change { session[:stored_params] }.from(nil).to({ param2: "param2" }.to_json)
end
end
end
describe '#retrieve_and_delete_stored_query_params' do
subject(:retrieve_and_delete_stored_query_params) { controller.retrieve_and_delete_stored_query_params }
context 'when there are no stored params' do
let(:params) { {} }
it 'returns an empty hash' do
expect(retrieve_and_delete_stored_query_params).to be_empty
end
end
context 'when params are stored' do
let(:params) { { param1: "param1", param2: "param2" } }
before { controller.store_query_params }
it 'deletes the stored params' do
expect { retrieve_and_delete_stored_query_params }.to change { session[:stored_params] }.to(nil)
end
it 'returns the stored params' do
expect(retrieve_and_delete_stored_query_params).to match(params.with_indifferent_access)
end
end
end
describe '#stored_query_params?' do
subject(:stored_query_params?) { controller.stored_query_params? }
before { controller.store_query_params }
context 'when query params have been stored' do
let(:params) { { param1: "param1", param2: "param2" } }
it { is_expected.to eq(true) }
end
context 'when query params have not been stored' do
let(:params) { {} }
it { is_expected.to eq(false) }
end
end
end