##// END OF EJS Templates
add test of attachment thumbnail etag...
Toshi MARUYAMA -
r12900:80ba11a02944
parent child
Show More
@@ -1,392 +1,398
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2014 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 AttachmentsControllerTest < ActionController::TestCase
23 23 fixtures :users, :projects, :roles, :members, :member_roles,
24 24 :enabled_modules, :issues, :trackers, :attachments,
25 25 :versions, :wiki_pages, :wikis, :documents
26 26
27 27 def setup
28 28 User.current = nil
29 29 set_fixtures_attachments_directory
30 30 end
31 31
32 32 def teardown
33 33 set_tmp_attachments_directory
34 34 end
35 35
36 36 def test_show_diff
37 37 ['inline', 'sbs'].each do |dt|
38 38 # 060719210727_changeset_utf8.diff
39 39 get :show, :id => 14, :type => dt
40 40 assert_response :success
41 41 assert_template 'diff'
42 42 assert_equal 'text/html', @response.content_type
43 43 assert_tag 'th',
44 44 :attributes => {:class => /filename/},
45 45 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
46 46 assert_tag 'td',
47 47 :attributes => {:class => /line-code/},
48 48 :content => /Demande créée avec succès/
49 49 end
50 50 set_tmp_attachments_directory
51 51 end
52 52
53 53 def test_show_diff_replace_cannot_convert_content
54 54 with_settings :repositories_encodings => 'UTF-8' do
55 55 ['inline', 'sbs'].each do |dt|
56 56 # 060719210727_changeset_iso8859-1.diff
57 57 get :show, :id => 5, :type => dt
58 58 assert_response :success
59 59 assert_template 'diff'
60 60 assert_equal 'text/html', @response.content_type
61 61 assert_tag 'th',
62 62 :attributes => {:class => "filename"},
63 63 :content => /issues_controller.rb\t\(r\?vision 1484\)/
64 64 assert_tag 'td',
65 65 :attributes => {:class => /line-code/},
66 66 :content => /Demande cr\?\?e avec succ\?s/
67 67 end
68 68 end
69 69 set_tmp_attachments_directory
70 70 end
71 71
72 72 def test_show_diff_latin_1
73 73 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
74 74 ['inline', 'sbs'].each do |dt|
75 75 # 060719210727_changeset_iso8859-1.diff
76 76 get :show, :id => 5, :type => dt
77 77 assert_response :success
78 78 assert_template 'diff'
79 79 assert_equal 'text/html', @response.content_type
80 80 assert_tag 'th',
81 81 :attributes => {:class => "filename"},
82 82 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
83 83 assert_tag 'td',
84 84 :attributes => {:class => /line-code/},
85 85 :content => /Demande créée avec succès/
86 86 end
87 87 end
88 88 set_tmp_attachments_directory
89 89 end
90 90
91 91 def test_save_diff_type
92 92 user1 = User.find(1)
93 93 user1.pref[:diff_type] = nil
94 94 user1.preference.save
95 95 user = User.find(1)
96 96 assert_nil user.pref[:diff_type]
97 97
98 98 @request.session[:user_id] = 1 # admin
99 99 get :show, :id => 5
100 100 assert_response :success
101 101 assert_template 'diff'
102 102 user.reload
103 103 assert_equal "inline", user.pref[:diff_type]
104 104 get :show, :id => 5, :type => 'sbs'
105 105 assert_response :success
106 106 assert_template 'diff'
107 107 user.reload
108 108 assert_equal "sbs", user.pref[:diff_type]
109 109 end
110 110
111 111 def test_diff_show_filename_in_mercurial_export
112 112 set_tmp_attachments_directory
113 113 a = Attachment.new(:container => Issue.find(1),
114 114 :file => uploaded_test_file("hg-export.diff", "text/plain"),
115 115 :author => User.find(1))
116 116 assert a.save
117 117 assert_equal 'hg-export.diff', a.filename
118 118
119 119 get :show, :id => a.id, :type => 'inline'
120 120 assert_response :success
121 121 assert_template 'diff'
122 122 assert_equal 'text/html', @response.content_type
123 123 assert_select 'th.filename', :text => 'test1.txt'
124 124 end
125 125
126 126 def test_show_text_file
127 127 get :show, :id => 4
128 128 assert_response :success
129 129 assert_template 'file'
130 130 assert_equal 'text/html', @response.content_type
131 131 set_tmp_attachments_directory
132 132 end
133 133
134 134 def test_show_text_file_utf_8
135 135 set_tmp_attachments_directory
136 136 a = Attachment.new(:container => Issue.find(1),
137 137 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
138 138 :author => User.find(1))
139 139 assert a.save
140 140 assert_equal 'japanese-utf-8.txt', a.filename
141 141
142 142 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
143 143 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
144 144
145 145 get :show, :id => a.id
146 146 assert_response :success
147 147 assert_template 'file'
148 148 assert_equal 'text/html', @response.content_type
149 149 assert_tag :tag => 'th',
150 150 :content => '1',
151 151 :attributes => { :class => 'line-num' },
152 152 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
153 153 end
154 154
155 155 def test_show_text_file_replace_cannot_convert_content
156 156 set_tmp_attachments_directory
157 157 with_settings :repositories_encodings => 'UTF-8' do
158 158 a = Attachment.new(:container => Issue.find(1),
159 159 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
160 160 :author => User.find(1))
161 161 assert a.save
162 162 assert_equal 'iso8859-1.txt', a.filename
163 163
164 164 get :show, :id => a.id
165 165 assert_response :success
166 166 assert_template 'file'
167 167 assert_equal 'text/html', @response.content_type
168 168 assert_tag :tag => 'th',
169 169 :content => '7',
170 170 :attributes => { :class => 'line-num' },
171 171 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
172 172 end
173 173 end
174 174
175 175 def test_show_text_file_latin_1
176 176 set_tmp_attachments_directory
177 177 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
178 178 a = Attachment.new(:container => Issue.find(1),
179 179 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
180 180 :author => User.find(1))
181 181 assert a.save
182 182 assert_equal 'iso8859-1.txt', a.filename
183 183
184 184 get :show, :id => a.id
185 185 assert_response :success
186 186 assert_template 'file'
187 187 assert_equal 'text/html', @response.content_type
188 188 assert_tag :tag => 'th',
189 189 :content => '7',
190 190 :attributes => { :class => 'line-num' },
191 191 :sibling => { :tag => 'td', :content => /Demande créée avec succès/ }
192 192 end
193 193 end
194 194
195 195 def test_show_text_file_should_send_if_too_big
196 196 with_settings :file_max_size_displayed => 512 do
197 197 Attachment.find(4).update_attribute :filesize, 754.kilobyte
198 198 get :show, :id => 4
199 199 assert_response :success
200 200 assert_equal 'application/x-ruby', @response.content_type
201 201 end
202 202 set_tmp_attachments_directory
203 203 end
204 204
205 205 def test_show_other
206 206 get :show, :id => 6
207 207 assert_response :success
208 208 assert_equal 'application/octet-stream', @response.content_type
209 209 set_tmp_attachments_directory
210 210 end
211 211
212 212 def test_show_file_from_private_issue_without_permission
213 213 get :show, :id => 15
214 214 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
215 215 set_tmp_attachments_directory
216 216 end
217 217
218 218 def test_show_file_from_private_issue_with_permission
219 219 @request.session[:user_id] = 2
220 220 get :show, :id => 15
221 221 assert_response :success
222 222 assert_tag 'h2', :content => /private.diff/
223 223 set_tmp_attachments_directory
224 224 end
225 225
226 226 def test_show_file_without_container_should_be_allowed_to_author
227 227 set_tmp_attachments_directory
228 228 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
229 229
230 230 @request.session[:user_id] = 2
231 231 get :show, :id => attachment.id
232 232 assert_response 200
233 233 end
234 234
235 235 def test_show_file_without_container_should_be_denied_to_other_users
236 236 set_tmp_attachments_directory
237 237 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
238 238
239 239 @request.session[:user_id] = 3
240 240 get :show, :id => attachment.id
241 241 assert_response 403
242 242 end
243 243
244 244 def test_show_invalid_should_respond_with_404
245 245 get :show, :id => 999
246 246 assert_response 404
247 247 end
248 248
249 249 def test_download_text_file
250 250 get :download, :id => 4
251 251 assert_response :success
252 252 assert_equal 'application/x-ruby', @response.content_type
253 253 etag = @response.etag
254 254 assert_not_nil etag
255 255
256 256 @request.env["HTTP_IF_NONE_MATCH"] = etag
257 257 get :download, :id => 4
258 258 assert_response 304
259 259
260 260 set_tmp_attachments_directory
261 261 end
262 262
263 263 def test_download_version_file_with_issue_tracking_disabled
264 264 Project.find(1).disable_module! :issue_tracking
265 265 get :download, :id => 9
266 266 assert_response :success
267 267 end
268 268
269 269 def test_download_should_assign_content_type_if_blank
270 270 Attachment.find(4).update_attribute(:content_type, '')
271 271
272 272 get :download, :id => 4
273 273 assert_response :success
274 274 assert_equal 'text/x-ruby', @response.content_type
275 275 set_tmp_attachments_directory
276 276 end
277 277
278 278 def test_download_missing_file
279 279 get :download, :id => 2
280 280 assert_response 404
281 281 set_tmp_attachments_directory
282 282 end
283 283
284 284 def test_download_should_be_denied_without_permission
285 285 get :download, :id => 7
286 286 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
287 287 set_tmp_attachments_directory
288 288 end
289 289
290 290 if convert_installed?
291 291 def test_thumbnail
292 292 Attachment.clear_thumbnails
293 293 @request.session[:user_id] = 2
294
295 294 get :thumbnail, :id => 16
296 295 assert_response :success
297 296 assert_equal 'image/png', response.content_type
297
298 etag = @response.etag
299 assert_not_nil etag
300
301 @request.env["HTTP_IF_NONE_MATCH"] = etag
302 get :thumbnail, :id => 16
303 assert_response 304
298 304 end
299 305
300 306 def test_thumbnail_should_not_exceed_maximum_size
301 307 Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 800}
302 308
303 309 @request.session[:user_id] = 2
304 310 get :thumbnail, :id => 16, :size => 2000
305 311 end
306 312
307 313 def test_thumbnail_should_round_size
308 314 Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 250}
309 315
310 316 @request.session[:user_id] = 2
311 317 get :thumbnail, :id => 16, :size => 260
312 318 end
313 319
314 320 def test_thumbnail_should_return_404_for_non_image_attachment
315 321 @request.session[:user_id] = 2
316 322
317 323 get :thumbnail, :id => 15
318 324 assert_response 404
319 325 end
320 326
321 327 def test_thumbnail_should_return_404_if_thumbnail_generation_failed
322 328 Attachment.any_instance.stubs(:thumbnail).returns(nil)
323 329 @request.session[:user_id] = 2
324 330
325 331 get :thumbnail, :id => 16
326 332 assert_response 404
327 333 end
328 334
329 335 def test_thumbnail_should_be_denied_without_permission
330 336 get :thumbnail, :id => 16
331 337 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fthumbnail%2F16'
332 338 end
333 339 else
334 340 puts '(ImageMagick convert not available)'
335 341 end
336 342
337 343 def test_destroy_issue_attachment
338 344 set_tmp_attachments_directory
339 345 issue = Issue.find(3)
340 346 @request.session[:user_id] = 2
341 347
342 348 assert_difference 'issue.attachments.count', -1 do
343 349 assert_difference 'Journal.count' do
344 350 delete :destroy, :id => 1
345 351 assert_redirected_to '/projects/ecookbook'
346 352 end
347 353 end
348 354 assert_nil Attachment.find_by_id(1)
349 355 j = Journal.order('id DESC').first
350 356 assert_equal issue, j.journalized
351 357 assert_equal 'attachment', j.details.first.property
352 358 assert_equal '1', j.details.first.prop_key
353 359 assert_equal 'error281.txt', j.details.first.old_value
354 360 assert_equal User.find(2), j.user
355 361 end
356 362
357 363 def test_destroy_wiki_page_attachment
358 364 set_tmp_attachments_directory
359 365 @request.session[:user_id] = 2
360 366 assert_difference 'Attachment.count', -1 do
361 367 delete :destroy, :id => 3
362 368 assert_response 302
363 369 end
364 370 end
365 371
366 372 def test_destroy_project_attachment
367 373 set_tmp_attachments_directory
368 374 @request.session[:user_id] = 2
369 375 assert_difference 'Attachment.count', -1 do
370 376 delete :destroy, :id => 8
371 377 assert_response 302
372 378 end
373 379 end
374 380
375 381 def test_destroy_version_attachment
376 382 set_tmp_attachments_directory
377 383 @request.session[:user_id] = 2
378 384 assert_difference 'Attachment.count', -1 do
379 385 delete :destroy, :id => 9
380 386 assert_response 302
381 387 end
382 388 end
383 389
384 390 def test_destroy_without_permission
385 391 set_tmp_attachments_directory
386 392 assert_no_difference 'Attachment.count' do
387 393 delete :destroy, :id => 3
388 394 end
389 395 assert_response 302
390 396 assert Attachment.find_by_id(3)
391 397 end
392 398 end
General Comments 0
You need to be logged in to leave comments. Login now