You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachments_controller_test.rb 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class AttachmentsControllerTest < Redmine::ControllerTest
  20. fixtures :users, :projects, :roles, :members, :member_roles,
  21. :enabled_modules, :issues, :trackers, :attachments,
  22. :versions, :wiki_pages, :wikis, :documents
  23. def setup
  24. User.current = nil
  25. set_fixtures_attachments_directory
  26. end
  27. def teardown
  28. set_tmp_attachments_directory
  29. end
  30. def test_show_diff
  31. ['inline', 'sbs'].each do |dt|
  32. # 060719210727_changeset_utf8.diff
  33. get(
  34. :show,
  35. :params => {
  36. :id => 14,
  37. :type => dt
  38. }
  39. )
  40. assert_response :success
  41. assert_equal 'text/html', @response.media_type
  42. assert_select 'th.filename', :text => /issues_controller.rb\t\(révision 1484\)/
  43. assert_select 'td.line-code', :text => /Demande créée avec succès/
  44. end
  45. end
  46. def test_show_diff_replace_cannot_convert_content
  47. with_settings :repositories_encodings => 'UTF-8' do
  48. ['inline', 'sbs'].each do |dt|
  49. # 060719210727_changeset_iso8859-1.diff
  50. get(
  51. :show,
  52. :params => {
  53. :id => 5,
  54. :type => dt
  55. }
  56. )
  57. assert_response :success
  58. assert_equal 'text/html', @response.media_type
  59. assert_select 'th.filename', :text => /issues_controller.rb\t\(r\?vision 1484\)/
  60. assert_select 'td.line-code', :text => /Demande cr\?\?e avec succ\?s/
  61. end
  62. end
  63. end
  64. def test_show_diff_latin_1
  65. with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
  66. ['inline', 'sbs'].each do |dt|
  67. # 060719210727_changeset_iso8859-1.diff
  68. get(
  69. :show,
  70. :params => {
  71. :id => 5,
  72. :type => dt
  73. }
  74. )
  75. assert_response :success
  76. assert_equal 'text/html', @response.media_type
  77. assert_select 'th.filename', :text => /issues_controller.rb\t\(révision 1484\)/
  78. assert_select 'td.line-code', :text => /Demande créée avec succès/
  79. end
  80. end
  81. end
  82. def test_show_should_save_diff_type_as_user_preference
  83. user1 = User.find(1)
  84. user1.pref[:diff_type] = nil
  85. user1.preference.save
  86. user = User.find(1)
  87. assert_nil user.pref[:diff_type]
  88. @request.session[:user_id] = 1 # admin
  89. get(
  90. :show,
  91. :params => {
  92. :id => 5
  93. }
  94. )
  95. assert_response :success
  96. user.reload
  97. assert_equal "inline", user.pref[:diff_type]
  98. get(
  99. :show,
  100. :params => {
  101. :id => 5,
  102. :type => 'sbs'
  103. }
  104. )
  105. assert_response :success
  106. user.reload
  107. assert_equal "sbs", user.pref[:diff_type]
  108. end
  109. def test_diff_show_filename_in_mercurial_export
  110. set_tmp_attachments_directory
  111. a = Attachment.new(:container => Issue.find(1),
  112. :file => uploaded_test_file("hg-export.diff", "text/plain"),
  113. :author => User.find(1))
  114. assert a.save
  115. assert_equal 'hg-export.diff', a.filename
  116. get(
  117. :show,
  118. :params => {
  119. :id => a.id,
  120. :type => 'inline'
  121. }
  122. )
  123. assert_response :success
  124. assert_equal 'text/html', @response.media_type
  125. assert_select 'th.filename', :text => 'test1.txt'
  126. end
  127. def test_show_text_file
  128. get(:show, :params => {:id => 4})
  129. assert_response :success
  130. assert_equal 'text/html', @response.media_type
  131. end
  132. def test_show_text_file_utf_8
  133. set_tmp_attachments_directory
  134. a = Attachment.new(:container => Issue.find(1),
  135. :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
  136. :author => User.find(1))
  137. assert a.save
  138. assert_equal 'japanese-utf-8.txt', a.filename
  139. get(:show, :params => {:id => a.id})
  140. assert_response :success
  141. assert_equal 'text/html', @response.media_type
  142. assert_select 'tr#L1' do
  143. assert_select 'th.line-num', :text => '1'
  144. assert_select 'td', :text => /日本語/
  145. end
  146. end
  147. def test_show_text_file_replace_cannot_convert_content
  148. set_tmp_attachments_directory
  149. with_settings :repositories_encodings => 'UTF-8' do
  150. a = Attachment.new(:container => Issue.find(1),
  151. :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
  152. :author => User.find(1))
  153. assert a.save
  154. assert_equal 'iso8859-1.txt', a.filename
  155. get(:show, :params => {:id => a.id})
  156. assert_response :success
  157. assert_equal 'text/html', @response.media_type
  158. assert_select 'tr#L7' do
  159. assert_select 'th.line-num', :text => '7'
  160. assert_select 'td', :text => /Demande cr\?\?e avec succ\?s/
  161. end
  162. end
  163. end
  164. def test_show_text_file_latin_1
  165. set_tmp_attachments_directory
  166. with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
  167. a = Attachment.new(:container => Issue.find(1),
  168. :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
  169. :author => User.find(1))
  170. assert a.save
  171. assert_equal 'iso8859-1.txt', a.filename
  172. get(:show, :params => {:id => a.id})
  173. assert_response :success
  174. assert_equal 'text/html', @response.media_type
  175. assert_select 'tr#L7' do
  176. assert_select 'th.line-num', :text => '7'
  177. assert_select 'td', :text => /Demande créée avec succès/
  178. end
  179. end
  180. end
  181. def test_show_text_file_should_show_other_if_too_big
  182. @request.session[:user_id] = 2
  183. with_settings :file_max_size_displayed => 512 do
  184. Attachment.find(4).update_attribute :filesize, 754.kilobyte
  185. get(:show, :params => {:id => 4})
  186. assert_response :success
  187. assert_equal 'text/html', @response.media_type
  188. assert_select '.nodata', :text => 'No preview available. Download the file instead.'
  189. end
  190. end
  191. def test_show_text_file_formated_markdown
  192. set_tmp_attachments_directory
  193. a = Attachment.new(:container => Issue.find(1),
  194. :file => uploaded_test_file('testfile.md', 'text/plain'),
  195. :author => User.find(1))
  196. assert a.save
  197. assert_equal 'testfile.md', a.filename
  198. get(:show, :params => {:id => a.id})
  199. assert_response :success
  200. assert_equal 'text/html', @response.media_type
  201. assert_select 'div.wiki', :html => "<h1>Header 1</h1>\n\n<h2>Header 2</h2>\n\n<h3>Header 3</h3>"
  202. end
  203. def test_show_text_file_fromated_textile
  204. set_tmp_attachments_directory
  205. a = Attachment.new(:container => Issue.find(1),
  206. :file => uploaded_test_file('testfile.textile', 'text/plain'),
  207. :author => User.find(1))
  208. assert a.save
  209. assert_equal 'testfile.textile', a.filename
  210. get(:show, :params => {:id => a.id})
  211. assert_response :success
  212. assert_equal 'text/html', @response.media_type
  213. assert_select 'div.wiki', :html => "<h1>Header 1</h1>\n\n\n\t<h2>Header 2</h2>\n\n\n\t<h3>Header 3</h3>"
  214. end
  215. def test_show_image
  216. @request.session[:user_id] = 2
  217. get(:show, :params => {:id => 16})
  218. assert_response :success
  219. assert_equal 'text/html', @response.media_type
  220. assert_select 'img.filecontent', :src => attachments(:attachments_010).filename
  221. end
  222. def test_show_other_with_no_preview
  223. @request.session[:user_id] = 2
  224. get(:show, :params => {:id => 6})
  225. assert_equal 'text/html', @response.media_type
  226. assert_select '.nodata', :text => 'No preview available. Download the file instead.'
  227. end
  228. def test_show_file_from_private_issue_without_permission
  229. get(:show, :params => {:id => 15})
  230. assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
  231. end
  232. def test_show_file_from_private_issue_with_permission
  233. @request.session[:user_id] = 2
  234. get(:show, :params => {:id => 15})
  235. assert_response :success
  236. assert_select 'h2', :text => /private.diff/
  237. end
  238. def test_show_file_without_container_should_be_allowed_to_author
  239. set_tmp_attachments_directory
  240. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
  241. @request.session[:user_id] = 2
  242. get(:show, :params => {:id => attachment.id})
  243. assert_response 200
  244. end
  245. def test_show_file_without_container_should_be_denied_to_other_users
  246. set_tmp_attachments_directory
  247. attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
  248. @request.session[:user_id] = 3
  249. get(:show, :params => {:id => attachment.id})
  250. assert_response 403
  251. end
  252. def test_show_issue_attachment_should_highlight_issues_menu_item
  253. get(:show, :params => {:id => 4})
  254. assert_response :success
  255. assert_select '#main-menu a.issues.selected'
  256. end
  257. def test_show_invalid_should_respond_with_404
  258. get(:show, :params => {:id => 999})
  259. assert_response 404
  260. end
  261. def test_show_renders_pagination
  262. get(:show, :params => {:id => 5, :type => 'inline'})
  263. assert_response :success
  264. assert_select 'ul.pages li.next', :text => /next/i
  265. assert_select 'ul.pages li.previous', :text => /previous/i
  266. end
  267. def test_download_text_file
  268. get(:download, :params => {:id => 4})
  269. assert_response :success
  270. assert_equal 'application/x-ruby', @response.media_type
  271. etag = @response.etag
  272. assert_not_nil etag
  273. @request.env["HTTP_IF_NONE_MATCH"] = etag
  274. get(:download, :params => {:id => 4})
  275. assert_response 304
  276. end
  277. def test_download_js_file
  278. set_tmp_attachments_directory
  279. attachment = Attachment.create!(
  280. :file => mock_file_with_options(:original_filename => "hello.js", :content_type => "text/javascript"),
  281. :author_id => 2,
  282. :container => Issue.find(1)
  283. )
  284. get(:download, :params => {:id => attachment.id})
  285. assert_response :success
  286. assert_equal 'text/javascript', @response.media_type
  287. end
  288. def test_download_version_file_with_issue_tracking_disabled
  289. Project.find(1).disable_module! :issue_tracking
  290. get(:download, :params => {:id => 9})
  291. assert_response :success
  292. end
  293. def test_download_should_assign_content_type_if_blank
  294. Attachment.find(4).update_attribute(:content_type, '')
  295. get(:download, :params => {:id => 4})
  296. assert_response :success
  297. assert_equal 'text/x-ruby', @response.media_type
  298. end
  299. def test_download_should_assign_better_content_type_than_application_octet_stream
  300. Attachment.find(4).update! :content_type => "application/octet-stream"
  301. get(:download, :params => {:id => 4})
  302. assert_response :success
  303. assert_equal 'text/x-ruby', @response.media_type
  304. end
  305. def test_download_should_assign_application_octet_stream_if_content_type_is_not_determined
  306. get(:download, :params => {:id => 22})
  307. assert_response :success
  308. assert_nil Redmine::MimeType.of(attachments(:attachments_022).filename)
  309. assert_equal 'application/octet-stream', @response.media_type
  310. end
  311. def test_download_missing_file
  312. get(:download, :params => {:id => 2})
  313. assert_response 404
  314. end
  315. def test_download_should_be_denied_without_permission
  316. get(:download, :params => {:id => 7})
  317. assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
  318. end
  319. if convert_installed?
  320. def test_thumbnail
  321. Attachment.clear_thumbnails
  322. @request.session[:user_id] = 2
  323. get(
  324. :thumbnail,
  325. :params => {
  326. :id => 16
  327. }
  328. )
  329. assert_response :success
  330. assert_equal 'image/png', response.media_type
  331. etag = @response.etag
  332. assert_not_nil etag
  333. @request.env["HTTP_IF_NONE_MATCH"] = etag
  334. get(
  335. :thumbnail,
  336. :params => {
  337. :id => 16
  338. }
  339. )
  340. assert_response 304
  341. end
  342. def test_thumbnail_should_not_exceed_maximum_size
  343. Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 800}
  344. @request.session[:user_id] = 2
  345. get(
  346. :thumbnail,
  347. :params => {
  348. :id => 16,
  349. :size => 2000
  350. }
  351. )
  352. end
  353. def test_thumbnail_should_round_size
  354. Redmine::Thumbnail.expects(:generate).with {|source, target, size| size == 300}
  355. @request.session[:user_id] = 2
  356. get(
  357. :thumbnail,
  358. :params => {
  359. :id => 16,
  360. :size => 260
  361. }
  362. )
  363. end
  364. def test_thumbnail_should_return_404_for_non_image_attachment
  365. @request.session[:user_id] = 2
  366. get(
  367. :thumbnail,
  368. :params => {
  369. :id => 15
  370. }
  371. )
  372. assert_response 404
  373. end
  374. def test_thumbnail_should_return_404_if_thumbnail_generation_failed
  375. Attachment.any_instance.stubs(:thumbnail).returns(nil)
  376. @request.session[:user_id] = 2
  377. get(
  378. :thumbnail,
  379. :params => {
  380. :id => 16
  381. }
  382. )
  383. assert_response 404
  384. end
  385. def test_thumbnail_should_be_denied_without_permission
  386. get(
  387. :thumbnail,
  388. :params => {
  389. :id => 16
  390. }
  391. )
  392. assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fthumbnail%2F16'
  393. end
  394. else
  395. puts '(ImageMagick convert not available)'
  396. end
  397. if gs_installed?
  398. def test_thumbnail_for_pdf_should_be_png
  399. skip unless convert_installed?
  400. Attachment.clear_thumbnails
  401. @request.session[:user_id] = 2
  402. get(
  403. :thumbnail,
  404. :params => {
  405. :id => 23 # ecookbook-gantt.pdf
  406. }
  407. )
  408. assert_response :success
  409. assert_equal 'image/png', response.media_type
  410. end
  411. else
  412. puts '(GhostScript convert not available)'
  413. end
  414. def test_edit_all
  415. @request.session[:user_id] = 2
  416. get(
  417. :edit_all,
  418. :params => {
  419. :object_type => 'issues',
  420. :object_id => '2'
  421. }
  422. )
  423. assert_response :success
  424. assert_select 'form[action=?]', '/attachments/issues/2' do
  425. Issue.find(2).attachments.each do |attachment|
  426. assert_select "tr#attachment-#{attachment.id}"
  427. end
  428. assert_select 'tr#attachment-4' do
  429. assert_select 'input[name=?][value=?]', 'attachments[4][filename]', 'source.rb'
  430. assert_select 'input[name=?][value=?]', 'attachments[4][description]', 'This is a Ruby source file'
  431. end
  432. end
  433. # Link to the container in heading
  434. assert_select 'h2 a', :text => "Feature request #2"
  435. end
  436. def test_edit_all_with_invalid_container_class_should_return_404
  437. get(
  438. :edit_all,
  439. :params => {
  440. :object_type => 'nuggets',
  441. :object_id => '3'
  442. }
  443. )
  444. assert_response 404
  445. end
  446. def test_edit_all_with_invalid_object_should_return_404
  447. get(
  448. :edit_all,
  449. :params => {
  450. :object_type => 'issues',
  451. :object_id => '999'
  452. }
  453. )
  454. assert_response 404
  455. end
  456. def test_edit_all_for_object_that_is_not_visible_should_return_403
  457. get(
  458. :edit_all,
  459. :params => {
  460. :object_type => 'issues',
  461. :object_id => '4'
  462. }
  463. )
  464. assert_response 403
  465. end
  466. def test_update_all
  467. @request.session[:user_id] = 2
  468. patch(
  469. :update_all,
  470. :params => {
  471. :object_type => 'issues',
  472. :object_id => '2',
  473. :attachments => {
  474. '1' => {
  475. :filename => 'newname.text',
  476. :description => ''
  477. },
  478. '4' => {
  479. :filename => 'newname.rb',
  480. :description => 'Renamed'
  481. },
  482. }
  483. }
  484. )
  485. assert_response 302
  486. attachment = Attachment.find(4)
  487. assert_equal 'newname.rb', attachment.filename
  488. assert_equal 'Renamed', attachment.description
  489. end
  490. def test_update_all_with_failure
  491. @request.session[:user_id] = 2
  492. patch(
  493. :update_all,
  494. :params => {
  495. :object_type => 'issues',
  496. :object_id => '3',
  497. :attachments => {
  498. '1' => {
  499. :filename => '',
  500. :description => ''
  501. },
  502. '4' => {
  503. :filename => 'newname.rb',
  504. :description => 'Renamed'
  505. },
  506. }
  507. }
  508. )
  509. assert_response :success
  510. assert_select_error /file cannot be blank/i
  511. # The other attachment should not be updated
  512. attachment = Attachment.find(4)
  513. assert_equal 'source.rb', attachment.filename
  514. assert_equal 'This is a Ruby source file', attachment.description
  515. end
  516. def test_destroy_issue_attachment
  517. set_tmp_attachments_directory
  518. issue = Issue.find(3)
  519. @request.session[:user_id] = 2
  520. assert_difference 'issue.attachments.count', -1 do
  521. assert_difference 'Journal.count' do
  522. delete(
  523. :destroy,
  524. :params => {
  525. :id => 1
  526. }
  527. )
  528. assert_redirected_to '/projects/ecookbook'
  529. end
  530. end
  531. assert_nil Attachment.find_by_id(1)
  532. j = Journal.order('id DESC').first
  533. assert_equal issue, j.journalized
  534. assert_equal 'attachment', j.details.first.property
  535. assert_equal '1', j.details.first.prop_key
  536. assert_equal 'error281.txt', j.details.first.old_value
  537. assert_equal User.find(2), j.user
  538. end
  539. def test_destroy_wiki_page_attachment
  540. set_tmp_attachments_directory
  541. @request.session[:user_id] = 2
  542. assert_difference 'Attachment.count', -1 do
  543. delete(
  544. :destroy,
  545. :params => {
  546. :id => 3
  547. }
  548. )
  549. assert_response 302
  550. end
  551. end
  552. def test_destroy_project_attachment
  553. set_tmp_attachments_directory
  554. @request.session[:user_id] = 2
  555. assert_difference 'Attachment.count', -1 do
  556. delete(
  557. :destroy,
  558. :params => {
  559. :id => 8
  560. }
  561. )
  562. assert_response 302
  563. end
  564. end
  565. def test_destroy_version_attachment
  566. set_tmp_attachments_directory
  567. @request.session[:user_id] = 2
  568. assert_difference 'Attachment.count', -1 do
  569. delete(
  570. :destroy,
  571. :params => {
  572. :id => 9
  573. }
  574. )
  575. assert_response 302
  576. end
  577. end
  578. def test_destroy_version_attachment_with_issue_tracking_disabled
  579. Project.find(1).disable_module! :issue_tracking
  580. set_tmp_attachments_directory
  581. @request.session[:user_id] = 2
  582. assert_difference 'Attachment.count', -1 do
  583. delete(
  584. :destroy,
  585. :params => {
  586. :id => 9
  587. }
  588. )
  589. assert_response 302
  590. end
  591. end
  592. def test_destroy_without_permission
  593. set_tmp_attachments_directory
  594. assert_no_difference 'Attachment.count' do
  595. delete(
  596. :destroy,
  597. :params => {
  598. :id => 3
  599. }
  600. )
  601. end
  602. assert_response 302
  603. assert Attachment.find_by_id(3)
  604. end
  605. end