Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Now including antivirus tests on Travis.
Browse files Browse the repository at this point in the history
Fixed bug in AF::Datastream decorator
Removed `installed?' method from Antivirus module
Factored out FileContentDatastream tests into new spec module
Load antivirus service in DulHydra initializer
  • Loading branch information
dchandekstark committed May 22, 2014
1 parent d1d3ee6 commit 0de78a6
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 56 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
language: ruby
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq clamav
- sudo freshclam -v
rvm:
- 2.0.0
before_script:
- "cp config/log4r.yml.sample config/log4r.yml"
bundler_args: --without development production --deployment
cache: bundler
script: "bundle exec rake dul_hydra:ci:build"
notifications:
email:
- [email protected]
# To exclude antivirus tests:
# env:
# - SPEC_OPTS="--tag ~antivirus"
# bundler_args: --without development production --deployment
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ gem 'grouper-rest-client'
gem 'hydra-editor', '0.2.2'
gem 'hydra-derivatives'
gem 'deprecation'

group :production, :development do
gem 'clamav'
end
gem 'clamav'

gem 'sqlite3'
gem 'log4r'
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/dul_hydra.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@
end

Blacklight::Configuration.default_values[:http_method] = :post

DulHydra::Services::Antivirus.load!
7 changes: 7 additions & 0 deletions lib/dul_hydra/datastreams/antivirus_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module DulHydra
module Datastreams
module AntivirusBehavior

end
end
end
1 change: 0 additions & 1 deletion lib/dul_hydra/datastreams/file_content_datastream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module DulHydra
module Datastreams
class FileContentDatastream < ActiveFedora::Datastream

DEFAULT_FILE_EXTENSION = "bin"
BUFSIZE = 8192

# Write datastream content to open file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

ActiveFedora::Datastream.class_eval do

DEFAULT_FILE_EXTENSION = "bin"

# Return default file extension for datastream based on MIME type
def default_file_extension
mimetypes = MIME::Types[self.mimeType]
Expand Down
18 changes: 3 additions & 15 deletions lib/dul_hydra/services/antivirus.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'clamav'

module DulHydra
module Services
class Antivirus
Expand All @@ -23,29 +25,16 @@ def scan_one(file)
end

def load!
raise DulHydra::Error, "Antivirus is not installed." unless installed?
load
loaded!
end

def version
# ClamAV sigtool may be installed on system,
# but we require that clamav gem is installed.
return unless installed?
# Engine and database versions
# E.g., ClamAV 0.98.3/19010/Tue May 20 21:46:01 2014
`sigtool --version`.strip
end

def installed?
@installed ||= begin
require 'clamav'
rescue LoadError
false
else
true
end
end

private

def loaded!
Expand All @@ -58,7 +47,6 @@ def loaded?

def load
engine.loaddb
loaded!
end

def get_file_path(file)
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/file_content_datastream_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'
require 'tempfile'

describe DulHydra::Datastreams::FileContentDatastream do
let(:obj) { FileContentObject.create }
let(:pid_prefix) { obj.pid.sub(':', '_') }
let(:ds) { obj.datastreams['content'] }
before(:all) do
class FileContentObject < ActiveFedora::Base
has_file_datastream name: 'content', type: DulHydra::Datastreams::FileContentDatastream
end
end
before(:each) do
obj.content.content = fixture_file_upload("library-devil.tiff", "image/tiff")
obj.save!
end
after(:each) { ActiveFedora::Base.destroy_all }
after(:all) { Object.send(:remove_const, :FileContentObject) }
it "should have a default file prefix, file extension, and file name" do
expect(ds.default_file_prefix).to eq("#{pid_prefix}_content")
expect(ds.default_file_extension).to eq("tiff")
expect(ds.default_file_name).to eq("#{pid_prefix}_content.tiff")
end
context "#write_content" do
it "should write the content to a file" do
Tempfile.new('content', :encoding => 'ascii-8bit') do |tmpfile|
tmppath = tmpfile.path
ds.write_content(tmpfile)
ds.size.should == File.size(tmppath)
end
end
end
end
6 changes: 1 addition & 5 deletions spec/services/antivirus_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require 'spec_helper'

def installed?
DulHydra::Services::Antivirus.installed?
end

describe DulHydra::Services::Antivirus, if: installed? do
describe DulHydra::Services::Antivirus, antivirus: true do
let(:file) { fixture_file_upload "library-devil.tiff" }
it "should report whether a file is infected" do
expect(described_class.scan(file)).not_to have_virus
Expand Down
56 changes: 26 additions & 30 deletions spec/support/shared_examples_for_has_content.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
require 'spec_helper'
require 'tempfile'

shared_examples "an object that can have content" do
let(:object) do
obj = described_class.new(title: "I Have Content!")
obj.save(validate: false)
obj
described_class.new(title: "I Have Content!")
# obj = described_class.new(title: "I Have Content!")
# obj.save(validate: false)
# obj
end
after { ActiveFedora::Base.destroy_all }
context "before new content is saved" do
context "when the content is a file" do
it "should run a virus scan"
context "and a virus is found" do
it "should not save the object"
end
end
context "when the content is not a file" do
it "should not run a virus scan"
end
end
context "after content is uploaded" do
before do
object.upload fixture_file_upload("library-devil.tiff", "image/tiff")
object.save(validate: false)
end
it "should have content" do
expect(object).to have_content
end
it "should have an original_filename" do
expect(object.original_filename).to eq("library-devil.tiff")
end
it "should have a content_type" do
expect(object.content_type).to eq("image/tiff")
end
it "should have a thumbnail (if it's an appropriate type)" do
expect(object.thumbnail).to be_present
end
it "should have a default file prefix, file extension, and file name" do
# XXX Does this belong in a test module for FileContentDatastream?
pid_prefix = object.pid.sub(':', '_')
object.content.default_file_prefix.should == "#{pid_prefix}_content"
object.content.default_file_extension.should == "tiff"
object.content.default_file_name.should == "#{pid_prefix}_content.tiff"
end
context "#write_content" do
let(:tmpfile) { Tempfile.new('content', :encoding => 'ascii-8bit') }
after { tmpfile.unlink }
it "should write the content to a file" do
tmppath = tmpfile.path
object.content.write_content(tmpfile)
tmpfile.close
object.content.content.size.should == File.size(tmppath)
context "after saving the object" do
before { object.save(validate: false) }
it "should have an original_filename" do
expect(object.original_filename).to eq("library-devil.tiff")
end
end
it "should have a content_type" do
expect(object.content_type).to eq("image/tiff")
end
it "should have a thumbnail (if it's an appropriate type)" do
expect(object.thumbnail).to be_present
end
end # after saving
end
context "after content is uploaded with a checksum" do
context "and the checksum matches" do
Expand Down

0 comments on commit 0de78a6

Please sign in to comment.