demarches-normaliennes/spec/controllers/instructeurs/export_templates_controller_spec.rb

207 lines
6.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2024-03-03 11:30:41 +01:00
describe Instructeurs::ExportTemplatesController, type: :controller do
before { sign_in(instructeur.user) }
let(:instructeur) { create(:instructeur) }
let(:procedure) do
create(
:procedure, instructeurs: [instructeur],
2024-07-15 23:26:49 +02:00
types_de_champ_public: [{ type: :piece_justificative, libelle: "pj1", stable_id: 3 }]
)
end
2024-03-03 11:30:41 +01:00
let(:groupe_instructeur) { procedure.defaut_groupe_instructeur }
2024-07-15 23:26:49 +02:00
let(:groupe_instructeur_id) { groupe_instructeur.id }
let(:export_template_params) do
{
name: "coucou",
kind: "zip",
groupe_instructeur_id:,
export_pdf:,
dossier_folder: item_params(text: "DOSSIER_"),
pjs: [pj_item_params(stable_id: 3, text: "avis-commission-"), pj_item_params(stable_id: 666, text: "evil-hack")]
}
end
let(:export_pdf) { item_params(text: "mon_export_") }
2024-03-03 11:30:41 +01:00
describe '#new' do
2024-07-15 23:26:49 +02:00
subject { get :new, params: { procedure_id: procedure.id } }
it do
subject
expect(assigns(:export_template)).to be_present
end
end
2024-03-03 11:30:41 +01:00
describe '#create' do
2024-07-15 23:26:49 +02:00
let(:create_params) { export_template_params }
subject { post :create, params: { procedure_id: procedure.id, export_template: create_params } }
2024-03-03 11:30:41 +01:00
context 'with valid params' do
it 'redirect to some page' do
subject
2024-07-15 23:26:49 +02:00
expect(response).to redirect_to(exports_instructeur_procedure_path(procedure))
2024-03-03 11:30:41 +01:00
expect(flash.notice).to eq "Le modèle d'export coucou a bien été créé"
end
end
context 'with invalid params' do
2024-07-15 23:26:49 +02:00
let(:export_pdf) do
item_params(text: 'toto').merge("template" => { "content" => [{ "content" => "invalid" }] }.to_json)
end
2024-03-03 11:30:41 +01:00
it 'display error notification' do
subject
expect(flash.alert).to be_present
end
end
context 'with procedure not accessible by current instructeur' do
let(:another_procedure) { create(:procedure) }
2024-07-15 23:26:49 +02:00
subject { post :create, params: { procedure_id: another_procedure.id, export_template: export_template_params } }
2024-03-03 11:30:41 +01:00
it 'raise exception' do
expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end
end
2024-07-15 23:26:49 +02:00
context 'with invalid groupe_instructeur_id' do
let(:groupe_instructeur_id) { create(:groupe_instructeur).id }
it 'display error notification' do
expect { subject }.not_to change(ExportTemplate, :count)
expect(flash.alert).to be_present
end
end
context 'without pjs' do
let(:create_params) { export_template_params.tap { _1.delete(:pjs) } }
it 'works' do
subject
expect(flash.notice).to eq "Le modèle d'export coucou a bien été créé"
expect(ExportTemplate.last.pjs).to match_array([])
end
end
2024-03-03 11:30:41 +01:00
end
describe '#edit' do
let(:export_template) { create(:export_template, groupe_instructeur:) }
2024-07-15 23:26:49 +02:00
subject { get :edit, params: { procedure_id: procedure.id, id: export_template.id } }
2024-03-03 11:30:41 +01:00
it 'render edit' do
subject
expect(response).to render_template(:edit)
end
context "with export_template not accessible by current instructeur" do
let(:another_groupe_instructeur) { create(:groupe_instructeur) }
let(:export_template) { create(:export_template, groupe_instructeur: another_groupe_instructeur) }
it 'raise exception' do
expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
describe '#update' do
let(:export_template) { create(:export_template, groupe_instructeur:) }
2024-07-15 23:26:49 +02:00
let(:export_pdf) { item_params(text: "exPort_") }
2024-03-03 11:30:41 +01:00
2024-07-15 23:26:49 +02:00
subject { put :update, params: { procedure_id: procedure.id, id: export_template.id, export_template: export_template_params } }
2024-03-03 11:30:41 +01:00
context 'with valid params' do
it 'redirect to some page' do
subject
2024-07-15 23:26:49 +02:00
expect(response).to redirect_to(exports_instructeur_procedure_path(procedure))
2024-03-03 11:30:41 +01:00
expect(flash.notice).to eq "Le modèle d'export coucou a bien été modifié"
2024-07-15 23:26:49 +02:00
export_template.reload
expect(export_template.export_pdf.template_json).to eq(item_params(text: "exPort_")["template"])
expect(export_template.pjs.map(&:template_json)).to eq([item_params(text: "avis-commission-")["template"]])
2024-03-03 11:30:41 +01:00
end
end
context 'with invalid params' do
2024-07-15 23:26:49 +02:00
let(:export_pdf) do
item_params(text: 'a').merge("template" => { "content" => [{ "content" => "invalid" }] }.to_json)
end
2024-03-03 11:30:41 +01:00
it 'display error notification' do
subject
expect(flash.alert).to be_present
end
end
2024-07-15 23:26:49 +02:00
context 'with invalid groupe_instructeur_id' do
let(:groupe_instructeur_id) { create(:groupe_instructeur).id }
it 'display error notification' do
subject
expect(export_template.export_pdf.template_json).not_to eq(item_params(text: "exPort_")["template"])
expect(flash.alert).to be_present
end
end
2024-03-03 11:30:41 +01:00
end
2024-03-12 17:28:43 +01:00
describe '#destroy' do
let(:export_template) { create(:export_template, groupe_instructeur:) }
2024-07-15 23:26:49 +02:00
subject { delete :destroy, params: { procedure_id: procedure.id, id: export_template.id } }
2024-03-12 17:28:43 +01:00
context 'with valid params' do
it 'redirect to some page' do
subject
2024-07-15 23:26:49 +02:00
expect(response).to redirect_to(exports_instructeur_procedure_path(procedure))
2024-03-12 17:28:43 +01:00
expect(flash.notice).to eq "Le modèle d'export Mon export a bien été supprimé"
end
end
end
describe '#preview' do
render_views
let(:export_template) { create(:export_template, groupe_instructeur:) }
context 'with put request' do
subject { put :preview, params: { procedure_id: procedure.id, id: export_template.id, export_template: export_template_params }, format: :turbo_stream }
it 'works with bigbig procedure' do
dossier = create(:dossier, procedure: procedure, for_procedure_preview: true)
subject
expect(response.body).to include "DOSSIER_#{dossier.id}"
expect(response.body).to include "mon_export_#{dossier.id}.pdf"
end
end
end
2024-07-15 23:26:49 +02:00
def pj_item_params(stable_id:, text:, enabled: true)
item_params(text: text, enabled: enabled).merge("stable_id" => stable_id.to_s)
end
def item_params(text:, enabled: true)
{
"enabled" => enabled,
"template" => {
"type" => "doc",
"content" => content(text:)
}.to_json
}
end
def content(text:)
[
{
"type" => "paragraph",
"content" => [
{ "text" => text, "type" => "text" },
{ "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }
]
}
]
end
2024-03-03 11:30:41 +01:00
end