tests: fix upload tests.

This commit is contained in:
evazion
2018-03-19 23:17:24 -05:00
parent 41a4ff15cd
commit d089be9f8a
13 changed files with 96 additions and 273 deletions

View File

@@ -1,18 +1,14 @@
module DownloadTestHelper
def assert_downloaded(expected_filesize, source)
tempfile = Tempfile.new("danbooru-test")
download = Downloads::File.new(source, tempfile.path)
assert_nothing_raised(Downloads::File::Error) do
download.download!
tempfile = Downloads::File.new(source).download!
assert_equal(expected_filesize, tempfile.size, "Tested source URL: #{source}")
tempfile.close!
end
assert_equal(expected_filesize, tempfile.size, "Tested source URL: #{source}")
end
def assert_rewritten(expected_source, test_source)
tempfile = Tempfile.new("danbooru-test")
download = Downloads::File.new(test_source, tempfile.path)
download = Downloads::File.new(test_source)
rewritten_source, _, _ = download.before_download(test_source, {})
assert_match(expected_source, rewritten_source, "Tested source URL: #{test_source}")

View File

@@ -1,23 +1,10 @@
module UploadTestHelper
def upload_file(path, content_type, filename)
tempfile = Tempfile.new(filename)
FileUtils.copy_file(path, tempfile.path)
def upload_file(path)
file = Tempfile.new(binmode: true)
IO.copy_stream("#{Rails.root}/#{path}", file.path)
uploaded_file = ActionDispatch::Http::UploadedFile.new(tempfile: file, filename: File.basename(path))
(class << tempfile; self; end).class_eval do
alias local_path path
define_method(:tempfile) {self}
define_method(:original_filename) {filename}
define_method(:content_type) {content_type}
end
tempfile
end
def upload_jpeg(path)
upload_file(path, "image/jpeg", File.basename(path))
end
def upload_zip(path)
upload_file(path, "application/zip", File.basename(path))
yield uploaded_file if block_given?
uploaded_file
end
end