Final implement of clamav gem

This commit is contained in:
Xavier J 2016-04-15 15:32:15 +02:00
parent 4099efd326
commit 900b377ae2
5 changed files with 37 additions and 16 deletions

View file

@ -56,7 +56,7 @@ class Users::DescriptionController < UsersController
end end
unless (errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)).empty? unless (errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)).empty?
flash.alert = errors_upload.full_messages.joins('<br>').html_safe flash.alert = errors_upload.html_safe
return render 'show' return render 'show'
end end

View file

@ -1,9 +1,12 @@
class ClamavService class ClamavService
def self.safe_io_data? path_file def self.safe_file? path_file
client = ClamAV::Client.new
FileUtils.chmod 0666, path_file
client = ClamAV::Client.new
response = client.execute(ClamAV::Commands::ScanCommand.new(path_file)) response = client.execute(ClamAV::Commands::ScanCommand.new(path_file))
puts response return false if response.first.class == ClamAV::VirusResponse
true
end end
end end

View file

@ -5,20 +5,18 @@ class PiecesJustificativesService
dossier.types_de_piece_justificative.each do |type_de_pieces_justificatives| dossier.types_de_piece_justificative.each do |type_de_pieces_justificatives|
unless params["piece_justificative_#{type_de_pieces_justificatives.id}"].nil? unless params["piece_justificative_#{type_de_pieces_justificatives.id}"].nil?
# unless ClamavService.safe_io_data? params["piece_justificative_#{type_de_pieces_justificatives.id}"].path if ClamavService.safe_file? params["piece_justificative_#{type_de_pieces_justificatives.id}"].path
# piece_justificative = PieceJustificative.new(content: params["piece_justificative_#{type_de_pieces_justificatives.id}"],
# end dossier: dossier,
type_de_piece_justificative: type_de_pieces_justificatives,
user: user)
piece_justificative = PieceJustificative.new(content: params["piece_justificative_#{type_de_pieces_justificatives.id}"], unless piece_justificative.save
dossier: dossier, errors << piece_justificative.errors.messages[:content][0]+" (#{piece_justificative.libelle})"+"<br>"
type_de_piece_justificative: type_de_pieces_justificatives, end
user: user) else
errors << params["piece_justificative_#{type_de_pieces_justificatives.id}"].original_filename+": <b>Virus détecté !!</b>"+"<br>"
unless piece_justificative.save
errors << piece_justificative.errors.messages[:content][0]+" (#{piece_justificative.libelle})"+"<br>"
end end
end end
end end
errors errors

View file

@ -18,6 +18,8 @@ describe Users::DescriptionController, type: :controller do
let(:piece_justificative_1) { Rack::Test::UploadedFile.new("./spec/support/files/#{name_piece_justificative_1}", 'application/pdf') } let(:piece_justificative_1) { Rack::Test::UploadedFile.new("./spec/support/files/#{name_piece_justificative_1}", 'application/pdf') }
before do before do
allow(ClamavService).to receive(:safe_file?).and_return(true)
sign_in dossier.user sign_in dossier.user
end end
@ -221,6 +223,20 @@ describe Users::DescriptionController, type: :controller do
dossier.reload dossier.reload
end end
describe 'clamav anti-virus presence' do
it 'ClamavService safe_file? is call' do
expect(ClamavService).to receive(:safe_file?).twice
post :create, {dossier_id: dossier_id,
nom_projet: nom_projet,
description: description,
'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0,
'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1}
end
end
context 'for piece 0' do context 'for piece 0' do
subject { dossier.retrieve_last_piece_justificative_by_type all_pj_type[0].to_s } subject { dossier.retrieve_last_piece_justificative_by_type all_pj_type[0].to_s }
it { expect(subject.content).not_to be_nil } it { expect(subject.content).not_to be_nil }

View file

@ -3,7 +3,10 @@ require 'spec_helper'
feature 'user is on description page' do feature 'user is on description page' do
let!(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, cerfa_flag: true) } let!(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, cerfa_flag: true) }
let!(:dossier) { create(:dossier, :with_entreprise, procedure: procedure) } let!(:dossier) { create(:dossier, :with_entreprise, procedure: procedure) }
before do before do
allow(ClamavService).to receive(:safe_file?).and_return(true)
visit users_dossier_description_path dossier visit users_dossier_description_path dossier
within('#new_user') do within('#new_user') do
@ -13,6 +16,7 @@ feature 'user is on description page' do
end end
end end
it { expect(page).to have_css('#description_page') } it { expect(page).to have_css('#description_page') }
context 'he fill description fields' do context 'he fill description fields' do