demarches-normaliennes/spec/controllers/users/commencer_controller_spec.rb

419 lines
15 KiB
Ruby
Raw Normal View History

2019-03-25 10:53:45 +01:00
describe Users::CommencerController, type: :controller do
2018-11-01 13:00:35 +01:00
let(:user) { create(:user) }
2023-09-07 10:10:07 +02:00
let(:published_procedure) { create(:procedure, :for_individual, :published) }
2019-01-16 11:57:58 +01:00
let(:draft_procedure) { create(:procedure, :with_path) }
2018-11-01 13:00:35 +01:00
2019-01-16 11:57:58 +01:00
describe '#commencer' do
2018-11-01 13:00:35 +01:00
subject { get :commencer, params: { path: path } }
2019-01-16 11:57:58 +01:00
context 'when the path is for a published procedure' do
let(:path) { published_procedure.path }
2018-11-01 13:00:35 +01:00
2019-01-16 11:57:58 +01:00
it 'renders the view' do
expect(subject.status).to eq(200)
expect(subject).to render_template('show')
expect(assigns(:procedure)).to eq published_procedure
2021-06-24 11:57:05 +02:00
expect(assigns(:revision)).to eq published_procedure.published_revision
2019-01-16 11:57:58 +01:00
end
end
context 'when the path is for a draft procedure' do
2019-01-16 11:57:58 +01:00
let(:path) { draft_procedure.path }
it 'renders the view' do
expect(subject.status).to eq(200)
expect(subject).to render_template('show')
expect(assigns(:procedure)).to eq draft_procedure
expect(assigns(:revision)).to eq draft_procedure.draft_revision
2019-01-16 11:57:58 +01:00
end
end
context 'when the path does not exist' do
2018-11-01 13:00:35 +01:00
let(:path) { 'hello' }
2019-01-16 11:57:58 +01:00
it 'redirects with an error message' do
expect(subject).to redirect_to(root_path)
end
2018-11-01 13:00:35 +01:00
end
context 'when procedure without service is closed' do
it 'works' do
published_procedure.service = nil
published_procedure.organisation = "hello"
published_procedure.close!
get :commencer, params: { path: published_procedure.path }
expect(response).to redirect_to(closing_details_path(published_procedure.path))
end
end
context 'when procedure with service is closed' do
it 'works' do
published_procedure.service = create(:service)
published_procedure.close!
get :commencer, params: { path: published_procedure.path }
expect(response).to redirect_to(closing_details_path(published_procedure.path))
end
end
context 'when procedure has a replaced_by_procedure' do
let(:path) { published_procedure.path }
it 'redirects to new procedure' do
replaced_by_procedure = create(:procedure, :published)
published_procedure.update!(replaced_by_procedure_id: replaced_by_procedure.id)
published_procedure.close!
expect(subject).to redirect_to(commencer_path(path: replaced_by_procedure.path))
end
end
2023-03-01 10:21:22 +01:00
context 'when a dossier has been prefilled by POST before' do
let(:dossier) { create(:dossier, :brouillon, :prefilled, user: user) }
let(:path) { dossier.procedure.path }
subject { get :commencer, params: { path: path, prefill_token: dossier.prefill_token } }
shared_examples 'a prefilled brouillon dossier retriever' do
context 'when the dossier is a prefilled brouillon and the prefill token is present' do
it 'retrieves the dossier' do
subject
expect(assigns(:prefilled_dossier)).to eq(dossier)
end
end
context 'when the dossier is not prefilled' do
before do
dossier.prefilled = false
dossier.save(validate: false)
end
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
context 'when the dossier is not a brouillon' do
before { dossier.en_construction! }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
context 'when the prefill token does not match any dossier' do
before { dossier.prefill_token = "totoro" }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
context 'when the user is unauthenticated' do
let(:user) { nil }
it_behaves_like 'a prefilled brouillon dossier retriever'
end
context 'when the user is authenticated' do
context 'when the dossier already has an owner' do
let(:user) { create(:user) }
context 'when the user is the dossier owner' do
before { sign_in user }
it_behaves_like 'a prefilled brouillon dossier retriever'
end
context 'when the user is not the dossier owner' do
before { sign_in create(:user) }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
context 'when the dossier does not have an owner yet' do
let(:user) { nil }
let(:newly_authenticated_user) { create(:user) }
before { sign_in newly_authenticated_user }
it { expect { subject }.to change { dossier.reload.user }.from(nil).to(newly_authenticated_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
end
2023-03-01 10:21:22 +01:00
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
2023-09-07 10:10:07 +02:00
subject { get :commencer, params: { path: path, "champ_#{type_de_champ_text.to_typed_id}" => "blabla", "identite_nom" => "Dupont" } }
2023-03-01 10:21:22 +01:00
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)
2023-09-07 10:10:07 +02:00
expect(session[:prefill_params_digest]).to eq(PrefillChamps.digest({ "champ_#{type_de_champ_text.to_typed_id}" => "blabla" }))
2023-03-01 10:21:22 +01:00
expect(Dossier.last.champs.where(type_de_champ: type_de_champ_text).first.value).to eq("blabla")
2023-09-07 10:10:07 +02:00
expect(Dossier.last.individual.nom).to eq("Dupont")
2023-03-01 10:21:22 +01:00
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
2023-03-01 10:21:22 +01:00
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
2023-03-01 10:21:22 +01:00
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"
2023-09-07 10:10:07 +02:00
session[:prefill_params_digest] = PrefillChamps.digest({ "champ_#{type_de_champ_text.to_typed_id}" => "blabla" })
2023-03-01 10:21:22 +01:00
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
2023-03-01 10:21:22 +01:00
context "when the associated dossier does not exists" do
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
end
2018-11-01 13:00:35 +01:00
end
shared_examples 'a prefill token storage' do
it 'stores the prefill token' do
subject
expect(controller.stored_location_for(:user)).to include('prefill_token')
end
end
2019-01-16 11:57:58 +01:00
describe '#sign_in' do
context 'for a published procedure' do
subject { get :sign_in, params: { path: published_procedure.path } }
it 'set the path to return after sign-in to the procedure start page' do
subject
expect(controller.stored_location_for(:user)).to eq(commencer_path(path: published_procedure.path))
end
2019-01-16 11:57:58 +01:00
it { expect(subject).to redirect_to(new_user_session_path) }
context 'when a prefill token is given' do
subject { get :sign_in, params: { path: published_procedure.path, prefill_token: 'prefill_token' } }
it_behaves_like 'a prefill token storage'
end
2019-01-16 11:57:58 +01:00
end
context 'for a draft procedure' do
subject { get :sign_in, params: { path: draft_procedure.path } }
it 'set the path to return after sign-in to the draft procedure start page' do
subject
expect(controller.stored_location_for(:user)).to eq(commencer_path(path: draft_procedure.path))
end
it { expect(subject).to redirect_to(new_user_session_path) }
context 'when a prefill token is given' do
subject { get :sign_in, params: { path: draft_procedure.path, prefill_token: 'prefill_token' } }
it_behaves_like 'a prefill token storage'
end
end
context 'when the path doesnt exist' do
subject { get :sign_in, params: { path: 'hello' } }
it 'redirects with an error message' do
expect(subject).to redirect_to(root_path)
end
end
2019-01-16 11:57:58 +01:00
end
describe '#sign_up' do
context 'for a published procedure' do
subject { get :sign_up, params: { path: published_procedure.path } }
2019-01-16 11:57:58 +01:00
it 'set the path to return after sign-up to the procedure start page' do
subject
expect(controller.stored_location_for(:user)).to eq(commencer_path(path: published_procedure.path))
end
it { expect(subject).to redirect_to(new_user_registration_path) }
context 'when a prefill token is given' do
subject { get :sign_up, params: { path: published_procedure.path, prefill_token: 'prefill_token' } }
it_behaves_like 'a prefill token storage'
end
2019-01-16 11:57:58 +01:00
end
context 'for a draft procedure' do
subject { get :sign_up, params: { path: draft_procedure.path } }
it 'set the path to return after sign-up to the draft procedure start page' do
subject
expect(controller.stored_location_for(:user)).to eq(commencer_path(path: draft_procedure.path))
end
it { expect(subject).to redirect_to(new_user_registration_path) }
context 'when a prefill token is given' do
subject { get :sign_up, params: { path: draft_procedure.path, prefill_token: 'prefill_token' } }
it_behaves_like 'a prefill token storage'
end
end
context 'when the path doesnt exist' do
subject { get :sign_up, params: { path: 'hello' } }
it 'redirects with an error message' do
expect(subject).to redirect_to(root_path)
end
end
2019-01-16 11:57:58 +01:00
end
describe '#france_connect' do
context 'for a published procedure' do
subject { get :france_connect, params: { path: published_procedure.path } }
it 'set the path to return after sign-up to the procedure start page' do
subject
expect(controller.stored_location_for(:user)).to eq(commencer_path(path: published_procedure.path))
end
it { expect(subject).to redirect_to(france_connect_particulier_path) }
context 'when a prefill token is given' do
subject { get :france_connect, params: { path: published_procedure.path, prefill_token: 'prefill_token' } }
it_behaves_like 'a prefill token storage'
end
end
context 'for a draft procedure' do
subject { get :france_connect, params: { path: draft_procedure.path } }
it 'set the path to return after sign-up to the draft procedure start page' do
subject
expect(controller.stored_location_for(:user)).to eq(commencer_path(path: draft_procedure.path))
end
it { expect(subject).to redirect_to(france_connect_particulier_path) }
context 'when a prefill token is given' do
subject { get :france_connect, params: { path: draft_procedure.path, prefill_token: 'prefill_token' } }
it_behaves_like 'a prefill token storage'
end
end
context 'when the path doesnt exist' do
subject { get :france_connect, params: { path: 'hello' } }
it 'redirects with an error message' do
expect(subject).to redirect_to(root_path)
end
end
end
describe '#dossier_vide_pdf' do
let(:procedure) { create(:procedure, :published, :with_service, :with_path) }
before { get :dossier_vide_pdf, params: { path: procedure.path } }
context 'published procedure' do
it 'works' do
expect(response).to have_http_status(:success)
end
end
context 'not yet published procedure' do
let(:procedure) { create(:procedure, :with_service, :with_path) }
it 'redirects to procedure not found' do
expect(response).to have_http_status(302)
end
end
context 'closed procedure' do
it 'works' do
procedure.service = create(:service)
procedure.close!
expect(response).to have_http_status(:success)
end
end
end
describe '#dossier_vide_test_pdf' do
render_views
before { get :dossier_vide_pdf_test, params: { path: procedure.path }, format: :pdf }
context 'not published procedure with service' do
let(:procedure) { create(:procedure, :with_service, :with_path) }
it 'works' do
expect(response).to have_http_status(:success)
end
end
context 'not published procedure without service' do
let(:procedure) { create(:procedure, :with_path, service: nil, organisation: nil) }
it 'works' do
expect(response).to have_http_status(:success)
end
end
end
2018-11-01 13:00:35 +01:00
end