##// END OF EJS Templates
Use fixture....
Jean-Philippe Lang -
r8863:4a6de486bae6
parent child
Show More
@@ -1,217 +1,217
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 :users, :projects, :roles, :members, :member_roles,
24 24 :enabled_modules, :issues, :trackers, :attachments
25 25
26 26 class MockFile
27 27 attr_reader :original_filename, :content_type, :content, :size
28 28
29 29 def initialize(attributes)
30 30 @original_filename = attributes[:original_filename]
31 31 @content_type = attributes[:content_type]
32 32 @content = attributes[:content] || "Content"
33 33 @size = content.size
34 34 end
35 35 end
36 36
37 37 def setup
38 38 set_tmp_attachments_directory
39 39 end
40 40
41 41 def test_container_for_new_attachment_should_be_nil
42 42 assert_nil Attachment.new.container
43 43 end
44 44
45 45 def test_create
46 46 a = Attachment.new(:container => Issue.find(1),
47 47 :file => uploaded_test_file("testfile.txt", "text/plain"),
48 48 :author => User.find(1))
49 49 assert a.save
50 50 assert_equal 'testfile.txt', a.filename
51 51 assert_equal 59, a.filesize
52 52 assert_equal 'text/plain', a.content_type
53 53 assert_equal 0, a.downloads
54 54 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
55 55 assert File.exist?(a.diskfile)
56 56 assert_equal 59, File.size(a.diskfile)
57 57 end
58 58
59 59 def test_size_should_be_validated_for_new_file
60 60 with_settings :attachment_max_size => 0 do
61 61 a = Attachment.new(:container => Issue.find(1),
62 62 :file => uploaded_test_file("testfile.txt", "text/plain"),
63 63 :author => User.find(1))
64 64 assert !a.save
65 65 end
66 66 end
67 67
68 68 def test_size_should_not_be_validated_when_copying
69 69 a = Attachment.create!(:container => Issue.find(1),
70 70 :file => uploaded_test_file("testfile.txt", "text/plain"),
71 71 :author => User.find(1))
72 72 with_settings :attachment_max_size => 0 do
73 73 copy = a.copy
74 74 assert copy.save
75 75 end
76 76 end
77 77
78 78 def test_destroy
79 79 a = Attachment.new(:container => Issue.find(1),
80 80 :file => uploaded_test_file("testfile.txt", "text/plain"),
81 81 :author => User.find(1))
82 82 assert a.save
83 83 assert_equal 'testfile.txt', a.filename
84 84 assert_equal 59, a.filesize
85 85 assert_equal 'text/plain', a.content_type
86 86 assert_equal 0, a.downloads
87 87 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
88 88 diskfile = a.diskfile
89 89 assert File.exist?(diskfile)
90 90 assert_equal 59, File.size(a.diskfile)
91 91 assert a.destroy
92 92 assert !File.exist?(diskfile)
93 93 end
94 94
95 95 def test_destroy_should_not_delete_file_referenced_by_other_attachment
96 96 a = Attachment.create!(:container => Issue.find(1),
97 97 :file => uploaded_test_file("testfile.txt", "text/plain"),
98 98 :author => User.find(1))
99 99 diskfile = a.diskfile
100 100
101 101 copy = a.copy
102 102 copy.save!
103 103
104 104 assert File.exists?(diskfile)
105 105 a.destroy
106 106 assert File.exists?(diskfile)
107 107 copy.destroy
108 108 assert !File.exists?(diskfile)
109 109 end
110 110
111 111 def test_create_should_auto_assign_content_type
112 112 a = Attachment.new(:container => Issue.find(1),
113 113 :file => uploaded_test_file("testfile.txt", ""),
114 114 :author => User.find(1))
115 115 assert a.save
116 116 assert_equal 'text/plain', a.content_type
117 117 end
118 118
119 119 def test_identical_attachments_at_the_same_time_should_not_overwrite
120 120 a1 = Attachment.create!(:container => Issue.find(1),
121 121 :file => uploaded_test_file("testfile.txt", ""),
122 122 :author => User.find(1))
123 123 a2 = Attachment.create!(:container => Issue.find(1),
124 124 :file => uploaded_test_file("testfile.txt", ""),
125 125 :author => User.find(1))
126 126 assert a1.disk_filename != a2.disk_filename
127 127 end
128 128
129 129 def test_filename_should_be_basenamed
130 130 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file"))
131 131 assert_equal 'file', a.filename
132 132 end
133 133
134 134 def test_filename_should_be_sanitized
135 135 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars"))
136 136 assert_equal 'valid_[] invalid_chars', a.filename
137 137 end
138 138
139 139 def test_diskfilename
140 140 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
141 141 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
142 142 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentuΓ©.txt")[13..-1]
143 143 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentuΓ©")[13..-1]
144 144 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentuΓ©.Γ§a")[13..-1]
145 145 end
146 146
147 147 def test_prune_should_destroy_old_unattached_attachments
148 148 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
149 149 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago)
150 150 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1)
151 151
152 152 assert_difference 'Attachment.count', -2 do
153 153 Attachment.prune
154 154 end
155 155 end
156 156
157 157 context "Attachmnet.attach_files" do
158 158 should "attach the file" do
159 159 issue = Issue.first
160 160 assert_difference 'Attachment.count' do
161 161 Attachment.attach_files(issue,
162 162 '1' => {
163 163 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
164 164 'description' => 'test'
165 165 })
166 166 end
167 167
168 168 attachment = Attachment.first(:order => 'id DESC')
169 169 assert_equal issue, attachment.container
170 170 assert_equal 'testfile.txt', attachment.filename
171 171 assert_equal 59, attachment.filesize
172 172 assert_equal 'test', attachment.description
173 173 assert_equal 'text/plain', attachment.content_type
174 174 assert File.exists?(attachment.diskfile)
175 175 assert_equal 59, File.size(attachment.diskfile)
176 176 end
177 177
178 178 should "add unsaved files to the object as unsaved attachments" do
179 179 # Max size of 0 to force Attachment creation failures
180 180 with_settings(:attachment_max_size => 0) do
181 @project = Project.generate!
181 @project = Project.find(1)
182 182 response = Attachment.attach_files(@project, {
183 183 '1' => {'file' => mock_file, 'description' => 'test'},
184 184 '2' => {'file' => mock_file, 'description' => 'test'}
185 185 })
186 186
187 187 assert response[:unsaved].present?
188 188 assert_equal 2, response[:unsaved].length
189 189 assert response[:unsaved].first.new_record?
190 190 assert response[:unsaved].second.new_record?
191 191 assert_equal response[:unsaved], @project.unsaved_attachments
192 192 end
193 193 end
194 194 end
195 195
196 196 def test_latest_attach
197 197 set_fixtures_attachments_directory
198 198 a1 = Attachment.find(16)
199 199 assert_equal "testfile.png", a1.filename
200 200 assert a1.readable?
201 201 assert (! a1.visible?(User.anonymous))
202 202 assert a1.visible?(User.find(2))
203 203 a2 = Attachment.find(17)
204 204 assert_equal "testfile.PNG", a2.filename
205 205 assert a2.readable?
206 206 assert (! a2.visible?(User.anonymous))
207 207 assert a2.visible?(User.find(2))
208 208 assert a1.created_on < a2.created_on
209 209
210 210 la1 = Attachment.latest_attach([a1, a2], "testfile.png")
211 211 assert_equal 17, la1.id
212 212 la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG")
213 213 assert_equal 17, la2.id
214 214
215 215 set_tmp_attachments_directory
216 216 end
217 217 end
General Comments 0
You need to be logged in to leave comments. Login now