2024-01-18 12:38:46 +01:00
|
|
|
|
describe Administrateurs::AttestationTemplateV2sController, type: :controller do
|
2024-06-01 22:50:48 +02:00
|
|
|
|
let(:admin) { administrateurs(:default_admin) }
|
2024-01-18 12:38:46 +01:00
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2) }
|
2024-06-06 14:48:50 +02:00
|
|
|
|
let(:procedure) { create(:procedure, :published, administrateur: admin, attestation_template:, libelle: "Ma démarche") }
|
2024-01-18 12:38:46 +01:00
|
|
|
|
let(:logo) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') }
|
|
|
|
|
let(:signature) { fixture_file_upload('spec/fixtures/files/black.png', 'image/png') }
|
|
|
|
|
|
2024-01-22 17:29:24 +01:00
|
|
|
|
let(:update_params) do
|
|
|
|
|
{
|
|
|
|
|
official_layout: true,
|
|
|
|
|
label_logo: "Ministère des specs",
|
|
|
|
|
label_direction: "RSPEC",
|
|
|
|
|
footer: "en bas",
|
2024-06-01 22:39:04 +02:00
|
|
|
|
activated: true,
|
2024-01-22 17:29:24 +01:00
|
|
|
|
tiptap_body: {
|
|
|
|
|
type: :doc,
|
2024-02-01 18:55:41 +01:00
|
|
|
|
content: [
|
|
|
|
|
{
|
|
|
|
|
type: :paragraph,
|
|
|
|
|
content: [{ text: "Yo from spec", type: :text }]
|
|
|
|
|
}
|
|
|
|
|
]
|
2024-01-22 17:29:24 +01:00
|
|
|
|
}.to_json
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2024-01-18 12:38:46 +01:00
|
|
|
|
before do
|
|
|
|
|
sign_in(admin.user)
|
|
|
|
|
Flipper.enable(:attestation_v2)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'GET #show' do
|
|
|
|
|
subject do
|
2024-06-07 12:06:41 +02:00
|
|
|
|
get :show, params: { procedure_id: procedure.id, format: }
|
2024-01-18 12:38:46 +01:00
|
|
|
|
response.body
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-07 12:06:41 +02:00
|
|
|
|
context 'html' do
|
|
|
|
|
let(:format) { :html }
|
2024-01-18 12:38:46 +01:00
|
|
|
|
render_views
|
|
|
|
|
|
|
|
|
|
context 'with preview dossier' do
|
|
|
|
|
let!(:dossier) { create(:dossier, :en_construction, procedure:, for_procedure_preview: true) }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to include("Mon titre pour Ma démarche")
|
|
|
|
|
is_expected.to include("n° #{dossier.id}")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'without preview dossier' do
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to include("Mon titre pour --dossier_procedure_libelle--")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with logo label' do
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to include("Ministère des devs")
|
|
|
|
|
is_expected.to match(/centered_marianne-\w+\.svg/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with label direction' do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2, label_direction: "calé à droite") }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to include("calé à droite")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with footer' do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2, footer: "c'est le pied") }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to include("c'est le pied")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with additional logo' do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2, logo:) }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to include("Ministère des devs")
|
|
|
|
|
is_expected.to include("white.png")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with signature' do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2, signature:) }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to include("black.png")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-06-07 12:06:41 +02:00
|
|
|
|
|
|
|
|
|
context 'pdf' do
|
|
|
|
|
render_views
|
|
|
|
|
let(:format) { :pdf }
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2, signature:) }
|
|
|
|
|
let(:dossier) { create(:dossier, :en_construction, procedure:, for_procedure_preview: true) }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
html_content = /Ministère des devs.+Mon titre pour Ma démarche.+n° #{dossier.id}/m
|
|
|
|
|
context = { procedure_id: procedure.id }
|
|
|
|
|
|
|
|
|
|
allow(WeasyprintService).to receive(:generate_pdf).with(a_string_matching(html_content), hash_including(context)).and_return('PDF_DATA')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
is_expected.to eq('PDF_DATA')
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-01-18 12:38:46 +01:00
|
|
|
|
end
|
2024-01-22 17:29:24 +01:00
|
|
|
|
|
|
|
|
|
describe 'GET edit' do
|
2024-06-06 14:48:50 +02:00
|
|
|
|
render_views
|
|
|
|
|
let(:attestation_template) { nil }
|
|
|
|
|
|
2024-01-22 17:29:24 +01:00
|
|
|
|
subject do
|
|
|
|
|
get :edit, params: { procedure_id: procedure.id }
|
|
|
|
|
response.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'if an attestation template does not exists yet on the procedure' do
|
|
|
|
|
it 'creates new v2 attestation template' do
|
|
|
|
|
subject
|
|
|
|
|
expect(assigns(:attestation_template).version).to eq(2)
|
2024-06-06 14:48:50 +02:00
|
|
|
|
expect(assigns(:attestation_template)).to be_draft
|
|
|
|
|
expect(response.body).to have_button("Publier")
|
|
|
|
|
expect(response.body).not_to have_link("Réinitialiser les modifications")
|
2024-01-22 17:29:24 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'if an attestation template already exist on v1' do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, version: 1) }
|
|
|
|
|
|
|
|
|
|
it 'build new v2 attestation template' do
|
|
|
|
|
subject
|
|
|
|
|
expect(assigns(:attestation_template).version).to eq(2)
|
2024-06-06 14:48:50 +02:00
|
|
|
|
expect(assigns(:attestation_template)).to be_draft
|
2024-07-02 14:10:23 +02:00
|
|
|
|
expect(attestation_template.reload).to be_present
|
2024-06-06 14:48:50 +02:00
|
|
|
|
end
|
2024-07-29 14:48:19 +02:00
|
|
|
|
|
|
|
|
|
context 'on a draft procedure' do
|
|
|
|
|
let(:procedure) { create(:procedure, :draft, administrateur: admin, attestation_template:, libelle: "Ma démarche") }
|
|
|
|
|
|
|
|
|
|
it 'build v2 as draft' do
|
|
|
|
|
subject
|
|
|
|
|
expect(assigns(:attestation_template).version).to eq(2)
|
|
|
|
|
expect(assigns(:attestation_template)).to be_draft
|
|
|
|
|
expect(attestation_template.reload).to be_present
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-06-06 14:48:50 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'attestation template published exist without draft' do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2, :published) }
|
|
|
|
|
|
|
|
|
|
it 'mention publication' do
|
|
|
|
|
subject
|
|
|
|
|
expect(assigns(:attestation_template)).to eq(attestation_template)
|
|
|
|
|
expect(response.body).not_to have_link("Réinitialiser les modifications")
|
|
|
|
|
expect(response.body).not_to have_button("Publier les modifications")
|
2024-01-22 17:29:24 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-06 14:48:50 +02:00
|
|
|
|
context 'attestation template draft already exist on v2' do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :v2, :draft) }
|
|
|
|
|
|
|
|
|
|
it 'assigns this draft' do
|
2024-01-22 17:29:24 +01:00
|
|
|
|
subject
|
|
|
|
|
expect(assigns(:attestation_template)).to eq(attestation_template)
|
2024-06-06 14:48:50 +02:00
|
|
|
|
expect(response.body).not_to have_link("Réinitialiser les modifications")
|
|
|
|
|
expect(response.body).to have_button("Publier")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'and a published template also exists' do
|
|
|
|
|
before { create(:attestation_template, :v2, :published, procedure:) }
|
|
|
|
|
|
|
|
|
|
it 'mention publication' do
|
|
|
|
|
subject
|
|
|
|
|
expect(assigns(:attestation_template)).to eq(attestation_template)
|
|
|
|
|
expect(response.body).to have_link("Réinitialiser les modifications")
|
|
|
|
|
expect(response.body).to have_button("Publier les modifications")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when procedure is draft' do
|
|
|
|
|
let(:procedure) { create(:procedure, :draft, administrateur: admin, attestation_template:, libelle: "Ma démarche") }
|
|
|
|
|
|
|
|
|
|
it 'built template is already live (published)' do
|
|
|
|
|
subject
|
|
|
|
|
expect(assigns(:attestation_template).version).to eq(2)
|
|
|
|
|
expect(assigns(:attestation_template)).to be_published
|
|
|
|
|
expect(response.body).not_to have_button(/Publier/)
|
2024-01-22 17:29:24 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'POST create' do
|
|
|
|
|
let(:attestation_template) { nil }
|
|
|
|
|
|
|
|
|
|
subject do
|
2024-02-01 18:55:41 +01:00
|
|
|
|
post :create, params: { procedure_id: procedure.id, attestation_template: update_params }, format: :turbo_stream
|
2024-01-22 17:29:24 +01:00
|
|
|
|
response.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when attestation template is valid' do
|
2024-02-01 18:55:41 +01:00
|
|
|
|
render_views
|
|
|
|
|
|
2024-01-22 17:29:24 +01:00
|
|
|
|
it "create template" do
|
|
|
|
|
subject
|
2024-05-28 17:47:05 +02:00
|
|
|
|
attestation_template = procedure.reload.attestation_templates.first
|
2024-01-22 17:29:24 +01:00
|
|
|
|
|
2024-05-28 17:47:05 +02:00
|
|
|
|
expect(attestation_template).to be_draft
|
2024-01-22 17:29:24 +01:00
|
|
|
|
expect(attestation_template.official_layout).to eq(true)
|
|
|
|
|
expect(attestation_template.label_logo).to eq("Ministère des specs")
|
|
|
|
|
expect(attestation_template.label_direction).to eq("RSPEC")
|
|
|
|
|
expect(attestation_template.footer).to eq("en bas")
|
2024-06-01 22:39:04 +02:00
|
|
|
|
expect(attestation_template.activated).to eq(true)
|
2024-01-22 17:29:24 +01:00
|
|
|
|
expect(attestation_template.tiptap_body).to eq(update_params[:tiptap_body])
|
|
|
|
|
|
2024-06-07 10:22:01 +02:00
|
|
|
|
expect(response.body).to include("Attestation enregistrée")
|
2024-01-22 17:29:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "with files" do
|
|
|
|
|
let(:update_params) { super().merge(logo:, signature:) }
|
|
|
|
|
|
|
|
|
|
it "upload files" do
|
|
|
|
|
subject
|
2024-05-28 17:47:05 +02:00
|
|
|
|
attestation_template = procedure.reload.attestation_templates.first
|
2024-01-22 17:29:24 +01:00
|
|
|
|
|
|
|
|
|
expect(attestation_template.logo.download).to eq(logo.read)
|
|
|
|
|
expect(attestation_template.signature.download).to eq(signature.read)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'PATCH update' do
|
2024-02-01 18:55:41 +01:00
|
|
|
|
render_views
|
2024-01-22 17:29:24 +01:00
|
|
|
|
subject do
|
2024-02-01 18:55:41 +01:00
|
|
|
|
patch :update, params: { procedure_id: procedure.id, attestation_template: update_params }, format: :turbo_stream
|
2024-01-22 17:29:24 +01:00
|
|
|
|
response.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when attestation template is valid' do
|
2024-05-28 17:47:05 +02:00
|
|
|
|
it "create a draft template" do
|
|
|
|
|
expect { subject }.to change { procedure.attestation_templates.count }.by(1)
|
|
|
|
|
|
|
|
|
|
# published remains inchanged
|
|
|
|
|
expect(attestation_template.reload).to be_published
|
|
|
|
|
expect(attestation_template.label_logo).to eq("Ministère des devs")
|
|
|
|
|
|
|
|
|
|
attestation_template = procedure.attestation_templates.draft.first
|
2024-01-22 17:29:24 +01:00
|
|
|
|
|
2024-05-28 17:47:05 +02:00
|
|
|
|
expect(attestation_template).to be_draft
|
2024-01-22 17:29:24 +01:00
|
|
|
|
expect(attestation_template.official_layout).to eq(true)
|
|
|
|
|
expect(attestation_template.label_logo).to eq("Ministère des specs")
|
|
|
|
|
expect(attestation_template.label_direction).to eq("RSPEC")
|
|
|
|
|
expect(attestation_template.footer).to eq("en bas")
|
2024-06-01 22:39:04 +02:00
|
|
|
|
expect(attestation_template.activated).to eq(true)
|
2024-01-22 17:29:24 +01:00
|
|
|
|
expect(attestation_template.tiptap_body).to eq(update_params[:tiptap_body])
|
|
|
|
|
|
2024-06-07 10:22:01 +02:00
|
|
|
|
expect(response.body).to include("Attestation enregistrée")
|
2024-05-28 17:47:05 +02:00
|
|
|
|
expect(response.body).to include("Publier")
|
2024-01-22 17:29:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "with files" do
|
|
|
|
|
let(:update_params) { super().merge(logo:, signature:) }
|
|
|
|
|
|
|
|
|
|
it "upload files" do
|
|
|
|
|
subject
|
2024-05-28 17:47:05 +02:00
|
|
|
|
|
|
|
|
|
attestation_template = procedure.attestation_templates.draft.first
|
2024-01-22 17:29:24 +01:00
|
|
|
|
|
|
|
|
|
expect(attestation_template.logo.download).to eq(logo.read)
|
|
|
|
|
expect(attestation_template.signature.download).to eq(signature.read)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-02-01 18:55:41 +01:00
|
|
|
|
|
|
|
|
|
context 'with error' do
|
|
|
|
|
let(:update_params) do
|
|
|
|
|
super().merge(tiptap_body: { type: :doc, content: [{ type: :mention, attrs: { id: "tdc12", label: "oops" } }] }.to_json)
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-06 14:47:36 +02:00
|
|
|
|
it "renders error" do
|
2024-02-01 18:55:41 +01:00
|
|
|
|
subject
|
2024-06-07 10:22:01 +02:00
|
|
|
|
expect(response.body).to include("Attestation en erreur")
|
2024-06-06 14:47:36 +02:00
|
|
|
|
expect(response.body).to include('Supprimer la balise')
|
2024-02-01 18:55:41 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2024-05-28 17:47:05 +02:00
|
|
|
|
|
|
|
|
|
context "publishing a draft" do
|
|
|
|
|
let(:attestation_template) { build(:attestation_template, :draft, :v2) }
|
|
|
|
|
let(:update_params) { super().merge(state: :published) }
|
|
|
|
|
|
|
|
|
|
it "publish and redirect with notice" do
|
|
|
|
|
subject
|
|
|
|
|
expect(attestation_template.reload).to be_published
|
|
|
|
|
expect(flash.notice).to eq("L’attestation a été publiée.")
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-01-22 17:29:24 +01:00
|
|
|
|
end
|
2024-06-01 22:39:04 +02:00
|
|
|
|
|
|
|
|
|
context 'toggle activation' do
|
|
|
|
|
let(:update_params) { super().merge(activated: false) }
|
|
|
|
|
|
|
|
|
|
it 'toggle attribute of current published attestation' do
|
|
|
|
|
subject
|
|
|
|
|
expect(procedure.attestation_templates.v2.count).to eq(1)
|
|
|
|
|
expect(procedure.attestation_templates.v2.first.activated?).to eq(false)
|
|
|
|
|
expect(flash.notice).to be_nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when there is a draft' do
|
|
|
|
|
before {
|
|
|
|
|
create(:attestation_template, :v2, :draft, procedure:)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it 'toggle attribute of both draft & published v2 attestations' do
|
|
|
|
|
subject
|
|
|
|
|
expect(procedure.attestation_templates.v2.count).to eq(2)
|
|
|
|
|
expect(procedure.attestation_templates.v2.all?(&:activated?)).to eq(false)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-01-22 17:29:24 +01:00
|
|
|
|
end
|
2024-05-31 10:03:23 +02:00
|
|
|
|
|
|
|
|
|
describe 'POST reset' do
|
|
|
|
|
render_views
|
|
|
|
|
|
|
|
|
|
before {
|
|
|
|
|
create(:attestation_template, :v2, :draft, procedure:)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subject do
|
|
|
|
|
patch :reset, params: { procedure_id: procedure.id }
|
|
|
|
|
response.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "delete draft, keep published" do
|
|
|
|
|
expect(procedure.attestation_templates.count).to eq(2)
|
|
|
|
|
expect(subject).to redirect_to(edit_admin_procedure_attestation_template_v2_path(procedure))
|
|
|
|
|
expect(flash.notice).to include("réinitialisées")
|
|
|
|
|
expect(procedure.attestation_templates.count).to eq(1)
|
|
|
|
|
expect(procedure.attestation_templates.first).to eq(attestation_template)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-01-18 12:38:46 +01:00
|
|
|
|
end
|