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

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