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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  19. class AttachmentsControllerTest < Redmine::ControllerTest
  20. fixtures :users, :user_preferences, :projects, :roles, :members, :member_roles,
  21. :enabled_modules, :issues, :trackers, :attachments, :issue_statuses, :journals, :journal_details,
  22. :versions, :wiki_pages, :wikis, :documents, :enumerations
  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 a[data-txt=?]', '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 a[data-txt=?]', '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 a[data-txt=?]', '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_formatted_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<h2>Header 2</h2>\n<h3>Header 3</h3>"
  202. end
  203. def test_show_text_file_formatted_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_object_should_return_404
  437. get(
  438. :edit_all,
  439. :params => {
  440. :object_type => 'issues',
  441. :object_id => '999'
  442. }
  443. )
  444. assert_response 404
  445. end
  446. def test_edit_all_for_object_that_is_not_visible_should_return_403
  447. get(
  448. :edit_all,
  449. :params => {
  450. :object_type => 'issues',
  451. :object_id => '4'
  452. }
  453. )
  454. assert_response 403
  455. end
  456. def test_edit_all_issue_attachment_by_user_without_edit_issue_permission_on_tracker_should_return_404
  457. role = Role.find(2)
  458. role.set_permission_trackers 'edit_issues', [2, 3]
  459. role.save!
  460. @request.session[:user_id] = 2
  461. get(
  462. :edit_all,
  463. :params => {
  464. :object_type => 'issues',
  465. :object_id => '4'
  466. }
  467. )
  468. assert_response 404
  469. end
  470. def test_update_all
  471. @request.session[:user_id] = 2
  472. patch(
  473. :update_all,
  474. :params => {
  475. :object_type => 'issues',
  476. :object_id => '2',
  477. :attachments => {
  478. '1' => {
  479. :filename => 'newname.text',
  480. :description => ''
  481. },
  482. '4' => {
  483. :filename => 'newname.rb',
  484. :description => 'Renamed'
  485. },
  486. }
  487. }
  488. )
  489. assert_response 302
  490. attachment = Attachment.find(4)
  491. assert_equal 'newname.rb', attachment.filename
  492. assert_equal 'Renamed', attachment.description
  493. end
  494. def test_update_all_with_failure
  495. @request.session[:user_id] = 2
  496. patch(
  497. :update_all,
  498. :params => {
  499. :object_type => 'issues',
  500. :object_id => '3',
  501. :attachments => {
  502. '1' => {
  503. :filename => '',
  504. :description => ''
  505. },
  506. '4' => {
  507. :filename => 'newname.rb',
  508. :description => 'Renamed'
  509. },
  510. }
  511. }
  512. )
  513. assert_response :success
  514. assert_select_error /file cannot be blank/i
  515. # The other attachment should not be updated
  516. attachment = Attachment.find(4)
  517. assert_equal 'source.rb', attachment.filename
  518. assert_equal 'This is a Ruby source file', attachment.description
  519. end
  520. def test_download_all_with_valid_container
  521. @request.session[:user_id] = 2
  522. get(
  523. :download_all,
  524. :params => {
  525. :object_type => 'issues',
  526. :object_id => '2'
  527. }
  528. )
  529. assert_response 200
  530. assert_equal response.headers['Content-Type'], 'application/zip'
  531. assert_match /issue-2-attachments.zip/, response.headers['Content-Disposition']
  532. assert_not_includes Dir.entries(Rails.root.join('tmp')), /attachments_zip/
  533. end
  534. def test_download_all_with_invalid_container
  535. @request.session[:user_id] = 2
  536. get(
  537. :download_all,
  538. :params => {
  539. :object_type => 'issues',
  540. :object_id => '999'
  541. }
  542. )
  543. assert_response 404
  544. end
  545. def test_download_all_without_readable_attachments
  546. @request.session[:user_id] = 2
  547. get(
  548. :download_all,
  549. :params => {
  550. :object_type => 'issues',
  551. :object_id => '1'
  552. }
  553. )
  554. assert_equal Issue.find(1).attachments, []
  555. assert_response 404
  556. end
  557. def test_download_all_with_invisible_journal
  558. Project.find(1).update_column :is_public, false
  559. Member.delete_all
  560. @request.session[:user_id] = 2
  561. User.current = User.find(2)
  562. assert_not Journal.find(3).journalized.visible?
  563. get(
  564. :download_all,
  565. :params => {
  566. :object_type => 'journals',
  567. :object_id => '3'
  568. }
  569. )
  570. assert_response 403
  571. end
  572. def test_download_all_with_maximum_bulk_download_size_larger_than_attachments
  573. with_settings :bulk_download_max_size => 0 do
  574. @request.session[:user_id] = 2
  575. get(
  576. :download_all,
  577. :params => {
  578. :object_type => 'issues',
  579. :object_id => '2',
  580. :back_url => '/issues/123'
  581. }
  582. )
  583. assert_redirected_to '/issues/123'
  584. assert_equal flash[:error], 'These attachments cannot be bulk downloaded because the total file size exceeds the maximum allowed size (0 Bytes)'
  585. end
  586. end
  587. def test_download_all_redirects_to_container_url_on_error
  588. with_settings :bulk_download_max_size => 0 do
  589. @request.session[:user_id] = 2
  590. get(
  591. :download_all,
  592. :params => {
  593. :object_type => 'issues',
  594. :object_id => '2',
  595. :back_url => 'https://example.com'
  596. }
  597. )
  598. assert_redirected_to '/issues/2'
  599. assert_equal flash[:error], 'These attachments cannot be bulk downloaded because the total file size exceeds the maximum allowed size (0 Bytes)'
  600. end
  601. end
  602. def test_destroy_issue_attachment
  603. set_tmp_attachments_directory
  604. issue = Issue.find(3)
  605. @request.session[:user_id] = 2
  606. assert_difference 'issue.attachments.count', -1 do
  607. assert_difference 'Journal.count' do
  608. delete(
  609. :destroy,
  610. :params => {
  611. :id => 1
  612. }
  613. )
  614. assert_redirected_to '/projects/ecookbook'
  615. end
  616. end
  617. assert_nil Attachment.find_by_id(1)
  618. j = Journal.order('id DESC').first
  619. assert_equal issue, j.journalized
  620. assert_equal 'attachment', j.details.first.property
  621. assert_equal '1', j.details.first.prop_key
  622. assert_equal 'error281.txt', j.details.first.old_value
  623. assert_equal User.find(2), j.user
  624. end
  625. def test_destroy_wiki_page_attachment
  626. set_tmp_attachments_directory
  627. @request.session[:user_id] = 2
  628. assert_difference 'Attachment.count', -1 do
  629. delete(
  630. :destroy,
  631. :params => {
  632. :id => 3
  633. }
  634. )
  635. assert_response 302
  636. end
  637. end
  638. def test_destroy_project_attachment
  639. set_tmp_attachments_directory
  640. @request.session[:user_id] = 2
  641. assert_difference 'Attachment.count', -1 do
  642. delete(
  643. :destroy,
  644. :params => {
  645. :id => 8
  646. }
  647. )
  648. assert_response 302
  649. end
  650. end
  651. def test_destroy_version_attachment
  652. set_tmp_attachments_directory
  653. @request.session[:user_id] = 2
  654. assert_difference 'Attachment.count', -1 do
  655. delete(
  656. :destroy,
  657. :params => {
  658. :id => 9
  659. }
  660. )
  661. assert_response 302
  662. end
  663. end
  664. def test_destroy_version_attachment_with_issue_tracking_disabled
  665. Project.find(1).disable_module! :issue_tracking
  666. set_tmp_attachments_directory
  667. @request.session[:user_id] = 2
  668. assert_difference 'Attachment.count', -1 do
  669. delete(
  670. :destroy,
  671. :params => {
  672. :id => 9
  673. }
  674. )
  675. assert_response 302
  676. end
  677. end
  678. def test_destroy_without_permission
  679. set_tmp_attachments_directory
  680. assert_no_difference 'Attachment.count' do
  681. delete(
  682. :destroy,
  683. :params => {
  684. :id => 3
  685. }
  686. )
  687. end
  688. assert_response 302
  689. assert Attachment.find_by_id(3)
  690. end
  691. def test_destroy_issue_attachment_by_user_without_edit_issue_permission_on_tracker
  692. role = Role.find(2)
  693. role.set_permission_trackers 'edit_issues', [2, 3]
  694. role.save!
  695. @request.session[:user_id] = 2
  696. set_tmp_attachments_directory
  697. assert_no_difference 'Attachment.count' do
  698. delete(
  699. :destroy,
  700. :params => {
  701. :id => 7
  702. }
  703. )
  704. end
  705. assert_response 403
  706. assert Attachment.find_by_id(7)
  707. end
  708. end