MandatoryAttachment: group missing errors logic together

and show errors only if not draft
This commit is contained in:
Simon Lehericey 2017-03-30 16:12:01 +02:00
parent 6c7ed9d04a
commit 2b969ef575
6 changed files with 103 additions and 102 deletions

View file

@ -1,15 +1,15 @@
require 'spec_helper'
describe ChampsService do
describe 'save_champs' do
let!(:champ) { Champ.create(value: 'toto', type_de_champ: TypeDeChamp.new) }
let!(:champ_mandatory_empty) { Champ.create(type_de_champ: TypeDeChamp.new(libelle: 'mandatory', mandatory: true)) }
let!(:champ_datetime) do
champ_datetime = TypeDeChamp.new(type_champ: 'datetime')
Champ.create(type_de_champ: champ_datetime)
end
let!(:champs) { [champ, champ_mandatory_empty, champ_datetime] }
let!(:champ) { Champ.create(value: 'toto', type_de_champ: TypeDeChamp.new) }
let!(:champ_mandatory_empty) { Champ.create(type_de_champ: TypeDeChamp.new(libelle: 'mandatory', mandatory: true)) }
let!(:champ_datetime) do
champ_datetime = TypeDeChamp.new(type_champ: 'datetime')
Champ.create(type_de_champ: champ_datetime)
end
let!(:champs) { [champ, champ_mandatory_empty, champ_datetime] }
describe 'save_champs' do
before :each do
params_hash = {
champs: {
@ -19,31 +19,22 @@ describe ChampsService do
time_hour: { "'#{champ_datetime.id}'" => '12' },
time_minute: { "'#{champ_datetime.id}'" => '24' }
}
@errors = ChampsService.save_champs(champs, params_hash, check_mandatory)
ChampsService.save_champs(champs, params_hash)
champs.each(&:reload)
end
context 'check_mandatory is true' do
let(:check_mandatory) { true }
it 'saves the changed champ' do
expect(champ.value).to eq('yop')
end
it 'parses and save the date' do
expect(champ_datetime.value).to eq('d 12:24')
end
it 'adds error for the missing mandatory champ' do
expect(@errors).to match(['Le champ mandatory doit être rempli.'])
end
it 'saves the changed champ' do
expect(champ.value).to eq('yop')
end
context 'check_mandatory is false' do
let(:check_mandatory) { false }
it 'parses and save the date' do
expect(champ_datetime.value).to eq('d 12:24')
end
end
it 'does not add errors' do
expect(@errors).to match([])
end
describe 'build_error_message' do
it 'adds error for the missing mandatory champ' do
expect(ChampsService.build_error_messages(champs)).to match(['Le champ mandatory doit être rempli.'])
end
end
end

View file

@ -8,74 +8,85 @@ describe PiecesJustificativesService do
allow(ClamavService).to receive(:safe_file?).and_return(safe_file)
end
let(:hash) { {} }
let!(:tpj_not_mandatory) do
TypeDePieceJustificative.create(libelle: 'not mandatory', mandatory: false)
end
let!(:tpj_mandatory) do
TypeDePieceJustificative.create(libelle: 'justificatif', mandatory: true)
end
let(:procedure) { Procedure.create(types_de_piece_justificative: tpjs) }
let(:dossier) { Dossier.create(procedure: procedure) }
let(:errors) { PiecesJustificativesService.upload!(dossier, user, hash) }
let(:tpjs) { [tpj_not_mandatory] }
describe 'self.upload!' do
let(:hash) { {} }
let!(:tpj_not_mandatory) do
TypeDePieceJustificative.create(libelle: 'not mandatory', mandatory: false)
end
let!(:tpj_mandatory) do
TypeDePieceJustificative.create(libelle: 'justificatif', mandatory: true)
end
let(:procedure) { Procedure.create(types_de_piece_justificative: tpjs) }
let(:dossier) { Dossier.create(procedure: procedure) }
let(:errors) { PiecesJustificativesService.upload!(dossier, user, hash) }
context 'when no piece justificative is required' do
let(:tpjs) { [tpj_not_mandatory] }
context 'when no params are given' do
it { expect(errors).to eq([]) }
end
context 'when sometihing wrong with file save' do
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'filename')
}
end
it { expect(errors).to match(["le fichier filename (not mandatory) n'a pas pu être sauvegardé"]) }
end
context 'when a virus is provided' do
let(:safe_file) { false }
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'bad_file')
}
end
it { expect(errors).to match(['bad_file : virus détecté']) }
end
context 'when no params are given' do
it { expect(errors).to eq([]) }
end
context 'when a piece justificative is required' do
let(:tpjs) { [tpj_mandatory] }
context 'when no params are given' do
it { expect(errors).to match(['La pièce jointe justificatif doit être fournie.']) }
context 'when there is something wrong with file save' do
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'filename')
}
end
context 'when the piece justificative is provided' do
before :each do
# we are messing around piece_justificative
# because directly doubling carrierwave params seems complicated
it { expect(errors).to match(["le fichier filename (not mandatory) n'a pas pu être sauvegardé"]) }
end
allow(PiecesJustificativesService).to receive(:save_pj).and_return(nil)
piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory)
expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double])
end
context 'when a virus is provided' do
let(:safe_file) { false }
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
double(path: '', original_filename: 'bad_file')
}
end
let(:hash) do
{
"piece_justificative_#{tpj_mandatory.id}" => double(path: '')
}
end
it { expect(errors).to match(['bad_file : virus détecté']) }
end
it { expect(errors).to match([]) }
context 'when a regular file is provided' do
let(:content) { double(path: '', original_filename: 'filename') }
let(:hash) do
{
"piece_justificative_#{tpj_not_mandatory.id}" =>
content
}
end
before :each do
expect(PiecesJustificativesService).to receive(:save_pj)
.with(content, dossier, tpj_not_mandatory, user)
.and_return(nil)
end
it 'is saved' do
expect(errors).to match([])
end
end
end
describe 'missing_pj_error_messages' do
let(:errors) { PiecesJustificativesService.missing_pj_error_messages(dossier) }
let(:tpjs) { [tpj_mandatory] }
context 'when no params are given' do
it { expect(errors).to match(['La pièce jointe justificatif doit être fournie.']) }
end
context 'when the piece justificative is provided' do
before :each do
# we are messing around piece_justificative
# because directly doubling carrierwave params seems complicated
piece_justificative_double = double(type_de_piece_justificative: tpj_mandatory)
expect(dossier).to receive(:pieces_justificatives).and_return([piece_justificative_double])
end
it { expect(errors).to match([]) }
end
end
end