To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / test / unit / attachment_test.rb @ 442:753f1380d6bc
History | View | Annotate | Download (3.52 KB)
| 1 | 0:513646585e45 | Chris | # encoding: utf-8
|
|---|---|---|---|
| 2 | #
|
||
| 3 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
| 4 | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
||
| 5 | 0:513646585e45 | Chris | #
|
| 6 | # This program is free software; you can redistribute it and/or
|
||
| 7 | # modify it under the terms of the GNU General Public License
|
||
| 8 | # as published by the Free Software Foundation; either version 2
|
||
| 9 | # of the License, or (at your option) any later version.
|
||
| 10 | 441:cbce1fd3b1b7 | Chris | #
|
| 11 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 14 | # GNU General Public License for more details.
|
||
| 15 | 441:cbce1fd3b1b7 | Chris | #
|
| 16 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 17 | # along with this program; if not, write to the Free Software
|
||
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 19 | |||
| 20 | 119:8661b858af72 | Chris | require File.expand_path('../../test_helper', __FILE__) |
| 21 | 0:513646585e45 | Chris | |
| 22 | class AttachmentTest < ActiveSupport::TestCase |
||
| 23 | fixtures :issues, :users |
||
| 24 | 441:cbce1fd3b1b7 | Chris | |
| 25 | 0:513646585e45 | Chris | def setup |
| 26 | end
|
||
| 27 | |||
| 28 | def test_create |
||
| 29 | a = Attachment.new(:container => Issue.find(1), |
||
| 30 | :file => uploaded_test_file("testfile.txt", "text/plain"), |
||
| 31 | :author => User.find(1)) |
||
| 32 | assert a.save |
||
| 33 | assert_equal 'testfile.txt', a.filename
|
||
| 34 | assert_equal 59, a.filesize
|
||
| 35 | assert_equal 'text/plain', a.content_type
|
||
| 36 | assert_equal 0, a.downloads
|
||
| 37 | 119:8661b858af72 | Chris | assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
|
| 38 | 0:513646585e45 | Chris | assert File.exist?(a.diskfile)
|
| 39 | end
|
||
| 40 | 441:cbce1fd3b1b7 | Chris | |
| 41 | 0:513646585e45 | Chris | def test_create_should_auto_assign_content_type |
| 42 | a = Attachment.new(:container => Issue.find(1), |
||
| 43 | :file => uploaded_test_file("testfile.txt", ""), |
||
| 44 | :author => User.find(1)) |
||
| 45 | assert a.save |
||
| 46 | assert_equal 'text/plain', a.content_type
|
||
| 47 | end
|
||
| 48 | 441:cbce1fd3b1b7 | Chris | |
| 49 | 0:513646585e45 | Chris | def test_identical_attachments_at_the_same_time_should_not_overwrite |
| 50 | a1 = Attachment.create!(:container => Issue.find(1), |
||
| 51 | :file => uploaded_test_file("testfile.txt", ""), |
||
| 52 | :author => User.find(1)) |
||
| 53 | a2 = Attachment.create!(:container => Issue.find(1), |
||
| 54 | :file => uploaded_test_file("testfile.txt", ""), |
||
| 55 | :author => User.find(1)) |
||
| 56 | assert a1.disk_filename != a2.disk_filename |
||
| 57 | end
|
||
| 58 | 441:cbce1fd3b1b7 | Chris | |
| 59 | 0:513646585e45 | Chris | def test_diskfilename |
| 60 | assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ |
||
| 61 | assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] |
||
| 62 | assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1] |
||
| 63 | assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1] |
||
| 64 | assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1] |
||
| 65 | end
|
||
| 66 | |||
| 67 | context "Attachmnet#attach_files" do |
||
| 68 | should "add unsaved files to the object as unsaved attachments" do |
||
| 69 | # Max size of 0 to force Attachment creation failures
|
||
| 70 | with_settings(:attachment_max_size => 0) do |
||
| 71 | @project = Project.generate! |
||
| 72 | response = Attachment.attach_files(@project, { |
||
| 73 | '1' => {'file' => mock_file, 'description' => 'test'}, |
||
| 74 | '2' => {'file' => mock_file, 'description' => 'test'} |
||
| 75 | }) |
||
| 76 | |||
| 77 | assert response[:unsaved].present?
|
||
| 78 | assert_equal 2, response[:unsaved].length |
||
| 79 | assert response[:unsaved].first.new_record?
|
||
| 80 | assert response[:unsaved].second.new_record?
|
||
| 81 | assert_equal response[:unsaved], @project.unsaved_attachments |
||
| 82 | end
|
||
| 83 | end
|
||
| 84 | end
|
||
| 85 | end |