Mercurial > hg > soundsoftware-site
comparison .svn/pristine/e1/e19cb23ac2d90f9735c47ccc904dcda6e52b2377.svn-base @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
1 # encoding: utf-8 | |
2 # | |
3 # Redmine - project management software | |
4 # Copyright (C) 2006-2013 Jean-Philippe Lang | |
5 # | |
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 # | |
11 # 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 # | |
16 # 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 require File.expand_path('../../test_helper', __FILE__) | |
21 | |
22 class AttachmentTest < ActiveSupport::TestCase | |
23 fixtures :users, :projects, :roles, :members, :member_roles, | |
24 :enabled_modules, :issues, :trackers, :attachments | |
25 | |
26 class MockFile | |
27 attr_reader :original_filename, :content_type, :content, :size | |
28 | |
29 def initialize(attributes) | |
30 @original_filename = attributes[:original_filename] | |
31 @content_type = attributes[:content_type] | |
32 @content = attributes[:content] || "Content" | |
33 @size = content.size | |
34 end | |
35 end | |
36 | |
37 def setup | |
38 set_tmp_attachments_directory | |
39 end | |
40 | |
41 def test_container_for_new_attachment_should_be_nil | |
42 assert_nil Attachment.new.container | |
43 end | |
44 | |
45 def test_create | |
46 a = Attachment.new(:container => Issue.find(1), | |
47 :file => uploaded_test_file("testfile.txt", "text/plain"), | |
48 :author => User.find(1)) | |
49 assert a.save | |
50 assert_equal 'testfile.txt', a.filename | |
51 assert_equal 59, a.filesize | |
52 assert_equal 'text/plain', a.content_type | |
53 assert_equal 0, a.downloads | |
54 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest | |
55 | |
56 assert a.disk_directory | |
57 assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory | |
58 | |
59 assert File.exist?(a.diskfile) | |
60 assert_equal 59, File.size(a.diskfile) | |
61 end | |
62 | |
63 def test_copy_should_preserve_attributes | |
64 a = Attachment.find(1) | |
65 copy = a.copy | |
66 | |
67 assert_save copy | |
68 copy = Attachment.order('id DESC').first | |
69 %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute| | |
70 assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different" | |
71 end | |
72 end | |
73 | |
74 def test_size_should_be_validated_for_new_file | |
75 with_settings :attachment_max_size => 0 do | |
76 a = Attachment.new(:container => Issue.find(1), | |
77 :file => uploaded_test_file("testfile.txt", "text/plain"), | |
78 :author => User.find(1)) | |
79 assert !a.save | |
80 end | |
81 end | |
82 | |
83 def test_size_should_not_be_validated_when_copying | |
84 a = Attachment.create!(:container => Issue.find(1), | |
85 :file => uploaded_test_file("testfile.txt", "text/plain"), | |
86 :author => User.find(1)) | |
87 with_settings :attachment_max_size => 0 do | |
88 copy = a.copy | |
89 assert copy.save | |
90 end | |
91 end | |
92 | |
93 def test_description_length_should_be_validated | |
94 a = Attachment.new(:description => 'a' * 300) | |
95 assert !a.save | |
96 assert_not_nil a.errors[:description] | |
97 end | |
98 | |
99 def test_destroy | |
100 a = Attachment.new(:container => Issue.find(1), | |
101 :file => uploaded_test_file("testfile.txt", "text/plain"), | |
102 :author => User.find(1)) | |
103 assert a.save | |
104 assert_equal 'testfile.txt', a.filename | |
105 assert_equal 59, a.filesize | |
106 assert_equal 'text/plain', a.content_type | |
107 assert_equal 0, a.downloads | |
108 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest | |
109 diskfile = a.diskfile | |
110 assert File.exist?(diskfile) | |
111 assert_equal 59, File.size(a.diskfile) | |
112 assert a.destroy | |
113 assert !File.exist?(diskfile) | |
114 end | |
115 | |
116 def test_destroy_should_not_delete_file_referenced_by_other_attachment | |
117 a = Attachment.create!(:container => Issue.find(1), | |
118 :file => uploaded_test_file("testfile.txt", "text/plain"), | |
119 :author => User.find(1)) | |
120 diskfile = a.diskfile | |
121 | |
122 copy = a.copy | |
123 copy.save! | |
124 | |
125 assert File.exists?(diskfile) | |
126 a.destroy | |
127 assert File.exists?(diskfile) | |
128 copy.destroy | |
129 assert !File.exists?(diskfile) | |
130 end | |
131 | |
132 def test_create_should_auto_assign_content_type | |
133 a = Attachment.new(:container => Issue.find(1), | |
134 :file => uploaded_test_file("testfile.txt", ""), | |
135 :author => User.find(1)) | |
136 assert a.save | |
137 assert_equal 'text/plain', a.content_type | |
138 end | |
139 | |
140 def test_identical_attachments_at_the_same_time_should_not_overwrite | |
141 a1 = Attachment.create!(:container => Issue.find(1), | |
142 :file => uploaded_test_file("testfile.txt", ""), | |
143 :author => User.find(1)) | |
144 a2 = Attachment.create!(:container => Issue.find(1), | |
145 :file => uploaded_test_file("testfile.txt", ""), | |
146 :author => User.find(1)) | |
147 assert a1.disk_filename != a2.disk_filename | |
148 end | |
149 | |
150 def test_filename_should_be_basenamed | |
151 a = Attachment.new(:file => MockFile.new(:original_filename => "path/to/the/file")) | |
152 assert_equal 'file', a.filename | |
153 end | |
154 | |
155 def test_filename_should_be_sanitized | |
156 a = Attachment.new(:file => MockFile.new(:original_filename => "valid:[] invalid:?%*|\"'<>chars")) | |
157 assert_equal 'valid_[] invalid_chars', a.filename | |
158 end | |
159 | |
160 def test_diskfilename | |
161 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ | |
162 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] | |
163 assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1] | |
164 assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1] | |
165 assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1] | |
166 end | |
167 | |
168 def test_title | |
169 a = Attachment.new(:filename => "test.png") | |
170 assert_equal "test.png", a.title | |
171 | |
172 a = Attachment.new(:filename => "test.png", :description => "Cool image") | |
173 assert_equal "test.png (Cool image)", a.title | |
174 end | |
175 | |
176 def test_prune_should_destroy_old_unattached_attachments | |
177 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago) | |
178 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1, :created_on => 2.days.ago) | |
179 Attachment.create!(:file => uploaded_test_file("testfile.txt", ""), :author_id => 1) | |
180 | |
181 assert_difference 'Attachment.count', -2 do | |
182 Attachment.prune | |
183 end | |
184 end | |
185 | |
186 def test_move_from_root_to_target_directory_should_move_root_files | |
187 a = Attachment.find(20) | |
188 assert a.disk_directory.blank? | |
189 # Create a real file for this fixture | |
190 File.open(a.diskfile, "w") do |f| | |
191 f.write "test file at the root of files directory" | |
192 end | |
193 assert a.readable? | |
194 Attachment.move_from_root_to_target_directory | |
195 | |
196 a.reload | |
197 assert_equal '2012/05', a.disk_directory | |
198 assert a.readable? | |
199 end | |
200 | |
201 test "Attachmnet.attach_files should attach the file" do | |
202 issue = Issue.first | |
203 assert_difference 'Attachment.count' do | |
204 Attachment.attach_files(issue, | |
205 '1' => { | |
206 'file' => uploaded_test_file('testfile.txt', 'text/plain'), | |
207 'description' => 'test' | |
208 }) | |
209 end | |
210 | |
211 attachment = Attachment.first(:order => 'id DESC') | |
212 assert_equal issue, attachment.container | |
213 assert_equal 'testfile.txt', attachment.filename | |
214 assert_equal 59, attachment.filesize | |
215 assert_equal 'test', attachment.description | |
216 assert_equal 'text/plain', attachment.content_type | |
217 assert File.exists?(attachment.diskfile) | |
218 assert_equal 59, File.size(attachment.diskfile) | |
219 end | |
220 | |
221 test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do | |
222 # Max size of 0 to force Attachment creation failures | |
223 with_settings(:attachment_max_size => 0) do | |
224 @project = Project.find(1) | |
225 response = Attachment.attach_files(@project, { | |
226 '1' => {'file' => mock_file, 'description' => 'test'}, | |
227 '2' => {'file' => mock_file, 'description' => 'test'} | |
228 }) | |
229 | |
230 assert response[:unsaved].present? | |
231 assert_equal 2, response[:unsaved].length | |
232 assert response[:unsaved].first.new_record? | |
233 assert response[:unsaved].second.new_record? | |
234 assert_equal response[:unsaved], @project.unsaved_attachments | |
235 end | |
236 end | |
237 | |
238 def test_latest_attach | |
239 set_fixtures_attachments_directory | |
240 a1 = Attachment.find(16) | |
241 assert_equal "testfile.png", a1.filename | |
242 assert a1.readable? | |
243 assert (! a1.visible?(User.anonymous)) | |
244 assert a1.visible?(User.find(2)) | |
245 a2 = Attachment.find(17) | |
246 assert_equal "testfile.PNG", a2.filename | |
247 assert a2.readable? | |
248 assert (! a2.visible?(User.anonymous)) | |
249 assert a2.visible?(User.find(2)) | |
250 assert a1.created_on < a2.created_on | |
251 | |
252 la1 = Attachment.latest_attach([a1, a2], "testfile.png") | |
253 assert_equal 17, la1.id | |
254 la2 = Attachment.latest_attach([a1, a2], "Testfile.PNG") | |
255 assert_equal 17, la2.id | |
256 | |
257 set_tmp_attachments_directory | |
258 end | |
259 | |
260 def test_thumbnailable_should_be_true_for_images | |
261 assert_equal true, Attachment.new(:filename => 'test.jpg').thumbnailable? | |
262 end | |
263 | |
264 def test_thumbnailable_should_be_true_for_non_images | |
265 assert_equal false, Attachment.new(:filename => 'test.txt').thumbnailable? | |
266 end | |
267 | |
268 if convert_installed? | |
269 def test_thumbnail_should_generate_the_thumbnail | |
270 set_fixtures_attachments_directory | |
271 attachment = Attachment.find(16) | |
272 Attachment.clear_thumbnails | |
273 | |
274 assert_difference "Dir.glob(File.join(Attachment.thumbnails_storage_path, '*.thumb')).size" do | |
275 thumbnail = attachment.thumbnail | |
276 assert_equal "16_8e0294de2441577c529f170b6fb8f638_100.thumb", File.basename(thumbnail) | |
277 assert File.exists?(thumbnail) | |
278 end | |
279 end | |
280 else | |
281 puts '(ImageMagick convert not available)' | |
282 end | |
283 end |