demarches-normaliennes/spec/models/champs/piece_justificative_champ_spec.rb

71 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'active_storage_validations/matchers'
describe Champs::PieceJustificativeChamp do
include ActiveStorageValidations::Matchers
describe "validations" do
let(:champ) { Champs::PieceJustificativeChamp.new }
subject { champ }
before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_piece_justificative)) }
context "by default" do
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') }
it { expect(champ.type_de_champ.skip_pj_validation).to be_falsy }
end
context "when validation is disabled" do
before { champ.type_de_champ.update(skip_pj_validation: true) }
it { is_expected.not_to validate_size_of(:piece_justificative_file).less_than(Champs::PieceJustificativeChamp::FILE_MAX_SIZE) }
end
2021-01-18 12:34:52 +01:00
context "when content-type validation is disabled" do
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
end
describe "#for_export" do
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 }
it { is_expected.to eq('toto.txt') }
context 'without attached file' do
before { champ.piece_justificative_file.purge }
2024-04-23 10:14:19 +02:00
it { is_expected.to eq(nil) }
end
end
describe '#for_api' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:champ) { dossier.champs.first }
before { champ.piece_justificative_file.first.blob.update(virus_scan_result:) }
subject { champ.for_api }
context 'when file is safe' do
let(:virus_scan_result) { ActiveStorage::VirusScanner::SAFE }
it { is_expected.to include("/rails/active_storage/disk/") }
end
context 'when file is not scanned' do
let(:virus_scan_result) { ActiveStorage::VirusScanner::PENDING }
it { is_expected.to include("/rails/active_storage/disk/") }
end
context 'when file is infected' do
let(:virus_scan_result) { ActiveStorage::VirusScanner::INFECTED }
it { is_expected.to be_nil }
end
end
end