2005-08-09 04:20:28 +02:00
|
|
|
#!/bin/env ruby
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'file_yard'
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
class FileYardTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
|
|
|
FileUtils.mkdir_p(file_path)
|
|
|
|
FileUtils.rm(Dir["#{file_path}/*"])
|
2005-02-21 14:36:53 +01:00
|
|
|
@yard = FileYard.new(file_path, 100)
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2005-02-21 15:00:00 +01:00
|
|
|
def test_check_upload_size
|
|
|
|
assert_nothing_raised { @yard.check_upload_size(100.kilobytes) }
|
|
|
|
assert_raises(Instiki::ValidationError) { @yard.check_upload_size(100.kilobytes + 1) }
|
|
|
|
end
|
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
def test_files
|
|
|
|
assert_equal [], @yard.files
|
|
|
|
|
|
|
|
# FileYard gets the list of files from directory in the constructor
|
|
|
|
@yard.upload_file('aaa', StringIO.new('file contents'))
|
|
|
|
assert_equal ["#{file_path}/aaa"], Dir["#{file_path}/*"]
|
|
|
|
assert_equal ['aaa'], @yard.files
|
|
|
|
assert @yard.has_file?('aaa')
|
|
|
|
assert_equal 'file contents', File.read(@yard.file_path('aaa'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_path
|
|
|
|
assert_equal "#{file_path}/abcd", @yard.file_path('abcd')
|
|
|
|
end
|
|
|
|
|
2005-02-21 15:00:00 +01:00
|
|
|
def test_size_limit
|
|
|
|
@yard = FileYard.new(file_path, 1)
|
2005-06-09 22:05:16 +02:00
|
|
|
one_kilobyte_string = "a" * 1.kilobyte
|
2005-02-21 15:00:00 +01:00
|
|
|
|
|
|
|
# as StringIO
|
|
|
|
assert_nothing_raised {
|
2005-06-09 22:05:16 +02:00
|
|
|
@yard.upload_file('acceptable_file', StringIO.new(one_kilobyte_string))
|
2005-02-21 15:00:00 +01:00
|
|
|
}
|
|
|
|
assert_raises(Instiki::ValidationError) {
|
2005-06-09 22:05:16 +02:00
|
|
|
@yard.upload_file('one_byte_too_long', StringIO.new(one_kilobyte_string + 'a'))
|
2005-02-21 15:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# as Tempfile
|
|
|
|
require 'tempfile'
|
2005-03-31 06:44:10 +02:00
|
|
|
|
|
|
|
Tempfile.open('acceptable_file') do |f|
|
2005-06-09 22:05:16 +02:00
|
|
|
f.write(one_kilobyte_string)
|
2005-02-21 15:00:00 +01:00
|
|
|
assert_nothing_raised {
|
|
|
|
@yard.upload_file('acceptable_file', f)
|
|
|
|
}
|
2005-03-31 06:44:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
Tempfile.open('one_byte_too_long') do |f|
|
2005-06-09 22:05:16 +02:00
|
|
|
f.write(one_kilobyte_string + 'a')
|
2005-02-21 15:00:00 +01:00
|
|
|
assert_nothing_raised {
|
|
|
|
@yard.upload_file('one_byte_too_long_2', f)
|
|
|
|
}
|
2005-03-31 06:44:10 +02:00
|
|
|
end
|
2005-02-21 15:00:00 +01:00
|
|
|
end
|
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
def file_path
|
|
|
|
"#{RAILS_ROOT}/storage/test/instiki"
|
|
|
|
end
|
|
|
|
|
2005-08-09 04:20:28 +02:00
|
|
|
end
|