Merge pull request #8622 from demarches-simplifiees/fix/stored_query_issue
Dossier prefill get without stored queries
This commit is contained in:
commit
687c05e6d1
15 changed files with 160 additions and 213 deletions
|
@ -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
|
|
@ -15,16 +15,6 @@ describe Users::CommencerController, type: :controller do
|
|||
expect(assigns(:procedure)).to eq published_procedure
|
||||
expect(assigns(:revision)).to eq published_procedure.published_revision
|
||||
end
|
||||
|
||||
context 'when there are query params' do
|
||||
subject { get :commencer, params: { path: path, any_param: "any param" } }
|
||||
|
||||
it "stores the parameters in session" do
|
||||
subject
|
||||
|
||||
expect(session[:stored_params]).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the path is for a draft procedure' do
|
||||
|
@ -73,7 +63,7 @@ describe Users::CommencerController, type: :controller do
|
|||
end
|
||||
end
|
||||
|
||||
context 'when a dossier has been prefilled' do
|
||||
context 'when a dossier has been prefilled by POST before' do
|
||||
let(:dossier) { create(:dossier, :brouillon, :prefilled, user: user) }
|
||||
let(:path) { dossier.procedure.path }
|
||||
|
||||
|
@ -151,6 +141,85 @@ describe Users::CommencerController, type: :controller do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a dossier is being prefilled by GET' do
|
||||
let(:type_de_champ_text) { create(:type_de_champ_text, procedure: published_procedure) }
|
||||
let(:path) { published_procedure.path }
|
||||
let(:user) { create(:user) }
|
||||
|
||||
context "when the dossier does not exists yet" do
|
||||
subject { get :commencer, params: { path: path, "champ_#{type_de_champ_text.to_typed_id}" => "blabla" } }
|
||||
|
||||
shared_examples 'a prefilled brouillon dossier creator' do
|
||||
it 'creates a dossier' do
|
||||
subject
|
||||
expect(Dossier.count).to eq(1)
|
||||
expect(session[:prefill_token]).to eq(Dossier.last.prefill_token)
|
||||
expect(session[:prefill_params]).to eq({ "action" => "commencer", "champ_#{type_de_champ_text.to_typed_id}" => "blabla", "controller" => "users/commencer", "path" => path.to_s })
|
||||
expect(Dossier.last.champs.where(type_de_champ: type_de_champ_text).first.value).to eq("blabla")
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the user is unauthenticated' do
|
||||
it_behaves_like 'a prefilled brouillon dossier creator'
|
||||
end
|
||||
|
||||
context 'when the user is authenticated' do
|
||||
before { sign_in user }
|
||||
|
||||
it_behaves_like 'a prefilled brouillon dossier creator'
|
||||
|
||||
it { expect { subject }.to change { Dossier.last&.user }.from(nil).to(user) }
|
||||
|
||||
it 'sends the notify_new_draft email' do
|
||||
expect { perform_enqueued_jobs { subject } }.to change { ActionMailer::Base.deliveries.count }.by(1)
|
||||
|
||||
dossier = Dossier.last
|
||||
mail = ActionMailer::Base.deliveries.last
|
||||
expect(mail.subject).to eq("Retrouvez votre brouillon pour la démarche « #{dossier.procedure.libelle} »")
|
||||
expect(mail.html_part.body).to include(dossier_path(dossier))
|
||||
end
|
||||
end
|
||||
end
|
||||
context "when prefilled params are passed" do
|
||||
subject { get :commencer, params: { path: path, prefill_token: "token", "champ_#{type_de_champ_text.to_typed_id}" => "blabla" } }
|
||||
|
||||
context "when the associated dossier exists" do
|
||||
let!(:dossier) { create(:dossier, :prefilled, prefill_token: "token") }
|
||||
let!(:champ_text) { create(:champ_text, dossier: dossier, type_de_champ: type_de_champ_text) }
|
||||
|
||||
it "does not create a new dossier" do
|
||||
subject
|
||||
expect(Dossier.count).to eq(1)
|
||||
expect(assigns(:prefilled_dossier)).to eq(dossier)
|
||||
end
|
||||
end
|
||||
context "when the associated dossier does not exists" do
|
||||
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
|
||||
end
|
||||
end
|
||||
context "when session params exists" do
|
||||
subject { get :commencer, params: { path: path, "champ_#{type_de_champ_text.to_typed_id}" => "blabla" } }
|
||||
|
||||
before do
|
||||
session[:prefill_token] = "token"
|
||||
session[:prefill_params] = { "action" => "commencer", "champ_#{type_de_champ_text.to_typed_id}" => "blabla", "controller" => "users/commencer", "path" => path.to_s }
|
||||
end
|
||||
|
||||
context "when the associated dossier exists" do
|
||||
let!(:dossier) { create(:dossier, :prefilled, prefill_token: "token") }
|
||||
|
||||
it "does not create a new dossier" do
|
||||
subject
|
||||
expect(Dossier.count).to eq(1)
|
||||
expect(assigns(:prefilled_dossier)).to eq(dossier)
|
||||
end
|
||||
end
|
||||
context "when the associated dossier does not exists" do
|
||||
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#commencer_test' do
|
||||
|
|
|
@ -1068,60 +1068,6 @@ describe Users::DossiersController, type: :controller do
|
|||
|
||||
it { is_expected.to redirect_to dossiers_path }
|
||||
end
|
||||
|
||||
context 'when prefill values have been stored in session before' do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:value_1) { "any value" }
|
||||
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) }
|
||||
let(:value_2) { "another value" }
|
||||
|
||||
let(:params) {
|
||||
{
|
||||
procedure_id: procedure_id,
|
||||
"champ_#{type_de_champ_1.to_typed_id_for_query}" => value_1,
|
||||
"champ_#{type_de_champ_2.to_typed_id_for_query}" => value_2
|
||||
}
|
||||
}
|
||||
|
||||
before { session[:stored_params] = params.to_json }
|
||||
|
||||
it { expect { subject }.to change { session[:stored_params] }.to(nil) }
|
||||
|
||||
it { expect { subject }.to change { Dossier.count }.by(1) }
|
||||
|
||||
it "prefills the dossier's champs with the given values" do
|
||||
subject
|
||||
|
||||
dossier = Dossier.last
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).value).to eq(value_1)
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).value).to eq(value_2)
|
||||
end
|
||||
|
||||
it { is_expected.to redirect_to siret_dossier_path(id: Dossier.last) }
|
||||
|
||||
context 'when prefill values contain a hash' do
|
||||
let(:value_2) { { evil: "payload" } }
|
||||
|
||||
it "prefills the dossier's champ with the hash stored as a string" do
|
||||
subject
|
||||
|
||||
dossier = Dossier.last
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).value).to eq("{\"evil\"=>\"payload\"}")
|
||||
end
|
||||
end
|
||||
|
||||
context 'when prefill values contain an array' do
|
||||
let(:value_2) { ["a", "b", "c"] }
|
||||
|
||||
it "prefills the dossier's champ with the array stored as a string" do
|
||||
subject
|
||||
|
||||
dossier = Dossier.last
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).value).to eq("[\"a\", \"b\", \"c\"]")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
context 'when user is not logged' do
|
||||
it { is_expected.to have_http_status(302) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue