Commit d4ed2d99 authored by jplang's avatar jplang

Cleanup tests with mock files.

git-svn-id: https://svn.redmine.org/redmine/trunk@16461 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 9d3b923b
......@@ -52,29 +52,18 @@ class ActiveSupport::TestCase
fixture_file_upload("files/#{name}", mime, true)
end
# Mock out a file
def self.mock_file
file = 'a_file.png'
file.stubs(:size).returns(32)
file.stubs(:original_filename).returns('a_file.png')
file.stubs(:content_type).returns('image/png')
file.stubs(:read).returns(false)
file
end
def mock_file(options=nil)
options ||= {
:original_filename => 'a_file.png',
:content_type => 'image/png',
:size => 32
}
def mock_file
self.class.mock_file
Redmine::MockFile.new(options)
end
def mock_file_with_options(options={})
file = ''
file.stubs(:size).returns(32)
original_filename = options[:original_filename] || nil
file.stubs(:original_filename).returns(original_filename)
content_type = options[:content_type] || nil
file.stubs(:content_type).returns(content_type)
file.stubs(:read).returns(false)
file
mock_file(options)
end
# Use a temporary directory for attachment related tests
......@@ -257,6 +246,26 @@ class ActiveSupport::TestCase
end
module Redmine
class MockFile
attr_reader :size, :original_filename, :content_type
def initialize(options={})
@size = options[:size] || 32
@original_filename = options[:original_filename] || options[:filename]
@content_type = options[:content_type]
@content = options[:content] || 'x'*size
end
def read(*args)
if @eof
false
else
@eof = true
@content
end
end
end
class RoutingTest < ActionDispatch::IntegrationTest
def should_route(arg)
arg = arg.dup
......
......@@ -27,17 +27,6 @@ class AttachmentTest < ActiveSupport::TestCase
# in transactional fixtures (https://github.com/rails/rails/pull/18458)
self.use_transactional_fixtures = false
class MockFile
attr_reader :original_filename, :content_type, :content, :size
def initialize(attributes)
@original_filename = attributes[:original_filename]
@content_type = attributes[:content_type]
@content = attributes[:content] || "Content"
@size = content.size
end
end
def setup
set_tmp_attachments_directory
end
......@@ -224,23 +213,29 @@ class AttachmentTest < ActiveSupport::TestCase
assert_equal 'text/plain', a.content_type
end
def test_identical_attachments_should_reuse_same_file
a1 = Attachment.create!(:container => Issue.find(1),
:file => uploaded_test_file("testfile.txt", ""),
:author => User.find(1))
a2 = Attachment.create!(:container => Issue.find(1),
:file => uploaded_test_file("testfile.txt", ""),
:author => User.find(1))
def test_attachments_with_same_content_should_reuse_same_file
a1 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
:file => mock_file(:filename => 'foo', :content => 'abcd'))
a2 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
:file => mock_file(:filename => 'bar', :content => 'abcd'))
assert_equal a1.diskfile, a2.diskfile
end
def test_attachments_with_same_filename_at_the_same_time_should_not_overwrite
a1 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
:file => mock_file(:filename => 'foo', :content => 'abcd'))
a2 = Attachment.create!(:container => Issue.find(1), :author => User.find(1),
:file => mock_file(:filename => 'foo', :content => 'efgh'))
assert_not_equal a1.diskfile, a2.diskfile
end
def test_filename_should_be_basenamed
a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
a = Attachment.new(:file => mock_file(:original_filename => "path/to/the/file"))
assert_equal 'file', a.filename
end
def test_filename_should_be_sanitized
a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
a = Attachment.new(:file => mock_file(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
assert_equal 'valid_[] invalid_chars', a.filename
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment