##// END OF EJS Templates
Add basic test for image view (#22482)....
Jean-Philippe Lang -
r15014:d8d23fbb6b23
parent child
Show More
@@ -1,467 +1,476
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2016 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_select 'th.filename', :text => /issues_controller.rb\t\(rΓ©vision 1484\)/
44 44 assert_select 'td.line-code', :text => /Demande créée avec succès/
45 45 end
46 46 set_tmp_attachments_directory
47 47 end
48 48
49 49 def test_show_diff_replace_cannot_convert_content
50 50 with_settings :repositories_encodings => 'UTF-8' do
51 51 ['inline', 'sbs'].each do |dt|
52 52 # 060719210727_changeset_iso8859-1.diff
53 53 get :show, :id => 5, :type => dt
54 54 assert_response :success
55 55 assert_template 'diff'
56 56 assert_equal 'text/html', @response.content_type
57 57 assert_select 'th.filename', :text => /issues_controller.rb\t\(r\?vision 1484\)/
58 58 assert_select 'td.line-code', :text => /Demande cr\?\?e avec succ\?s/
59 59 end
60 60 end
61 61 set_tmp_attachments_directory
62 62 end
63 63
64 64 def test_show_diff_latin_1
65 65 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
66 66 ['inline', 'sbs'].each do |dt|
67 67 # 060719210727_changeset_iso8859-1.diff
68 68 get :show, :id => 5, :type => dt
69 69 assert_response :success
70 70 assert_template 'diff'
71 71 assert_equal 'text/html', @response.content_type
72 72 assert_select 'th.filename', :text => /issues_controller.rb\t\(rΓ©vision 1484\)/
73 73 assert_select 'td.line-code', :text => /Demande créée avec succès/
74 74 end
75 75 end
76 76 set_tmp_attachments_directory
77 77 end
78 78
79 79 def test_save_diff_type
80 80 user1 = User.find(1)
81 81 user1.pref[:diff_type] = nil
82 82 user1.preference.save
83 83 user = User.find(1)
84 84 assert_nil user.pref[:diff_type]
85 85
86 86 @request.session[:user_id] = 1 # admin
87 87 get :show, :id => 5
88 88 assert_response :success
89 89 assert_template 'diff'
90 90 user.reload
91 91 assert_equal "inline", user.pref[:diff_type]
92 92 get :show, :id => 5, :type => 'sbs'
93 93 assert_response :success
94 94 assert_template 'diff'
95 95 user.reload
96 96 assert_equal "sbs", user.pref[:diff_type]
97 97 end
98 98
99 99 def test_diff_show_filename_in_mercurial_export
100 100 set_tmp_attachments_directory
101 101 a = Attachment.new(:container => Issue.find(1),
102 102 :file => uploaded_test_file("hg-export.diff", "text/plain"),
103 103 :author => User.find(1))
104 104 assert a.save
105 105 assert_equal 'hg-export.diff', a.filename
106 106
107 107 get :show, :id => a.id, :type => 'inline'
108 108 assert_response :success
109 109 assert_template 'diff'
110 110 assert_equal 'text/html', @response.content_type
111 111 assert_select 'th.filename', :text => 'test1.txt'
112 112 end
113 113
114 114 def test_show_text_file
115 115 get :show, :id => 4
116 116 assert_response :success
117 117 assert_template 'file'
118 118 assert_equal 'text/html', @response.content_type
119 119 set_tmp_attachments_directory
120 120 end
121 121
122 122 def test_show_text_file_utf_8
123 123 set_tmp_attachments_directory
124 124 a = Attachment.new(:container => Issue.find(1),
125 125 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
126 126 :author => User.find(1))
127 127 assert a.save
128 128 assert_equal 'japanese-utf-8.txt', a.filename
129 129
130 130 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e".force_encoding('UTF-8')
131 131
132 132 get :show, :id => a.id
133 133 assert_response :success
134 134 assert_template 'file'
135 135 assert_equal 'text/html', @response.content_type
136 136 assert_select 'tr#L1' do
137 137 assert_select 'th.line-num', :text => '1'
138 138 assert_select 'td', :text => /#{str_japanese}/
139 139 end
140 140 end
141 141
142 142 def test_show_text_file_replace_cannot_convert_content
143 143 set_tmp_attachments_directory
144 144 with_settings :repositories_encodings => 'UTF-8' do
145 145 a = Attachment.new(:container => Issue.find(1),
146 146 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
147 147 :author => User.find(1))
148 148 assert a.save
149 149 assert_equal 'iso8859-1.txt', a.filename
150 150
151 151 get :show, :id => a.id
152 152 assert_response :success
153 153 assert_template 'file'
154 154 assert_equal 'text/html', @response.content_type
155 155 assert_select 'tr#L7' do
156 156 assert_select 'th.line-num', :text => '7'
157 157 assert_select 'td', :text => /Demande cr\?\?e avec succ\?s/
158 158 end
159 159 end
160 160 end
161 161
162 162 def test_show_text_file_latin_1
163 163 set_tmp_attachments_directory
164 164 with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
165 165 a = Attachment.new(:container => Issue.find(1),
166 166 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
167 167 :author => User.find(1))
168 168 assert a.save
169 169 assert_equal 'iso8859-1.txt', a.filename
170 170
171 171 get :show, :id => a.id
172 172 assert_response :success
173 173 assert_template 'file'
174 174 assert_equal 'text/html', @response.content_type
175 175 assert_select 'tr#L7' do
176 176 assert_select 'th.line-num', :text => '7'
177 177 assert_select 'td', :text => /Demande créée avec succès/
178 178 end
179 179 end
180 180 end
181 181
182 182 def test_show_text_file_should_send_if_too_big
183 183 with_settings :file_max_size_displayed => 512 do
184 184 Attachment.find(4).update_attribute :filesize, 754.kilobyte
185 185 get :show, :id => 4
186 186 assert_response :success
187 187 assert_equal 'application/x-ruby', @response.content_type
188 188 end
189 189 set_tmp_attachments_directory
190 190 end
191 191
192 def test_show_image
193 @request.session[:user_id] = 2
194 get :show, :id => 16
195 assert_response :success
196 assert_template 'image'
197 assert_equal 'text/html', @response.content_type
198 assert_select 'img.filecontent', :src => attachments(:attachments_010).filename
199 end
200
192 201 def test_show_other
193 202 get :show, :id => 6
194 203 assert_response :success
195 204 assert_equal 'application/zip', @response.content_type
196 205 set_tmp_attachments_directory
197 206 end
198 207
199 208 def test_show_file_from_private_issue_without_permission
200 209 get :show, :id => 15
201 210 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
202 211 set_tmp_attachments_directory
203 212 end
204 213
205 214 def test_show_file_from_private_issue_with_permission
206 215 @request.session[:user_id] = 2
207 216 get :show, :id => 15
208 217 assert_response :success
209 218 assert_select 'h2', :text => /private.diff/
210 219 set_tmp_attachments_directory
211 220 end
212 221
213 222 def test_show_file_without_container_should_be_allowed_to_author
214 223 set_tmp_attachments_directory
215 224 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
216 225
217 226 @request.session[:user_id] = 2
218 227 get :show, :id => attachment.id
219 228 assert_response 200
220 229 end
221 230
222 231 def test_show_file_without_container_should_be_denied_to_other_users
223 232 set_tmp_attachments_directory
224 233 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
225 234
226 235 @request.session[:user_id] = 3
227 236 get :show, :id => attachment.id
228 237 assert_response 403
229 238 end
230 239
231 240 def test_show_invalid_should_respond_with_404
232 241 get :show, :id => 999
233 242 assert_response 404
234 243 end
235 244
236 245 def test_download_text_file
237 246 get :download, :id => 4
238 247 assert_response :success
239 248 assert_equal 'application/x-ruby', @response.content_type
240 249 etag = @response.etag
241 250 assert_not_nil etag
242 251
243 252 @request.env["HTTP_IF_NONE_MATCH"] = etag
244 253 get :download, :id => 4
245 254 assert_response 304
246 255
247 256 set_tmp_attachments_directory
248 257 end
249 258
250 259 def test_download_version_file_with_issue_tracking_disabled
251 260 Project.find(1).disable_module! :issue_tracking
252 261 get :download, :id => 9
253 262 assert_response :success
254 263 end
255 264
256 265 def test_download_should_assign_content_type_if_blank
257 266 Attachment.find(4).update_attribute(:content_type, '')
258 267
259 268 get :download, :id => 4
260 269 assert_response :success
261 270 assert_equal 'text/x-ruby', @response.content_type
262 271 set_tmp_attachments_directory
263 272 end
264 273
265 274 def test_download_should_assign_better_content_type_than_application_octet_stream
266 275 Attachment.find(4).update! :content_type => "application/octet-stream"
267 276
268 277 get :download, :id => 4
269 278 assert_response :success
270 279 assert_equal 'text/x-ruby', @response.content_type
271 280 set_tmp_attachments_directory
272 281 end
273 282
274 283 def test_download_missing_file
275 284 get :download, :id => 2
276 285 assert_response 404
277 286 set_tmp_attachments_directory
278 287 end
279 288
280 289 def test_download_should_be_denied_without_permission
281 290 get :download, :id => 7
282 291 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
283 292 set_tmp_attachments_directory
284 293 end
285 294
286 295 if convert_installed?
287 296 def test_thumbnail
288 297 Attachment.clear_thumbnails
289 298 @request.session[:user_id] = 2
290 299 get :thumbnail, :id => 16
291 300 assert_response :success
292 301 assert_equal 'image/png', response.content_type
293 302
294 303 etag = @response.etag
295 304 assert_not_nil etag
296 305
297 306 @request.env["HTTP_IF_NONE_MATCH"] = etag
298 307 get :thumbnail, :id => 16
299 308 assert_response 304
300 309 end
301 310
302 311 def test_thumbnail_should_not_exceed_maximum_size
303 312 Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 800}
304 313
305 314 @request.session[:user_id] = 2
306 315 get :thumbnail, :id => 16, :size => 2000
307 316 end
308 317
309 318 def test_thumbnail_should_round_size
310 319 Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 250}
311 320
312 321 @request.session[:user_id] = 2
313 322 get :thumbnail, :id => 16, :size => 260
314 323 end
315 324
316 325 def test_thumbnail_should_return_404_for_non_image_attachment
317 326 @request.session[:user_id] = 2
318 327
319 328 get :thumbnail, :id => 15
320 329 assert_response 404
321 330 end
322 331
323 332 def test_thumbnail_should_return_404_if_thumbnail_generation_failed
324 333 Attachment.any_instance.stubs(:thumbnail).returns(nil)
325 334 @request.session[:user_id] = 2
326 335
327 336 get :thumbnail, :id => 16
328 337 assert_response 404
329 338 end
330 339
331 340 def test_thumbnail_should_be_denied_without_permission
332 341 get :thumbnail, :id => 16
333 342 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fthumbnail%2F16'
334 343 end
335 344 else
336 345 puts '(ImageMagick convert not available)'
337 346 end
338 347
339 348 def test_edit
340 349 @request.session[:user_id] = 2
341 350 get :edit, :object_type => 'issues', :object_id => '2'
342 351 assert_response :success
343 352 assert_template 'edit'
344 353
345 354 container = Issue.find(2)
346 355 assert_equal container, assigns(:container)
347 356 assert_equal container.attachments.size, assigns(:attachments).size
348 357
349 358 assert_select 'form[action=?]', '/attachments/issues/2' do
350 359 assert_select 'tr#attachment-4' do
351 360 assert_select 'input[name=?][value=?]', 'attachments[4][filename]', 'source.rb'
352 361 assert_select 'input[name=?][value=?]', 'attachments[4][description]', 'This is a Ruby source file'
353 362 end
354 363 end
355 364 end
356 365
357 366 def test_edit_invalid_container_class_should_return_404
358 367 get :edit, :object_type => 'nuggets', :object_id => '3'
359 368 assert_response 404
360 369 end
361 370
362 371 def test_edit_invalid_object_should_return_404
363 372 get :edit, :object_type => 'issues', :object_id => '999'
364 373 assert_response 404
365 374 end
366 375
367 376 def test_edit_for_object_that_is_not_visible_should_return_403
368 377 get :edit, :object_type => 'issues', :object_id => '4'
369 378 assert_response 403
370 379 end
371 380
372 381 def test_update
373 382 @request.session[:user_id] = 2
374 383 patch :update, :object_type => 'issues', :object_id => '2', :attachments => {
375 384 '1' => {:filename => 'newname.text', :description => ''},
376 385 '4' => {:filename => 'newname.rb', :description => 'Renamed'},
377 386 }
378 387
379 388 assert_response 302
380 389 attachment = Attachment.find(4)
381 390 assert_equal 'newname.rb', attachment.filename
382 391 assert_equal 'Renamed', attachment.description
383 392 end
384 393
385 394 def test_update_with_failure
386 395 @request.session[:user_id] = 2
387 396 patch :update, :object_type => 'issues', :object_id => '3', :attachments => {
388 397 '1' => {:filename => '', :description => ''},
389 398 '4' => {:filename => 'newname.rb', :description => 'Renamed'},
390 399 }
391 400
392 401 assert_response :success
393 402 assert_template 'edit'
394 403 assert_select_error /file cannot be blank/i
395 404
396 405 # The other attachment should not be updated
397 406 attachment = Attachment.find(4)
398 407 assert_equal 'source.rb', attachment.filename
399 408 assert_equal 'This is a Ruby source file', attachment.description
400 409 end
401 410
402 411 def test_destroy_issue_attachment
403 412 set_tmp_attachments_directory
404 413 issue = Issue.find(3)
405 414 @request.session[:user_id] = 2
406 415
407 416 assert_difference 'issue.attachments.count', -1 do
408 417 assert_difference 'Journal.count' do
409 418 delete :destroy, :id => 1
410 419 assert_redirected_to '/projects/ecookbook'
411 420 end
412 421 end
413 422 assert_nil Attachment.find_by_id(1)
414 423 j = Journal.order('id DESC').first
415 424 assert_equal issue, j.journalized
416 425 assert_equal 'attachment', j.details.first.property
417 426 assert_equal '1', j.details.first.prop_key
418 427 assert_equal 'error281.txt', j.details.first.old_value
419 428 assert_equal User.find(2), j.user
420 429 end
421 430
422 431 def test_destroy_wiki_page_attachment
423 432 set_tmp_attachments_directory
424 433 @request.session[:user_id] = 2
425 434 assert_difference 'Attachment.count', -1 do
426 435 delete :destroy, :id => 3
427 436 assert_response 302
428 437 end
429 438 end
430 439
431 440 def test_destroy_project_attachment
432 441 set_tmp_attachments_directory
433 442 @request.session[:user_id] = 2
434 443 assert_difference 'Attachment.count', -1 do
435 444 delete :destroy, :id => 8
436 445 assert_response 302
437 446 end
438 447 end
439 448
440 449 def test_destroy_version_attachment
441 450 set_tmp_attachments_directory
442 451 @request.session[:user_id] = 2
443 452 assert_difference 'Attachment.count', -1 do
444 453 delete :destroy, :id => 9
445 454 assert_response 302
446 455 end
447 456 end
448 457
449 458 def test_destroy_version_attachment_with_issue_tracking_disabled
450 459 Project.find(1).disable_module! :issue_tracking
451 460 set_tmp_attachments_directory
452 461 @request.session[:user_id] = 2
453 462 assert_difference 'Attachment.count', -1 do
454 463 delete :destroy, :id => 9
455 464 assert_response 302
456 465 end
457 466 end
458 467
459 468 def test_destroy_without_permission
460 469 set_tmp_attachments_directory
461 470 assert_no_difference 'Attachment.count' do
462 471 delete :destroy, :id => 3
463 472 end
464 473 assert_response 302
465 474 assert Attachment.find_by_id(3)
466 475 end
467 476 end
General Comments 0
You need to be logged in to leave comments. Login now