2018-01-25 17:41:57 +01:00
|
|
|
|
include ActionDispatch::TestProcess
|
2020-03-30 11:42:18 +02:00
|
|
|
|
|
2017-06-09 13:28:47 +02:00
|
|
|
|
describe Admin::AttestationTemplatesController, type: :controller do
|
|
|
|
|
let!(:attestation_template) { create(:attestation_template) }
|
|
|
|
|
let(:admin) { create(:administrateur) }
|
|
|
|
|
let!(:procedure) { create :procedure, administrateur: admin, attestation_template: attestation_template }
|
2018-10-16 00:01:53 +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') }
|
2020-06-22 17:20:32 +02:00
|
|
|
|
let(:invalid_logo) { fixture_file_upload('spec/fixtures/files/invalid_file_format.json', 'application/json') }
|
2017-06-09 13:28:47 +02:00
|
|
|
|
|
|
|
|
|
before do
|
2019-08-09 10:46:39 +02:00
|
|
|
|
sign_in(admin.user)
|
2018-10-25 15:11:12 +02:00
|
|
|
|
Timecop.freeze(Time.zone.now)
|
2017-05-31 17:39:15 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-11-29 16:07:39 +01:00
|
|
|
|
after { Timecop.return }
|
|
|
|
|
|
2020-03-30 11:42:18 +02:00
|
|
|
|
shared_examples 'rendering a PDF successfully' do
|
|
|
|
|
render_views
|
|
|
|
|
it 'renders a PDF' do
|
|
|
|
|
expect(subject.status).to eq(200)
|
|
|
|
|
expect(subject.content_type).to eq('application/pdf')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-05-31 17:39:15 +02:00
|
|
|
|
describe 'POST #preview' do
|
|
|
|
|
let(:upload_params) { { title: 't', body: 'b', footer: 'f' } }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
post :preview,
|
2018-01-15 15:34:26 +01:00
|
|
|
|
params: {
|
|
|
|
|
procedure_id: procedure.id,
|
|
|
|
|
attestation_template: upload_params
|
|
|
|
|
}
|
2020-02-24 11:17:52 +01:00
|
|
|
|
procedure.reload
|
2017-05-31 17:39:15 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-07-05 16:05:37 +02:00
|
|
|
|
context 'with an interlaced png' do
|
|
|
|
|
let(:upload_params) { { logo: interlaced_logo } }
|
2020-06-22 17:20:32 +02:00
|
|
|
|
|
|
|
|
|
it 'displays the logo' do
|
|
|
|
|
expect(assigns(:attestation)[:logo].read).to eq(uninterlaced_logo.read)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'doesn’t save the logo permanently yet' do
|
|
|
|
|
expect(procedure.attestation_template.logo.attached?).to be(false)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with an invalid logo' do
|
|
|
|
|
let(:upload_params) { { logo: invalid_logo } }
|
|
|
|
|
it { expect(procedure.attestation_template.logo.attached?).to be false }
|
|
|
|
|
it { expect(flash.alert).to be_present }
|
2017-07-05 16:05:37 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-05-31 17:39:15 +02:00
|
|
|
|
context 'if an attestation template does not exist on the procedure' do
|
|
|
|
|
let(:attestation_template) { nil }
|
2020-03-30 11:42:18 +02:00
|
|
|
|
|
2020-03-30 14:48:25 +02:00
|
|
|
|
context 'with images' do
|
|
|
|
|
let(:upload_params) { { title: 't', body: 'b', footer: 'f', logo: interlaced_logo } }
|
|
|
|
|
|
|
|
|
|
it { expect(assigns(:attestation)).to include({ title: 't', body: 'b', footer: 'f' }) }
|
|
|
|
|
it { expect(assigns(:attestation)[:logo]).to be_present }
|
|
|
|
|
it_behaves_like 'rendering a PDF successfully'
|
|
|
|
|
end
|
|
|
|
|
|
2020-03-30 11:42:18 +02:00
|
|
|
|
context 'without images' do
|
|
|
|
|
let(:upload_params) { { title: 't', body: 'b', footer: 'f' } }
|
|
|
|
|
|
|
|
|
|
it { expect(assigns(:attestation)).to include(upload_params) }
|
|
|
|
|
it_behaves_like 'rendering a PDF successfully'
|
|
|
|
|
end
|
2017-05-31 17:39:15 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'if an attestation template exists on the procedure' do
|
2019-09-10 11:09:51 +02:00
|
|
|
|
after { procedure.attestation_template.destroy }
|
|
|
|
|
|
2020-03-30 11:42:18 +02:00
|
|
|
|
context 'with images' do
|
2017-05-31 17:39:15 +02:00
|
|
|
|
let!(:attestation_template) do
|
|
|
|
|
create(:attestation_template, logo: logo, signature: signature)
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-28 14:27:41 +02:00
|
|
|
|
it { expect(assigns(:attestation)).to include(upload_params) }
|
|
|
|
|
it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
|
|
|
|
|
it { expect(assigns(:attestation)[:logo].download).to eq(logo2.read) }
|
|
|
|
|
it { expect(assigns(:attestation)[:signature].download).to eq(signature2.read) }
|
2020-03-30 11:42:18 +02:00
|
|
|
|
it_behaves_like 'rendering a PDF successfully'
|
2019-09-10 11:09:51 +02:00
|
|
|
|
end
|
|
|
|
|
|
2020-03-30 11:42:18 +02:00
|
|
|
|
context 'without images' do
|
2019-08-28 14:27:41 +02:00
|
|
|
|
it { expect(assigns(:attestation)).to include(upload_params) }
|
|
|
|
|
it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
|
|
|
|
|
it { expect(assigns(:attestation)[:logo]).to eq(nil) }
|
|
|
|
|
it { expect(assigns(:attestation)[:signature]).to eq(nil) }
|
2020-03-30 11:42:18 +02:00
|
|
|
|
it_behaves_like 'rendering a PDF successfully'
|
2017-05-31 17:39:15 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2017-06-09 13:28:47 +02:00
|
|
|
|
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 }
|
|
|
|
|
it { expect(subject.status).to eq(200) }
|
|
|
|
|
it { expect(assigns(:attestation_template).id).to be_nil }
|
|
|
|
|
it { expect(assigns(:attestation_template)).to be_an_instance_of(AttestationTemplate) }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'POST #create' do
|
|
|
|
|
let(:attestation_template) { nil }
|
|
|
|
|
let(:attestation_params) { { title: 't', body: 'b', footer: 'f' } }
|
|
|
|
|
|
|
|
|
|
context 'nominal' do
|
|
|
|
|
before do
|
|
|
|
|
post :create,
|
2018-01-15 15:34:26 +01:00
|
|
|
|
params: {
|
|
|
|
|
procedure_id: procedure.id,
|
|
|
|
|
attestation_template: attestation_params.merge(logo: logo, signature: signature)
|
|
|
|
|
}
|
2017-06-09 13:28:47 +02:00
|
|
|
|
procedure.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(procedure.attestation_template).to have_attributes(attestation_params) }
|
|
|
|
|
it { expect(procedure.attestation_template.activated).to be true }
|
2019-08-28 13:11:58 +02:00
|
|
|
|
it { expect(procedure.attestation_template.logo.download).to eq(logo2.read) }
|
|
|
|
|
it { expect(procedure.attestation_template.signature.download).to eq(signature2.read) }
|
2017-06-09 13:28:47 +02:00
|
|
|
|
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
|
|
|
|
|
it { expect(flash.notice).to eq("L'attestation a bien été sauvegardée") }
|
|
|
|
|
|
|
|
|
|
after { procedure.attestation_template.destroy }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when something wrong happens in the attestation template creation' do
|
|
|
|
|
before do
|
|
|
|
|
expect_any_instance_of(AttestationTemplate).to receive(:save)
|
|
|
|
|
.and_return(false)
|
|
|
|
|
expect_any_instance_of(AttestationTemplate).to receive(:errors)
|
|
|
|
|
.and_return(double(full_messages: ['nop']))
|
|
|
|
|
|
|
|
|
|
post :create,
|
2018-01-15 15:34:26 +01:00
|
|
|
|
params: {
|
|
|
|
|
procedure_id: procedure.id,
|
|
|
|
|
attestation_template: attestation_params
|
|
|
|
|
}
|
2017-06-09 13:28:47 +02:00
|
|
|
|
procedure.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
|
|
|
|
|
it { expect(flash.alert).to eq('nop') }
|
|
|
|
|
end
|
|
|
|
|
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,
|
2018-01-15 15:34:26 +01:00
|
|
|
|
params: {
|
|
|
|
|
procedure_id: procedure.id,
|
|
|
|
|
attestation_template: attestation_params_with_images
|
|
|
|
|
}
|
2017-06-09 13:28:47 +02:00
|
|
|
|
procedure.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(procedure.attestation_template).to have_attributes(attestation_params) }
|
2019-08-28 13:11:58 +02:00
|
|
|
|
it { expect(procedure.attestation_template.logo.download).to eq(logo2.read) }
|
|
|
|
|
it { expect(procedure.attestation_template.signature.download).to eq(signature2.read) }
|
2017-06-09 13:28:47 +02:00
|
|
|
|
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
|
|
|
|
|
it { expect(flash.notice).to eq("L'attestation a bien été modifiée") }
|
|
|
|
|
|
|
|
|
|
after { procedure.attestation_template.destroy }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when something wrong happens in the attestation template creation' do
|
|
|
|
|
before do
|
2018-03-02 16:27:03 +01:00
|
|
|
|
expect_any_instance_of(AttestationTemplate).to receive(:update).and_return(false)
|
2017-06-09 13:28:47 +02:00
|
|
|
|
expect_any_instance_of(AttestationTemplate).to receive(:errors)
|
|
|
|
|
.and_return(double(full_messages: ['nop']))
|
|
|
|
|
|
|
|
|
|
patch :update,
|
2018-01-15 15:34:26 +01:00
|
|
|
|
params: {
|
|
|
|
|
procedure_id: procedure.id,
|
|
|
|
|
attestation_template: attestation_params_with_images
|
|
|
|
|
}
|
2017-06-09 13:28:47 +02:00
|
|
|
|
procedure.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
|
|
|
|
|
it { expect(flash.alert).to eq('nop') }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'post #disactivate' do
|
|
|
|
|
context 'when the attestation_template is activated' do
|
|
|
|
|
let(:attestation_template) { create(:attestation_template, activated: true) }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
post :disactivate, params: { procedure_id: procedure.id }
|
|
|
|
|
attestation_template.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(attestation_template.activated).to be false }
|
|
|
|
|
it { expect(flash.notice).to eq("L'attestation a bien été désactivée") }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|