diff --git a/app/controllers/administrateurs/attestation_template_v2s_controller.rb b/app/controllers/administrateurs/attestation_template_v2s_controller.rb index a38c6d97d..296e3bcf5 100644 --- a/app/controllers/administrateurs/attestation_template_v2s_controller.rb +++ b/app/controllers/administrateurs/attestation_template_v2s_controller.rb @@ -68,11 +68,17 @@ module Administrateurs end @attestation_template.update!(attestation_params) + flash.notice = "Le modèle de l’attestation a été modifié" + + respond_to do |format| + format.turbo_stream + format.html do + redirect_to edit_admin_procedure_attestation_template_path(@procedure) + end + end end - def create - @attestation_template.update!(editor_params) - end + def create = update private diff --git a/spec/controllers/administrateurs/attestation_template_v2s_controller_spec.rb b/spec/controllers/administrateurs/attestation_template_v2s_controller_spec.rb index 6c8dba830..61b5a13c9 100644 --- a/spec/controllers/administrateurs/attestation_template_v2s_controller_spec.rb +++ b/spec/controllers/administrateurs/attestation_template_v2s_controller_spec.rb @@ -5,6 +5,25 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do let(:logo) { fixture_file_upload('spec/fixtures/files/white.png', 'image/png') } let(:signature) { fixture_file_upload('spec/fixtures/files/black.png', 'image/png') } + let(:update_params) do + { + official_layout: true, + label_logo: "Ministère des specs", + label_direction: "RSPEC", + footer: "en bas", + activated: false, + tiptap_body: { + type: :doc, + content: [ + { + type: :paragraph, + content: [{ text: "Yo from spec" }] + } + ] + }.to_json + } + end + before do sign_in(admin.user) Flipper.enable(:attestation_v2) @@ -75,4 +94,108 @@ describe Administrateurs::AttestationTemplateV2sController, type: :controller do end end end + + describe 'GET edit' do + 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 + let(:attestation_template) { nil } + + it 'creates new v2 attestation template' do + subject + expect(assigns(:attestation_template).version).to eq(2) + 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) + end + end + + context 'if attestation template already exist on v2' do + it 'assigns v2 attestation template' do + subject + expect(assigns(:attestation_template)).to eq(attestation_template) + end + end + end + + describe 'POST create' do + let(:attestation_template) { nil } + + subject do + post :create, params: { procedure_id: procedure.id, attestation_template: update_params } + response.body + end + + context 'when attestation template is valid' do + it "create template" do + subject + attestation_template = procedure.reload.attestation_template + + 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") + expect(attestation_template.activated).to eq(false) + expect(attestation_template.tiptap_body).to eq(update_params[:tiptap_body]) + + expect(flash.notice).to be_present + end + + context "with files" do + let(:update_params) { super().merge(logo:, signature:) } + + it "upload files" do + subject + attestation_template = procedure.reload.attestation_template + + 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 + subject do + patch :update, params: { procedure_id: procedure.id, attestation_template: update_params } + response.body + end + + context 'when attestation template is valid' do + it "update template" do + subject + attestation_template.reload + + 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") + expect(attestation_template.activated).to eq(false) + expect(attestation_template.tiptap_body).to eq(update_params[:tiptap_body]) + + expect(flash.notice).to be_present + end + + context "with files" do + let(:update_params) { super().merge(logo:, signature:) } + + it "upload files" do + subject + attestation_template.reload + + expect(attestation_template.logo.download).to eq(logo.read) + expect(attestation_template.signature.download).to eq(signature.read) + end + end + end + end end diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index 064b151d0..a29cf8eb7 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -5,9 +5,6 @@ require 'selenium/webdriver' def setup_driver(app, download_path, options) Capybara::Selenium::Driver.new(app, browser: :chrome, options:).tap do |driver| - # Set download dir for Chrome < 77 - driver.browser.download_path = download_path - if ENV['MAKE_IT_SLOW'].present? driver.browser.network_conditions = { offline: false, diff --git a/spec/system/administrateurs/procedure_attestation_template_spec.rb b/spec/system/administrateurs/procedure_attestation_template_spec.rb index a7d987c4b..375c6a9b0 100644 --- a/spec/system/administrateurs/procedure_attestation_template_spec.rb +++ b/spec/system/administrateurs/procedure_attestation_template_spec.rb @@ -28,6 +28,8 @@ describe 'As an administrateur, I want to manage the procedure’s attestation', expect(page).to have_content('Désactivée') find_attestation_card(with_nested_selector: ".fr-badge") + expect(page).not_to have_content("Nouvel éditeur d’attestation") + # now process to enable attestation find_attestation_card.click fill_in "Titre de l’attestation", with: 'BOOM' @@ -61,4 +63,62 @@ describe 'As an administrateur, I want to manage the procedure’s attestation', find_attestation_card(with_nested_selector: ".fr-badge") end end + + context 'Update attestation v2' do + before { Flipper.enable(:attestation_v2) } + + scenario do + visit admin_procedure_path(procedure) + find_attestation_card(with_nested_selector: ".fr-badge") + + find_attestation_card.click + within(".fr-alert", text: /Nouvel éditeur/) do + find("a").click + end + + expect(procedure.reload.attestation_template_v2).to be_nil + + expect(page).to have_css("label", text: "Logo additionnel") + + fill_in "Intitulé du logo Marianne", with: "System Test" + fill_in "Intitulé de la direction", with: "The boss" + + attestation = nil + wait_until { + attestation = procedure.reload.attestation_template_v2 + attestation.present? + } + expect(attestation.label_logo).to eq("System Test") + expect(attestation.activated?).to be_falsey + + click_on "date de décision" + + # TODO find a way to fill in tiptap + + attach_file('Tampon ou signature', Rails.root + 'spec/fixtures/files/white.png') + wait_until { attestation.reload.signature.attached? } + + fill_in "Contenu du pied de page", with: "Footer" + + wait_until { + body = JSON.parse(attestation.reload.tiptap_body) + body.dig("content").first&.dig("content")&.first&.dig("content")&.first&.dig("content") == [{ "type" => "mention", "attrs" => { "id" => "dossier_processed_at", "label" => "date de décision" } }] + } + + find("label", text: /à la charte de l’état/).click + + expect(page).not_to have_css("label", text: "Logo additionnel", visible: true) + expect(page).not_to have_css("label", text: "Intitulé du logo", visible: true) + + attach_file('Logo', Rails.root + 'spec/fixtures/files/black.png') + + wait_until { + attestation.reload.logo.attached? && attestation.signature.attached? && !attestation.official_layout? + } + + # footer is rows-limited + fill_in "Contenu du pied de page", with: ["line1", "line2", "line3", "line4"].join("\n") + expect(page).to have_field("Contenu du pied de page", with: "line1\nline2\nline3line4") + end + end end