2020-03-26 17:28:24 +01:00
|
|
|
require 'active_storage_validations/matchers'
|
|
|
|
|
2019-02-05 20:57:58 +01:00
|
|
|
describe Champs::PieceJustificativeChamp do
|
2020-03-26 17:28:24 +01:00
|
|
|
include ActiveStorageValidations::Matchers
|
|
|
|
|
2020-07-08 14:46:31 +02:00
|
|
|
describe "validations" do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:champ) { Champs::PieceJustificativeChamp.new }
|
|
|
|
subject { champ }
|
|
|
|
before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_piece_justificative)) }
|
2020-07-15 16:06:24 +02:00
|
|
|
|
|
|
|
context "by default" do
|
2021-09-14 18:03:40 +02:00
|
|
|
it { is_expected.to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::FILE_MAX_SIZE) }
|
2021-01-18 12:34:52 +01:00
|
|
|
it { is_expected.to validate_content_type_of(:piece_justificative_file).rejecting('application/x-ms-dos-executable') }
|
2024-07-01 15:31:32 +02:00
|
|
|
it { expect(champ.type_de_champ.skip_pj_validation).to be_falsy }
|
2020-07-15 16:06:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when validation is disabled" do
|
2024-07-01 15:31:32 +02:00
|
|
|
before { champ.type_de_champ.update(skip_pj_validation: true) }
|
2020-03-26 17:28:24 +01:00
|
|
|
|
2021-09-14 18:03:40 +02:00
|
|
|
it { is_expected.not_to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::FILE_MAX_SIZE) }
|
2020-07-15 16:06:24 +02:00
|
|
|
end
|
2021-01-18 12:34:52 +01:00
|
|
|
|
|
|
|
context "when content-type validation is disabled" do
|
2024-07-01 15:31:32 +02:00
|
|
|
before { champ.type_de_champ.update(skip_content_type_pj_validation: true) }
|
2021-01-18 12:34:52 +01:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_content_type_of(:piece_justificative_file).rejecting('application/x-ms-dos-executable') }
|
|
|
|
end
|
2020-03-26 17:28:24 +01:00
|
|
|
end
|
|
|
|
|
2020-05-13 16:24:35 +02:00
|
|
|
describe "#for_export" do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) }
|
|
|
|
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
|
|
|
let(:champ) { dossier.champs.first }
|
|
|
|
subject { champ.for_export }
|
2020-05-13 16:24:35 +02:00
|
|
|
|
2022-12-06 15:05:37 +01:00
|
|
|
it { is_expected.to eq('toto.txt') }
|
2020-05-13 16:24:35 +02:00
|
|
|
|
|
|
|
context 'without attached file' do
|
2024-07-01 15:31:32 +02:00
|
|
|
before { champ.piece_justificative_file.purge }
|
2024-04-23 10:14:19 +02:00
|
|
|
it { is_expected.to eq(nil) }
|
2020-05-13 16:24:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-05 20:57:58 +01:00
|
|
|
describe '#for_api' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) }
|
|
|
|
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
|
|
|
let(:champ) { dossier.champs.first }
|
2019-02-05 20:57:58 +01:00
|
|
|
|
2024-07-01 15:31:32 +02:00
|
|
|
before { champ.piece_justificative_file.first.blob.update(virus_scan_result:) }
|
2019-02-05 20:57:58 +01:00
|
|
|
|
2024-07-01 15:31:32 +02:00
|
|
|
subject { champ.for_api }
|
2019-02-05 20:57:58 +01:00
|
|
|
|
|
|
|
context 'when file is safe' do
|
2022-12-22 18:20:58 +01:00
|
|
|
let(:virus_scan_result) { ActiveStorage::VirusScanner::SAFE }
|
2022-12-06 14:41:03 +01:00
|
|
|
it { is_expected.to include("/rails/active_storage/disk/") }
|
2019-02-05 20:57:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is not scanned' do
|
2022-12-22 18:20:58 +01:00
|
|
|
let(:virus_scan_result) { ActiveStorage::VirusScanner::PENDING }
|
2022-12-06 14:41:03 +01:00
|
|
|
it { is_expected.to include("/rails/active_storage/disk/") }
|
2019-02-05 20:57:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is infected' do
|
2022-12-22 18:20:58 +01:00
|
|
|
let(:virus_scan_result) { ActiveStorage::VirusScanner::INFECTED }
|
2022-12-06 14:41:03 +01:00
|
|
|
it { is_expected.to be_nil }
|
2019-02-05 20:57:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|