##// END OF EJS Templates
use temporary attachments directory in unit attachment test...
Toshi MARUYAMA -
r7772:b8b2c1cce4df
parent child
Show More
@@ -1,123 +1,124
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2011 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.expand_path('../../test_helper', __FILE__)
21 21
22 22 class AttachmentTest < ActiveSupport::TestCase
23 23 fixtures :issues, :users
24 24
25 25 def setup
26 set_tmp_attachments_directory
26 27 end
27 28
28 29 def test_create
29 30 a = Attachment.new(:container => Issue.find(1),
30 31 :file => uploaded_test_file("testfile.txt", "text/plain"),
31 32 :author => User.find(1))
32 33 assert a.save
33 34 assert_equal 'testfile.txt', a.filename
34 35 assert_equal 59, a.filesize
35 36 assert_equal 'text/plain', a.content_type
36 37 assert_equal 0, a.downloads
37 38 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
38 39 assert File.exist?(a.diskfile)
39 40 assert_equal 59, File.size(a.diskfile)
40 41 end
41 42
42 43 def test_destroy
43 44 a = Attachment.new(:container => Issue.find(1),
44 45 :file => uploaded_test_file("testfile.txt", "text/plain"),
45 46 :author => User.find(1))
46 47 assert a.save
47 48 assert_equal 'testfile.txt', a.filename
48 49 assert_equal 59, a.filesize
49 50 assert_equal 'text/plain', a.content_type
50 51 assert_equal 0, a.downloads
51 52 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
52 53 diskfile = a.diskfile
53 54 assert File.exist?(diskfile)
54 55 assert_equal 59, File.size(a.diskfile)
55 56 assert a.destroy
56 57 assert !File.exist?(diskfile)
57 58 end
58 59
59 60 def test_create_should_auto_assign_content_type
60 61 a = Attachment.new(:container => Issue.find(1),
61 62 :file => uploaded_test_file("testfile.txt", ""),
62 63 :author => User.find(1))
63 64 assert a.save
64 65 assert_equal 'text/plain', a.content_type
65 66 end
66 67
67 68 def test_identical_attachments_at_the_same_time_should_not_overwrite
68 69 a1 = Attachment.create!(:container => Issue.find(1),
69 70 :file => uploaded_test_file("testfile.txt", ""),
70 71 :author => User.find(1))
71 72 a2 = Attachment.create!(:container => Issue.find(1),
72 73 :file => uploaded_test_file("testfile.txt", ""),
73 74 :author => User.find(1))
74 75 assert a1.disk_filename != a2.disk_filename
75 76 end
76 77
77 78 def test_diskfilename
78 79 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
79 80 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
80 81 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1]
81 82 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1]
82 83 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1]
83 84 end
84 85
85 86 context "Attachmnet.attach_files" do
86 87 should "attach the file" do
87 88 issue = Issue.first
88 89 assert_difference 'Attachment.count' do
89 90 Attachment.attach_files(issue,
90 91 '1' => {
91 92 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
92 93 'description' => 'test'
93 94 })
94 95 end
95 96
96 97 attachment = Attachment.first(:order => 'id DESC')
97 98 assert_equal issue, attachment.container
98 99 assert_equal 'testfile.txt', attachment.filename
99 100 assert_equal 59, attachment.filesize
100 101 assert_equal 'test', attachment.description
101 102 assert_equal 'text/plain', attachment.content_type
102 103 assert File.exists?(attachment.diskfile)
103 104 assert_equal 59, File.size(attachment.diskfile)
104 105 end
105 106
106 107 should "add unsaved files to the object as unsaved attachments" do
107 108 # Max size of 0 to force Attachment creation failures
108 109 with_settings(:attachment_max_size => 0) do
109 110 @project = Project.generate!
110 111 response = Attachment.attach_files(@project, {
111 112 '1' => {'file' => mock_file, 'description' => 'test'},
112 113 '2' => {'file' => mock_file, 'description' => 'test'}
113 114 })
114 115
115 116 assert response[:unsaved].present?
116 117 assert_equal 2, response[:unsaved].length
117 118 assert response[:unsaved].first.new_record?
118 119 assert response[:unsaved].second.new_record?
119 120 assert_equal response[:unsaved], @project.unsaved_attachments
120 121 end
121 122 end
122 123 end
123 124 end
General Comments 0
You need to be logged in to leave comments. Login now