From 1b81a030ce5295f522a3d7fa6b70c786a801e642 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 3 Apr 2017 12:48:58 +0000 Subject: [PATCH] Cleanup tests with mock files. git-svn-id: http://svn.redmine.org/redmine/trunk@16461 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- test/test_helper.rb | 47 +++++++++++++++++++++--------------- test/unit/attachment_test.rb | 35 ++++++++++++--------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 20ef4b154..4cee54472 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index 0b188f8a1..6d988c274 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -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 -- 2.39.5