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_test.rb 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../../test_helper', __FILE__)
  18. class Redmine::ApiTest::AttachmentsTest < Redmine::ApiTest::Base
  19. fixtures :projects, :trackers, :issue_statuses, :issues,
  20. :enumerations, :users, :issue_categories,
  21. :projects_trackers,
  22. :roles,
  23. :member_roles,
  24. :members,
  25. :enabled_modules,
  26. :attachments
  27. def setup
  28. super
  29. set_fixtures_attachments_directory
  30. end
  31. def teardown
  32. super
  33. set_tmp_attachments_directory
  34. end
  35. test "GET /attachments/:id.xml should return the attachment" do
  36. get '/attachments/7.xml', {}, credentials('jsmith')
  37. assert_response :success
  38. assert_equal 'application/xml', @response.content_type
  39. assert_select 'attachment id', :text => '7' do
  40. assert_select '~ filename', :text => 'archive.zip'
  41. assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/7/archive.zip'
  42. end
  43. end
  44. test "GET /attachments/:id.xml for image should include thumbnail_url" do
  45. get '/attachments/16.xml', {}, credentials('jsmith')
  46. assert_response :success
  47. assert_equal 'application/xml', @response.content_type
  48. assert_select 'attachment id:contains(16)' do
  49. assert_select '~ thumbnail_url', :text => 'http://www.example.com/attachments/thumbnail/16'
  50. end
  51. end
  52. test "GET /attachments/:id.xml should deny access without credentials" do
  53. get '/attachments/7.xml'
  54. assert_response 401
  55. set_tmp_attachments_directory
  56. end
  57. test "GET /attachments/download/:id/:filename should return the attachment content" do
  58. get '/attachments/download/7/archive.zip', {}, credentials('jsmith')
  59. assert_response :success
  60. assert_equal 'application/zip', @response.content_type
  61. set_tmp_attachments_directory
  62. end
  63. test "GET /attachments/download/:id/:filename should deny access without credentials" do
  64. get '/attachments/download/7/archive.zip'
  65. assert_response 302
  66. set_tmp_attachments_directory
  67. end
  68. test "GET /attachments/thumbnail/:id should return the thumbnail" do
  69. skip unless convert_installed?
  70. get '/attachments/thumbnail/16', {}, credentials('jsmith')
  71. assert_response :success
  72. end
  73. test "DELETE /attachments/:id.xml should return ok and delete Attachment" do
  74. assert_difference 'Attachment.count', -1 do
  75. delete '/attachments/7.xml', {}, credentials('jsmith')
  76. assert_response :ok
  77. assert_equal '', response.body
  78. end
  79. assert_nil Attachment.find_by_id(7)
  80. end
  81. test "DELETE /attachments/:id.json should return ok and delete Attachment" do
  82. assert_difference 'Attachment.count', -1 do
  83. delete '/attachments/7.json', {}, credentials('jsmith')
  84. assert_response :ok
  85. assert_equal '', response.body
  86. end
  87. assert_nil Attachment.find_by_id(7)
  88. end
  89. test "PATCH /attachments/:id.json should update the attachment" do
  90. patch '/attachments/7.json',
  91. {:attachment => {:filename => 'renamed.zip', :description => 'updated'}},
  92. credentials('jsmith')
  93. assert_response :ok
  94. assert_equal 'application/json', response.content_type
  95. attachment = Attachment.find(7)
  96. assert_equal 'renamed.zip', attachment.filename
  97. assert_equal 'updated', attachment.description
  98. end
  99. test "PATCH /attachments/:id.json with failure should return the errors" do
  100. patch '/attachments/7.json',
  101. {:attachment => {:filename => '', :description => 'updated'}},
  102. credentials('jsmith')
  103. assert_response 422
  104. assert_equal 'application/json', response.content_type
  105. json = ActiveSupport::JSON.decode(response.body)
  106. assert_include "File cannot be blank", json['errors']
  107. end
  108. test "POST /uploads.xml should return the token" do
  109. set_tmp_attachments_directory
  110. assert_difference 'Attachment.count' do
  111. post '/uploads.xml', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
  112. assert_response :created
  113. assert_equal 'application/xml', response.content_type
  114. end
  115. xml = Hash.from_xml(response.body)
  116. assert_kind_of Hash, xml['upload']
  117. token = xml['upload']['token']
  118. assert_not_nil token
  119. attachment_id = xml['upload']['id']
  120. assert_not_nil attachment_id
  121. attachment = Attachment.order('id DESC').first
  122. assert_equal token, attachment.token
  123. assert_equal attachment_id, attachment.id.to_s
  124. assert_nil attachment.container
  125. assert_equal 2, attachment.author_id
  126. assert_equal 'File content'.size, attachment.filesize
  127. assert attachment.content_type.blank?
  128. assert attachment.filename.present?
  129. assert_match /\d+_[0-9a-z]+/, attachment.diskfile
  130. assert File.exist?(attachment.diskfile)
  131. assert_equal 'File content', File.read(attachment.diskfile)
  132. end
  133. test "POST /uploads.json should return the token" do
  134. set_tmp_attachments_directory
  135. assert_difference 'Attachment.count' do
  136. post '/uploads.json', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
  137. assert_response :created
  138. assert_equal 'application/json', response.content_type
  139. end
  140. json = ActiveSupport::JSON.decode(response.body)
  141. assert_kind_of Hash, json['upload']
  142. token = json['upload']['token']
  143. assert_not_nil token
  144. attachment = Attachment.order('id DESC').first
  145. assert_equal token, attachment.token
  146. end
  147. test "POST /uploads.xml should accept :filename param as the attachment filename" do
  148. set_tmp_attachments_directory
  149. assert_difference 'Attachment.count' do
  150. post '/uploads.xml?filename=test.txt', 'File content', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
  151. assert_response :created
  152. end
  153. attachment = Attachment.order('id DESC').first
  154. assert_equal 'test.txt', attachment.filename
  155. assert_match /_test\.txt$/, attachment.diskfile
  156. end
  157. test "POST /uploads.xml should not accept other content types" do
  158. set_tmp_attachments_directory
  159. assert_no_difference 'Attachment.count' do
  160. post '/uploads.xml', 'PNG DATA', {"CONTENT_TYPE" => 'image/png'}.merge(credentials('jsmith'))
  161. assert_response 406
  162. end
  163. end
  164. test "POST /uploads.xml should return errors if file is too big" do
  165. set_tmp_attachments_directory
  166. with_settings :attachment_max_size => 1 do
  167. assert_no_difference 'Attachment.count' do
  168. post '/uploads.xml', ('x' * 2048), {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
  169. assert_response 422
  170. assert_select 'error', :text => /exceeds the maximum allowed file size/
  171. end
  172. end
  173. end
  174. test "POST /uploads.json should create an empty file and return a valid token" do
  175. set_tmp_attachments_directory
  176. assert_difference 'Attachment.count' do
  177. post '/uploads.json', '', {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
  178. assert_response :created
  179. end
  180. json = ActiveSupport::JSON.decode(response.body)
  181. assert_kind_of Hash, json['upload']
  182. token = json['upload']['token']
  183. assert token.present?
  184. assert attachment = Attachment.find_by_token(token)
  185. assert_equal 0, attachment.filesize
  186. assert attachment.digest.present?
  187. assert File.exist? attachment.diskfile
  188. end
  189. end