2021-11-25 16:26:55 +01:00
describe Administrateurs :: AttestationTemplatesController , type : :controller do
2020-07-29 12:20:05 +02:00
let ( :admin ) { create ( :administrateur ) }
2022-02-02 16:10:29 +01:00
let ( :attestation_template ) { build ( :attestation_template ) }
2022-11-09 12:09:24 +01:00
let ( :procedure ) { create ( :procedure , administrateur : admin , attestation_template : attestation_template ) }
2020-07-29 12:20:05 +02:00
let ( :logo ) { fixture_file_upload ( 'spec/fixtures/files/white.png' , 'image/png' ) }
let ( :logo2 ) { fixture_file_upload ( 'spec/fixtures/files/white.png' , 'image/png' ) }
let ( :signature ) { fixture_file_upload ( 'spec/fixtures/files/black.png' , 'image/png' ) }
let ( :signature2 ) { fixture_file_upload ( 'spec/fixtures/files/black.png' , 'image/png' ) }
let ( :interlaced_logo ) { fixture_file_upload ( 'spec/fixtures/files/interlaced-black.png' , 'image/png' ) }
let ( :uninterlaced_logo ) { fixture_file_upload ( 'spec/fixtures/files/uninterlaced-black.png' , 'image/png' ) }
let ( :invalid_logo ) { fixture_file_upload ( 'spec/fixtures/files/invalid_file_format.json' , 'application/json' ) }
before do
2022-02-11 08:43:29 +01:00
procedure
2020-07-29 12:20:05 +02:00
sign_in ( admin . user )
Timecop . freeze ( Time . zone . now )
end
after { Timecop . return }
shared_examples 'rendering a PDF successfully' do
render_views
it 'renders a PDF' do
expect ( subject . status ) . to eq ( 200 )
2020-07-13 17:36:28 +02:00
expect ( subject . media_type ) . to eq ( 'application/pdf' )
2020-07-29 12:20:05 +02:00
end
end
describe 'GET #preview' do
let ( :attestation_params ) do
{ title : 't' , body : 'b' , footer : 'f' }
end
before do
get :preview ,
params : {
procedure_id : procedure . id
}
end
context 'if an attestation template exists on the procedure' do
context 'with images' do
2022-11-09 12:09:24 +01:00
let ( :attestation_template ) { build ( :attestation_template , attestation_params . merge ( logo : logo , signature : signature ) ) }
2020-07-29 12:20:05 +02:00
2022-11-09 12:09:24 +01:00
it do
expect ( assigns ( :attestation ) ) . to include ( attestation_params )
expect ( assigns ( :attestation ) [ :created_at ] ) . to eq ( Time . zone . now )
expect ( assigns ( :attestation ) [ :logo ] . download ) . to eq ( logo2 . read )
expect ( assigns ( :attestation ) [ :signature ] . download ) . to eq ( signature2 . read )
end
2020-07-29 12:20:05 +02:00
it_behaves_like 'rendering a PDF successfully'
end
context 'without images' do
2022-11-09 12:09:24 +01:00
let ( :attestation_template ) { build ( :attestation_template , attestation_params ) }
2020-07-29 12:20:05 +02:00
2022-11-09 12:09:24 +01:00
it do
expect ( assigns ( :attestation ) ) . to include ( attestation_params )
expect ( assigns ( :attestation ) [ :created_at ] ) . to eq ( Time . zone . now )
expect ( assigns ( :attestation ) [ :logo ] ) . to eq ( nil )
2023-09-19 15:41:51 +02:00
expect ( assigns ( :attestation ) [ :signature ] ) . not_to be_attached
2022-11-09 12:09:24 +01:00
end
2020-07-29 12:20:05 +02:00
it_behaves_like 'rendering a PDF successfully'
end
2020-09-25 15:19:59 +02:00
context 'with empty footer' do
2022-11-09 12:09:24 +01:00
let ( :attestation_template ) { build ( :attestation_template , { title : 't' , body : 'b' , footer : nil } ) }
2020-09-25 15:19:59 +02:00
it_behaves_like 'rendering a PDF successfully'
end
context 'with large footer' do
2022-11-09 12:09:24 +01:00
let ( :attestation_params ) { build ( :attestation_template , { title : 't' , body : 'b' , footer : ' ' * 190 } ) }
2020-09-25 15:19:59 +02:00
it_behaves_like 'rendering a PDF successfully'
end
2020-07-29 12:20:05 +02:00
end
end
describe 'GET #edit' do
before { get :edit , params : { procedure_id : procedure . id } }
context 'if an attestation template exists on the procedure' do
it { expect ( subject . status ) . to eq ( 200 ) }
it { expect ( assigns ( :attestation_template ) ) . to eq ( attestation_template ) }
end
context 'if an attestation template does not exist on the procedure' do
let ( :attestation_template ) { nil }
2022-11-09 12:09:24 +01:00
it do
expect ( subject . status ) . to eq ( 200 )
expect ( assigns ( :attestation_template ) . id ) . to be_nil
expect ( assigns ( :attestation_template ) ) . to be_an_instance_of ( AttestationTemplate )
end
2020-07-29 12:20:05 +02:00
end
end
describe 'POST #create' do
let ( :attestation_template ) { nil }
let ( :attestation_params ) { { title : 't' , body : 'b' , footer : 'f' , activated : true } }
context 'nominal' do
before do
post :create ,
params : {
procedure_id : procedure . id ,
attestation_template : attestation_params . merge ( logo : logo , signature : signature )
}
end
2022-11-09 12:09:24 +01:00
it do
expect ( procedure . attestation_template ) . to have_attributes ( attestation_params )
expect ( procedure . attestation_template . activated ) . to be true
expect ( procedure . attestation_template . logo . download ) . to eq ( logo2 . read )
expect ( procedure . attestation_template . signature . download ) . to eq ( signature2 . read )
expect ( response ) . to redirect_to edit_admin_procedure_attestation_template_path ( procedure )
2022-12-15 17:08:24 +01:00
expect ( flash . notice ) . to eq ( " Le modèle de l’ attestation a bien été enregistré " )
2022-11-09 12:09:24 +01:00
end
2020-07-29 12:20:05 +02:00
end
context 'when something wrong happens in the attestation template creation' do
let ( :invalid_footer ) { 'f' * 200 }
let ( :attestation_params ) { { title : 't' , body : 'b' , footer : invalid_footer , activated : true } }
before do
post :create ,
params : {
procedure_id : procedure . id ,
attestation_template : attestation_params
}
end
2022-11-09 12:09:24 +01:00
it do
expect ( flash . alert ) . to be_present
2022-11-24 15:49:15 +01:00
expect ( procedure . attestation_template ) . to be nil
2022-11-09 12:09:24 +01:00
end
2020-07-29 12:20:05 +02:00
end
2022-02-11 08:43:29 +01:00
context 'when procedure is published' do
2022-11-09 12:09:24 +01:00
let ( :procedure ) { create ( :procedure , :published , administrateur : admin ) }
2022-02-11 08:43:29 +01:00
let ( :attestation_template ) { nil }
let ( :attestation_params ) { { title : 't' , body : 'b' , footer : '' , activated : true } }
let ( :revisions_enabled ) { false }
before do
if revisions_enabled
Flipper . enable ( :procedure_revisions , procedure )
end
post :create ,
params : {
procedure_id : procedure . id ,
attestation_template : attestation_params
}
end
2022-11-09 12:09:24 +01:00
it do
expect ( procedure . attestation_template ) . to eq ( procedure . attestation_template )
expect ( procedure . attestation_template . title ) . to eq ( 't' )
2022-02-11 08:43:29 +01:00
end
end
2020-07-29 12:20:05 +02:00
end
describe 'PATCH #update' do
let ( :attestation_params ) { { title : 't' , body : 'b' , footer : 'f' } }
let ( :attestation_params_with_images ) { attestation_params . merge ( logo : logo , signature : signature ) }
context 'nominal' do
before do
patch :update ,
params : {
procedure_id : procedure . id ,
attestation_template : attestation_params_with_images
}
end
2022-11-09 12:09:24 +01:00
it do
expect ( procedure . attestation_template ) . to have_attributes ( attestation_params )
expect ( procedure . attestation_template . logo . download ) . to eq ( logo2 . read )
expect ( procedure . attestation_template . signature . download ) . to eq ( signature2 . read )
expect ( response ) . to redirect_to edit_admin_procedure_attestation_template_path ( procedure )
2022-12-16 14:56:46 +01:00
expect ( flash . notice ) . to eq ( " Le modèle de l’ attestation a bien été modifié " )
2020-07-29 12:20:05 +02:00
end
end
2022-02-11 08:43:29 +01:00
context 'when procedure is published' do
2024-01-23 09:52:20 +01:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :text } , { type : :text } , { type : :text } ] , administrateur : admin , attestation_template : attestation_template ) }
2022-11-09 12:09:24 +01:00
let ( :dossier ) { }
let ( :attestation_template ) { build ( :attestation_template , title : 'a' ) }
let ( :attestation_params ) do
{
title : title ,
body : body ,
footer : '' ,
activated : true
}
end
let ( :type_de_champ ) { procedure . draft_revision . types_de_champ_public [ 0 ] }
let ( :removed_type_de_champ ) { procedure . draft_revision . types_de_champ_public [ 1 ] }
let ( :removed_and_published_type_de_champ ) { procedure . draft_revision . types_de_champ_public [ 2 ] }
let ( :new_type_de_champ ) { procedure . draft_revision . types_de_champ_public . find_by ( libelle : 'new type de champ' ) }
let ( :draft_type_de_champ ) { procedure . draft_revision . types_de_champ_public . find_by ( libelle : 'draft type de champ' ) }
let ( :title ) { 'title --numéro du dossier--' }
let ( :body ) { " body -- #{ type_de_champ . libelle } -- et -- #{ new_type_de_champ . libelle } -- " }
2022-02-11 08:43:29 +01:00
before do
2022-11-09 12:09:24 +01:00
procedure . publish!
procedure . reload
procedure . draft_revision . remove_type_de_champ ( removed_and_published_type_de_champ . stable_id )
2024-01-23 09:52:20 +01:00
procedure . draft_revision . add_type_de_champ ( libelle : 'new type de champ' , type_champ : 'text' , after_stable_id : procedure . draft_revision . types_de_champ_public . last . stable_id )
2022-11-09 12:09:24 +01:00
procedure . publish_revision!
procedure . reload
procedure . draft_revision . remove_type_de_champ ( removed_type_de_champ . stable_id )
2024-01-23 09:52:20 +01:00
procedure . draft_revision . reload
procedure . draft_revision . add_type_de_champ ( libelle : 'draft type de champ' , type_champ : 'text' , after_stable_id : procedure . draft_revision . types_de_champ_public . last . stable_id )
2022-11-09 12:09:24 +01:00
dossier
2022-02-11 08:43:29 +01:00
patch :update ,
params : {
procedure_id : procedure . id ,
attestation_template : attestation_params
}
end
2022-11-09 12:09:24 +01:00
context 'normal' do
2022-02-11 08:43:29 +01:00
it do
2022-11-09 12:09:24 +01:00
expect ( response ) . to redirect_to edit_admin_procedure_attestation_template_path ( procedure )
expect ( procedure . attestation_template . title ) . to eq ( title )
expect ( procedure . attestation_template . body ) . to eq ( body )
2022-02-11 08:43:29 +01:00
end
end
2022-11-09 12:09:24 +01:00
context 'with invalid tag' do
let ( :body ) { 'body --yolo--' }
2022-12-16 14:56:46 +01:00
it { expect ( flash . alert ) . to eq ( " Le modèle de l’ attestation contient des erreurs et n'a pas pu être enregistré. Veuiller les corriger " ) }
2022-11-09 12:09:24 +01:00
end
context 'with removed champ' do
2022-12-15 13:08:20 +01:00
render_views
2022-11-09 12:09:24 +01:00
let ( :body ) { " body -- #{ removed_type_de_champ . libelle } -- " }
2022-12-16 14:56:46 +01:00
it { expect ( response . body ) . to have_content ( " Le champ « Contenu de l’ attestation » contient la balise \" #{ removed_type_de_champ . libelle } \" qui a été supprimée mais la suppression n’ est pas encore publiée. Publier la nouvelle version de la démarche et recommencer " ) }
2022-11-09 12:09:24 +01:00
end
context 'with removed and published' do
2022-12-15 13:08:20 +01:00
render_views
2022-11-09 12:09:24 +01:00
let ( :body ) { " body -- #{ removed_and_published_type_de_champ . libelle } -- " }
2022-12-16 14:56:46 +01:00
it { expect ( response . body ) . to have_content ( " Le champ « Contenu de l’ attestation » contient la balise \" #{ removed_and_published_type_de_champ . libelle } \" qui a été supprimée. " ) }
2022-11-09 12:09:24 +01:00
end
context 'with new champ missing on dossier submitted on previous revision' do
2022-12-15 13:08:20 +01:00
render_views
2022-11-09 12:09:24 +01:00
let ( :dossier ) { create ( :dossier , :en_construction , procedure : procedure , revision : procedure . revisions . first ) }
let ( :body ) { " body -- #{ new_type_de_champ . libelle } -- " }
2022-12-16 14:56:46 +01:00
it { expect ( response . body ) . to have_content ( " Le champ « Contenu de l’ attestation » contient la balise \" #{ new_type_de_champ . libelle } \" qui n’ existe pas sur un des dossiers en cours de traitement " ) }
2022-11-09 12:09:24 +01:00
end
context 'with champ on draft' do
2022-12-15 13:08:20 +01:00
render_views
2022-11-09 12:09:24 +01:00
let ( :body ) { " body -- #{ draft_type_de_champ . libelle } -- " }
2022-12-16 14:56:46 +01:00
it { expect ( response . body ) . to have_content ( " Le champ « Contenu de l’ attestation » contient la balise \" #{ draft_type_de_champ . libelle } \" qui n’ est pas encore publiée " ) }
2022-02-11 08:43:29 +01:00
end
end
2020-07-29 12:20:05 +02:00
end
end