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-15 16:06:24 +02:00
|
|
|
describe "skip_validation is not set anymore" do
|
2020-07-09 11:45:20 +02:00
|
|
|
subject { champ_pj.type_de_champ.skip_pj_validation }
|
|
|
|
|
|
|
|
context 'before_save' do
|
|
|
|
let(:champ_pj) { build (:champ_piece_justificative) }
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
context 'after_save' do
|
|
|
|
let(:champ_pj) { create (:champ_piece_justificative) }
|
2020-07-15 16:06:24 +02:00
|
|
|
it { is_expected.to be_falsy }
|
2020-07-09 11:45:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-08 14:46:31 +02:00
|
|
|
describe "validations" do
|
2020-07-15 16:06:24 +02:00
|
|
|
let(:champ_pj) { create(:champ_piece_justificative) }
|
|
|
|
subject { champ_pj }
|
|
|
|
|
|
|
|
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') }
|
2020-07-15 16:06:24 +02:00
|
|
|
it { expect(champ_pj.type_de_champ.skip_pj_validation).to be_falsy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when validation is disabled" do
|
|
|
|
before { champ_pj.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
|
|
|
|
before { champ_pj.type_de_champ.update(skip_content_type_pj_validation: true) }
|
|
|
|
|
|
|
|
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
|
|
|
|
let(:champ_pj) { create(:champ_piece_justificative) }
|
|
|
|
subject { champ_pj.for_export }
|
|
|
|
|
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
|
|
|
|
before { champ_pj.piece_justificative_file.purge }
|
2022-12-06 15:05:37 +01:00
|
|
|
it { is_expected.to eq('') }
|
2020-05-13 16:24:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-05 20:57:58 +01:00
|
|
|
describe '#for_api' do
|
|
|
|
let(:champ_pj) { create(:champ_piece_justificative) }
|
|
|
|
|
2022-12-22 18:20:58 +01:00
|
|
|
before { champ_pj.piece_justificative_file.first.blob.update(virus_scan_result:) }
|
2019-02-05 20:57:58 +01:00
|
|
|
|
|
|
|
subject { champ_pj.for_api }
|
|
|
|
|
|
|
|
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
|